Events broadcast data for use in mods.
ModAPI.addEventListener(eventName: String, callback: Function) : void
function myHandler(event) {
console.log(event);
}
ModAPI.addEventListener("update", myHandler);
ModAPI.removeEventListener(eventName: String, callback: Function) : void
function myHandler(event) {
console.log(event);
}
ModAPI.removeEventListener("update", myHandler);
update
:
load
:
sendchatmessage
:
message: String
preventDefault: Boolean
false
.event
:
event: String
data: Object
frame
:
render
:
partialTicks
: number representing the fraction of ticks passed at this frame.
Can only be used in the context of the dedicated server. More: DedicatedServerDocumentation
serverstart
:
bootstrap
:
serverstop
:
tick
:
preventDefault: Boolean
false
.receivechatmessage
:
message: String
preventDefault: Boolean
false
.processcommand
:
command: String
sender: ICommandSender
preventDefault: Boolean
false
.The events global, ModAPI.events
, allows you to register new event types and call them.
You can register new events using ModAPI, as long as the event name starts with custom:
(lib:
is only useful for library loading). 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")
.
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:
// Mod #1, registers event handler for custom event
ModAPI.addEventListener("custom:myevent", (e)=>{
alert(e.secretCode);
});
// Mod #2, registers and calls custom event
ModAPI.events.newEvent("custom:myevent");
ModAPI.events.callEvent("custom:myevent", {
secretCode: "1234"
});
// Mod #2, registers and calls lib event
ModAPI.events.newEvent("lib:mylibrary:loaded");
ModAPI.events.callEvent("lib:mylibrary:loaded", {});
// Mod #1, registers event handler for lib event
ModAPI.addEventListener("lib:mylibrary:loaded", (e)=>{
//Lib events function differently to normal events, as when they are called once, any new event listener with automatically fire upon being registered.
});