Add Tick Event
All checks were successful
ScriptCraft/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-05-03 19:28:23 -04:00
parent 82b00bdaec
commit 0a3883513e
4 changed files with 48 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package com.thebrokenrail.scriptcraft.api; package com.thebrokenrail.scriptcraft.api;
import com.thebrokenrail.scriptcraft.api.bridge.Bridges; import com.thebrokenrail.scriptcraft.api.bridge.Bridges;
import com.thebrokenrail.scriptcraft.api.event.Events;
import com.thebrokenrail.scriptcraft.core.ScriptCraftEntrypoint; import com.thebrokenrail.scriptcraft.core.ScriptCraftEntrypoint;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -22,5 +23,6 @@ public class ScriptCraftAPI implements ScriptCraftEntrypoint {
static { static {
Bridges.init(); Bridges.init();
Events.init();
} }
} }

View File

@ -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));
}
}

View File

@ -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)));

View File

@ -16,3 +16,4 @@ export { LivingEntity, PlayerEntity } from './entity';
export { CompoundTag, ListTag, NumberType } from './tag'; export { CompoundTag, ListTag, NumberType } from './tag';
export { Registry } from './registry'; export { Registry } from './registry';
export { Inventory } from './inventory'; export { Inventory } from './inventory';
export { TickEvent } from './event';