Skip to content
Lenni0451 edited this page Oct 27, 2023 · 4 revisions

There are 12 Nbt Tags implemented:

  • ByteTag
  • ShortTag
  • IntTag
  • LongTag
  • FloatTag
  • DoubleTag
  • ByteArrayTag
  • StringTag
  • ListTag
  • CompoundTag
  • IntArrayTag
  • LongArrayTag (Added in 1.12)

Every tag has its corresponding NbtType used for identification.

Nbt Numbers

The following tags are considered a Nbt Number:

  • ByteTag
  • ShortTag
  • IntTag
  • LongTag
  • FloatTag
  • DoubleTag

Nbt Numbers have the following util methods:

  • byteValue()
  • shortValue()
  • intValue()
  • longValue()
  • floatValue()
  • doubleValue()
  • numberValue()

Reading/Writing

To read and write Nbt from/to a byte stream you can use the NbtIO class.
There is currently only one implementation for the Java Edition NbtIO.JAVA.
To create your own implementation you need two classes with the following interfaces implemented:

  • INbtReader
  • INbtWriter

Read/Write Files

Reading Files:

NbtIO.JAVA.readFile(file, nbtReadTracker); //Read an uncompressed file
NbtIO.JAVA.readCompressedFile(file, nbtReadTracker); //Read a gzip compressed file
NbtIO.JAVA.readFile(file, compressed, nbtReadTracker); //Read a file with or without compression

Writing Files:

NbtIO.JAVA.writeFile(file, rootTagName, nbtTag); //Write an uncompressed file
NbtIO.JAVA.writeCompressedFile(file, rootTagName, nbtTag); //Write a gzip compressed file
NbtIO.JAVA.writeFile(file, rootTagName, nbtTag, compressed); //Write a file with or without compression

Read/Write Streams

Reading Streams:

NbtIO.JAVA.read(inputStream, compressed, nbtReadTracker); //Read a stream with or without compression
NbtIO.JAVA.read(dataInput, nbtReadTracker); //Read a data input

Writing Streams:

NbtIO.JAVA.write(outputStream, rootTagName, nbtTag, compressed); //Write a stream with or without compression
NbtIO.JAVA.write(dataOutput, rootTagName, nbtTag); //Write a data output

NbtReadTracker

The NbtReadTracker is used to limit the size of Nbt Tags when they are read.
This prevents potential crashes when reading user generated (potentially malicious) Nbt data.

When creating a NbtReadTracker you can specify the maximum size in bytes and the maximum depth of the Nbt tree.
An IllegalStateException is thrown when the maximum size or depth is exceeded.
If you don't want to limit the size you can use NbtReadTracker.unlimited() (depth is still limited to 512).

Nameless Tags

Minecraft 1.20.2 introduced nameless tags for the network protocol.
To read/write those tags you can use the NbtIO.JAVA.readUnnamed() and NbtIO.JAVA.writeUnnamed() methods.

Clone this wiki locally