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#
Will also disable all listeners for the given event, including JsMacros own event listeners.

Parameters:

  • event: the event to remove all listeners from
disableAllListeners(): voidSince1.8.4#
Will also disable all listeners, including JsMacros own event listeners.

disableScriptListeners ​

disableScriptListeners(String event): voidSince1.8.4#
Removes script-created listeners for the given event, leaving built-in listeners untouched. Use disableAllListeners(String) to remove both kinds.

Parameters:

  • event: the event to remove all listeners from
disableScriptListeners(): voidSince1.8.4#
Removes script-created listeners for all events, leaving built-in 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#

Parameters:

  • event: event name
Returns: listeners for this event other than built-in BaseListener instances

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.

Parameters:

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

See:

on(String event, boolean joined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.0#
Registers a listener for an event. 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 aliases
  • joined: whether to request waiting for this listener on a joinable event; the request does nothing if the event is not joinable
  • 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.

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 joined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.1#
Registers a listener for an event, optionally filtering which events reach the callback. 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 aliases
  • filterer: filterer that must support this event; null accepts events without filtering
  • joined: whether to request waiting for this listener on a joinable event; the request does nothing if the event is not joinable
  • 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.

Parameters:

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

See:

once(String event, boolean joined, MethodWrapper<BaseEvent, EventContainer<?>, Object, ?> callback): IEventListenerSince1.9.0#
Registers a listener that is removed when the event triggers; joined requests joined handling when supported.

Parameters:

  • event: registered event name; script subscriptions are not remapped from old aliases
  • joined: whether to request joined handling for a joinable event
  • 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.

waitForEvent ​

waitForEvent(String event): FJsMacros.EventAndContext<?>Since1.5.0#
Waits for the next event of the given name.

Parameters:

  • event: registered event name
Returns: the event and its context. This form requests no explicit join, but cancellable events still join. Release the returned context's lock promptly after processing a held event.
waitForEvent(String event, boolean join): FJsMacros.EventAndContext<?>Since1.9.0#
Waits for the next event, optionally requesting a join when the event supports it.

Parameters:

  • event: registered event name
  • join: whether to request joining a joinable event; cancellable events join regardless
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.

Parameters:

  • event: registered event name
  • filter: accepts an event, or null to accept the first event
Returns: the event and its context. This form requests no explicit join, but cancellable events still join. Release the returned context's lock promptly after processing a held event.
waitForEvent(String event, boolean join, MethodWrapper<BaseEvent, Object, Boolean, ?> filter): FJsMacros.EventAndContext<?>Since1.9.0#

Parameters:

  • event: registered event name
  • join: whether to request joining a joinable event; cancellable events join regardless
  • 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.

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. This form requests no explicit join, but cancellable events still join. Release the returned context's lock promptly after processing a held event.
waitForEvent(String event, boolean join, 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. 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 name
  • join: whether to request joining a joinable event; cancellable events join regardless
  • 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