JsMacros
com.jsmacrosce.jsmacros.core.library.impl.FJsMacros
Script library for event listeners, script execution, and profile services.
Scripts access this library as JsMacros for event handling, script execution, and profile services.
const listener = JsMacros.on("Tick", JavaWrapper.methodToJava(function (event) {
Chat.log(event.getEventName());
}));
JsMacros.off(listener);
JsMacros.runScript("helper.js", null, JavaWrapper.methodToJava(function () {
Chat.log("helper.js is done");
}));Accessible in scripts via the global JsMacros variable.
Nested types
Methods
assertEvent
type; also narrows the event type in generated TypeScript declarations.Parameters:
event: event to checktype: expected event name
createComposedEventFilterer
and and or methods to add conditions.Parameters:
initial: initial filterer in the first AND group
initialcreateCustomEvent
// Store arguments, register the name, then dispatch.
const ping = JsMacros.createCustomEvent("MyPluginPing");
ping.putString("from", "my-plugin");
ping.putInt("count", 1);
ping.registerEvent();
ping.trigger();
// Custom events can also be passed as script arguments.
const data = JsMacros.createCustomEvent("MyPluginJob");
data.putString("task", "rebuild-world");
data.putBoolean("urgent", true);
JsMacros.runScript("worker.js", data);Parameters:
eventName: name used to dispatch the event
createEventFilterer
// Narrow a block-update listener to a position and block id.
const near = JsMacros.createEventFilterer("BlockUpdate")
.setPos(0, -60, 0)
.setBlockId("minecraft:chest");
JsMacros.on("BlockUpdate", near, JavaWrapper.methodToJava(function (event) {
const pos = event.block.getBlockPos();
Chat.log(`the chest at ${pos.getX()}, ${pos.getY()}, ${pos.getZ()} changed`);
}));
// Change the filter without replacing the listener.
near.setBlockId("minecraft:furnace").setUpdateType("ENTITY");
// A modulus filterer can be used with any event.
JsMacros.on("Tick", JsMacros.createModulusEventFilterer(20),
JavaWrapper.methodToJava(function () {
Chat.log("about a second went by");
}));Parameters:
event: registered event name with an available filterer class
createModulusEventFilterer
quotientth filter call; a fresh filterer rejects calls until the threshold is reached. The count is shared by listeners using the same filterer. The constructor applies Math.abs(int); zero and Integer.MIN_VALUE make every call pass.Parameters:
quotient: threshold in filter calls; its absolute value is used except forInteger.MIN_VALUE
disableAllListeners
off().Parameters:
event: the event whose listeners to stop
disableAllListeners(String) for what happens to each kind.disableScriptListeners
disableAllListeners(String) to stop those too, or JsMacros.macroTriggers() to edit the macro triggers directly.Parameters:
event: the event whose script listeners to remove
getConfig
getConfig(): ConfigManager#getOpenContexts
getProfile
getProfile(): BaseProfile#getServiceManager
invertEventFilterer
base is null.Parameters:
base: filterer to invert
listeners
JsMacros.macroTriggers() to read the macro editor's triggers, including disabled ones.Parameters:
event: event name
macroTriggers
JsMacros.on(...) and related methods are not included. Each item is the live trigger used for dispatch. Changing it, such as with ScriptTrigger.setEnabled(false), takes effect on the next occurrence and is written to the profile on the next save. A run that is already in progress is not stopped. // Turn off every macro registered for Tick without touching script listeners.
const macros = JsMacros.macroTriggers();
for (let i = 0; i < macros.size(); i++) {
const trigger = macros.get(i);
if (trigger.getEvent() === "Tick") {
trigger.setEnabled(false);
}
}off
Parameters:
listener: listener to remove
true if it was removed from an event, or false if it was not registeredSee:
Parameters:
event: event name under which the listener is registeredlistener: listener to remove from that event
true if it was removed from that event, or false if it was not registered thereSee:
on
on(String event, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.2.7#Parameters:
event: registered event name; script subscriptions are not remapped from old aliasescallback: calls your method as a BiConsumer<BaseEvent, EventContainer>
null if the callback is nullSee:
on(String event, boolean requestJoined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.0#Parameters:
event: registered event name; script subscriptions are not remapped from old aliasesrequestJoined: whether to request joined handling; ignored by events that do not permit joiningcallback: calls your method as a BiConsumer<BaseEvent, EventContainer>
null if the callback is nullSee:
on(String event, EventFilterer filterer, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.1#Parameters:
event: registered event name; script subscriptions are not remapped from old aliasesfilterer: filterer that must support this event;nullaccepts events without filteringcallback: calls your method as a BiConsumer<BaseEvent, EventContainer>
null if the callback is nullSee:
on(String event, EventFilterer filterer, boolean requestJoined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.1#Parameters:
event: registered event name; script subscriptions are not remapped from old aliasesfilterer: filterer that must support this event;nullaccepts events without filteringrequestJoined: whether to request joined handling; ignored by events that do not permit joiningcallback: calls your method as a BiConsumer<BaseEvent, EventContainer>
null if the callback is nullSee:
once
once(String event, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.2.7#Parameters:
event: registered event name; script subscriptions are not remapped from old aliasescallback: calls your method as a BiConsumer<BaseEvent, EventContainer>
null if the callback is nullSee:
once(String event, boolean requestJoined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.0#Parameters:
event: registered event name; script subscriptions are not remapped from old aliasesrequestJoined: whether to request joined handling; ignored by events that do not permit joiningcallback: calls your method as a BiConsumer<BaseEvent, EventContainer>
null if the callback is nullSee:
runScript
Parameters:
file: path of the script file
Parameters:
file: path of the script filefakeEvent: event supplied to the script, ornull
runScript(String file, BaseEvent fakeEvent, MethodWrapper<Throwable, Object, Object, ?> callback): EventContainer<?>Since1.6.3#Parameters:
file: path of the script filefakeEvent: event supplied to the script, ornullcallback: called when the script completes, ornull
Parameters:
language: script languagescript: script source
runScript(String language, String script, MethodWrapper<Throwable, Object, Object, ?> callback): EventContainer<?>Since1.2.4#Parameters:
language: script languagescript: script sourcecallback: completion callback, ornull
runScript(String language, String script, String file, MethodWrapper<Throwable, Object, Object, ?> callback): EventContainer<?>Since1.6.0#null means no file path is supplied.Parameters:
language: script languagescript: script sourcefile: optional path relative to this script's foldercallback: completion callback, ornull
runScript(String language, String script, String file, BaseEvent event, MethodWrapper<Throwable, Object, Object, ?> callback): EventContainer<?>Since1.7.0#null omits it. The event is supplied to the script.Parameters:
language: script languagescript: script sourcefile: optional path relative to this script's folderevent: event supplied to the script, ornullcallback: completion callback, ornull
stopScript
stopScript(): void#stopScript() cannot run.waitForEvent
Parameters:
event: registered event name
Parameters:
event: registered event namerequestJoined: whether to request joined handling; the request is ignored when the event does not permit joining
waitForEvent(String event, MethodWrapper<BaseEvent, Object, Boolean, ?> filter): FJsMacros.EventAndContext<?>Since1.5.0#Parameters:
event: registered event namefilter: accepts an event, ornullto accept the first event
waitForEvent(String event, boolean requestJoined, MethodWrapper<BaseEvent, Object, Boolean, ?> filter): FJsMacros.EventAndContext<?>Since1.9.0#Parameters:
event: registered event namerequestJoined: whether to request joined handling; the request is ignored when the event does not permit joiningfilter: accepts an event, ornullto accept the first event
waitForEvent(String event, MethodWrapper<BaseEvent, Object, Boolean, ?> filter, MethodWrapper<Object, Object, Object, ?> runBeforeWaiting): FJsMacros.EventAndContext<?>Since1.5.0#runBeforeWaiting, then releases any currently bound event lock before waiting. The wait does not request joined handling, so the returned event is read-only.Parameters:
event: registered event namefilter: accepts an event, ornullto accept the first eventrunBeforeWaiting: callback run after listener registration and before waiting
waitForEvent(String event, boolean requestJoined, MethodWrapper<BaseEvent, Object, Boolean, ?> filter, MethodWrapper<Object, Object, Object, ?> runBeforeWaiting): FJsMacros.EventAndContext<?>Since1.9.0#runBeforeWaiting runs; any currently bound event lock is released before waiting. There is no timeout, and filter exceptions are wrapped in a RuntimeException. A joined wait lets the script cancel or change the event; an unjoined wait receives a read-only delivery that dispatch does not wait for. The request is ignored when the event does not permit joining.Parameters:
event: registered event namerequestJoined: whether to request joined handling; the request is ignored when the event does not permit joiningfilter: accepts an event, ornullto accept the first eventrunBeforeWaiting: callback run after listener registration and before waiting
wrapScriptRun
Parameters:
file: script file path
Type Parameters:
T: callback input typeU: callback context typeR: callback result type
Parameters:
language: script languagescript: script source
Type Parameters:
T: callback input typeU: callback context typeR: callback result type
Parameters:
language: script languagescript: script sourcefile: optional path relative to this script's folder
Type Parameters:
T: callback input typeU: callback context typeR: callback result type
wrapScriptRunAsync
Parameters:
file: script file path
Type Parameters:
T: callback input typeU: callback context typeR: callback result type
Parameters:
language: script languagescript: script source
Type Parameters:
T: callback input typeU: callback context typeR: callback result type
wrapScriptRunAsync(String language, String script, String file): MethodWrapper<T, U, R, ?>Since1.7.0#Parameters:
language: script languagescript: script sourcefile: optional path relative to this script's folder
Type Parameters:
T: callback input typeU: callback context typeR: callback result type