Client#

class minecraft.Client#

The Minecraft client.

This abstracts away the connection and provides a simple interface for sending and receiving packets.

Variables:
  • connection – The connection that this client uses. (Connection)

  • username – The username of the client. (str)

  • uuid – The UUID of the client. (str)

  • access_token – The access token of the client. Used for authentication with the Mojang session server. (str)

add_handler(packet_type: type[minecraft.packets.base.Packet], handler: Callable[[Packet], Coroutine[None, None, None]]) None#

Add a packet handler.

Parameters:
  • packet_type (type[Packet]) – The type of packet to handle.

  • handler (Callable[[Packet], Coroutine[None, None, None]]) – The handler to call when the packet is received. Must be an async function.

async close() None#

Close the connection.

async connect(host: str, port: int = 25565) None#

Connect to the server.

Parameters:
  • host (str) – The host to connect to.

  • port (int) – The port to connect to.

handle(packet_type: type[minecraft.packets.base.Packet]) Callable[[Callable[[Packet], Coroutine[None, None, None]]], Callable[[Packet], Coroutine[None, None, None]]]#

A decorator that adds a packet handler.

Parameters:

packet_type (type[Packet]) – The type of packet to handle.

async handler_error(handler, error: Exception) None#

Called whenever a packet handler raises an exception.

The default behavior is to print the traceback.

Parameters:
  • handler (function) – The handler that raised the exception.

  • error (Exception) – The exception that was raised.

async microsoft_auth(client_id: str) None#

Authenticate with Microsoft.

Parameters:

client_id (str) – The client ID of the application.

remove_handler(packet_type: type[minecraft.packets.base.Packet], handler: Callable[[Packet], Coroutine[None, None, None]]) None#

Removes a handler for a packet.

Parameters:
  • packet_type (type[Packet]) – The packet that the handler is reacting to.

  • handler (Callable[[Packet], Coroutine[None, None, None]]) – The handler that should be removed.

Raises:

ValueError – The handler is not registered for the packet.

run(host: str, port: int) None#

Run the bot.

This will block until the connection is closed, and will also handle keyboard interrupts and the event loop for you.

Parameters:
  • host (str) – The host to connect to.

  • port (int) – The port to connect to.

async send_packet(packet: Packet) None#

Send a packet to the server.

Note

This does not wait for the packet to send. Instead, it adds it to a queue.

Parameters:

packet (Packet) – The packet to send.

set_auth_info(username: str, uuid: str, access_token: str) None#

Set the authentication information.

Parameters:
  • username (str) – The username of the client.

  • uuid (str) – The UUID of the client.

  • access_token (str) – The access token of the client.

async setup()#

A utility method that is called before the connection is started.

This takes no parameters and does nothing unless overridden.

async start(host: str, port: int = 25565) None#

Setup the bot and connect to the server.

This will also wait until the connection is closed before returning.

Parameters:
  • host (str) – The host to connect to.

  • port (int) – The port to connect to.

async wait_for_packet(packet_type: type[minecraft.packets.base.Packet], *, timeout: float = None) Packet#

Wait for a packet to be received.

Parameters:
  • packet_type (type[Packet]) – The type of packet to wait for.

  • timeout (float) – The amount of time to wait before timing out.

Returns:

The packet that was received.

Return type:

Packet

Raises:

asyncio.TimeoutError – The packet was not received before the timeout.