Client
com.jsmacrosce.jsmacros.client.api.library.impl.FClient
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
Removes currently connected server-status ping connections and disconnects them. This does not establish that pending or in-flight ping callbacks are cancelled.
connect
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
See:
createPacketByteBuffer
Returns a new empty helper buffer. Packet events provide their own packet-backed buffers.
Returns: an empty packet buffer helper
disconnect
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, ornullfor none
exitGameForcefully
Terminates the JVM with exit status
0.exitGamePeacefully
Requests a normal game shutdown without waiting. In contrast, shutdown() queues shutdown and then blocks callers not joined to the main thread indefinitely.
getClipboard
getFPS
getGameOptions
Returns a helper over the game's settings.
Returns: a helper which gives access to all game options and some other useful features.
getLoadedMods
Returns the mod loader's loaded-mod collection.
Returns: the loaded mods reported by the mod loader
getMinecraft
getMod
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
getRegisteredBlocks
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
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
Returns a helper for looking up registered game objects by ID.
Returns: the registry helper
grabMouse
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
Returns whether the mod loader considers the current environment a development environment.
Returns:
true when the mod loader reports a development environmentisModLoaded
loadWorld
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
ping
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 upcallback: called with what the server reported, or withnulland the error
receivePacket
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
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 runwatchdogMaxTime: 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 runawait: whether to block until the task has runwatchdogMaxTime: watchdog limit in milliseconds
sendPacket
Sends a packet through the current server connection, if one exists.
Parameters:
packet: the packet to send
See:
setClipboard
shutdown
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
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();
}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