Skip to content

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.

js
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 ​

assertEvent(BaseEvent event, String type): voidSince1.9.0#
Throws AssertionError unless the event's Event annotation value equals type; also narrows the event type in generated TypeScript declarations.

Parameters:

  • event: event to check
  • type: expected event name

createComposedEventFilterer ​

createComposedEventFilterer(EventFilterer initial): FiltererComposedSince1.9.1#
Creates a filterer that combines conditions as OR groups of AND conditions; use its and and or methods to add conditions.

Parameters:

  • initial: initial filterer in the first AND group
Returns: a composed filterer initialized with initial

createCustomEvent ​

createCustomEvent(String eventName): EventCustomSince1.2.8#
Creates a named event that can carry key-value arguments. Call EventCustom.registerEvent() to register its name for subscription and GUI visibility, then register listeners before calling EventCustom.trigger(), which dispatches immediately without queuing. Creation and storing arguments do not dispatch.
// 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
Returns: the event object

createEventFilterer ​

createEventFilterer(String event): EventFiltererSince1.9.1#
Creates a filterer for an event with a registered filterer class. Filterers are checked before the listener callback runs; available fields and setters depend on the event. Throws IllegalArgumentException if the event is unregistered or has no filterer.
// 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
Returns: a new filterer for the event

createModulusEventFilterer ​

createModulusEventFilterer(int quotient): FiltererModulusSince1.9.1#
Creates a stateful filterer that passes every 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 for Integer.MIN_VALUE
Returns: the new modulus filterer

disableAllListeners ​

disableAllListeners(String event): voidSince1.8.4#
Stops every listener for the given event: script-created listeners are removed, profile macros are disabled on their stored trigger, and JsMacros' own listeners receive off().

Parameters:

  • event: the event whose listeners to stop
disableAllListeners(): voidSince1.8.4#
Stops every listener, for every event. See disableAllListeners(String) for what happens to each kind.

disableScriptListeners ​

disableScriptListeners(String event): voidSince1.8.4#
Removes script-created listeners for the given event, leaving profile macros and JsMacros' own listeners untouched. Use disableAllListeners(String) to stop those too, or JsMacros.macroTriggers() to edit the macro triggers directly.

Parameters:

  • event: the event whose script listeners to remove
disableScriptListeners(): voidSince1.8.4#
Removes script-created listeners for all events, leaving profile macros and JsMacros' own listeners untouched.

getConfig ​

getConfig(): ConfigManager#
Returns: this script's configuration manager.

getOpenContexts ​

getOpenContexts(): List<BaseScriptContext<?>>Since1.4.0#
Returns: an immutable snapshot of currently open script contexts

getProfile ​

getProfile(): BaseProfile#
Returns: this script's profile.

getServiceManager ​

getServiceManager(): ServiceManagerSince1.6.3#
Returns the manager for this profile's background services.
Returns: the service manager

invertEventFilterer ​

invertEventFilterer(EventFilterer base): EventFiltererSince1.9.1#
Inverts a filterer's test result; inverting an already inverted filterer returns its base. Throws IllegalArgumentException if base is null.

Parameters:

  • base: filterer to invert
Returns: the inverted filterer, or its base if already inverted

listeners ​

listeners(String event): List<IEventListener>Since1.2.3#
Returns the listeners registered for an event, excluding profile macros. Use JsMacros.macroTriggers() to read the macro editor's triggers, including disabled ones.

Parameters:

  • event: event name
Returns: listeners for this event other than profile macros

macroTriggers ​

macroTriggers(): List<ScriptTrigger>Since2.2.0#
Returns the profile macros and key macros configured in the macro editor, including disabled ones. These are the triggers that run the scripts listed there; listeners created by 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);
  }
}
Returns: the currently registered macro triggers

off ​

off(IEventListener listener): booleanSince1.2.3#

Parameters:

  • listener: listener to remove
Returns: true if it was removed from an event, or false if it was not registered

See:

off(String event, IEventListener listener): booleanSince1.2.3#
Removes a IEventListener from an event.

Parameters:

  • event: event name under which the listener is registered
  • listener: listener to remove from that event
Returns: true if it was removed from that event, or false if it was not registered there

See:

on ​

