encoding

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: AGPL-3.0, MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuncIOSlice

func FuncIOSlice[T any, S ~*[]T](r IO, x S, f func(IO, *T))

FuncIOSlice reads/writes a slice of T using a function f with a varuint32 length prefix.

func FuncIOSliceOfLen

func FuncIOSliceOfLen[T any, S ~*[]T](r IO, l uint32, x S, f func(IO, *T))

FuncIOSliceOfLen reads/writes the elements of a slice of type T with length l using func f.

func FuncIOSliceUint32Length

func FuncIOSliceUint32Length[T any, S ~*[]T](r IO, x S, f func(IO, *T))

FuncIOSliceUint32Length reads/writes a slice of T using a function with a uint32 length prefix.

func FuncSlice

func FuncSlice[T any, S ~*[]T](r IO, x S, f func(*T))

FuncSlice reads/writes a slice of T using function f with a varuint32 length prefix.

func FuncSliceOfLen

func FuncSliceOfLen[T any, S ~*[]T](r IO, l uint32, x S, f func(*T))

FuncSliceOfLen reads/writes the elements of a slice of type T with length l using func f.

func FuncSliceUint16Length

func FuncSliceUint16Length[T any, S ~*[]T](r IO, x S, f func(*T))

FuncSliceUint16Length reads/writes a slice of T using function f with a uint16 length prefix.

func FuncSliceUint32Length

func FuncSliceUint32Length[T any, S ~*[]T](r IO, x S, f func(*T))

FuncSliceUint32Length reads/writes a slice of T using function f with a uint32 length prefix.

func FuncSliceVarint32Length

func FuncSliceVarint32Length[T any, S ~*[]T](r IO, x S, f func(*T))

FuncSliceVarint32Length reads/writes a slice of T with a varint32 prefix.

func IDOrXFunc

func IDOrXFunc[T any](r IO, x *IDOrX[T], f func(*T))

IDOrXFunc reads/writes an Optional[T].

func IDOrXMarshaler

func IDOrXMarshaler[T any, A PtrMarshaler[T]](r IO, x *IDOrX[T])

IDOrXMarshaler reads/writes an Optional assuming *T implements Marshaler.

func OptionalFunc

func OptionalFunc[T any](r IO, x *Optional[T], f func(*T)) any

OptionalFunc reads/writes an Optional[T].

func OptionalFuncIO

func OptionalFuncIO[T any](r IO, x *Optional[T], f func(IO, *T)) any

OptionalFuncIO reads/writes an Optional[T].

func OptionalMarshaler

func OptionalMarshaler[T any, A PtrMarshaler[T]](r IO, x *Optional[T])

OptionalMarshaler reads/writes an Optional assuming *T implements Marshaler.

func OptionalPointerMarshaler

func OptionalPointerMarshaler[T any, A PtrMarshaler[T]](r IO, x *Optional[*T])

OptionalPointerMarshaler reads/writes an Optional assuming *T implements Marshaler.

func OptionalSlice

func OptionalSlice[T any, S ~[]T](r IO, x *Optional[S], f func(*T)) any

OptionalSlice reads/writes an Optional[S]. Note that:

  • S must be a slice that satisfy []T.
  • f is used to read/write T.

func OptionalSliceMarshaler

func OptionalSliceMarshaler[T any, S ~[]T, A PtrMarshaler[T]](r IO, x *Optional[S]) any

OptionalSliceFunc reads/writes an Optional[S]. Note that:

  • S must be a slice that satisfy []T.
  • T must have implements Marshaler.

func Single

func Single[T any, S PtrMarshaler[T]](r IO, x S)

Single reads/writes a single Marshaler x.

func Slice

func Slice[T any, S ~*[]T, A PtrMarshaler[T]](r IO, x S)

Slice reads/writes a slice of T with a varuint32 prefix.

func SliceOfLen

func SliceOfLen[T any, S ~*[]T, A PtrMarshaler[T]](r IO, l uint32, x S)

SliceOfLen reads/writes the elements of a slice of type T with length l.

func SliceUint16Length

func SliceUint16Length[T any, S ~*[]T, A PtrMarshaler[T]](r IO, x S)

SliceUint16Length reads/writes a slice of T with a uint16 prefix.

func SliceUint32Length

func SliceUint32Length[T any, S ~*[]T, A PtrMarshaler[T]](r IO, x S)

SliceUint32Length reads/writes a slice of T with a uint32 prefix.

func SliceUint8Length

