# Client

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

**Since:** 1.2.9

Miscellaneous functions for interacting with Minecraft, exposed to scripts as `Client`.  
 
 Use [runOnMainThread(MethodWrapper)](#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_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 {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_cancelAllPings-}

```js
cancelAllPings(): void
```

Removes currently connected server-status ping connections and disconnects them. This does
 not establish that pending or in-flight ping callbacks are cancelled.

**Since:** 1.6.5

### connect {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_connect-String-}

```js
connect(String ip): void
```

Connects to a server using an address string, optionally including its port.  
 
 The address is parsed by Minecraft and passed to [connect(String, int)](#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_connect-String-int-).

**Parameters:**
- `ip`: the server address, with an optional `:port`

**Since:** 1.2.3

**Note:** was in the `jsmacros` library until 1.2.9

**See:**
- [connect(String, int)](#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_connect-String-int-)

### connect {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_connect-String-int-}

```js
connect(String ip, int port): void
```

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

**Since:** 1.2.3

**Note:** was in the `jsmacros` library until 1.2.9

### createPacketByteBuffer {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_createPacketByteBuffer-}

```js
createPacketByteBuffer(): PacketByteBufferHelper
```

Returns a new empty helper buffer. Packet events provide their own packet-backed buffers.

**Returns:** an empty packet buffer helper

**Since:** 1.8.4

### disconnect {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_disconnect-}

```js
disconnect(): void
```

Disconnects from the current world or server without a callback.

**Since:** 1.2.3

**Note:** was in the `jsmacros` library until 1.2.9

**See:**
- [disconnect(MethodWrapper)](#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_disconnect-MethodWrapper-)

### disconnect {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_disconnect-MethodWrapper-}

```js
disconnect(MethodWrapper<Boolean, Object, Object, ?> callback): void
```

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

**Since:** 1.2.3

**Note:** was in the `jsmacros` library until 1.2.9

### exitGameForcefully {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_exitGameForcefully-}

```js
exitGameForcefully(): void
```

Terminates the JVM with exit status `0`.

**Since:** 1.8.4

### exitGamePeacefully {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_exitGamePeacefully-}

```js
exitGamePeacefully(): void
```

Requests a normal game shutdown without waiting. In contrast, [shutdown()](#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_shutdown-) queues
 shutdown and then blocks callers not joined to the main thread indefinitely.

**Since:** 1.8.4

### getClipboard {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getClipboard-}

```js
getClipboard(): String
```

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

**Since:** 2.1.0

### getFPS {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getFPS-}

```js
getFPS(): String
```

Returns the game's FPS readout as a formatted string, not a numeric frame rate.

**Returns:** the fps debug string from minecraft.

**Since:** 1.2.0

**Note:** was in the `jsmacros` library until 1.2.9

### getGameOptions {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getGameOptions-}

```js
getGameOptions(): OptionsHelper
```

Returns a helper over the game's settings.

**Returns:** a helper which gives access to all game options and some other useful features.

**Since:** 1.1.7

**Note:** was in the `jsmacros` library until 1.2.9

### getLoadedMods {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getLoadedMods-}

```js
getLoadedMods(): List<? extends ModContainerHelper<?>>
```

Returns the mod loader's loaded-mod collection.

**Returns:** the loaded mods reported by the mod loader

**Since:** 1.8.4

### getMinecraft {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getMinecraft-}

```js
getMinecraft(): Minecraft
```

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

**Since:** 1.0.0

**Note:** was in the `jsmacros` library until 1.2.9

### getMod {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getMod-String-}

```js
getMod(String modId): ModContainerHelper<?>
```

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.

**Since:** 1.8.4

### getModLoader {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getModLoader-}

```js
getModLoader(): String
```

Returns the mod loader's name.

**Returns:** the name of the mod loader.

**Since:** 1.8.4

### getRegisteredBlocks {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getRegisteredBlocks-}

```js
getRegisteredBlocks(): List<BlockHelper>
```

Returns helpers for all entries in the block registry, independent of the current world.

**Returns:** a list of all loaded blocks as [BlockHelper](../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/world/BlockHelper.md) objects.

**Since:** 1.8.4

### getRegisteredItems {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getRegisteredItems-}

```js
getRegisteredItems(): List<ItemHelper>
```

Returns helpers for all entries in the item registry, independent of the current inventory.

**Returns:** a list of all loaded items as [ItemHelper](../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/inventory/ItemHelper.md) objects.

**Since:** 1.8.4

### getRegistryManager {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_getRegistryManager-}

```js
getRegistryManager(): RegistryHelper
```

Returns a helper for looking up registered game objects by ID.

**Returns:** the registry helper

**Since:** 1.8.4

### grabMouse {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_grabMouse-}

```js
grabMouse(): void
```

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.

**Since:** 1.8.4

### isDevEnv {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_isDevEnv-}

```js
isDevEnv(): boolean
```

Returns whether the mod loader considers the current environment a development environment.

**Returns:** `true` when the mod loader reports a development environment

**Since:** 1.8.4

### isModLoaded {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_isModLoaded-String-}

```js
isModLoaded(String modId): boolean
```

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.

**Since:** 1.8.4

### loadWorld {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_loadWorld-String-}

```js
loadWorld(String folderName): void
```

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

**Since:** 1.6.6

### mcVersion {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_mcVersion-}

```js
mcVersion(): String
```

Returns the version identifier with which the game was launched.

**Returns:** the current minecraft version as a [String](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html).

**Since:** 1.1.2

**Note:** was in the `jsmacros` library until 1.2.9

### ping {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_ping-String-}

```js
ping(String ip): ServerInfoHelper
```

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.  
 ```js
// 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

**Since:** 1.6.5

### pingAsync {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_pingAsync-String-MethodWrapper-}

```js
pingAsync(String ip, MethodWrapper<ServerInfoHelper, IOException, Object, ?> callback): void
```

Starts a server status ping on an asynchronous task. The callback receives the helper after
 ping completion, or `null` and the [IOException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/IOException.html) 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](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/net/UnknownHostException.html), are delivered to the
 callback rather than thrown from this method.  
 ```js
// 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

**Since:** 1.6.5

### receivePacket {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_receivePacket-Packet-}

```js
receivePacket(Packet<ClientGamePacketListener> packet): void
```

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

**Since:** 1.8.4

**See:**
- [createPacketByteBuffer()](#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_createPacketByteBuffer-)

### runOnMainThread {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_runOnMainThread-MethodWrapper-}

```js
runOnMainThread(MethodWrapper<Object, Object, Object, ?> runnable): void
```

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.  
 ```js
// 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

**Since:** 1.4.0

### runOnMainThread {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_runOnMainThread-MethodWrapper-long-}

```js
runOnMainThread(MethodWrapper<Object, Object, Object, ?> runnable, long watchdogMaxTime): void
```

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

**Since:** 1.6.5

### runOnMainThread {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_runOnMainThread-MethodWrapper-boolean-long-}

```js
runOnMainThread(MethodWrapper<Object, Object, Object, ?> runnable, boolean await, long watchdogMaxTime): void
```

Runs a task on the Minecraft main thread, optionally waiting for it to finish.  
 
 This is the full form of [runOnMainThread(MethodWrapper)](#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_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](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/IllegalThreadStateException.html) (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

**Since:** 1.9.1

### sendPacket {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_sendPacket-Packet-}

```js
sendPacket(Packet<?> packet): void
```

Sends a packet through the current server connection, if one exists.

**Parameters:**
- `packet`: the packet to send

**Since:** 1.8.4

**See:**
- [createPacketByteBuffer()](#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_createPacketByteBuffer-)

### setClipboard {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_setClipboard-String-}

```js
setClipboard(String text): void
```

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

**Since:** 2.1.0

### shutdown {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_shutdown-}

```js
shutdown(): void
```

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.

**Since:** 1.6.0

### waitTick {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_waitTick-}

```js
waitTick(): void
```

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.  
 ```js
// three steps, a tick apart
for (const step of ["one", "two", "three"]) {
  Chat.log(step);  
  Client.waitTick();  
}
 ```

**Since:** 1.2.4

### waitTick {#com.jsmacrosce.jsmacros.client.api.library.impl.FClient_waitTick-int-}

```js
waitTick(int i): void
```

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.  
 ```js
// 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

**Since:** 1.2.6
