This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/scriptcraft/src/main/ts/src/minecraft/event.ts

219 lines
5.0 KiB
TypeScript
Raw Normal View History

2020-05-03 23:28:23 +00:00
import { World } from './world';
import { addBridge } from 'scriptcraft-core';
2020-05-05 00:36:32 +00:00
import { PlayerEntity } from './entity';
import { ActionResult, Hand, Pos, Direction } from './core';
2020-05-03 23:28:23 +00:00
/**
2020-05-05 00:36:32 +00:00
* Event
2020-06-02 17:24:32 +00:00
* @typeParam T Event Data Type
* @typeParam K Event Return Type
2020-05-03 23:28:23 +00:00
*/
2020-06-02 17:24:32 +00:00
export class Event<T, K> {
2020-05-05 00:36:32 +00:00
#listeners: ((obj: T) => K)[] = [];
#defaultValue: K;
2020-05-03 23:28:23 +00:00
/**
2020-06-02 17:24:32 +00:00
* Create Event
* @param defaultValue Default Return Value If There Are No Events (Can be Null)
2020-05-03 23:28:23 +00:00
*/
2020-05-05 00:36:32 +00:00
constructor(defaultValue: K) {
this.#defaultValue = defaultValue;
}
2020-05-03 23:28:23 +00:00
/**
* Add Event Listener
* @param listener Event Listener
*/
2020-05-05 00:36:32 +00:00
addListener(listener: (obj: T) => K): void {
2020-05-03 23:28:23 +00:00
this.#listeners.push(listener);
}
2020-05-05 00:36:32 +00:00
/**
* Remove Event Listener
* @param listener Event Listener
*/
removeListener(listener: (obj: T) => K): void {
const index = this.#listeners.indexOf(listener, 0);
if (index > -1) {
this.#listeners.splice(index, 1);
}
}
2020-05-03 23:28:23 +00:00
/**
2020-06-02 17:24:32 +00:00
* Trigger Event
* @param obj Event Data
* @returns Event Return Value
2020-05-03 23:28:23 +00:00
*/
2020-06-02 17:24:32 +00:00
trigger(obj: T): K {
2020-05-03 23:28:23 +00:00
for (const listener of this.#listeners) {
2020-05-05 00:36:32 +00:00
const value = listener(obj);
if (this.#defaultValue !== null && value != this.#defaultValue) {
return value;
}
2020-05-03 23:28:23 +00:00
}
2020-05-05 00:36:32 +00:00
return this.#defaultValue;
}
}
/**
2020-06-02 17:24:32 +00:00
* World Event
2020-05-05 00:36:32 +00:00
*/
2020-06-02 17:24:32 +00:00
export class WorldEvent {
2020-05-05 00:36:32 +00:00
readonly #world: World;
/**
* Get World
* @returns World
*/
getWorld(): World {
return this.#world;
}
/**
2020-06-02 17:24:32 +00:00
* Create World Event
* @param world World
2020-05-05 00:36:32 +00:00
*/
constructor(world: World) {
this.#world = world;
}
}
/**
* Block Event
*/
2020-06-02 17:24:32 +00:00
export class BlockEvent {
2020-05-05 00:36:32 +00:00
readonly #world: World;
readonly #player: PlayerEntity;
readonly #hand: Hand;
readonly #pos: Pos;
readonly #side: Direction;
/**
* Get World
* @returns World
*/
getWorld(): World {
return this.#world;
}
/**
* Get Player
* @returns Player
*/
getPlayer(): PlayerEntity {
return this.#player;
}
/**
* Get Hand
* @returns Hand
*/
getHand(): Hand {
return this.#hand;
}
/**
* Get Block Position
* @returns Block Position
*/
getPos(): Pos {
return this.#pos;
}
/**
* Get Block Side
* @returns Block Side
*/
getSide(): Direction {
return this.#side;
}
/**
2020-06-02 17:24:32 +00:00
* Create Block Event
* @param player Player
* @param world World
* @param hand Hand
* @param pos Position
* @param side Block Side
2020-05-05 00:36:32 +00:00
*/
constructor(player: PlayerEntity, world: World, hand: Hand, pos: Pos, side: Direction) {
this.#player = player;
this.#world = world;
this.#hand = hand;
this.#pos = pos;
this.#side = side;
}
}
/**
* Item Event
*/
2020-06-02 17:24:32 +00:00
export class ItemEvent {
2020-05-05 00:36:32 +00:00
readonly #world: World;
readonly #player: PlayerEntity;
readonly #hand: Hand;
/**
* Get World
* @returns World
*/
getWorld(): World {
return this.#world;
}
/**
* Get Player
* @returns Player
*/
getPlayer(): PlayerEntity {
return this.#player;
2020-05-03 23:28:23 +00:00
}
2020-05-05 00:36:32 +00:00
/**
* Get Hand
* @returns Hand
*/
getHand(): Hand {
return this.#hand;
}
/**
2020-06-02 17:24:32 +00:00
* Create Item Event
* @param player Player
* @param world World
* @param hand Hand
2020-05-05 00:36:32 +00:00
*/
constructor(player: PlayerEntity, world: World, hand: Hand) {
this.#player = player;
this.#world = world;
this.#hand = hand;
}
}
/**
* All Events
*/
export class Events {
/**
* World Tick Event
*/
2020-06-02 17:24:32 +00:00
static WORLD_TICK = new Event<WorldEvent, void>(null);
2020-05-05 00:36:32 +00:00
/**
* Attack Block Event
*/
static ATTACK_BLOCK = new Event<BlockEvent, ActionResult>(ActionResult.PASS);
/**
* Use Block Event
*/
static USE_BLOCK = new Event<BlockEvent, ActionResult>(ActionResult.PASS);
/**
* Use Item Event
*/
static USE_ITEM = new Event<ItemEvent, ActionResult>(ActionResult.PASS);
2020-05-03 23:28:23 +00:00
}
2020-06-02 17:24:32 +00:00
addBridge('Event.worldTick', (world: JavaObject) => Events.WORLD_TICK.trigger(new WorldEvent(new World(world))));
addBridge('Event.attackBlock', (player: JavaObject, world: JavaObject, hand: keyof typeof Hand, x: number, y: number, z: number, side: keyof typeof Direction) => Events.ATTACK_BLOCK.trigger(new BlockEvent(new PlayerEntity(player), new World(world), Hand[hand], new Pos(x, y, z), Direction[side])));
addBridge('Event.useBlock', (player: JavaObject, world: JavaObject, hand: keyof typeof Hand, x: number, y: number, z: number, side: keyof typeof Direction) => Events.USE_BLOCK.trigger(new BlockEvent(new PlayerEntity(player), new World(world), Hand[hand], new Pos(x, y, z), Direction[side])));
addBridge('Event.useItem', (player: JavaObject, world: JavaObject, hand: keyof typeof Hand) => Events.USE_ITEM.trigger(new ItemEvent(new PlayerEntity(player), new World(world), Hand[hand])));