func SliceUint8Length[T any, S *[]T, A PtrMarshaler[T]](r IO, x S)

SliceUint8Length reads/writes a slice of T with a uint8 prefix.

func SliceVarint32Length

func SliceVarint32Length[T any, S ~*[]T, A PtrMarshaler[T]](r IO, x S)

SliceVarint32Length reads/writes a slice of T with a varint32 prefix.

Types

type ChestSlot

type ChestSlot struct {
	Name   string
	Count  uint8
	Damage uint16
	Slot   uint8
}

ChestSlot ..

func (*ChestSlot) Marshal

func (c *ChestSlot) Marshal(io IO)

type CommandBlockData

type CommandBlockData struct {
	Mode               uint32
	Command            string
	CustomName         string
	LastOutput         string
	TickDelay          int32
	ExecuteOnFirstTick bool
	TrackOutput        bool
	Conditional        bool
	NeedsRedstone      bool
}

CommandBlockData ..

func (*CommandBlockData) Marshal

func (c *CommandBlockData) Marshal(io IO)

type IDOrX

type IDOrX[T any] struct {
	// 0 if value of type X is given inline;
	// otherwise registry ID + 1.
	ID int32
	// Only present if ID is 0.
	Value T
}

IDOrX is `ID or X` data type in the protocol.

type IO

type IO interface {
	Uint32(x *uint32)
	Uint64(x *uint64)
	Varuint64(x *uint64)
	Varuint32(x *uint32)
	Varuint16(x *uint16)
	Varint16(x *int16)

	Bool(x *bool)
	Int8(x *int8)
	Uint8(x *uint8)
	Int16(x *int16)
	Uint16(x *uint16)
	Int32(x *int32)
	Int64(x *int64)
	Float32(x *float32)
	Float64(x *float64)
	String(x *string)
	CString(x *string)
	Varint32(x *int32)
	Varint64(x *int64)
	Angle(x *float32)
	UUID(x *uuid.UUID)

	NBT(m *map[string]any, encoding nbt.Encoding)
	NBTList(m *[]any, encoding nbt.Encoding)
	NBTString(s *string, encoding nbt.Encoding)

	Vec3(x *mgl32.Vec3)
	Vec4(x *mgl32.Vec4)
	RGB(x *color.RGBA)
	RGBA(x *color.RGBA)

	Bytes(x *[]byte)
	UnknownEnumOption(value any, enum string)
	InvalidValue(value any, forField, reason string)
}

IO represents a packet IO direction that based on BasicIO.

Implementations of this interface are Reader and Writer.

Reader reads data from the input stream into the pointers passed, whereas Writer writes the values the pointers point point to the output stream.

type Marshaler

type Marshaler interface {
	Marshal(r IO)
}

Marshaler is a type that can be written to or read from an IO.

type Optional

type Optional[T any] struct {
	// contains filtered or unexported fields
}

Optional is an optional type in the protocol. If not set, only a false bool is written. If set, a true bool is written and the Marshaler.

func Option

func Option[T any](val T) Optional[T]

Option creates an Optional[T] with the value passed.

func (Optional[T]) Value

func (o Optional[T]) Value() (T, bool)

Value returns the value set in the Optional. If no value was set, false is returned.

type PtrMarshaler

type PtrMarshaler[T any] interface {
	Marshaler
	*T
}

PtrMarshaler represents a type that implements Marshaler for its pointer.

type Reader

type Reader struct {
	*basic_encoding.BasicReader
}

Reader implements reading operations for reading types from Minecraft packets.

Each Packet implementation has one passed to it.

Reader's uses should always be encapsulated with a deferred recovery. Reader panics on invalid data.

func NewReader

func NewReader(r interface {
	io.Reader
	io.ByteReader
}) *Reader

NewReader creates a new Reader using the io.ByteReader passed as underlying source to read bytes from.

func (*Reader) Angle

func (r *Reader) Angle(x *float32)

Angle reads a rotational float32 from a single byte.

func (*Reader) Bool

func (r *Reader) Bool(x *bool)

Bool reads a bool from the underlying buffer.

func (*Reader) Bytes

func (r *Reader) Bytes(p *[]byte)

Bytes reads the leftover bytes into a byte slice.

func (*Reader) CString

func (r *Reader) CString(x *string)

CString reads a C string which ended with byte 0 from the underlying buffer.

func (*Reader) InvalidValue

func (r *Reader) InvalidValue(value any, forField, reason string)

InvalidValue panics with an error indicating that the value passed is not valid for a specific field.

