mirror of
https://github.com/eaglerforge/EaglerForgeInjector
synced 2025-07-25 15:06:56 -09:00
Add custom events
This commit is contained in:
parent
846eb6533b
commit
33d1a71b30
@ -67,3 +67,28 @@ Can only be used in the context of the dedicated server. More: [DedicatedServerD
|
|||||||
- String representing the command.
|
- String representing the command.
|
||||||
- `preventDefault: Boolean`
|
- `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"
|
||||||
|
});
|
||||||
|
```
|
@ -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.
|
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:
|
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
|
```javascript
|
||||||
|
4
examplemods/lib.customitems.js
Normal file
4
examplemods/lib.customitems.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
// Library to make adding custom items with EaglerForgeInjector much easier.
|
||||||
|
(function LibItems() {
|
||||||
|
ModAPI.events.newEvent()
|
||||||
|
})();
|
@ -293,7 +293,7 @@ globalThis.modapi_postinit = `(() => {
|
|||||||
if (!callback || typeof callback !== "function") {
|
if (!callback || typeof callback !== "function") {
|
||||||
throw new Error("[ModAPI] Invalid callback!");
|
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])) {
|
if (!Array.isArray(ModAPI.events.listeners[name])) {
|
||||||
ModAPI.events.listeners[name] = [];
|
ModAPI.events.listeners[name] = [];
|
||||||
}
|
}
|
||||||
|
@ -293,7 +293,7 @@
|
|||||||
if (!callback || typeof callback !== "function") {
|
if (!callback || typeof callback !== "function") {
|
||||||
throw new Error("[ModAPI] Invalid callback!");
|
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])) {
|
if (!Array.isArray(ModAPI.events.listeners[name])) {
|
||||||
ModAPI.events.listeners[name] = [];
|
ModAPI.events.listeners[name] = [];
|
||||||
}
|
}
|
||||||
@ -327,9 +327,11 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ModAPI.events.newEvent = function newEvent(name, side) {
|
ModAPI.events.newEvent = function newEvent(name, side = "unknown") {
|
||||||
|
if (!ModAPI.events.types.includes(name)) {
|
||||||
console.log("[ModAPI] Registered " + side + " event: " + name);
|
console.log("[ModAPI] Registered " + side + " event: " + name);
|
||||||
ModAPI.events.types.push(name);
|
ModAPI.events.types.push(name);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ModAPI.events.callEvent = function callEvent(name, data) {
|
ModAPI.events.callEvent = function callEvent(name, data) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user