diff --git a/docs/apidoc/events.md b/docs/apidoc/events.md index 6e79485..75094e9 100644 --- a/docs/apidoc/events.md +++ b/docs/apidoc/events.md @@ -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`. \ No newline at end of file + - 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" +}); +``` \ No newline at end of file diff --git a/docs/apidoc/index.md b/docs/apidoc/index.md index 52125a6..821495e 100644 --- a/docs/apidoc/index.md +++ b/docs/apidoc/index.md @@ -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 diff --git a/examplemods/lib.customitems.js b/examplemods/lib.customitems.js new file mode 100644 index 0000000..0c02d49 --- /dev/null +++ b/examplemods/lib.customitems.js @@ -0,0 +1,4 @@ +// Library to make adding custom items with EaglerForgeInjector much easier. +(function LibItems() { + ModAPI.events.newEvent() +})(); \ No newline at end of file diff --git a/postinit.injector.js b/postinit.injector.js index 7cc7668..87db7b2 100644 --- a/postinit.injector.js +++ b/postinit.injector.js @@ -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] = []; } diff --git a/postinit.js b/postinit.js index f9573f1..13fdd76 100644 --- a/postinit.js +++ b/postinit.js @@ -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) {