pyCraft Documentation

repository·master·Indexed 21 days ago

https://github.com/ammaraskar/pycraft

A modern, Python 3-compatible library for communicating with Minecraft servers as a headless client. It provides utilities for Mojang Yggdrasil authentication via the AuthenticationToken class and core networking capabilities through the Connection class, allowing users to write packets, register packet listeners, and implement custom packet classes. Supports Minecraft versions 1.8 through 1.18.1 and Python 3.5 through 3.9.

Tokens
2.3K
Snippets
10
Records
15
Agent score
63%

What's inside pyCraft

  1. Overview of pyCraft networking and authentication

    master

    pyCraft is a Python project designed to handle networking between a Minecraft server and a client. It is divided into two primary functional areas:

    1. Authentication: A package containing utilities to manage communication with Mojang's authentication servers. This allows you to log in with a Minecraft account, edit profiles, and more.
    2. Networking: Centered around the Connection class, this package handles the core client-server interaction, including connecting to a server, sending packets, and listening for incoming packets.
  2. Supported Minecraft and Python versions

    master

    Minecraft Versions

    pyCraft supports a wide range of Minecraft releases, including:

    • 1.8 through 1.8.9
    • 1.9 through 1.9.4
    • 1.10 through 1.10.2
    • 1.11 through 1.11.2
    • 1.12 through 1.12.2
    • 1.13 through 1.13.2
    • 1.14 through 1.14.4
    • 1.15 through 1.15.2
    • 1.16 through 1.16.5
    • 1.17 through 1.17.1
    • 1.18 through 1.18.1

    Note: For a full list of supported versions and protocol numbers, refer to minecraft/__init__.py.

    Python Versions

    Compatible with:

    • Python 3.5, 3.6, 3.7, 3.8, 3.9
    • PyPy
  3. Implement custom packets

    master
    You can implement custom packets by subclassing minecraft.networking.packets.Packet. For advanced requirements, such as creating packets that are compatible across multiple protocol versions, refer to the docstrings in packets.py and the examples within its subpackages.
  4. Extending pyCraft packet support

    master
    pyCraft only encodes/decodes a subset of Minecraft packets (primarily those needed for connection maintenance and chat). To use other functionality, you must implement new packet classes and add them under the minecraft/networking/packets directory.
  5. Use the Connection class to interact with servers

    master

    The Connection class in minecraft.networking.connection is the primary interface for interacting with a Minecraft server. It handles the lifecycle of the connection and provides methods for writing packets and registering listeners.

    from minecraft.networking.connection import Connection
    
    connection = Connection(address, port, auth_token=auth_token)
    connection.connect()
  6. Log in to a Minecraft account using AuthenticationToken

    master

    To authenticate a Minecraft account, create an instance of the AuthenticationToken class and call its authenticate method with the user's username and password.

    If authentication is successful, the method returns True. If it fails (e.g., due to incorrect credentials or a network error), it raises a YggdrasilError.

    from minecraft.authentication import AuthenticationToken
    
    token = AuthenticationToken()
    success = token.authenticate(username='your_username', password='your_password')
    if success:
        print("Logged in successfully!")
  7. Write packets to a server

    master

    To send data to a server, instantiate a specific packet class and use connection.write_packet(packet).

    When setting values on a packet, ensure the attribute names you use match the names defined in the packet's definition attribute. For example, if a packet definition uses keep_alive_id, you must set packet.keep_alive_id.

    from minecraft.networking.packets import serverbound
    import random
    
    # Create the packet instance
    packet = serverbound.play.KeepAlivePacket()
    # Set values based on the packet's definition
    packet.keep_alive_id = random.randint(0, 5000)
    # Send it via the connection
    connection.write_packet(packet)
  8. Debug packets using --dump-packets

    master

    When running the client, you can inspect the network traffic by using the --dump-packets flag.

    • Standard Packets: Known packet types are printed to stderr with a prefix indicating direction (--> for incoming, <-- for outgoing).
    • Unknown Packets: By default, packets that do not match a known type (instances of the base Packet class) are ignored. To see these, you must also include the --dump-unknown-packets flag. Unknown packets will be printed as --> [unknown packet] <packet_representation>.

    All packet debugging output is sent to sys.stderr to avoid interfering with standard input/output.

  9. Run the headless client example

    master

    The repository includes a file named start.py which serves as a basic example of a headless client using the pyCraft library. You can run this script to see the library in action. Use the --help flag to view available command-line options.

    python start.py --help
  10. Listen for incoming packets using register_packet_listener

    master

    If you cannot use decorators (e.g., the function is defined elsewhere), use the connection.register_packet_listener(callback, PacketClass) method to register a listener manually.

    from minecraft.networking.packets.clientbound.play import ChatMessagePacket
    
    def print_chat(chat_packet):
        print "Position: " + str(chat_packet.position)
        print "Data: " + chat_packet.json_data
    
    connection.register_packet_listener(print_chat, ChatMessagePacket)
  11. Make arbitrary requests to the Yggdrasil service

    master

    You can perform custom operations on Mojang's Yggdrasil authentication service by using the _make_request method. To target the authentication service specifically, pass authentication.AUTH_SERVER as the server parameter. This is useful for actions like signing out.

    import minecraft.authentication as authentication
    
    payload = {
        'username': 'your_username',
        'password': 'your_password'
    }
    
    # Example: making a signout request
    authentication._make_request(authentication.AUTH_SERVER, "signout", payload)