Skip to content

1.16.5 changes #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions encoding/short.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package encoding

import (
"encoding/binary"
"io"
)

type Short int16

func (s *Short) Write(w io.Writer) error {
return binary.Write(w, binary.BigEndian, s)
}

func (s *Short) Read(r io.Reader) error {
return binary.Read(r, binary.BigEndian, s)
}
66 changes: 66 additions & 0 deletions encoding/short_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package encoding

import (
"bytes"
"math"
"testing"
)

func TestWriteShort(t *testing.T) {
tests := []struct {
Value Short
Expected []byte
}{
{Value: 0, Expected: []byte{0x00, 0x00}},
{Value: 5, Expected: []byte{0x00, 0x05}},
{Value: math.MaxInt8 + 1, Expected: []byte{0x00, 0x80}},
{Value: math.MaxInt16 / 2, Expected: []byte{0x3f, 0xff}},
{Value: math.MaxInt16, Expected: []byte{0x7f, 0xff}},
}

var buff bytes.Buffer

for _, test := range tests {
if err := test.Value.Write(&buff); err != nil {
t.Error(err)
}

actual := buff.Bytes()

if bytes.Compare(test.Expected, actual) != 0 {
// Not equal
t.Errorf("Unable to convert %d: %v != %v", test.Value, actual, test.Expected)
}
}
}

func TestReadShort(t *testing.T) {
tests := []struct {
Expected Short
Value []byte
}{
{Expected: 0, Value: []byte{0x00, 0x00}},
{Expected: 5, Value: []byte{0x00, 0x05}},
{Expected: math.MaxInt8 + 1, Value: []byte{0x00, 0x80}},
{Expected: math.MaxInt16 / 2, Value: []byte{0x3f, 0xff}},
{Expected: math.MaxInt16, Value: []byte{0x7f, 0xff}},
}

var buff bytes.Buffer

for _, test := range tests {
buff.Write(test.Value)

var actual Short
if err := actual.Read(&buff); err != nil {
t.Error(err)
}

if actual != test.Expected {
// Not equal
t.Errorf("Unable to convert %v: %d != %d", test.Value, actual, test.Expected)
}

buff.Reset()
}
}
20 changes: 3 additions & 17 deletions encoding/uuid.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
package encoding

import (
"encoding/binary"
"github.com/google/uuid"
"io"
)

type UUID uuid.UUID

func (u *UUID) Write(w io.Writer) error {
str := String(uuid.UUID(*u).String())
return str.Write(w)
return binary.Write(w, binary.BigEndian, u)
}

func (u *UUID) Read(r io.Reader) error {
var err error
var str String
return binary.Read(r, binary.BigEndian, u)

if err = str.Read(r); err != nil {
return err
}

uid, err := uuid.Parse(string(str))

if err != nil {
return err
}

*u = UUID(uid)

return nil
}
71 changes: 71 additions & 0 deletions encoding/uuid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package encoding

import (
"bytes"
"github.com/google/uuid"
"testing"
)

func TestWriteUUID(t *testing.T) {
tests := []struct {
Value UUID
Expected []byte
}{
{
Value: UUID(uuid.Nil),
Expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
Value: UUID(uuid.MustParse("70d651be-e8c1-4214-951e-fe92dea07cb6")),
Expected: []byte{0x70, 0xd6, 0x51, 0xbe, 0xe8, 0xc1, 0x42, 0x14, 0x95, 0x1e, 0xfe, 0x92, 0xde, 0xa0, 0x7c, 0xb6},
},
}

var buff bytes.Buffer

for _, test := range tests {
if err := test.Value.Write(&buff); err != nil {
t.Error(err)
}

actual := buff.Bytes()

if bytes.Compare(test.Expected, actual) != 0 {
// Not equal
t.Errorf("Unable to convert %d: %v != %v", test.Value, actual, test.Expected)
}
}
}

