Skip to content

Client ​

com.jsmacrosce.jsmacros.client.api.library.impl.FClient

Since1.2.9

Miscellaneous functions for interacting with Minecraft, exposed to scripts as Client.

Use runOnMainThread(MethodWrapper) for client-thread work. Tick waits and synchronous pings reject script threads joined to that thread; tick waits do not independently reject a call made directly on the client thread.

js
// ask the main thread to do something to the world
Client.runOnMainThread(JavaWrapper.methodToJava(function () {
 Chat.log("the main thread says hello");  
}));  

// and wait for a tick, which is only safe off the main thread
Client.waitTick(20);  

// look a server up without joining it
const info = Client.ping("mc.hypixel.net");  
Chat.log(`${info.getAddress()}: ${info.getPlayerCountLabel()}`);

Accessible in scripts via the global Client variable.

Methods ​

cancelAllPings ​

cancelAllPings(): voidSince1.6.5#
Removes currently connected server-status ping connections and disconnects them. This does not establish that pending or in-flight ping callbacks are cancelled.

connect ​

connect(String ip): voidSince1.2.3#
Connects to a server using an address string, optionally including its port. The address is parsed by Minecraft and passed to connect(String, int).

Parameters:

  • ip: the server address, with an optional :port

Note: was in the jsmacros library until 1.2.9

See:

connect(String ip, int port): voidSince1.2.3#
Queues a connection to the given server host and port on the Minecraft main thread.

Parameters:

  • ip: server host name or address
  • port: server port

Note: was in the jsmacros library until 1.2.9

createPacketByteBuffer ​

createPacketByteBuffer(): PacketByteBufferHelperSince1.8.4#
Returns a new empty helper buffer. Packet events provide their own packet-backed buffers.
Returns: an empty packet buffer helper

disconnect ​

disconnect(): voidSince1.2.3#
Disconnects from the current world or server without a callback.

Note: was in the jsmacros library until 1.2.9

See:

disconnect(MethodWrapper<Boolean, Object, Object, ?> callback): voidSince1.2.3#
Disconnects from the current world or server and optionally invokes a callback. The callback is queued on the main thread and receives whether a world was open when this method was called. Exceptions thrown by the callback are caught and printed.

Parameters:

  • callback: callback to invoke with whether a world was open, or null for none

Note: was in the jsmacros library until 1.2.9

exitGameForcefully ​

exitGameForcefully(): voidSince1.8.4#
Terminates the JVM with exit status 0.

exitGamePeacefully ​

exitGamePeacefully(): voidSince1.8.4#
Requests a normal game shutdown without waiting. In contrast, shutdown() queues shutdown and then blocks callers not joined to the main thread indefinitely.

getClipboard ​

getClipboard(): StringSince2.0.0#
Returns the clipboard text through the client's keyboard handler.
Returns: clipboard text, or an empty string if the underlying clipboard read returns no text

getFPS ​

getFPS(): StringSince1.2.0#
Returns the game's FPS readout as a formatted string, not a numeric frame rate.
Returns: the fps debug string from minecraft.

Note: was in the jsmacros library until 1.2.9

getGameOptions ​

getGameOptions(): OptionsHelperSince1.1.7#
Returns a helper over the game's settings.
Returns: a helper which gives access to all game options and some other useful features.

Note: was in the jsmacros library until 1.2.9

getLoadedMods ​

getLoadedMods(): List<? extends ModContainerHelper<?>>Since1.8.4#
Returns the mod loader's loaded-mod collection.
Returns: the loaded mods reported by the mod loader

getMinecraft ​

getMinecraft(): MinecraftSince1.0.0#
Returns the raw Minecraft client instance rather than a JsMacros wrapper. Its fields may be unavailable (including null) outside the relevant game state.
Returns: the client instance

Note: was in the jsmacros library until 1.2.9

getMod ​

getMod(String modId): ModContainerHelper<?>Since1.8.4#
Returns the mod container for the specified mod ID, or null if none is found.

Parameters:

  • modId: the mod ID to look up
Returns: the mod container for the given modId or null if the mod is not loaded.

getModLoader ​

getModLoader(): StringSince1.8.4#
Returns the mod loader's name.
Returns: the name of the mod loader.

getRegisteredBlocks ​

getRegisteredBlocks(): List<BlockHelper>Since1.8.4#
Returns helpers for all entries in the block registry, independent of the current world.
Returns: a list of all loaded blocks as BlockHelper objects.

getRegisteredItems ​

getRegisteredItems(): List<ItemHelper>Since1.8.4#
Returns helpers for all entries in the item registry, independent of the current inventory.
Returns: a list of all loaded items as ItemHelper objects.

getRegistryManager ​

getRegistryManager(): RegistryHelperSince1.8.4#
Returns a helper for looking up registered game objects by ID.
Returns: the registry helper

grabMouse ​

grabMouse(): voidSince1.8.4#
Disables pause-on-lost-focus and grabs the mouse. On Minecraft versions before 26.1, it also marks the window active. The option change is not restored by this method.

isDevEnv ​

isDevEnv(): booleanSince1.8.4#
Returns whether the mod loader considers the current environment a development environment.
Returns: true when the mod loader reports a development environment

isModLoaded ​

isModLoaded(String modId): booleanSince1.8.4#
Returns whether the mod loader reports the specified mod ID as loaded.

Parameters:

  • modId: the mod ID to check
Returns: true if the mod with the given modId is loaded, false otherwise.

