Documentation
¶
Overview ¶
The `java_protocol` package contains the core structs and functions for working with the Java Edition protocol.
> The Minecraft server accepts connections from TCP clients and communicates with them using packets. A packet is a sequence of bytes sent over the TCP connection (note: see `net_structures.ByteArray`). The meaning of a packet depends both on its packet ID and the current state of the connection (note: each state has its own packet ID counter, so packets in different states can have the same packet ID). The initial state of each connection is Handshaking, and state is switched using the packets 'Handshake' and 'Login Success'."
Packet format:
> Packets cannot be larger than (2^21) − 1 or 2 097 151 bytes (the maximum that can be sent in a 3-byte VarInt). Moreover, the length field must not be longer than 3 bytes, even if the encoded value is within the limit. Unnecessarily long encodings at 3 bytes or below are still allowed. For compressed packets, this applies to the Packet Length field, i. e. the compressed length.
Index ¶
- func ReadPacket[T any, PT interface{ ... }](wire *WirePacket) (PT, error)
- type Bound
- type Conn
- type Packet
- type State
- type TCPClient
- func (c *TCPClient) Close() error
- func (c *TCPClient) CompressionThreshold() int
- func (c *TCPClient) Conn() *Conn
- func (c *TCPClient) Connect(address string) (host string, port string, err error)
- func (c *TCPClient) EnableDebug(enabled bool)
- func (c *TCPClient) ReadWirePacket() (*WirePacket, error)
- func (c *TCPClient) SetCompressionThreshold(threshold int)
- func (c *TCPClient) SetConn(conn *Conn)
- func (c *TCPClient) SetLogger(l *log.Logger)
- func (c *TCPClient) SetState(state State)
- func (c *TCPClient) State() State
- func (c *TCPClient) WritePacket(p Packet) error
- func (c *TCPClient) WriteWirePacket(pkt *WirePacket) error
- type WirePacket
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadPacket ¶
func ReadPacket[T any, PT interface { *T Packet }](wire *WirePacket) (PT, error)
ReadPacket deserializes a WirePacket into a typed Packet using generics. This provides type-safe packet reading without manual type assertions.
Example:
wire, _ := client.ReadWirePacket() login, err := ReadPacket[LoginSuccessPacket](wire)
Types ¶
type Bound ¶
type Bound uint8
Bound is the direction that the packet is going.
Serverbound: Client -> Server (C2S)
Clientbound: Server -> Client (S2C)
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn wraps a net.Conn with optional encryption. It implements io.Reader and io.Writer, transparently handling encryption/decryption when enabled.
func (*Conn) Encryption ¶
func (c *Conn) Encryption() *crypto.Encryption
Encryption returns the encryption instance for configuration.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address.
type Packet ¶
type Packet interface {
// ID returns the packet ID for this packet type.
ID() ns.VarInt
// State returns the protocol state this packet belongs to.
State() State
// Bound returns the direction of this packet (C2S or S2C).
Bound() Bound
// Read deserializes the packet data from the buffer.
Read(buf *ns.PacketBuffer) error
// Write serializes the packet data to the buffer.
Write(buf *ns.PacketBuffer) error
}
Packet is the interface that all typed packet implementations must satisfy. Each packet knows its ID, protocol state, and direction.
type State ¶
type State uint8
State is the phase that the packet is in (handshake, status, login, configuration, play). This is not sent over network (server and client automatically transition phases).
type TCPClient ¶
type TCPClient struct {
// contains filtered or unexported fields
}
TCPClient is a Minecraft protocol client connection.
func (*TCPClient) CompressionThreshold ¶
CompressionThreshold returns the current compression threshold.
func (*TCPClient) Connect ¶
Connect connects to a Minecraft server. The address can be "host", "host:port", or will use SRV records if no port specified. Returns the resolved host and port.
func (*TCPClient) EnableDebug ¶
EnableDebug enables or disables debug logging.
func (*TCPClient) ReadWirePacket ¶
func (c *TCPClient) ReadWirePacket() (*WirePacket, error)
ReadWirePacket reads a raw WirePacket from the connection.
func (*TCPClient) SetCompressionThreshold ¶
SetCompressionThreshold enables compression with the given threshold. Use -1 to disable compression.
func (*TCPClient) SetConn ¶
SetConn sets the underlying connection (for testing or server-accepted connections).
func (*TCPClient) WritePacket ¶
WritePacket writes a typed Packet to the connection. Safe for concurrent use from multiple goroutines.
func (*TCPClient) WriteWirePacket ¶
func (c *TCPClient) WriteWirePacket(pkt *WirePacket) error
WriteWirePacket writes a raw WirePacket to the connection. Safe for concurrent use from multiple goroutines.
type WirePacket ¶
type WirePacket struct {
// Length is the total length of (PacketID + Data) as read from the wire.
Length ns.VarInt
// PacketID is the packet identifier.
PacketID ns.VarInt
// Data is the raw payload bytes (without the packet ID).
Data ns.ByteArray
}
WirePacket represents the raw packet as it appears on the wire. It contains only wire-level data without typed field information.
func ReadWirePacketFrom ¶
func ReadWirePacketFrom(r io.Reader, compressionThreshold int) (*WirePacket, error)
ReadWirePacketFrom reads a WirePacket from the given reader. Handles both compressed and uncompressed packet formats based on compressionThreshold. Use compressionThreshold < 0 to disable compression.
func ToWire ¶
func ToWire(p Packet) (*WirePacket, error)
ToWire converts a typed Packet to a WirePacket by serializing its data. The resulting WirePacket can then be written to a connection via WriteTo() or converted to bytes via ToBytes().
func (*WirePacket) Clone ¶
func (w *WirePacket) Clone() *WirePacket
Clone returns a deep copy of the wire packet.
func (*WirePacket) ReadInto ¶
func (w *WirePacket) ReadInto(p Packet) error
ReadInto deserializes the wire packet's raw data into a typed Packet. Returns an error if the packet ID doesn't match.
func (*WirePacket) WriteTo ¶
func (w *WirePacket) WriteTo(writer io.Writer, compressionThreshold int) error
WriteTo writes the WirePacket to the given writer. Handles both compressed and uncompressed packet formats based on compressionThreshold. Use compressionThreshold < 0 to disable compression.
Compression behavior (per Minecraft protocol):
- If size >= threshold: packet is zlib compressed
- If size < threshold: packet is sent uncompressed (with Data Length = 0)
- The vanilla server rejects compressed packets smaller than the threshold
See https://minecraft.wiki/w/Java_Edition_protocol/Packets#Packet_format
Directories
¶
| Path | Synopsis |
|---|---|
|
These types handle common patterns like length-prefixed arrays, boolean-prefixed optionals, and bit sets.
|
These types handle common patterns like length-prefixed arrays, boolean-prefixed optionals, and bit sets. |