func (*Reader) NBT

func (r *Reader) NBT(m *map[string]any, encoding nbt.Encoding)

NBT reads a compound tag into a map from the underlying buffer.

func (*Reader) NBTList

func (r *Reader) NBTList(m *[]any, encoding nbt.Encoding)

NBTList reads a list of NBT tags from the underlying buffer.

func (*Reader) NBTString

func (r *Reader) NBTString(s *string, encoding nbt.Encoding)

NBTString reads a string tag into a string from the underlying buffer.

func (*Reader) RGB

func (r *Reader) RGB(x *color.RGBA)

RGB reads a color.RGBA x from a 0xRRGGBB uint32.

func (*Reader) RGBA

func (r *Reader) RGBA(x *color.RGBA)

RGBA reads a color.RGBA x from a 0xAARRGGBB uint32.

func (*Reader) String

func (r *Reader) String(x *string)

String reads a string from the underlying buffer.

func (*Reader) UUID

func (r *Reader) UUID(x *uuid.UUID)

UUID reads a uuid.UUID from the underlying buffer.

func (*Reader) UnknownEnumOption

func (r *Reader) UnknownEnumOption(value any, enum string)

UnknownEnumOption panics with an unknown enum option error.

func (*Reader) Vec3

func (r *Reader) Vec3(x *mgl32.Vec3)

Vec3 reads three float32s into an mgl32.Vec3 from the underlying buffer.

func (*Reader) Vec4

func (r *Reader) Vec4(x *mgl32.Vec4)

Vec4 reads four float32s into an mgl32.Vec4 from the underlying buffer.

type Writer

type Writer struct {
	*basic_encoding.BasicWriter
}

Writer implements writing methods for data types from Minecraft packets.

Each Packet implementation has one passed to it when writing.

Writer implements methods where values are passed using a pointer, so that Reader and Writer have a synonymous interface and both implement the IO interface.

func NewWriter

func NewWriter(w interface {
	io.Writer
	io.ByteWriter
}, shieldID int32) *Writer

NewWriter creates a new initialised Writer with an underlying io.ByteWriter to write to.

func (*Writer) Angle

func (w *Writer) Angle(x *float32)

Angle writes a rotational float32 as a single byte to the underlying buffer.

func (*Writer) Bool

func (w *Writer) Bool(x *bool)

Bool writes a bool as either 0 or 1 to the underlying buffer.

func (*Writer) Bytes

func (w *Writer) Bytes(x *[]byte)

Bytes appends a []byte to the underlying buffer.

func (*Writer) CString

func (w *Writer) CString(x *string)

CString writes a C string, which ended with byte 0, to the underlying buffer.

func (*Writer) InvalidValue

func (w *Writer) InvalidValue(value any, forField, reason string)

InvalidValue panics with an invalid value error.

func (*Writer) NBT

func (w *Writer) NBT(x *map[string]any, encoding nbt.Encoding)

NBT writes a map as NBT to the underlying buffer using the encoding passed.

func (*Writer) NBTList

func (w *Writer) NBTList(x *[]any, encoding nbt.Encoding)

NBTList writes a slice as NBT to the underlying buffer using the encoding passed.

func (*Writer) NBTString

func (w *Writer) NBTString(x *string, encoding nbt.Encoding)

NBTString writes a string as NBT to the underlying buffer using the encoding passed.

func (*Writer) RGB

func (w *Writer) RGB(x *color.RGBA)

RGB writes a color.RGBA x as a uint32 0xRRGGBB the underlying buffer.

func (*Writer) RGBA

func (w *Writer) RGBA(x *color.RGBA)

RGBA writes a color.RGBA x as a uint32 0xAARRGGBB to the underlying buffer.

func (*Writer) String

func (w *Writer) String(x *string)

String writes a string, prefixed with a varint16, to the underlying buffer.

func (*Writer) UUID

func (w *Writer) UUID(x *uuid.UUID)

UUID writes a UUID to the underlying buffer.

func (*Writer) UnknownEnumOption

func (w *Writer) UnknownEnumOption(value any, enum string)

UnknownEnumOption panics with an unknown enum option error.

func (*Writer) Vec3

func (w *Writer) Vec3(x *mgl32.Vec3)

Vec3 writes an mgl32.Vec3 as 3 float32s to the underlying buffer.

func (*Writer) Vec4

func (w *Writer) Vec4(x *mgl32.Vec4)

Vec4 writes an mgl32.Vec4 as 4 float32s to the underlying buffer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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