mcproto

package
v1.40.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package mcproto provides functions to read types and decode frames declared at https://minecraft.wiki/w/Java_Edition_protocol

Index

Constants

View Source
const (
	PacketIdHandshake            = 0x00
	PacketIdLogin                = 0x00 // during StateLogin
	PacketIdLegacyServerListPing = 0xFE
	PacketIdStatusRequest        = 0x00
	PacketIdPingRequest          = 0x01
)
View Source
const (
	PacketIdStatusResponse = 0x00
	PackedIdPongResponse   = 0x01
)
View Source
const MaxFrameLength = 2097151

MaxFrameLength is declared at https://minecraft.wiki/w/Java_Edition_protocol#Packet_format to be 2^21 - 1

View Source
const (
	PacketLengthFieldBytes = 1
)

Variables

This section is empty.

Functions

func ReadBoolean

func ReadBoolean(reader io.Reader) (bool, error)

func ReadByte

func ReadByte(reader io.Reader) (byte, error)

func ReadByteArray

func ReadByteArray(reader io.Reader, length int) ([]byte, error)

func ReadLong

func ReadLong(reader io.Reader) (int64, error)

func ReadString

func ReadString(reader io.Reader) (string, error)

func ReadUTF16BEString

func ReadUTF16BEString(reader io.Reader, symbolLen uint16) (string, error)

func ReadUnsignedInt

func ReadUnsignedInt(reader io.Reader) (uint32, error)

func ReadUnsignedShort

func ReadUnsignedShort(reader io.Reader) (uint16, error)

func ReadUuid

func ReadUuid(reader io.Reader) (uuid.UUID, error)

func ReadVarInt

func ReadVarInt(reader io.Reader) (int, error)

func WriteLegacySLPResponse added in v1.38.0

func WriteLegacySLPResponse(w io.Writer, protocol int, version string, motd string, online int, max int) error

WriteLegacySLPResponse writes the 1.6-compatible legacy response packet (0xFF) Format: FF, [length short], UTF16BE string beginning with "\u00A7\u0031\u0000" then null-delimited fields fields: protocol, version, motd, online, max

func WritePongPacket added in v1.38.0

func WritePongPacket(w io.Writer, timestamp int64) error

WritePongPacket writes Pong (packet 0x01) with the same payload

func WriteStatusFromStruct added in v1.38.0

func WriteStatusFromStruct(w io.Writer, status StatusResponse) error

WriteStatusFromStruct writes a Status Response from a struct

func WriteStatusJSONPacket added in v1.38.0

func WriteStatusJSONPacket(w io.Writer, jsonString string) error

WriteStatusJSONPacket writes a Status Response (packet 0x00) with the provided JSON string

func WriteString added in v1.38.0

func WriteString(w io.Writer, s string) error

WriteString writes a Minecraft length-prefixed string

func WriteVarInt added in v1.38.0

func WriteVarInt(w io.Writer, value int32) error

WriteVarInt writes a VarInt (Minecraft format) to w

Types

type ByteReader

type ByteReader interface {
	ReadByte() (byte, error)
}

type Frame

type Frame struct {
	Length  int
	Payload []byte
}

func ReadFrame

func ReadFrame(reader io.Reader, addr net.Addr) (*Frame, error)

func (*Frame) String

func (f *Frame) String() string

type Handshake

type Handshake struct {
	ProtocolVersion ProtocolVersion
	ServerAddress   string
	ServerPort      uint16
	NextState       State
}

func DecodeHandshake

func DecodeHandshake(data interface{}) (*Handshake, error)

DecodeHandshake takes the Packet.Data bytes and decodes a Handshake message from it

type LegacyServerListPing

type LegacyServerListPing struct {
	ProtocolVersion int
	ServerAddress   string
	ServerPort      uint16
}

type LoginStart

type LoginStart struct {
	Name       string
	PlayerUuid uuid.UUID
}

func DecodeLoginStart

func DecodeLoginStart(protocolVersion ProtocolVersion, data interface{}) (*LoginStart, error)

DecodeLoginStart takes the Packet.Data bytes and decodes a LoginStart message from it

func NewLoginStart

func NewLoginStart() *LoginStart

type Packet

type Packet struct {
	Length   int
	PacketID int
	// Data is either a byte slice of raw content or a decoded message
	Data interface{}
}

func ReadLegacyServerListPing

func ReadLegacyServerListPing(reader *bufio.Reader, addr net.Addr) (*Packet, error)

func ReadPacket

func ReadPacket(reader *bufio.Reader, addr net.Addr, state State) (*Packet, error)

ReadPacket reads a packet from the given reader based on the provided connection state. Returns a pointer to the Packet and an error if reading fails. Handles legacy server list ping packet when in the handshaking state. The provided addr is used for logging purposes.

func (*Packet) String

func (p *Packet) String() string

type PingPayload added in v1.38.0

type PingPayload struct {
	Timestamp int64
}

PingPayload represents the status ping payload (packet 0x01)

type ProtocolVersion

type ProtocolVersion int
const (
	// ProtocolVersion1_18_2 is the protocol version for Minecraft 1.18.2
	// Docs: https://minecraft.wiki/w/Java_Edition_protocol/Packets?oldid=2772791
	ProtocolVersion1_18_2 ProtocolVersion = 758
	// ProtocolVersion1_19 is the protocol version for Minecraft 1.19
	// Docs: https://minecraft.wiki/w/Java_Edition_protocol/Packets?oldid=2772904
	ProtocolVersion1_19 ProtocolVersion = 759
	// ProtocolVersion1_19_2 is the protocol version for Minecraft 1.19.2
	// Docs: https://minecraft.wiki/w/Java_Edition_protocol/Packets?oldid=2772944
	ProtocolVersion1_19_2 ProtocolVersion = 760
	// ProtocolVersion1_19_2 is the protocol version for Minecraft 1.19.3
	ProtocolVersion1_19_3 ProtocolVersion = 761
	// ProtocolVersion1_20_2 is the protocol version for Minecraft 1.20.2
	ProtocolVersion1_20_2 ProtocolVersion = 764
	// ProtocolVersion1_21_5 is the protocol version for Minecraft 1.21.5
	ProtocolVersion1_21_5 ProtocolVersion = 770
)

Source: https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge/Protocol_History

type State

type State int
const (
	StateHandshaking State = 0
	StateStatus      State = 1
	StateLogin       State = 2
)

Handshaking -> Status Handshaking -> Login -> ...

type StatusResponse added in v1.38.0

type StatusResponse struct {
	Version struct {
		Name     string `json:"name"`
		Protocol int    `json:"protocol"`
	} `json:"version"`
	Players struct {
		Max    int `json:"max"`
		Online int `json:"online"`
		Sample []struct {
			Name string `json:"name"`
			ID   string `json:"id"`
		} `json:"sample,omitempty"`
	} `json:"players"`
	Description        map[string]interface{} `json:"description"`
	Favicon            string                 `json:"favicon,omitempty"`
	EnforcesSecureChat *bool                  `json:"enforcesSecureChat,omitempty"`
}

StatusResponse is a minimal structure for the status JSON

Jump to

Keyboard shortcuts

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