loadWorld ​

loadWorld(String folderName): voidSince1.6.6#
Opens a singleplayer world by its save-folder name. This is the saves folder name rather than a path, so for a world at .minecraft/saves/myworld the argument is myworld. The name is checked against available saves; if the open succeeds, loading is queued on the main thread and may not have completed when this method returns.

Parameters:

  • folderName: the name of the save folder under the world's saves directory

mcVersion ​

mcVersion(): StringSince1.1.2#
Returns the version identifier with which the game was launched.
Returns: the current minecraft version as a String.

Note: was in the jsmacros library until 1.2.9

ping ​

ping(String ip): ServerInfoHelperSince1.6.5#
Starts a server status ping and waits for its completion signal, then returns the server data helper. The signal is issued after the status response's pong; it does not indicate that status data was successfully received. Throws if the calling script thread is joined to the main thread.
// what a server says about itself
const info = Client.ping("mc.hypixel.net");
Chat.log(`${info.getAddress()}: ${info.getPlayerCountLabel()} in ${info.getVersion()}`);

Parameters:

  • ip: the server address to look up
Returns: the server data helper, which may contain failure or incomplete status data

pingAsync ​

pingAsync(String ip, MethodWrapper<ServerInfoHelper, IOException, Object, ?> callback): voidSince1.6.5#
Starts a server status ping on an asynchronous task. The callback receives the helper after ping completion, or null and the IOException if starting the ping fails. The callback runs on the ping completion thread or, for a startup failure, the asynchronous task thread. Startup failures, including UnknownHostException, are delivered to the callback rather than thrown from this method.
// look several servers up at once, without waiting for any of them
for (const address of ["mc.hypixel.net", "play.cubecraft.net"]) {
  Client.pingAsync(address, JavaWrapper.methodToJava(function (info, error) {
    if (info !== null) {
      Chat.log(`${address}: ${info.getPlayerCountLabel()}`);
    } else {
      Chat.log(`${address} did not answer`);
    }
  }));
}

Parameters:

  • ip: the server address to look up
  • callback: called with what the server reported, or with null and the error

receivePacket ​

receivePacket(Packet<ClientGamePacketListener> packet): voidSince1.8.4#
Invokes a clientbound packet's handler directly, passing the current connection.

Parameters:

  • packet: the clientbound packet to handle as if it arrived from the server

See:

runOnMainThread ​

runOnMainThread(MethodWrapper<Object, Object, Object, ?> runnable): voidSince1.4.0#
Runs a task on the Minecraft client thread without waiting for queued execution to finish. If already on that thread, the task runs immediately. Otherwise it is queued and this method returns without waiting. Exceptions thrown by the task are printed; a thread joined to the client thread is rejected to avoid deadlock.
// change the world on the main thread
Client.runOnMainThread(JavaWrapper.methodToJava(function () {
  const player = Player.getPlayer();
  if (player !== null) {
    player.setPos(0.5, 70, 0.5);
  }
}));

Parameters:

  • runnable: task to run
runOnMainThread(MethodWrapper<Object, Object, Object, ?> runnable, long watchdogMaxTime): voidSince1.6.5#
Queues a task on the Minecraft main thread with a specified watchdog limit. The limit is in milliseconds. Unlike the no-limit overload, this overload does not wait for the task to finish; the watchdog can terminate the task's script if it exceeds the limit.

Parameters:

  • runnable: task to run
  • watchdogMaxTime: watchdog limit in milliseconds
runOnMainThread(MethodWrapper<Object, Object, Object, ?> runnable, boolean await, long watchdogMaxTime): voidSince1.9.1#
Runs a task on the Minecraft main thread, optionally waiting for it to finish. This is the full form of runOnMainThread(MethodWrapper). When await is true, the caller waits for the task to finish; otherwise it returns after queuing it. Calls from a thread joined to the main thread throw IllegalThreadStateException (unless already on the main thread).

Parameters:

  • runnable: task to run
  • await: whether to block until the task has run
  • watchdogMaxTime: watchdog limit in milliseconds

sendPacket ​

sendPacket(Packet<?> packet): voidSince1.8.4#
Sends a packet through the current server connection, if one exists.

Parameters:

  • packet: the packet to send

See:

setClipboard ​

setClipboard(String text): voidSince2.0.0#
Sets the clipboard text through the client's keyboard handler. Empty strings are ignored, so passing "" does not clear the clipboard.

Parameters:

  • text: the text to set

shutdown ​

shutdown(): voidSince1.6.0#
Queues a client stop. A caller not joined to the main thread then sleeps indefinitely, ignoring interruption; a joined caller returns after queuing the stop.

waitTick ​

waitTick(): voidSince1.2.4#
Waits until the client tick counter advances, using the shared TickSync synchronizer. Throws if the calling script thread is joined to the main thread; the method does not independently check whether the caller is the main thread.
// three steps, a tick apart
for (const step of ["one", "two", "three"]) {
  Chat.log(step);
  Client.waitTick();
}
waitTick(int i): voidSince1.2.6#
Waits for the specified number of client ticks; for callers not joined to the main thread, zero or negative counts return without waiting. Throws if the calling script thread is joined to the main thread, even for a nonpositive count.
// hold a key down for about half a second of game time
KeyBind.pressKeyBind("key.forward");
Client.waitTick(10);
KeyBind.releaseKeyBind("key.forward");

Parameters:

  • i: how many client ticks to wait for