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

238 lines
4.8 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-05-03 23:28:23 +00:00
*/
2020-05-05 00:36:32 +00:00
class Event<T, K> {
/**
* @internal
*/
#listeners: ((obj: T) => K)[] = [];
2020-05-03 23:28:23 +00:00
/**
2020-05-05 00:36:32 +00:00
* @internal
2020-05-03 23:28:23 +00:00
*/
2020-05-05 00:36:32 +00:00
#defaultValue: K;
2020-05-03 23:28:23 +00:00
/**
* @internal
*/
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
/**
* @internal
*/
2020-05-05 00:36:32 +00:00
run(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;
}
}
/**
* Tick Event
*/
class WorldTickEvent {
/**
* @internal
*/
readonly #world: World;
/**
* Get World
* @returns World
*/
getWorld(): World {
return this.#world;
}
/**
* @internal
*/
constructor(world: World) {
this.#world = world;
}
}
/**
* Block Event
*/
class BlockEvent {
/**
* @internal
*/
readonly #world: World;
/**
* @internal
*/
readonly #player: PlayerEntity;
/**
* @internal
*/
readonly #hand: Hand;
/**
* @internal
*/
readonly #pos: Pos;
/**
* @internal
*/
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;
}
/**
* @internal
*/
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
*/
class ItemEvent {
/**
* @internal
*/
readonly #world: World;
/**
* @internal
*/
readonly #player: PlayerEntity;
/**
* @internal
*/
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;
}
/**
* @internal
*/
constructor(player: PlayerEntity, world: World, hand: Hand) {
this.#player = player;
this.#world = world;
this.#hand = hand;
}
}
/**
* All Events
*/
export class Events {
/**
* World Tick Event
*/
static WORLD_TICK = new Event<WorldTickEvent, void>(null);
/**
* 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-05-05 00:36:32 +00:00
addBridge('Event.worldTick', (world: JavaObject) => Events.WORLD_TICK.run(new WorldTickEvent(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.run(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.run(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.run(new ItemEvent(new PlayerEntity(player), new World(world), Hand[hand])));