Interface INbtAdapter<T>

Type Parameters:
T - The exact type this adapter serializes. Must match the registration type precisely.
All Known Implementing Classes:
GenericNbtAdapter

public interface INbtAdapter<T>
Defines a type-specific serialization contract for converting objects to/from NBT format.

Implementations provide custom persistence logic for complex types that cannot be automatically handled by reflection-based serialization in NbtUtils. Registered adapters take precedence over default serialization mechanisms, allowing fine-grained control over specific types.

The system falls back to GenericNbtAdapterwhich uses NbtUtils's reflection-based approach to handle common Java types.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> INbtAdapter<T>
    Retrieves the most appropriate adapter for a given type from the registry.
    Returns the exact class this adapter is designed to handle.
    load(net.minecraft.nbt.Tag tag)
    Reconstructs an object instance from its NBT representation.
    net.minecraft.nbt.Tag
    save(T value)
    Converts an object instance into its NBT representation.
  • Method Details

    • getTargetType

      Class<T> getTargetType()
      Returns the exact class this adapter is designed to handle.

      Used by the adapter registry for type matching. The registry will use this adapter for all serialization tasks where Class.equals() matches the target type exactly.

    • save

      net.minecraft.nbt.Tag save(T value)
      Converts an object instance into its NBT representation.
      Parameters:
      value - Non-null instance to serialize. Implementations may assume valid non-null input when called through standard serialization pathways
      Returns:
      Non-null NBT structure containing complete object state. Must be compatible with load(Tag) for reconstruction
      Throws:
      IllegalArgumentException - If the object contains unsupported data types or violates serialization constraints
    • load

      T load(net.minecraft.nbt.Tag tag)
      Reconstructs an object instance from its NBT representation.
      Parameters:
      tag - Non-null NBT data structure created by save(T). Must maintain the same structural format as originally serialized
      Returns:
      Fully reconstructed object instance. May return null only if serialization explicitly preserved a null reference (not typical in standard use)
      Throws:
      ClassCastException - If the tag structure doesn't match the expected format
      IllegalArgumentException - If tag contains invalid or incompatible data
    • getAdapterFor

      static <T> INbtAdapter<T> getAdapterFor(Class<T> type)
      Retrieves the most appropriate adapter for a given type from the registry.

      Performs automatic adapter discovery using the following priority:

      1. Exact match for registered adapter type
      2. GenericNbtAdapter as fallback for unregistered types
      Parameters:
      type - Target class needing serialization support
      Returns:
      Non-null adapter instance capable of handling the specified type
      Throws:
      IllegalArgumentException - If no adapter exists and NbtUtils cannot handle the type through the generic fallback