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
Parameters:
event: the event to remove all listeners from
disableScriptListeners
Parameters:
event: the event to remove all listeners from
getConfig
getConfig(): ConfigManager#getOpenContexts
getProfile
getProfile(): BaseProfile#getServiceManager
invertEventFilterer
base is null.Parameters:
base: filterer to invert
listeners
Parameters:
event: event name
BaseListener instancesoff
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 joined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.0#joined asks a joinable event to wait for this listener; cancellable events wait regardless. Joined handling needs the listener to belong to another script, because a script cannot wait on a listener in its own run.Parameters:
event: registered event name; script subscriptions are not remapped from old aliasesjoined: whether to request waiting for this listener on a joinable event; the request does nothing if the event is not joinablecallback: 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 joined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.1#joined asks a joinable event to wait for this listener; cancellable events wait regardless. Joined handling needs the listener to belong to another script, because a script cannot wait on a listener in its own run.Parameters:
event: registered event name; script subscriptions are not remapped from old aliasesfilterer: filterer that must support this event;nullaccepts events without filteringjoined: whether to request waiting for this listener on a joinable event; the request does nothing if the event is not joinablecallback: 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 joined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.0#joined requests joined handling when supported.Parameters:
event: registered event name; script subscriptions are not remapped from old aliasesjoined: whether to request joined handling for a joinable eventcallback: 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#waitForEvent
Parameters:
event: registered event name
Parameters:
event: registered event namejoin: whether to request joining a joinable event; cancellable events join regardless
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 join, MethodWrapper<BaseEvent, Object, Boolean, ?> filter): FJsMacros.EventAndContext<?>Since1.9.0#Parameters:
event: registered event namejoin: whether to request joining a joinable event; cancellable events join regardlessfilter: 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.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 join, 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. Cancellable events join regardless of join; other events join only when requested and joinable. Release the returned context's lock promptly when the event is held.Parameters:
event: registered event namejoin: whether to request joining a joinable event; cancellable events join regardlessfilter: 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