Skip to content

Commit

Permalink
Add custom events
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Sep 7, 2024
1 parent 846eb65 commit 33d1a71
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
27 changes: 26 additions & 1 deletion docs/apidoc/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
```
2 changes: 1 addition & 1 deletion docs/apidoc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions examplemods/lib.customitems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Library to make adding custom items with EaglerForgeInjector much easier.
(function LibItems() {
ModAPI.events.newEvent()
})();
2 changes: 1 addition & 1 deletion postinit.injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}
Expand Down
10 changes: 6 additions & 4 deletions postinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 33d1a71

Please sign in to comment.