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 BytesToPacketData(data ns.ByteArray, v any) error
- func PacketDataToBytes(v any) (ns.ByteArray, error)
- func UnmarshalField(field reflect.Value, data ns.ByteArray) (int, error)
- func UnmarshalFieldWithTag(field reflect.Value, data ns.ByteArray, tag string) (int, error)
- type BaseTCP
- type Bound
- type Packet
- type State
- type TCPClient
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToPacketData ¶
BytesToPacketData converts bytes to a struct using reflection
func PacketDataToBytes ¶
PacketDataToBytes converts a struct to bytes using reflection
func UnmarshalField ¶
UnmarshalField unmarshals a field without tag information (for backwards compatibility)
Types ¶
type BaseTCP ¶
type BaseTCP struct {
// contains filtered or unexported fields
}
func NewBaseTCP ¶
func (*BaseTCP) EnableDebug ¶
func (*BaseTCP) GetEncryption ¶
func (b *BaseTCP) GetEncryption() *mc_crypto.Encryption
type Bound ¶
type Bound uint8
Bound is the direction that the packet is going.
Serverbound: Client -> Server (C2S)
Clientbound: Server -> Client (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 (*Packet) ToBytes ¶
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
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 { *BaseTCP // contains filtered or unexported fields }
func NewTCPClient ¶
func NewTCPClient() *TCPClient