Class TagWriter<T>

java.lang.Object
java.io.Writer
com.saicone.nbt.io.TagWriter<T>
Type Parameters:
T - the tag object implementation.
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class TagWriter<T> extends Writer
Writes tag objects into delegated Writer as SNBT format.
The formatting method aims to be compatible with older Minecraft versions by avoiding the usage of single quotes.
See Also:
  • Constructor Details

  • Method Details

    • of

      Create a tag writer that accepts nbt-represented java objects with provided Writer.
      Parameters:
      writer - the delegated writer to append characters.
      Returns:
      a newly generated tag writer.
    • of

      @NotNull public static <T> @NotNull TagWriter<T> of(@NotNull @NotNull Writer writer, @NotNull @NotNull TagMapper<T> mapper)
      Create a tag writer with provided Writer and TagMapper.
      Type Parameters:
      T - the tag object implementation.
      Parameters:
      writer - the delegated writer to append characters.
      mapper - the mapper to extract values from tags
      Returns:
      a newly generated tag writer.
    • isUnquoted

      protected boolean isUnquoted(@NotNull @NotNull String s)
      Check if the provided string should be unquoted.
      Parameters:
      s - the string to check.
      Returns:
      true if the string is unquoted, false otherwise.
    • isUnquoted

      protected boolean isUnquoted(char c)
      Check if the provided char is an allowed unquoted character.
      Parameters:
      c - the char to check.
      Returns:
      true if the char should be unquoted, false otherwise.
    • getWriter

      @NotNull public @NotNull Writer getWriter()
      Get the delegated writer.
      Returns:
      a writer that is used to append characters.
    • getMapper

      @NotNull public @NotNull TagMapper<T> getMapper()
      Get the mapper that is used to extract values.
      Returns:
      a tag mapper.
    • writeTag

      public void writeTag(@Nullable T t) throws IOException
      Write to provided tag object.
      Parameters:
      t - the tag object to write.
      Throws:
      IOException - if any I/O exception occurs.
    • writePrimitiveTag

      public <V> void writePrimitiveTag(@NotNull @NotNull TagType<V> type, @NotNull V v) throws IOException
      Write to provided primitive tag value with associated type.
      Type Parameters:
      V - the nbt-represented value type implementation.
      Parameters:
      type - the type of tag.
      v - the tag value to write.
      Throws:
      IOException - if any I/O exception occurs.
    • writeStringTag

      public void writeStringTag(@NotNull @NotNull String s) throws IOException
      Write string tag value, any " will be replaced with \" to match SNBT compatibility.
      Parameters:
      s - the tag value to write.
      Throws:
      IOException - if any I/O exception occurs.
    • writeByteArrayTag

      public void writeByteArrayTag(byte[] bytes) throws IOException
      Write byte array tag value.
      Parameters:
      bytes - the tag value to write.
      Throws:
      IOException - if any I/O exception occurs.
    • writeBooleanArrayTag

      public void writeBooleanArrayTag(boolean[] booleans) throws IOException
      Write fake boolean array tag value.
      Parameters:
      booleans - the tag value to write.
      Throws:
      IOException - if any I/O exception occurs.
    • writeIntArrayTag

      public void writeIntArrayTag(int[] ints) throws IOException
      Write int array tag value.
      Parameters:
      ints - the tag value to write.
      Throws:
      IOException - if any I/O exception occurs.
    • writeLongArrayTag

      public void writeLongArrayTag(long[] longs) throws IOException
      Write long array tag value.
      Parameters:
      longs - the tag value to write.
      Throws:
      IOException - if any I/O exception occurs.
    • writeListTag

      public void writeListTag(@NotNull @NotNull List<T> list) throws IOException
      Write list of tag objects value from list tag type.
      Parameters:
      list - the tag value to write.
      Throws:
      IOException - if any I/O exception occurs.
    • writeCompoundTag

      public void writeCompoundTag(@NotNull @NotNull Map<String,T> map) throws IOException
      Write map of string-tag entries value from compound tag type.
      Parameters:
      map - the tag value to write.
      Throws:
      IOException - if any I/O exception occurs.
    • write

      public void write(@NotNull @org.jetbrains.annotations.NotNull char[] cbuf, int off, int len) throws IOException
      Specified by:
      write in class Writer
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
      Throws:
      IOException
    • toString

      @NotNull public static @NotNull String toString(@Nullable @Nullable Object object)
      Convert nbt-represented java object into SNBT.
      Parameters:
      object - the nbt-represented java object to convert.
      Returns:
      a SNBT that represent the java object.
    • toString

      @NotNull public static <T> @NotNull String toString(@Nullable T t, @NotNull @NotNull TagMapper<T> mapper)
      Convert tag object into SNBT with provided TagMapper.
      Type Parameters:
      T - the tag object implementation.
      Parameters:
      t - the tag object to convert.
      mapper - the mapper to extract value from tag.
      Returns:
      a SNBT that represent the tag object.