Skip to content

Commit

Permalink
Fix compound lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Bradley Watton committed Oct 22, 2020
1 parent d2fe201 commit b96692b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ dist

# Compiled
lib/

test/
31 changes: 29 additions & 2 deletions src/BinaryData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BinaryData as BData, DataLengths } from '@strdst/utils.binary'
import { EndTag, ListTag, Tag, TagMapper, TagType } from '@strdst/utils.nbt'
import { CompoundTag, EndTag, ListTag, Tag, TagMapper, TagType } from '@strdst/utils.nbt'

export class BinaryData extends BData {

Expand Down Expand Up @@ -31,7 +31,7 @@ export class BinaryData extends BData {
case TagType.Int:
return this.readTagArray('readInt', 'readInt')
case TagType.Compound:
return this.readTagArray('readTag', 'readInt')
return this.readCompoundList()
default:
throw new Error(`Don't know how to read ListTag.valueType of ${list.valueType}`)
}
Expand All @@ -46,6 +46,33 @@ export class BinaryData extends BData {
return super.readTagValue(tag)
}

public readCompoundList(): CompoundTag[] {
const count = this.readInt()

let gotEnd = false

const compounds: CompoundTag[] = []
while(!gotEnd) {
const tag = new CompoundTag()

const tags: Record<string, Tag> = {}
for(let i = 0; i < count; i++) {
const tag = this.readTag()

if(tag instanceof EndTag) {
gotEnd = true
continue
}

tags[tag.name] = tag
}

compounds.push(tag.assign('', tags))
}

return compounds
}

public readUnsignedInt(skip = true): number {
const v = this.buf.readUInt32BE(this.pos)

Expand Down

0 comments on commit b96692b

Please sign in to comment.