-
Notifications
You must be signed in to change notification settings - Fork 4
NBT
from python_nbt import nbt
This is the basic module of this package. It contains everything you need to read, edit and write NBT format files such as *.mca, *.dat inside Minecraft saves folder, *.nbt, and so on. Also, it can export your NBT to json and import them json!
Also named and can be referred as "TAG_Base", which is the name on Minecraft Wiki. This is the super class for all other NBT types and as a result, DO NOT instanciate this class directly. Use its subclasses instead.
NBTTagBase(value=None, **kwargs)
Parameter | Default Value | Description |
---|---|---|
value |
None | value to initialize. If value has attribute read , it will be treated as buffer. |
buffer |
None | buffer of binary data to be read from |
Name | Description | Get | Set |
---|---|---|---|
type_id |
NBT type id, always an int
|
✔ | |
value |
Value stored in NBT | ✔ |
Return an object that can be accepted by json
module built in Python.
Parameter | Default Value | Description |
---|---|---|
full_json |
True |
When is True by default, function will return a dict containing tag type id and its values. When set as False , function will return an object that omits tag type id and can be accepted by json module containing only this tag's value. |
For example, in IDLE
>>> nbt_str = nbt.NBTTagString('I\'m a string!')
>>> nbt_str.json_obj()
{'type_id': 8, 'value': "I'm a string!"}
>>> a.json_obj(False)
"I'm a string!"
Equals when:
- NBTTagBase or its subclasses:
True
for sametype_id
and samevalue
,False
for otherwise. - Other types:
True
whenvalue
equalsobj
,False
for otherwise
Of course !=
is also overriden automatically.
Returns str(tag.json_obj())
Returns repr(tag.json_obj())
Contains NBTTagByte
, NBTTagShort
, NBTTagInt
, NBTTagLong
, NBTTagFloat
, NBTTagDouble
, and NBTTagString
.
Added validator for values.
Trying to input invalid values will raise a ValueError
.
NBTTagBase
Same as NBTTagBase.
Name | Description | Get | Set |
---|---|---|---|
value |
Value stored in NBT | ✔ | ✔ |
Class | Description | Value Requirements |
---|---|---|
NBTTagByte | Class for TAG_Byte | Integer in range(-128, 128)
|
NBTTagShort | Class for TAG_Short | Integer in range(-65536, 65536)
|
NBTTagInt | Class for TAG_Int | Integer in range(-2,147,483,648, 2,147,483,648)
|
NBTTagLong | Class for TAG_Long | Interger in range(-9,223,372,036,854,775,808, 9,223,372,036,854,775,808)
|
NBTTagFloat | Class for TAG_Float | float |
NBTTagDouble | Class for TAG_Double | float |
NBTTagString | Class for TAG_String | String with 65535 bytes of data or less |
Contains NBTTagByteArray
, NBTTagIntArray
, and NBTTagLongArray
. Added validator for entries.
Trying to input invalid entries will raise a ValueError
.
These classes are also subclasses of list
, so you can add, edit, delete, get items like doing so in a list.
NBTTagBase
, _util.RestrictedList
Same as NBTTagBase, but value
accepts iterable objects.
Name | Description | Get | Set |
---|---|---|---|
value |
Value list stored in NBT | ✔ |
Class | Description | Valid entries |
---|---|---|
NBTTagByteArray | Class for TAG_Byte_Array | integer in range(-128, 128)
|
NBTTagIntArray | Class for TAG_Int_Array | integer in range(-2,147,483,648, 2,147,483,648)
|
NBTTagLongArray | Class for TAG_Long_Array | interger in range(-9,223,372,036,854,775,808, 9,223,372,036,854,775,808)
|
Added validator for elements inside, if applied invalid values, it will raise an ValueError
.
This class is also a subclass of list
, so feel free to use them like list
s.
NBTTagBase
, _util.RestrictedList
NBTTagList(value=None, **kwargs)
Same as NBTTagBase, but must specify tag_type
if not reading from buffer.
tag_type
should be a subclass of NBTTagBase.
Name | Description | Get | Set |
---|---|---|---|
value |
Value list stored in NBT | ✔ | |
tag_type |
Valid value type | ✔ | |
tag_type_id |
ID of valid value type | ✔ |
Entries inside must be instances of tag_type
.
A subclass of dict
, so you can add, edit, delete entries like doing so to a dict
.
Added validator to ensure that keys are str
and values are NBTTagBase
.
Trying to input invalid keys or values will raise ValueError
.
NBTTagBase
, _util.TypeRestrictedDict
NBTTagCompound(value=None, **kwargs)
Same as NBTTagBase.
value
must be an instance of dict
whose keys must be instances of str
and values must be instances of NBTTagBase
if you want to initialize it with value, or an object with attribute read
to read from binary data.
Name | Description | Get | Set |
---|---|---|---|
value |
Entries dict stored in NBT | ✔ | ✔ |
Read from NBT file. Returns an NBTTagBase.
file
can be path to file, or an file pointer with mode 'rb'
.
Write an NBT tag
to binary file.
file
can be path to file, or an file pointer with mode 'wb'
.
tag
must be an NBTTagCompound
.
name
can be whatever string you want, this will be the name of root tag of this file. However, this is not used in Minecraft or mods of the game, so leave it default.
Return an NBT object according to json_obj
.
And its argument must be as same format as what NBTTagBase.json_obj(full_json=True)
exports.
TAG_END = 0
TAG_BYTE = 1
TAG_SHORT = 2
TAG_INT = 3
TAG_LONG = 4
TAG_FLOAT = 5
TAG_DOUBLE = 6
TAG_BYTE_ARRAY = 7
TAG_STRING = 8
TAG_LIST = 9
TAG_COMPOUND = 10
TAG_INT_ARRAY = 11
TAG_LONG_ARRAY = 12
TAGLIST
: A dict maps ids of different types NBT to their classes.
For compatibility with names on Minecraft Wiki, just in case.
TAG = NBTTagBase
TAG_Byte = NBTTagByte
TAG_Short = NBTTagShort
TAG_Int = NBTTagInt
TAG_Long = NBTTagLong
TAG_Float = NBTTagFloat
TAG_Double = NBTTagDouble
TAG_Byte_Array = NBTTagByteArray
TAG_Int_Array = NBTTagIntArray
TAG_Long_Array = NBTTagLongArray
TAG_String = NBTTagString
TAG_List = NBTTagList
TAG_Compound = NBTTagCompound
TAG_End = NBTTagEnd
- Home
-
NBT
-
Classes
- NBTTagBase (TAG_Base)
-
Single Value NBT
- NBTTagByte (TAG_Byte)
- NBTTagShort (TAG_Short)
- NBTTagInt (TAG_Int)
- NBTTagLong (TAG_Long)
- NBTTagFloat (TAG_Float)
- NBTTagDouble (TAG_Double)
- NBTTagString (TAG_String)
-
Array-like NBT
- NBTTagByteArray (TAG_Byte_Array)
- NBTTagIntArray (TAG_Int_Array)
- NBTTagLongArray (TAG_Long_Array)
- NBTTagList (TAG_List)
- NBTTagCompound (TAG_Compound)
- Functions
- Constants
-
Classes