packet

package
v1.16.5-pre.0...-185f6e5 Latest Latest
Warning

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

Go to latest
Published: May 21, 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 interface{} // Value or Pointer of any integer type, only needed in ReadFrom
	Ary interface{} // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field)
}

Ary is used to send or receive the packet field like "Array of X" which has a count must be known from the context.

Typically, you must decode an integer representing the length. Then receive the corresponding amount of data according to the length. In this case, the field Len should be a pointer of integer type so the value can be updating when Packet.Scan() method is decoding the previous field. In some special cases, you might want to read an "Array of X" with a fix length. So it's allowed to directly set an integer type Len, but not a pointer.

Note that Ary DO NOT read or write the Len. You are controlling it manually.

func (Ary) ReadFrom

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

import (
	pk "github.com/RavMda/go-mc/net/packet"
)

func main() {
	var length pk.VarInt
	var data []pk.String

	var p pk.Packet // = conn.ReadPacket()
	if err := p.Scan(

		&length, // decode length first
		pk.Ary{ // then decode Ary according to length
			Len: &length,
			Ary: &data,
		},
	); err != nil {
		panic(err)
	}
}
Output:

func (Ary) WriteTo

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

import (
	pk "github.com/RavMda/go-mc/net/packet"
)

func main() {
	data := []pk.Int{0, 1, 2, 3, 4, 5, 6}
	// Len is completely ignored by WriteTo method.
	// The length is inferred from the length of Ary.
	pk.Marshal(
		0x00,
		// It's important to remember that
		// typically the responsibility of
		// sending the length field
		// is on you.
		pk.VarInt(len(data)),
		pk.Ary{
			Len: len(data), // this line can be removed
			Ary: data,
		},
	)
}
Output:

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   interface{} // Pointer of bool, or `func() bool`
	Field interface{} // FieldEncoder, FieldDecoder or both (Field)
}

func (Opt) ReadFrom

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

import (
	"fmt"

	pk "github.com/RavMda/go-mc/net/packet"
)

func main() {
	var has pk.Boolean

	var data pk.String
	p1 := pk.Packet{Data: []byte{
		0x01,                  // pk.Boolean(true)
		4, 'T', 'n', 'z', 'e', // pk.String
	}}
	if err := p1.Scan(
		&has,
		pk.Opt{
			Has: &has, Field: &data,
		},
	); err != nil {
		panic(err)
	}
	fmt.Println(data)

	var data2 pk.String = "WILL NOT BE READ, WILL NOT BE COVERED"
	p2 := pk.Packet{Data: []byte{
		0x00, // pk.Boolean(false)
		// empty
	}}
	if err := p2.Scan(
		&has,
		pk.Opt{
			Has: &has, Field: &data2,
		},
	); err != nil {
		panic(err)
	}
	fmt.Println(data2)

}
Output:

Tnze
WILL NOT BE READ, WILL NOT BE COVERED
Example (Func)
package main

import (
	"fmt"

	pk "github.com/RavMda/go-mc/net/packet"
)

func main() {
	// As an example, we define this packet as this:
	// +------+-----------------+----------------------------------+
	// | Name | Type            | Notes                            |
	// +------+-----------------+----------------------------------+
	// | Flag | Unsigned Byte   | Odd if the following is present. |
	// +------+-----------------+----------------------------------+
	// | User | Optional String | The player's name.               |
	// +------+-----------------+----------------------------------+
	// So we need a function to decide if the User field is present.
	var flag pk.Byte
	var data pk.String
	p := pk.Packet{Data: []byte{
		0b_0010_0011,          // pk.Byte(flag)
		4, 'T', 'n', 'z', 'e', // pk.String
	}}
	if err := p.Scan(
		&flag,
		pk.Opt{
			Has: func() bool {
				return flag&1 != 0
			},
			Field: &data,
		},
	); err != nil {
		panic(err)
	}
	fmt.Println(data)

}
Output:

Tnze

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/RavMda/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

Example
package main

import (
	pk "github.com/RavMda/go-mc/net/packet"
)

func main() {
	// When you need to read an "Optional Array of X":
	var has pk.Boolean
	var arylen pk.Int
	var ary []pk.String

	var p pk.Packet // = conn.ReadPacket()
	if err := p.Scan(
		&has,
		pk.Opt{
			Has: &has,
			Field: pk.Tuple{
				&arylen,
				pk.Ary{
					Len: &arylen,
					Ary: &ary,
				},
			},
		},
	); err != nil {
		panic(err)
	}
}
Output:

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