Client
Source: client.ts:257
High-level client.
Example
const client = new Client({
playerName: "bot", // Put your name here
host: "localhost", // Put the server address
port: 25565, // 25565 is the default port, change to your server's port if needed
version: "26.2", // The client version
loadRegistry: false, //
// auth: { // This things is not working yet
// client_id: "d86254d8-edf7-4640-90eb-643c99af188e",
// method: "loopback",
// openBrowser: true,
// port: 12345
// },
// shouldVerifyMessageOrder: true,
// shouldVerifyMessageSignature: true
});
// MineJs is a event driven library, the same way DiscordJS work (if you know them).
// One thing to note is most of method in this client require client to be ready,
// meaning that unless the "ready" event is fired, you can't do, mostly, anything :D
client.on("ready", () => console.log("Client is ready :yay:"));
// The init step before is just init the data and does not create the connection yet.
// This method will create that connection.
client.connect();Constructors
new Client(options: ClientOption): Client
Parameters
options(ClientOption)
Returns
Client
Properties
emit: (event: K, args: ClientEvents[K] extends any[] ? any[any] : never) => void
on: (event: K, func: ClientEvents[K] extends any[] ? (args: any[any]) => void : never) => void
once: (event: K, func: ClientEvents[K] extends any[] ? (args: any[any]) => void : never) => void
Methods
chat(message: string): void
Send a message
Parameters
message(string)
Returns
void
Throws
- When the client is not ready
closeContainer(): void
Close the opening container, for example, close opening chest.
Note The getOpeningContainer() will return undefined after call this method.
Returns
void
Throws
- When the client is not ready
connect(): Promise<void>
Create a connection to the server
Returns
Promise<void>
disconnect(reason: string, raw?: TextComponent): void
Destroy the connection.
Calling this method will also fire the "disconnect" event.
Parameters
reason(string, default: "\"Disconnect\"")raw(TextComponent, optional)
Returns
void
getBlock
Get the block state at position
Throws
- When the client is not ready
getBlock(x: number, y: number, z: number): BlockState | nullParameters
x(number)y(number)z(number)
Returns
BlockState| null
getBlock(position: BaseVec3): BlockState | nullGet the block state at position
Parameters
position(BaseVec3)
Returns
BlockState| null
getCarryingItem(): Slot | undefined
Get carrying item.
Return undefined if nothing is carried
Returns
Slot| undefined
Throws
- When the client is not ready
getHealth(): Health
Get the player's health
Returns
Throws
- When the client is not ready
getInventory(): Container | undefined
Get player's inventory
Returns
Container| undefined
Throws
- When the client is not ready
getOpeningContainer(): Container | undefined
Get opening container.
Return undefined if nothing is opening
Returns
Container| undefined
Throws
- When the client is not ready
getPosition(): BaseVec3
Get player position
Returns
Throws
- When the client is not ready
getTps(): number
Get the server TPS or tick per second.
Normally, server runs at 20 TPS.
If you get suspiciously low TPS or -1 TPS when you just start the client, let it has sometime to populate the tick calculator.
The difference between this method and getTpsWithWindow is this method is depend on how large the tickBufferLength option be, while the getTpsWithWindow depend on the window parameter.
Returns
number— -1 if there isn't any tick stored- the TPS
Throws
- When the client is not ready
getTpsWithWindow(window: number): number | null
Return the TPS in a time window (eg. 5 minutes)
This could be useful when you just need the TPS in certain window of time.
Note The window time must be smaller than the tickBufferLength option, for detail:
tickBufferLength => <windows time>Parameters
window(number) — In second
Returns
number | null—-1if there isn't any tick storednullif the window is larger than the ticks stored- or else, return the TPS
Throws
- When the client is not ready
hold(input: Input): void
Press and hold a key.
This is pretty the same idea as when you play Minecraft. When you need to go forward, you have to hold the key W, S for backward, etc...
Parameters
input(Input)
Returns
void
Throws
- When the client is not ready
lookAt(yaw: number, pitch: number): void
Rotate your face
"Yaw is measured in degrees and does not follow classical trigonometry rules. The unit circle of yaw on the XZ-plane starts at (0, 1) and turns counterclockwise, with 90 at (-1, 0), 180 at (0, -1) and 270 at (1, 0).
Pitch is measured in degrees, where 0 is looking straight ahead, -90 is looking straight up, and 90 is looking straight down."
Parameters
yaw(number) — In degreepitch(number) — In degree
Returns
void
Throws
- When the client is not ready
openBlock
Open a block.
Throws
- When the client is not ready
openBlock(x: number, y: number, z: number): booleanParameters
x(number)y(number)z(number)
Returns
boolean—trueif the target block is a solid block, or else, returnfalse
openBlock(position: BaseVec3): booleanOpen a block.
Parameters
position(BaseVec3)
Returns
boolean—trueif the target block is a solid block, or else, returnfalse
ping(): Promise<number>
Get player latency
Returns
Promise<number>— latency, in ms
Throws
- When the client is not ready
release(input: Input): void
Release a key.
This is also pretty the same idea as when you play Minecraft. Need to stop, stop holding W, S key, etc...
Parameters
input(Input)
Returns
void
Throws
- When the client is not ready
respawn(): void
Send the respawn request.
It is unclear what happen when you send this packet when you are still alive.
Returns
void
Throws
- When the client is not ready
setCarriedSlot(slot: number): void
Changes the slot selection in the hotbar.
Parameters
slot(number) — slot which the player has selected (0–8)
Returns
void
Throws
- When the client is not ready
- when submit invalid number (a float, a number out of 0-8 range)
stopMoving(): void
Release all key.
This is the shortcut for release all keys. Instead of calling release method for each keys, just call this
Returns
void
Throws
- When the client is not ready