Class NbtUtils

java.lang.Object
net.xun.lib.common.internal.misc.NbtUtils

@Internal public class NbtUtils extends Object
Core NBT serialization utilities with extended type support and reflection capabilities.

Provides bidirectional conversion between Java objects and NBT structures, handling:

  • Primitives and their boxed counterparts
  • String, UUID, and NBT array types
  • Common Java collections (Iterable, Map with string keys)
  • Objects with fields annotated by PersistentNbt

Used in conjunction with INbtAdapter for custom serialization logic. When processing custom objects, fields must be accessible and annotated with PersistentNbt to participate in serialization. Reflection failures during custom object handling are silently ignored, potentially resulting in IllegalArgumentException if no valid fields are found.

  • Constructor Details

    • NbtUtils

      public NbtUtils()
  • Method Details

    • writeField

      public static net.minecraft.nbt.Tag writeField(Object value)
      Serializes an object to NBT with complete type preservation and structure.
      Parameters:
      value - Non-null object to serialize. Supported types include primitives, strings, UUIDs, arrays, collections, maps with string keys, and objects with PersistentNbt annotations
      Returns:
      NBT representation of the input object
      Throws:
      IllegalArgumentException - If null is provided, or if the type hierarchy contains unsupported types not handled by registered INbtAdapters
    • readField

      public static Object readField(net.minecraft.nbt.Tag tag)
      Reconstructs an object from NBT data with automatic type resolution.
      Parameters:
      tag - NBT data structure to deserialize. Must match the expected format for the corresponding Java type
      Returns:
      Reconstructed Java object. Specific return types include:
      • Primitive wrappers for numeric tags
      • String for string tags
      • Arrays for array tags
      • HashMap for compound tags (unless UUID structure detected)
      • ArrayList for list tags
      Throws:
      IllegalArgumentException - If the tag structure doesn't match expected formats for type reconstruction