packet

package
v1.16.5-pre2.0...-5b980bd Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const MaxVarIntLen = 5
View Source
const MaxVarLongLen = 10

Variables

This section is empty.

Functions

This section is empty.

Types

type Angle

type Angle Byte

Angle is rotation angle in steps of 1/256 of a full turn

func (*Angle) ReadFrom

func (a *Angle) ReadFrom(r io.Reader) (int64, error)

func (Angle) ToDeg

func (a Angle) ToDeg() float64

ToDeg convert Angle to Degree

func (Angle) ToRad

func (a Angle) ToRad() float64

ToRad convert Angle to Radian

func (Angle) WriteTo

func (a Angle) WriteTo(w io.Writer) (int64, error)

type Ary

type Ary struct {
	Len Field       // Pointer of VarInt, VarLong, Int or Long
	Ary interface{} // Slice of FieldEncoder, FieldDecoder or both (Field)
}

func (Ary) ReadFrom

func (a Ary) ReadFrom(r io.Reader) (n int64, err error)

func (Ary) WriteTo

func (a Ary) WriteTo(r io.Writer) (n int64, err error)

type Boolean

type Boolean bool

Boolean of True is encoded as 0x01, false as 0x00.

func (*Boolean) ReadFrom

func (b *Boolean) ReadFrom(r io.Reader) (n int64, err error)

Decode a Boolean

func (Boolean) WriteTo

func (b Boolean) WriteTo(w io.Writer) (int64, error)

Encode a Boolean

type Builder

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

func (*Builder) Packet

func (p *Builder) Packet(id int32) Packet

func (*Builder) WriteField

func (p *Builder) WriteField(fields ...FieldEncoder)

type Byte

type Byte int8

Byte is signed 8-bit integer, two's complement

func (*Byte) ReadFrom

func (b *Byte) ReadFrom(r io.Reader) (n int64, err error)

Decode a Byte

func (Byte) WriteTo

func (b Byte) WriteTo(w io.Writer) (n int64, err error)

Encode a Byte

type ByteArray

type ByteArray []byte

ByteArray is []byte with prefix VarInt as length

func (*ByteArray) ReadFrom

func (b *ByteArray) ReadFrom(r io.Reader) (n int64, err error)

Decode a ByteArray

func (ByteArray) WriteTo

func (b ByteArray) WriteTo(w io.Writer) (n int64, err error)

Encode a ByteArray

type Chat

type Chat = String

Chat is encoded as a String with max length of 32767. Deprecated: Use chat.Message

type Double

type Double float64

A Double is a double-precision 64-bit IEEE 754 floating point number

func (*Double) ReadFrom

func (d *Double) ReadFrom(r io.Reader) (n int64, err error)

Decode a Double

func (Double) WriteTo

func (d Double) WriteTo(w io.Writer) (n int64, err error)

Encode a Double

type Field

type Field interface {
	FieldEncoder
	FieldDecoder
}

A Field is both FieldEncoder and FieldDecoder

func NBT

func NBT(v interface{}) Field

NBT encode a value as Named Binary Tag

type FieldDecoder

type FieldDecoder io.ReaderFrom

A FieldDecoder can Decode from minecraft protocol

type FieldEncoder

type FieldEncoder io.WriterTo

A FieldEncoder can be encode as minecraft protocol used.

type Float

type Float float32

A Float is a single-precision 32-bit IEEE 754 floating point number

func (*Float) ReadFrom

func (f *Float) ReadFrom(r io.Reader) (n int64, err error)

Decode a Float

func (Float) WriteTo

func (f Float) WriteTo(w io.Writer) (n int64, err error)

Encode a Float

type Identifier

type Identifier = String

Identifier is encoded as a String with max length of 32767.

type Int

type Int int32

Int is signed 32-bit integer, two's complement

func (*Int) ReadFrom

func (i *Int) ReadFrom(r io.Reader) (n int64, err error)

Decode a Int

func (Int) WriteTo

func (i Int) WriteTo(w io.Writer) (int64, error)

Encode a Int

type Long

type Long int64

Long is signed 64-bit integer, two's complement

func (*Long) ReadFrom

func (l *Long) ReadFrom(r io.Reader) (n int64, err error)

Decode a Long

func (Long) WriteTo

func (l Long) WriteTo(w io.Writer) (int64, error)

Encode a Long

type Opt

type Opt struct {
	Has   *Boolean
	Field interface{} // FieldEncoder, FieldDecoder or both (Field)
}

func (Opt) ReadFrom

func (o Opt) ReadFrom(r io.Reader) (int64, error)

func (Opt) WriteTo

func (o Opt) WriteTo(w io.Writer) (int64, error)

type Packet

type Packet struct {
	ID   int32
	Data []byte
}

Packet define a net data package

func Marshal

func Marshal(id int32, fields ...FieldEncoder) (pk Packet)

Marshal generate Packet with the ID and Fields

func (*Packet) Pack

func (p *Packet) Pack(w io.Writer, threshold int) error

Pack 打包一个数据包

func (Packet) Scan

func (p Packet) Scan(fields ...FieldDecoder) error

Scan decode the packet and fill data into fields

Example (JoinGame)
package main