func TestReadUUID(t *testing.T) {
tests := []struct {
Expected UUID
Value []byte
}{
{
Expected: UUID(uuid.Nil),
Value: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
Expected: UUID(uuid.MustParse("70d651be-e8c1-4214-951e-fe92dea07cb6")),
Value: []byte{0x70, 0xd6, 0x51, 0xbe, 0xe8, 0xc1, 0x42, 0x14, 0x95, 0x1e, 0xfe, 0x92, 0xde, 0xa0, 0x7c, 0xb6},
},
}
var buff bytes.Buffer

for _, test := range tests {
buff.Write(test.Value)

var actual UUID
if err := actual.Read(&buff); err != nil {
t.Error(err)
}

if actual != test.Expected {
// Not equal
t.Errorf("Unable to convert %v: %d != %d", test.Value, actual, test.Expected)
}

buff.Reset()
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ require (
github.com/dave/jennifer v1.4.1
github.com/google/uuid v1.1.1
github.com/stretchr/testify v1.7.0
github.com/yuin/stagparser v0.0.0-20181218160030-e10a81132760
golang.org/x/tools v0.1.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/stagparser v0.0.0-20181218160030-e10a81132760 h1:i+m5+M2renk/OmDHvzRjlBQXm+5X6WR6xPjWfHNzvcM=
github.com/yuin/stagparser v0.0.0-20181218160030-e10a81132760/go.mod h1:+qbo7cNcx8dT/77C41x4MZbasDrLuUDI/04ZGR/7IqM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
6 changes: 6 additions & 0 deletions nbt/byte.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nbt

import enc "github.com/Raqbit/mcproto/encoding"

var WriteTagByte = enc.WriteByte
var ReadTagByte = enc.ReadByte
21 changes: 21 additions & 0 deletions nbt/byte_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package nbt

import "io"

func WriteTagByteArray(value []byte) []byte {
b := WriteTagInt(int32(len(value)))
b = append(b, value...)
return b
}

func ReadTagByteArray(r io.Reader) ([]byte, error) {
l, err := ReadTagInt(r)

if err != nil {
return nil, err
}

buf := make([]byte, int(l))
_, err = io.ReadFull(r, buf)
return buf, err
}
6 changes: 6 additions & 0 deletions nbt/compound.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nbt

type NamedTag struct {
typ TagType
name string
}
6 changes: 6 additions & 0 deletions nbt/double.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nbt

import enc "github.com/Raqbit/mcproto/encoding"

var WriteTagDouble = enc.WriteDouble
var ReadTagDouble = enc.ReadDouble
6 changes: 6 additions & 0 deletions nbt/float.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nbt

import enc "github.com/Raqbit/mcproto/encoding"

var WriteTagFloat = enc.WriteFloat
var ReadTagFloat = enc.ReadFloat
6 changes: 6 additions & 0 deletions nbt/int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nbt

import enc "github.com/Raqbit/mcproto/encoding"

var WriteTagInt = enc.WriteInt
var ReadTagInt = enc.ReadInt
29 changes: 29 additions & 0 deletions nbt/int_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nbt

import "io"

func WriteTagIntArray(value []int32) []byte {
b := WriteTagInt(int32(len(value)))
for _, i := range value {
b = append(b, WriteTagInt(i)...)
}
return b
}

func ReadTagIntArray(r io.Reader) ([]int32, error) {
length, err := ReadTagInt(r)

if err != nil {
return nil, err
}

buf := make([]int32, int(length))

for i := 0; i < int(length); i++ {
if buf[i], err = ReadTagInt(r); err != nil {
return nil, err
}
}

return buf, err
}
6 changes: 6 additions & 0 deletions nbt/long.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nbt

import enc "github.com/Raqbit/mcproto/encoding"

var WriteTagLong = enc.WriteLong
var ReadTagLong = enc.ReadLong
29 changes: 29 additions & 0 deletions nbt/long_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nbt

import "io"

func WriteTagLongArray(value []int64) []byte {
b := WriteTagInt(int32(len(value)))
for _, i := range value {
b = append(b, WriteTagLong(i)...)
}
return b
}

func ReadTagLongArray(r io.Reader) ([]int64, error) {
length, err := ReadTagInt(r)

if err != nil {
return nil, err
}

buf := make([]int64, int(length))

for i := 0; i < int(length); i++ {
if buf[i], err = ReadTagLong(r); err != nil {
return nil, err
}
}

return buf, err
}
6 changes: 6 additions & 0 deletions nbt/short.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nbt

import enc "github.com/Raqbit/mcproto/encoding"

var WriteTagShort = enc.WriteShort
var ReadTagShort = enc.ReadShort
23 changes: 23 additions & 0 deletions nbt/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nbt

import (
enc "github.com/Raqbit/mcproto/encoding"
"io"
)

func WriteTagString(str string) []byte {
b := enc.WriteUnsignedShort(uint16(len(str)))
b = append(b, []byte(str)...)
return b
}

func ReadTagString(r io.Reader) (string, error) {
s, err := enc.ReadUnsignedByte(r)
if err != nil {
return "", nil
}

stringBuff := make([]byte, int(s))
_, err = io.ReadFull(r, stringBuff)
return string(stringBuff), err
}
22 changes: 22 additions & 0 deletions nbt/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nbt

//go:generate stringer -type=TagType -output tags_string.go -linecomment

// TagType is the type of an NBT tag
type TagType byte

const (
TagTypeEnd TagType = 0 // End
TagTypeByte TagType = 1 // Byte
TagTypeShort TagType = 2 // Short
TagTypeInt TagType = 3 // Int
TagTypeLong TagType = 4 // Long
TagTypeFloat TagType = 5 // Float
TagTypeDouble TagType = 6 // Double
TagTypeByteArray TagType = 7 // Byte Array
TagTypeString TagType = 8 // String
TagTypeList TagType = 9 // List
TagTypeCompound TagType = 10 // Compound
TagTypeIntArray TagType = 11 // Int Array
TagTypeLongArray TagType = 12 // Long Array
)
Loading