I am trying to add a Compound to a List to make a Compound List:
import nbtlib
from nbtlib.tag import *
CpdList = List(Compound())
Cpd = Compound()
Cpd['name'] = String("Name")
Cpd['test'] = Byte(0)
CpdList.append(Cpd)
This returns the following error:
>>> nbtlib.tag.IncompatibleItemType: Compound({'name': String('Name'), 'test': Byte(0)}) should be a End tag
I don't understand what an End tag is, and how to solve this issue.
I tried adding an End tag to the Compound, but without success:
Cpd['End'] = End
nbtlib.tag.List
with the Compound
type and properly make a call:import nbtlib
from nbtlib.tag import Compound, List, String, Byte
CpdList = List[Compound]()
Cpd = Compound()
Cpd['name'] = String("Name")
Cpd['test'] = Byte(0)
CpdList.append(Cpd)
print(CpdList)
Note that the nbtlib.tag.List
expects the same type of elements, and the Compound
type is not directly allowed in a List
tag. The error mentions the need for an End
tag, which is a terminator for Compound
tags.
In case you need a dynamic type list, you can use python native list()
:
import nbtlib
from nbtlib.tag import Compound, String, Byte
CpdList = list[Compound]()
Cpd = Compound()
Cpd['name'] = String("Name")
Cpd['test'] = Byte(0)
CpdList += [Cpd, 0, 'alice', {1: 2}]
print(CpdList)
[Compound({'name': String('Name'), 'test': Byte(0)}), 0, 'alice', {1: 2}]
Which is same as:
import nbtlib
from nbtlib.tag import Compound, String, Byte
CpdList = list(Compound())
Cpd = Compound()
Cpd['name'] = String("Name")
Cpd['test'] = Byte(0)
CpdList += [Cpd, 0, 'alice', {1: 2}]
print(CpdList)