import (
	"fmt"

	_ "embed"
	pk "github.com/ArkPixel/go-mc/net/packet"
)

//go:embed joingame_test.bin
var testJoinGameData []byte

func main() {
	p := pk.Packet{ID: 0x24, Data: testJoinGameData}
	var (
		EID            pk.Int
		Hardcore       pk.Boolean
		Gamemode       pk.UnsignedByte
		PreGamemode    pk.Byte
		WorldCount     pk.VarInt
		WorldNames     = make([]pk.Identifier, 0) // This cannot replace with "var WorldNames []pk.Identifier" because "nil" has no type information
		DimensionCodec struct {
			DimensionType interface{} `nbt:"minecraft:dimension_type"`
			WorldgenBiome interface{} `nbt:"minecraft:worldgen/biome"`
		}
		Dimension                 interface{}
		WorldName                 pk.Identifier
		HashedSeed                pk.Long
		MaxPlayers                pk.VarInt
		ViewDistance              pk.VarInt
		RDI, ERS, IsDebug, IsFlat pk.Boolean
	)
	err := p.Scan(
		&EID,
		&Hardcore,
		&Gamemode,
		&PreGamemode,
		&WorldCount,
		pk.Ary{
			Len: &WorldCount,
			Ary: &WorldNames,
		},
		pk.NBT(&DimensionCodec),
		pk.NBT(&Dimension),
		&WorldName,
		&HashedSeed,
		&MaxPlayers,
		&ViewDistance,
		&RDI, &ERS, &IsDebug, &IsFlat,
	)
	fmt.Print(err)
}
Output:

<nil>

func (*Packet) UnPack

func (p *Packet) UnPack(r io.Reader, threshold int) error

UnPack in-place decompression a packet

type Position

type Position struct {
	X, Y, Z int
}

Position x as a 26-bit integer, followed by y as a 12-bit integer, followed by z as a 26-bit integer (all signed, two's complement)

func (*Position) ReadFrom

func (p *Position) ReadFrom(r io.Reader) (n int64, err error)

Decode a Position

func (Position) WriteTo

func (p Position) WriteTo(w io.Writer) (n int64, err error)

Encode a Position

type Short

type Short int16

Short is signed 16-bit integer, two's complement

func (*Short) ReadFrom

func (s *Short) ReadFrom(r io.Reader) (n int64, err error)

Decode a Short

func (Short) WriteTo

func (s Short) WriteTo(w io.Writer) (int64, error)

Encode a Signed Short

type String

type String string

String is sequence of Unicode scalar values

func (*String) ReadFrom

func (s *String) ReadFrom(r io.Reader) (n int64, err error)

Decode a String

func (String) WriteTo

func (s String) WriteTo(w io.Writer) (int64, error)

Encode a String

type Tuple

type Tuple []interface{} // FieldEncoder, FieldDecoder or both (Field)

func (Tuple) ReadFrom

func (t Tuple) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom read Tuple from io.Reader, panic when any of field don't implement FieldDecoder

func (Tuple) WriteTo

func (t Tuple) WriteTo(w io.Writer) (n int64, err error)

WriteTo write Tuple to io.Writer, panic when any of filed don't implement FieldEncoder

type UUID

type UUID uuid.UUID

UUID encoded as an unsigned 128-bit integer

func (*UUID) ReadFrom

func (u *UUID) ReadFrom(r io.Reader) (n int64, err error)

Decode a UUID

func (UUID) WriteTo

func (u UUID) WriteTo(w io.Writer) (n int64, err error)

Encode a UUID

type UnsignedByte

type UnsignedByte uint8

UnsignedByte is unsigned 8-bit integer

func (*UnsignedByte) ReadFrom

func (u *UnsignedByte) ReadFrom(r io.Reader) (n int64, err error)

Decode a UnsignedByte

func (UnsignedByte) WriteTo

func (u UnsignedByte) WriteTo(w io.Writer) (n int64, err error)

Encode a UnsignedByte

type UnsignedShort

type UnsignedShort uint16

UnsignedShort is unsigned 16-bit integer

func (*UnsignedShort) ReadFrom

func (us *UnsignedShort) ReadFrom(r io.Reader) (n int64, err error)

Decode a UnsignedShort

func (UnsignedShort) WriteTo

func (us UnsignedShort) WriteTo(w io.Writer) (int64, error)

Encode a Unsigned Short

type VarInt

type VarInt int32

VarInt is variable-length data encoding a two's complement signed 32-bit integer

func (*VarInt) ReadFrom

func (v *VarInt) ReadFrom(r io.Reader) (n int64, err error)

Decode a VarInt

func (VarInt) WriteTo

func (v VarInt) WriteTo(w io.Writer) (n int64, err error)

Encode a VarInt

type VarLong

type VarLong int64

VarLong is variable-length data encoding a two's complement signed 64-bit integer

func (*VarLong) ReadFrom

func (v *VarLong) ReadFrom(r io.Reader) (n int64, err error)

Decode a VarLong

func (VarLong) WriteTo

func (v VarLong) WriteTo(w io.Writer) (n int64, err error)

Encode a VarLong

Jump to

Keyboard shortcuts

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