on(String event, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.2.7#
Registers a listener for an event. The listener does not request joined handling, so dispatch does not wait for it and it receives the event as read-only. Use the overload with a boolean to request joined handling and keep the ability to cancel or change the event.

Parameters:

Returns: the registered listener, or null if the callback is null

See:

on(String event, boolean requestJoined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.0#
Registers a listener for an event and chooses whether it requests joined handling. A joined listener waits for the event lock before the raiser continues, so it may cancel or change the event; a non-joined listener receives a read-only delivery that dispatch does not wait for. Cancelling or changing a non-joined delivery throws. The request only applies when the event permits joining. 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 aliases
  • requestJoined: whether to request joined handling; ignored by events that do not permit joining
  • callback: calls your method as a BiConsumer<BaseEvent, EventContainer>
Returns: the registered listener, or null if the callback is null

See:

on(String event, EventFilterer filterer, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.1#
Registers a listener for an event, optionally filtering which events reach the callback. The listener does not request joined handling, so it receives the event as read-only.

Parameters:

  • event: registered event name; script subscriptions are not remapped from old aliases
  • filterer: filterer that must support this event; null accepts events without filtering
  • callback: calls your method as a BiConsumer<BaseEvent, EventContainer>
Returns: the registered listener, or null if the callback is null

See:

on(String event, EventFilterer filterer, boolean requestJoined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.1#
Registers a listener for an event, optionally filtering which events reach the callback, and chooses whether it requests joined handling. A joined listener waits for the event lock before the raiser continues, so it may cancel or change the event; a non-joined listener receives a read-only delivery that dispatch does not wait for. The request only applies when the event permits joining. 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 aliases
  • filterer: filterer that must support this event; null accepts events without filtering
  • requestJoined: whether to request joined handling; ignored by events that do not permit joining
  • callback: calls your method as a BiConsumer<BaseEvent, EventContainer>
Returns: the registered listener, or null if the callback is null

See:

once ​

once(String event, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.2.7#
Registers a listener that is removed when the event triggers. The listener does not request joined handling, so it receives the event as read-only.

Parameters:

Returns: the registered listener, or null if the callback is null

See:

once(String event, boolean requestJoined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.0#
Registers a listener that is removed when the event triggers and chooses whether it requests joined handling. A joined listener may cancel or change the event; a non-joined listener receives a read-only delivery that dispatch does not wait for.

Parameters:

  • event: registered event name; script subscriptions are not remapped from old aliases
  • requestJoined: whether to request joined handling; ignored by events that do not permit joining
  • callback: calls your method as a BiConsumer<BaseEvent, EventContainer>
Returns: the registered listener, or null if the callback is null

See:

runScript ​

runScript(String file): EventContainer<?>Since1.1.5#
Runs a script file without supplying an event or completion callback.

Parameters:

  • file: path of the script file
Returns: the container for the running script
runScript(String file, BaseEvent fakeEvent): EventContainer<?>Since1.6.3#
Runs a script file with the supplied event as its trigger.

Parameters:

  • file: path of the script file
  • fakeEvent: event supplied to the script, or null
Returns: the container for the running script
runScript(String file, BaseEvent fakeEvent, MethodWrapper<Throwable, Object, Object, ?> callback): EventContainer<?>Since1.6.3#
Runs a script file with an event and optional completion callback.

Parameters:

  • file: path of the script file
  • fakeEvent: event supplied to the script, or null
  • callback: called when the script completes, or null
Returns: the container for the running script

Note: 1.1.5 - 1.6.3 didn't have fakeEvent

runScript(String language, String script): EventContainer<?>Since1.2.4#
Runs a script string in the specified language without a completion callback.

Parameters:

  • language: script language
  • script: script source
Returns: the container for the running script
runScript(String language, String script, MethodWrapper<Throwable, Object, Object, ?> callback): EventContainer<?>Since1.2.4#
Runs a script string in the specified language, optionally calling back when it completes.

Parameters:

  • language: script language
  • script: script source
  • callback: completion callback, or null
Returns: the container for the running script
runScript(String language, String script, String file, MethodWrapper<Throwable, Object, Object, ?> callback): EventContainer<?>Since1.6.0#
Runs script source in the specified language, optionally using a file path as its source file and calling back on completion. The file path is resolved relative to this script's folder; null means no file path is supplied.

Parameters:

  • language: script language
  • script: script source
  • file: optional path relative to this script's folder
  • callback: completion callback, or null
Returns: the container for the running script
runScript(String language, String script, String file, BaseEvent event, MethodWrapper<Throwable, Object, Object, ?> callback): EventContainer<?>Since1.7.0#
Runs script source in the specified language with an optional file path, event, and completion callback. The file path is resolved relative to this script's folder; null omits it. The event is supplied to the script.

Parameters:

  • language: script language
  • script: script source
  • file: optional path relative to this script's folder
  • event: event supplied to the script, or null
  • callback: completion callback, or null
Returns: the container for the running script

stopScript ​

stopScript(): void#
Stops this script run and removes the event listeners it registered. Other runs and the trigger that started this run are not stopped. Bound event locks are released and bound threads are interrupted. Close sockets, screens, and other resources explicitly before stopping; arbitrary resources are not automatically cleaned up. For a running service, its stop callbacks and configured resource cleanup also run. If called from a service stop callback, that callback finishes as shutdown is already underway. Everywhere else the call does not return to the script: it throws BaseScriptContext.ScriptContextClosedException, whether this call or another thread started the shutdown, so a statement after stopScript() cannot run.

waitForEvent ​

waitForEvent(String event): FJsMacros.EventAndContext<?>Since1.5.0#
Waits for the next event of the given name without requesting joined handling. The returned event is read-only; use the overload with a boolean to request joined handling when the event supports it.

Parameters:

  • event: registered event name
Returns: the event and its context; release the context lock promptly after processing a held event
waitForEvent(String event, boolean requestJoined): FJsMacros.EventAndContext<?>Since1.9.0#
Waits for the next event, optionally requesting joined handling when the event supports it.

Parameters:

  • event: registered event name
  • requestJoined: whether to request joined handling; the request is ignored when the event does not permit joining
Returns: the event and its context; release the context lock promptly if the event is held
waitForEvent(String event, MethodWrapper<BaseEvent, Object, Boolean, ?> filter): FJsMacros.EventAndContext<?>Since1.5.0#
Waits until an event arrives for which the optional filter returns true. The listener is registered before waiting, and any currently bound event lock is released before blocking. There is no timeout; a filter exception ends the wait with a RuntimeException. The wait does not request joined handling, so the returned event is read-only.

Parameters:

  • event: registered event name
  • filter: accepts an event, or null to accept the first event
Returns: the event and its context; release the context lock promptly after processing a held event
waitForEvent(String event, boolean requestJoined, MethodWrapper<BaseEvent, Object, Boolean, ?> filter): FJsMacros.EventAndContext<?>Since1.9.0#

Parameters:

  • event: registered event name
  • requestJoined: whether to request joined handling; the request is ignored when the event does not permit joining
  • filter: accepts an event, or null to accept the first event
Returns: the event and its context; release the context lock promptly if the event is held
waitForEvent(String event, MethodWrapper<BaseEvent, Object, Boolean, ?> filter, MethodWrapper<Object, Object, Object, ?> runBeforeWaiting): FJsMacros.EventAndContext<?>Since1.5.0#
Waits until an event arrives for which the optional filter returns true. Registers its listener before running 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 name
  • filter: accepts an event, or null to accept the first event
  • runBeforeWaiting: callback run after listener registration and before waiting
Returns: the event and its context; release the context lock promptly after processing a held event
waitForEvent(String event, boolean requestJoined, MethodWrapper<BaseEvent, Object, Boolean, ?> filter, MethodWrapper<Object, Object, Object, ?> runBeforeWaiting): FJsMacros.EventAndContext<?>Since1.9.0#
Waits until an event passes the optional filter. The listener is registered before 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 name
  • requestJoined: whether to request joined handling; the request is ignored when the event does not permit joining
  • filter: accepts an event, or null to accept the first event
  • runBeforeWaiting: callback run after listener registration and before waiting
Returns: the event and its context

wrapScriptRun ​

wrapScriptRun(String file): MethodWrapper<T, U, R, ?>Since1.7.0#
Creates a callback that runs the script file for each invocation, passing the triggering event to it. The file path is resolved from the process working directory.

Parameters:

  • file: script file path

Type Parameters:

  • T: callback input type
  • U: callback context type
  • R: callback result type
Returns: the wrapped script callback
wrapScriptRun(String language, String script): MethodWrapper<T, U, R, ?>Since1.7.0#
Creates a callback that runs the script source for each invocation, passing the triggering event to it.

Parameters:

  • language: script language
  • script: script source

Type Parameters:

  • T: callback input type
  • U: callback context type
  • R: callback result type
Returns: the wrapped script callback
wrapScriptRun(String language, String script, String file): MethodWrapper<T, U, R, ?>Since1.7.0#
Creates a callback that runs the script source for each invocation, passing the triggering event to it. The optional file path is resolved relative to this script's folder.

Parameters:

  • language: script language
  • script: script source
  • file: optional path relative to this script's folder

Type Parameters:

  • T: callback input type
  • U: callback context type
  • R: callback result type
Returns: the wrapped script callback

wrapScriptRunAsync ​

wrapScriptRunAsync(String file): MethodWrapper<T, U, R, ?>Since1.7.0#
Creates an asynchronous callback that runs the script file for each invocation, passing the triggering event to it. The file path is resolved from the process working directory.

Parameters:

  • file: script file path

Type Parameters:

  • T: callback input type
  • U: callback context type
  • R: callback result type
Returns: the wrapped asynchronous script callback
wrapScriptRunAsync(String language, String script): MethodWrapper<T, U, R, ?>Since1.7.0#
Creates an asynchronous callback that runs the script source for each invocation, passing the triggering event to it.

Parameters:

  • language: script language
  • script: script source

Type Parameters:

  • T: callback input type
  • U: callback context type
  • R: callback result type
Returns: the wrapped asynchronous script callback
wrapScriptRunAsync(String language, String script, String file): MethodWrapper<T, U, R, ?>Since1.7.0#
Creates an asynchronous callback that runs the script source for each invocation, passing the triggering event to it. The optional file path is resolved relative to this script's folder.

Parameters:

  • language: script language
  • script: script source
  • file: optional path relative to this script's folder

Type Parameters:

  • T: callback input type
  • U: callback context type
  • R: callback result type
Returns: the wrapped asynchronous script callback