Refactoring package go-mc/bot

This commit is contained in:
Tnze
2021-02-27 01:06:07 +08:00
parent e864580903
commit 3da9321f59
44 changed files with 564 additions and 3715 deletions

View File

@ -1,11 +1,14 @@
package packet
package packet_test
import (
"bytes"
_ "embed"
"testing"
pk "github.com/Tnze/go-mc/net/packet"
)
var VarInts = []VarInt{0, 1, 2, 127, 128, 255, 2147483647, -1, -2147483648}
var VarInts = []pk.VarInt{0, 1, 2, 127, 128, 255, 2147483647, -1, -2147483648}
var PackedVarInts = [][]byte{
{0x00},
@ -35,7 +38,7 @@ func TestPackVarInt(t *testing.T) {
}
func TestUnpackVarInt(t *testing.T) {
for i, v := range PackedVarInts {
var vi VarInt
var vi pk.VarInt
if _, err := vi.ReadFrom(bytes.NewReader(v)); err != nil {
t.Errorf("unpack \"% x\" error: %v", v, err)
}
@ -46,7 +49,7 @@ func TestUnpackVarInt(t *testing.T) {
}
func TestUnpackVarInt_TooLongData(t *testing.T) {
var vi VarInt
var vi pk.VarInt
var data = []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
if _, err := vi.ReadFrom(bytes.NewReader(data)); err != nil {
t.Logf("unpack \"% x\" error: %v", data, err)
@ -55,7 +58,7 @@ func TestUnpackVarInt_TooLongData(t *testing.T) {
}
}
var VarLongs = []VarLong{0, 1, 2, 127, 128, 255, 2147483647, 9223372036854775807, -1, -2147483648, -9223372036854775808}
var VarLongs = []pk.VarLong{0, 1, 2, 127, 128, 255, 2147483647, 9223372036854775807, -1, -2147483648, -9223372036854775808}
var PackedVarLongs = [][]byte{
{0x00},
@ -81,7 +84,7 @@ func TestPackVarLong(t *testing.T) {
}
func TestUnpackVarLong(t *testing.T) {
for i, v := range PackedVarLongs {
var vi VarLong
var vi pk.VarLong
if _, err := vi.ReadFrom(bytes.NewReader(v)); err != nil {
t.Errorf("unpack \"% x\" error: %v", v, err)
}
@ -90,3 +93,46 @@ func TestUnpackVarLong(t *testing.T) {
}
}
}
//go:embed joingame_test.bin
var joingame []byte
func TestJoinGamePacket(t *testing.T) {
p := pk.Packet{ID: 0x24, Data: joingame}
var (
EID pk.Int
Hardcore pk.Boolean
Gamemode pk.UnsignedByte
PreGamemode pk.Byte
WorldCount pk.VarInt
WorldNames = pk.Ary{Len: &WorldCount, Ary: &[]pk.String{}}
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,
WorldNames,
&pk.NBT{V: &DimensionCodec},
&pk.NBT{V: &Dimension},
&WorldName,
&HashedSeed,
&MaxPlayers,
&ViewDistance,
&RDI, &ERS, &IsDebug, &IsFlat,
)
if err != nil {
t.Error(err)
}
}