java_protocol

package
v0.0.0-...-0e3a23d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 13 Imported by: 3

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.

See https://minecraft.wiki/w/Java_Edition_protocol/Packets

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToPacketData

func BytesToPacketData(data ns.ByteArray, v any) error

BytesToPacketData converts bytes to a struct using reflection

func PacketDataToBytes

func PacketDataToBytes(v any) (ns.ByteArray, error)

PacketDataToBytes converts a struct to bytes using reflection

func UnmarshalField

func UnmarshalField(field reflect.Value, data ns.ByteArray) (int, error)

UnmarshalField unmarshals a field without tag information (for backwards compatibility)

func UnmarshalFieldWithTag

func UnmarshalFieldWithTag(field reflect.Value, data ns.ByteArray, tag string) (int, error)

UnmarshalFieldWithTag unmarshals a field with optional struct tag information

Types

type BaseTCP

type BaseTCP struct {
	// contains filtered or unexported fields
}

func NewBaseTCP

func NewBaseTCP(conn net.Conn) *BaseTCP

func (*BaseTCP) Close

func (b *BaseTCP) Close() error

func (*BaseTCP) Connect

func (b *BaseTCP) Connect(address string) error

func (*BaseTCP) EnableDebug

func (b *BaseTCP) EnableDebug(enabled bool)

func (*BaseTCP) GetConn

func (b *BaseTCP) GetConn() net.Conn

func (*BaseTCP) GetEncryption

func (b *BaseTCP) GetEncryption() *mc_crypto.Encryption

func (*BaseTCP) SetLogger

func (b *BaseTCP) SetLogger(l *log.Logger)

type Bound

type Bound uint8

Bound is the direction that the packet is going.

Serverbound: Client -> Server (C2S)

Clientbound: Server -> Client (S2C)

const (
	// Client -> Server (C2S, serverbound)
	C2S Bound = iota
	// Server -> Client (S2C, clientbound)
	S2C
)

type Packet

type Packet struct {
	// The state of the packet (handshake, status, login, configuration, play), not sent over network
	State State
	// The direction of the packet, not sent over network
	Bound Bound
	// The ID of the packet, represented as a `VarInt`.
	PacketID ns.VarInt
	// The raw body bytes of the packet (already-encoded fields minus the Packet ID)
	Data ns.ByteArray
}

The top-level packet struct.

This is the base struct for all packets. It contains the phase, bound, packet ID, and data, as Go structs.

func NewPacket

func NewPacket(state State, bound Bound, packetID ns.VarInt) *Packet

NewPacket creates a packet template with no body data.

func (*Packet) ToBytes

func (p *Packet) ToBytes(compressionThreshold int) (ns.ByteArray, error)

ToBytes marshals the packet into a byte array that can be sent over the network.

If `compressionThreshold` is non-negative, compression is enabled. The format of the raw packet varies, depending on compression.

> Once a Set Compression packet (with a non-negative threshold) is sent, zlib compression is enabled for all following packets. The format of a packet changes slightly to include the size of the uncompressed packet. For serverbound packets, the uncompressed length of (Packet ID + Data) must not be greater than 223 or 8388608 bytes. Note that a length equal to 223 is permitted, which differs from the compressed length limit. The vanilla client, on the other hand, has no limit for the uncompressed length of incoming compressed packets.

> If the size of the buffer containing the packet data and ID (as a VarInt) is smaller than the threshold specified in the packet Set Compression. It will be sent as uncompressed. This is done by setting the data length as 0. (Comparable to sending a non-compressed format with an extra 0 between the length, and packet data).

> If it's larger than or equal to the threshold, then it follows the regular compressed protocol format.

> The vanilla server (but not client) rejects compressed packets smaller than the threshold. > Uncompressed packets exceeding the threshold, however, are accepted.

> Compression can be disabled by sending the packet Set Compression with a negative Threshold, > or not sending the Set Compression packet at all."

See https://minecraft.wiki/w/Java_Edition_protocol/Packets#Packet_format

func (*Packet) WithData

func (p *Packet) WithData(v any) (*Packet, error)

WithData marshals the provided packet data struct into bytes and returns the original packet with the data bytes set.

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).

const (
	StateHandshake State = iota
	StateStatus
	StateLogin
	StateConfiguration
	StatePlay
)

type TCPClient

type TCPClient struct {
	*BaseTCP
	// contains filtered or unexported fields
}

func NewTCPClient

func NewTCPClient() *TCPClient

func (*TCPClient) GetState

func (c *TCPClient) GetState() State

func (*TCPClient) ReadPacket

func (c *TCPClient) ReadPacket() (*Packet, error)

func (*TCPClient) SetCompressionThreshold

func (c *TCPClient) SetCompressionThreshold(threshold int)

func (*TCPClient) SetState

func (c *TCPClient) SetState(state State)

func (*TCPClient) WritePacket

func (c *TCPClient) WritePacket(packet *Packet) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL