diff --git a/nbt/snbt_decode.go b/nbt/snbt_decode.go index 96e46a9..c6c000d 100644 --- a/nbt/snbt_decode.go +++ b/nbt/snbt_decode.go @@ -105,8 +105,7 @@ func writeCompoundPayload(e *Encoder, d *decodeState) error { func writeListOrArray(e *Encoder, d *decodeState, tagName string) error { d.scanWhile(scanSkipSpace) if d.opcode == scanEndValue { // ']', empty TAG_List - e.writeTag(TagList, tagName) - e.writeInt32(0) + e.writeListHeader(TagEnd, tagName, 0) return nil } @@ -150,8 +149,8 @@ func writeListOrArray(e *Encoder, d *decodeState, tagName string) error { if d.opcode != scanListValue { panic(phasePanicMsg) } - start = d.readIndex() d.scanNext() + start = d.readIndex() d.scanWhile(scanContinue) literal = d.data[start:d.readIndex()] } @@ -159,6 +158,27 @@ func writeListOrArray(e *Encoder, d *decodeState, tagName string) error { e.w.Write(buf.Bytes()) case scanBeginList: // TAG_List case scanBeginCompound: // TAG_List + for { + d.scanWhile(scanSkipSpace) + if d.opcode != scanBeginCompound { + return errors.New("different TagType in List") + } + writeCompoundPayload(e2, d) + count++ + // read ',' or ']' + if d.opcode == scanSkipSpace { + d.scanWhile(scanSkipSpace) + } + d.scanNext() + if d.opcode == scanEndValue { + break + } + if d.opcode != scanListValue { + panic(phasePanicMsg) + } + } + e.writeListHeader(TagCompound, tagName, count) + e.w.Write(buf.Bytes()) } return nil } diff --git a/nbt/snbt_decode_test.go b/nbt/snbt_decode_test.go index 51d31c4..83b1889 100644 --- a/nbt/snbt_decode_test.go +++ b/nbt/snbt_decode_test.go @@ -32,6 +32,7 @@ func TestEncoder_WriteSNBT(t *testing.T) { {`[1b,2b,3b]`, []byte{9, 0, 0, 1, 0, 0, 0, 3, 1, 2, 3}}, {`[a,"b",'c']`, []byte{9, 0, 0, 8, 0, 0, 0, 3, 0, 1, 'a', 0, 1, 'b', 0, 1, 'c'}}, {`[{},{a:1b},{}]`, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}}, + {`[ { } , { a : 1b } , { } ] `, []byte{9, 0, 0, 10, 0, 0, 0, 3, 0, 1, 0, 1, 'a', 1, 0, 0}}, {`[[],[]]`, []byte{9, 0, 0, 9, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}}, } for i := range testCases {