From 0a3883513e8e6339dd1d608e335c9d4b8e43d1da Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sun, 3 May 2020 19:28:23 -0400 Subject: [PATCH] Add Tick Event --- .../scriptcraft/api/ScriptCraftAPI.java | 2 ++ .../scriptcraft/api/event/Events.java | 10 ++++++ src/main/ts/src/minecraft/event.ts | 35 +++++++++++++++++++ src/main/ts/src/minecraft/index.ts | 1 + 4 files changed, 48 insertions(+) create mode 100644 src/main/java/com/thebrokenrail/scriptcraft/api/event/Events.java create mode 100644 src/main/ts/src/minecraft/event.ts diff --git a/src/main/java/com/thebrokenrail/scriptcraft/api/ScriptCraftAPI.java b/src/main/java/com/thebrokenrail/scriptcraft/api/ScriptCraftAPI.java index f45a946..e0f1e7b 100644 --- a/src/main/java/com/thebrokenrail/scriptcraft/api/ScriptCraftAPI.java +++ b/src/main/java/com/thebrokenrail/scriptcraft/api/ScriptCraftAPI.java @@ -1,6 +1,7 @@ package com.thebrokenrail.scriptcraft.api; import com.thebrokenrail.scriptcraft.api.bridge.Bridges; +import com.thebrokenrail.scriptcraft.api.event.Events; import com.thebrokenrail.scriptcraft.core.ScriptCraftEntrypoint; @SuppressWarnings("unused") @@ -22,5 +23,6 @@ public class ScriptCraftAPI implements ScriptCraftEntrypoint { static { Bridges.init(); + Events.init(); } } \ No newline at end of file diff --git a/src/main/java/com/thebrokenrail/scriptcraft/api/event/Events.java b/src/main/java/com/thebrokenrail/scriptcraft/api/event/Events.java new file mode 100644 index 0000000..61a843d --- /dev/null +++ b/src/main/java/com/thebrokenrail/scriptcraft/api/event/Events.java @@ -0,0 +1,10 @@ +package com.thebrokenrail.scriptcraft.api.event; + +import com.thebrokenrail.scriptcraft.core.ScriptCraftCore; +import net.fabricmc.fabric.api.event.world.WorldTickCallback; + +public class Events { + public static void init() { + WorldTickCallback.EVENT.register(world -> ScriptCraftCore.useBridge("TickEvent.run", world)); + } +} diff --git a/src/main/ts/src/minecraft/event.ts b/src/main/ts/src/minecraft/event.ts new file mode 100644 index 0000000..ec6a6b1 --- /dev/null +++ b/src/main/ts/src/minecraft/event.ts @@ -0,0 +1,35 @@ +import { World } from './world'; +import { addBridge } from 'scriptcraft-core'; + +/** + * Tick Event + */ +export class TickEvent { + /** + * Instance + */ + static INSTANCE: TickEvent = new TickEvent(); + /** + * @internal + */ + #listeners: ((world: World) => void)[]; + + /** + * Add Event Listener + * @param listener Event Listener + */ + addListener(listener: (world: World) => void): void { + this.#listeners.push(listener); + } + + /** + * @internal + */ + run(world: World): void { + for (const listener of this.#listeners) { + listener(world); + } + } +} + +addBridge('TickEvent.run', (world: JavaObject) => TickEvent.INSTANCE.run(new World(world))); diff --git a/src/main/ts/src/minecraft/index.ts b/src/main/ts/src/minecraft/index.ts index a60210d..8fba097 100644 --- a/src/main/ts/src/minecraft/index.ts +++ b/src/main/ts/src/minecraft/index.ts @@ -16,3 +16,4 @@ export { LivingEntity, PlayerEntity } from './entity'; export { CompoundTag, ListTag, NumberType } from './tag'; export { Registry } from './registry'; export { Inventory } from './inventory'; +export { TickEvent } from './event';