mirror of
https://github.com/SikongJueluo/kubejs-utils.git
synced 2026-01-11 08:47:50 +08:00
refector(EventBus): splite EventBus type definition and its implemention
This commit is contained in:
43
src/startup_scripts/EventBus.js
Normal file
43
src/startup_scripts/EventBus.js
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Create a simple event bus for handling custom events in KubeJS environment.
|
||||||
|
* @returns {EventBus}
|
||||||
|
*/
|
||||||
|
function createEventBus() {
|
||||||
|
/**
|
||||||
|
* @type {EventBus}
|
||||||
|
*/
|
||||||
|
const bus = {
|
||||||
|
eventMap: {},
|
||||||
|
|
||||||
|
register: function (eventName, callback) {
|
||||||
|
this.eventMap[eventName] = callback;
|
||||||
|
},
|
||||||
|
|
||||||
|
emit: function (eventName, event) {
|
||||||
|
const callback = this.eventMap[eventName];
|
||||||
|
if (callback) {
|
||||||
|
return callback(event);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return bus;
|
||||||
|
}
|
||||||
|
|
||||||
|
global["eventBus"] = createEventBus();
|
||||||
|
|
||||||
|
// ==================== Forge Event Listeners ====================
|
||||||
|
|
||||||
|
ForgeEvents.onEvent(
|
||||||
|
"net.minecraftforge.event.entity.living.LivingEntityUseItemEvent$Finish",
|
||||||
|
(event) => {
|
||||||
|
eventBus.emit("LivingEntityUseItemEvent$Finish", event);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
ForgeEvents.onEvent(
|
||||||
|
"net.minecraftforge.event.entity.player.ItemFishedEvent",
|
||||||
|
(event) => {
|
||||||
|
eventBus.emit("PlayerItemFishedEvent", event);
|
||||||
|
},
|
||||||
|
);
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
/**
|
|
||||||
* @typedef {{
|
|
||||||
* level: Internal.Level,
|
|
||||||
* blockPos: {x: number, y: number, z: number},
|
|
||||||
* player: Internal.Player,
|
|
||||||
* explosionTime: number,
|
|
||||||
* explosionPower: number
|
|
||||||
* }} C4ActivatedEvent
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {(
|
|
||||||
* "PlayerItemFishedEvent" |
|
|
||||||
* "LivingEntityUseItemEvent$Finish" |
|
|
||||||
* "C4Activated"
|
|
||||||
* )} EventName
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {(
|
|
||||||
* ((event: Internal.ItemFishedEvent) => any) |
|
|
||||||
* ((event: Internal.LivingEntityUseItemEvent$Finish) => any) |
|
|
||||||
* ((event: C4ActivatedEvent) => any)
|
|
||||||
* )} EventCallback
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple event bus for handling custom events in KubeJS environment.
|
|
||||||
* @namespace
|
|
||||||
*/
|
|
||||||
const eventBus = {
|
|
||||||
/**
|
|
||||||
* Map storing event names and their corresponding callback functions.
|
|
||||||
* @type {{[key: string]: Function}}
|
|
||||||
*/
|
|
||||||
eventMap: {},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers a callback function for a specific event.
|
|
||||||
* @overload
|
|
||||||
* @param {"PlayerItemFishedEvent"} eventName - The name of the event to listen for.
|
|
||||||
* @param {(event: Internal.ItemFishedEvent) => any} callback - The callback function.
|
|
||||||
* @returns {void}
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Registers a callback function for a specific event.
|
|
||||||
* @overload
|
|
||||||
* @param {"LivingEntityUseItemEvent$Finish"} eventName - The name of the event to listen for.
|
|
||||||
* @param {(event: Internal.LivingEntityUseItemEvent$Finish) => any} callback - The callback function.
|
|
||||||
* @returns {void}
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Registers a callback function for a specific event.
|
|
||||||
* @overload
|
|
||||||
* @param {"C4Activated"} eventName - The name of the event to listen for.
|
|
||||||
* @param {(event: C4ActivatedEvent) => any} callback - The callback function.
|
|
||||||
* @returns {void}
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Registers a callback function for a specific event.
|
|
||||||
* @param {EventName} eventName - The name of the event to listen for.
|
|
||||||
* @param {EventCallback} callback - The callback function to execute when the event is emitted.
|
|
||||||
* @returns {void}
|
|
||||||
*/
|
|
||||||
register: function (eventName, callback) {
|
|
||||||
this.eventMap[eventName] = callback;
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Emits an event, calling the registered callback function if it exists.
|
|
||||||
* @overload
|
|
||||||
* @param {"PlayerItemFishedEvent"} eventName - The name of the event to emit.
|
|
||||||
* @param {Internal.ItemFishedEvent} event - The event data.
|
|
||||||
* @returns {any}
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Emits an event, calling the registered callback function if it exists.
|
|
||||||
* @overload
|
|
||||||
* @param {"LivingEntityUseItemEvent$Finish"} eventName - The name of the event to emit.
|
|
||||||
* @param {Internal.LivingEntityUseItemEvent$Finish} event - The event data.
|
|
||||||
* @returns {any}
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Emits an event, calling the registered callback function if it exists.
|
|
||||||
* @overload
|
|
||||||
* @param {"C4Activated"} eventName - The name of the event to emit.
|
|
||||||
* @param {C4ActivatedEvent} event - The event data.
|
|
||||||
* @returns {any}
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Emits an event, calling the registered callback function if it exists.
|
|
||||||
* @param {string} eventName - The name of the event to emit.
|
|
||||||
* @param {*} event - The event data to pass to the callback function.
|
|
||||||
* @returns {*} The return value of the callback function, or undefined if no callback is registered.
|
|
||||||
*/
|
|
||||||
emit: function (eventName, event) {
|
|
||||||
const callback = this.eventMap[eventName];
|
|
||||||
if (callback) {
|
|
||||||
return callback(event);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {typeof eventBus} EventBus
|
|
||||||
*/
|
|
||||||
|
|
||||||
global["eventBus"] = eventBus;
|
|
||||||
|
|
||||||
// ==================== Forge Event Listeners ====================
|
|
||||||
|
|
||||||
ForgeEvents.onEvent(
|
|
||||||
"net.minecraftforge.event.entity.living.LivingEntityUseItemEvent$Finish",
|
|
||||||
(event) => {
|
|
||||||
eventBus.emit("LivingEntityUseItemEvent$Finish", event);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
ForgeEvents.onEvent(
|
|
||||||
"net.minecraftforge.event.entity.player.ItemFishedEvent",
|
|
||||||
(event) => {
|
|
||||||
eventBus.emit("PlayerItemFishedEvent", event);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
65
types/EventBus.d.ts
vendored
Normal file
65
types/EventBus.d.ts
vendored
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
/**
|
||||||
|
* Event data for C4 use started events.
|
||||||
|
*/
|
||||||
|
interface C4UseStartedEvent {
|
||||||
|
player: Internal.Player;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event data for C4 activation events.
|
||||||
|
*/
|
||||||
|
interface C4ActivatedEvent {
|
||||||
|
level: Internal.Level;
|
||||||
|
blockPos: { x: number; y: number; z: number };
|
||||||
|
player: Internal.Player;
|
||||||
|
explosionTime: number;
|
||||||
|
explosionPower: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mapping of event names to their corresponding event types.
|
||||||
|
*/
|
||||||
|
interface EventMap {
|
||||||
|
PlayerItemFishedEvent: Internal.ItemFishedEvent;
|
||||||
|
LivingEntityUseItemEvent$Finish: Internal.LivingEntityUseItemEvent$Finish;
|
||||||
|
C4Activated: C4ActivatedEvent;
|
||||||
|
C4UseStarted: C4UseStartedEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Union type of all valid event names.
|
||||||
|
*/
|
||||||
|
type EventName = keyof EventMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback function type for a specific event.
|
||||||
|
*/
|
||||||
|
type EventCallback<T extends EventName> = (event: EventMap[T]) => any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A simple event bus for handling custom events in KubeJS environment.
|
||||||
|
*/
|
||||||
|
interface EventBus {
|
||||||
|
/**
|
||||||
|
* Map storing event names and their corresponding callback functions.
|
||||||
|
*/
|
||||||
|
eventMap: { [key: string]: Function };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a callback function for a specific event.
|
||||||
|
* @param eventName - The name of the event to listen for.
|
||||||
|
* @param callback - The callback function to execute when the event is emitted.
|
||||||
|
*/
|
||||||
|
register<T extends EventName>(
|
||||||
|
eventName: T,
|
||||||
|
callback: EventCallback<T>,
|
||||||
|
): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emits an event, calling the registered callback function if it exists.
|
||||||
|
* @param eventName - The name of the event to emit.
|
||||||
|
* @param event - The event data to pass to the callback function.
|
||||||
|
* @returns The return value of the callback function, or undefined if no callback is registered.
|
||||||
|
*/
|
||||||
|
emit<T extends EventName>(eventName: T, event: EventMap[T]): any;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user