Add custom events

This commit is contained in:
ZXMushroom63 2024-09-07 12:38:57 +08:00
parent 846eb6533b
commit 33d1a71b30
5 changed files with 38 additions and 7 deletions

View File

@ -66,4 +66,29 @@ Can only be used in the context of the dedicated server. More: [DedicatedServerD
- `command: String`
- String representing the command.
- `preventDefault: Boolean`
- Boolean representing whether or not to cancel processing the command. Default is `false`.
- Boolean representing whether or not to cancel processing the command. Default is `false`.
### Events Global, adding new events
The events global, `ModAPI.events`, allows you to register new event types and call them.
#### ModAPI.events.newEvent(eventName: String)
You can register new events using ModAPI, as long as the event name starts with `custom:`. For example, if I want to add a new event that can be used by other mods, I can use `ModAPI.events.newEvent("custom:myevent")`.
#### ModAPI.events.callEvent(eventName: String, data: Object)
You can then call events via `ModAPI.events.callEvent`. For example, to trigger `custom:myevent` with a secret code value, I can run `ModAPI.events.callEvent("custom:myevent", {secretCode: "1234"});`.
Here is an example on using this:
```javascript
// Mod #1, registers event handler for custom event
ModAPI.addEventListener("custom:myevent", (e)=>{
alert(e.secretCode);
});
```
```javascript
// Mod #2, registers and calls custom event
ModAPI.events.newEvent("custom:myevent");
ModAPI.events.callEvent("custom:myevent", {
secretCode: "1234"
});
```

View File

@ -112,7 +112,7 @@ In ModAPI's architecture, when you request an object like `ModAPI.player`, inste
However, when calling methods via `ModAPI.hooks`, `ModAPI.reflect`, or even just running a method that takes in object arguments on something like `ModAPI.player`, passing in these ModAPI proxies will cause an error.
To pass in raw java data simply call `getRef()` on the proxym which will return the raw, unmodified version of it.
To pass in raw java data simply call `getRef()` on the proxy which will return the raw, unmodified version of it.
For example, take the method `setRenderViewEntity()` on `ModAPI.mcinstance`. Instead of passing an entity from `ModAPI.world.loadedEntityList.get(index)` directly, you need to use `ModAPI.world.loadedEntityList.get(index).getRef()`. Demo code:
```javascript

View File

@ -0,0 +1,4 @@
// Library to make adding custom items with EaglerForgeInjector much easier.
(function LibItems() {
ModAPI.events.newEvent()
})();

View File

@ -293,7 +293,7 @@ globalThis.modapi_postinit = `(() => {
if (!callback || typeof callback !== "function") {
throw new Error("[ModAPI] Invalid callback!");
}
if (ModAPI.events.types.includes(name)) {
if (ModAPI.events.types.includes(name) || name.startsWith("custom:")) {
if (!Array.isArray(ModAPI.events.listeners[name])) {
ModAPI.events.listeners[name] = [];
}

View File

@ -293,7 +293,7 @@
if (!callback || typeof callback !== "function") {
throw new Error("[ModAPI] Invalid callback!");
}
if (ModAPI.events.types.includes(name)) {
if (ModAPI.events.types.includes(name) || name.startsWith("custom:")) {
if (!Array.isArray(ModAPI.events.listeners[name])) {
ModAPI.events.listeners[name] = [];
}
@ -327,9 +327,11 @@
});
}
};
ModAPI.events.newEvent = function newEvent(name, side) {
console.log("[ModAPI] Registered " + side + " event: " + name);
ModAPI.events.types.push(name);
ModAPI.events.newEvent = function newEvent(name, side = "unknown") {
if (!ModAPI.events.types.includes(name)) {
console.log("[ModAPI] Registered " + side + " event: " + name);
ModAPI.events.types.push(name);
}
};
ModAPI.events.callEvent = function callEvent(name, data) {