Skip to content

Using Writer

SNBT comes with a powerful SNbtWriter that allows you to write SNBT values, from simple values like TAG_Byte to complex values like multiple nested components.

Implementing the Interface

Warning

Do not use ISNbtValue. It is deprecated, and hurts performance due to operating on string appending.

Most of times, you implement ISNbtWritable for a data type that can be converted to SNBT through SNbtWriter.

Writing Values

Compounds

First, write the beginning of a compound. This will write a left-curly-brace ({) character and reset the comma status.

This can be done by:

writer.WriteBeginCompound();

Then, write the properties:

writer.WriteProperty("property_a", "value");
writer.WriteProperty("property_b", 12345);

And finally, write the end of a compound (the right curly brace, }):

writer.WriteEndCompound();

You will get the following component:

{property_a:"value",property_b:12345}

Technical Details

Comma

The writer keeps an internal flag to be known as the "comma status".

When you need to write a comma, either explicitly or implicitly through writing properties, the writer checks the comma status. If it is true, the writer writes the comma, and if it is false, the writer would not write the comma but flip the comma status to true.

The comma status is also set:

  • To false when writing the beginning of a compound or list
  • To true when writing the end of a compound or list