Skip to content

Commit

Permalink
nbt/encode.go: Write tagEnd for empty slices instead of tagByte.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Jun 20, 2023
1 parent 721d59b commit 98d32b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
31 changes: 16 additions & 15 deletions minecraft/nbt/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ func (e *Encoder) Encode(v any) error {
// are found in the list below.
//
// The following Go types are converted to tags as such:
// byte/uint8: TAG_Byte
// bool: TAG_Byte
// int16: TAG_Short
// int32: TAG_Int
// int64: TAG_Long
// float32: TAG_Float
// float64: TAG_Double
// [...]byte: TAG_ByteArray
// [...]int32: TAG_IntArray
// [...]int64: TAG_LongArray
// string: TAG_String
// []<type>: TAG_List
// struct{...}: TAG_Compound
// map[string]<type/any>: TAG_Compound
//
// byte/uint8: TAG_Byte
// bool: TAG_Byte
// int16: TAG_Short
// int32: TAG_Int
// int64: TAG_Long
// float32: TAG_Float
// float64: TAG_Double
// [...]byte: TAG_ByteArray
// [...]int32: TAG_IntArray
// [...]int64: TAG_LongArray
// string: TAG_String
// []<type>: TAG_List
// struct{...}: TAG_Compound
// map[string]<type/any>: TAG_Compound
//
// Marshal accepts struct fields with the 'nbt' struct tag. The 'nbt' struct tag allows setting the name of
// a field that some tag should be decoded in. Setting the struct tag to '-' means that field will never be
Expand Down Expand Up @@ -202,7 +203,7 @@ func (e *Encoder) encode(val reflect.Value, tagName string) error {
if val.Len() == 0 {
// If the slice is empty, we cannot find out the type of the interface slice. Luckily the NBT
// format allows a byte type for empty lists.
elemType = byteType
elemType = nil
} else {
// The slice is not empty, so we'll simply get the tag type from the first element.
elemType = val.Index(0).Elem().Type()
Expand Down
3 changes: 3 additions & 0 deletions minecraft/nbt/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func (t tagType) IsValid() bool {
// tagFromType matches a reflect.Type with a tag type that can hold its value. If none is found, math.MaxUint8
// is returned.
func tagFromType(p reflect.Type) tagType {
if p == nil {
return tagEnd
}
switch p.Kind() {
case reflect.Uint8, reflect.Bool:
return tagByte
Expand Down

0 comments on commit 98d32b8

Please sign in to comment.