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

Nbt

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.

Read/Write Files

Reading Files:

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

Writing Files:

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

Read/Write Streams

Reading Streams:

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

Writing Streams:

NbtIO.write(outputStream, rootTagName, nbtTag, compressed); //Write a stream with or without compression
NbtIO.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).

Clone this wiki locally