-
Notifications
You must be signed in to change notification settings - Fork 1
NBT
NBT (Named Binary Tag) is a data format originally created by Markuss Person for Minecraft. This format can be extremely compact and usefull for storage.
ID | Name | Description |
---|---|---|
0 | End | Used to mark de end of a NBTTagCompound |
1 | Byte | Store a signed byte |
2 | Short | Store a signed short |
3 | Int | Store a signed int |
4 | Long | Store a signed long |
5 | Float | Store a signed float |
6 | Double | Store a signed double |
7 | Byte Array | Store an array of signed bytes. Prefixed by a signed int representing its length. In this API the array become a List |
8 | String | Store a String prefixed by an unsigned short representing his length in bytes |
9 | List | Represent a List of nameless NBT Tags. Prefixed by the type of the contained NBT and the length of the list |
10 | Compound | Represent a list of named NBT Tags. It can be compared to a Map. Note all NBT files represent a NBTTagCompound |
11 | Int Array | Store an array of signed integers. Prefixed by a signed int representing its length. In this API the array become a List |
Firstly, you need to get the entity's NBTTagCompound by using NBTManager#getNBTTag(Entity).
NBTManager nbtManager = SpigotMetadataAPI.getAPI().getNBTManager();
NBTTagCompound nbtTag = nbtManager.getNBTTag(entity);
Now you can edit the entity's NBTTagCompound.
nbtTag.setInt("Fire", 100); //Ignites the entity for 100 ticks (5 seconds)
Now, you need to update the entity's tags.
nbtManager.setNBTTag(entity, nbtTag);
Done !
Since 2.0, there is an I/O support for NBT Tags. With GZIP, data become extremely lightweight. This storing way can be used to store a lot of data or to send data.
You can read .dat files by using NBTInputStream. It works as a normal DataInputStream with new methods like readTag() or readNamedTag(). readTag() return a NBTBase and readNamedTag() a NamedNBT. Note: Minecraft vanilla nbt files contain only a named NBTTagCompound containing all data and are often GZIPped.
You can write in nbt files by using NBTOuputStream. It add writeTag(NBTBase) and writeNamedTag(NamedNBT) methods.