Package net.xun.lib.common.internal.misc
Class NbtUtils
java.lang.Object
net.xun.lib.common.internal.misc.NbtUtils
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,Mapwith 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectreadField(net.minecraft.nbt.Tag tag) Reconstructs an object from NBT data with automatic type resolution.static net.minecraft.nbt.TagwriteField(Object value) Serializes an object to NBT with complete type preservation and structure.
-
Constructor Details
-
NbtUtils
public NbtUtils()
-
-
Method Details
-
writeField
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 withPersistentNbtannotations- Returns:
- NBT representation of the input object
- Throws:
IllegalArgumentException- If null is provided, or if the type hierarchy contains unsupported types not handled by registeredINbtAdapters
-
readField
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:
- Throws:
IllegalArgumentException- If the tag structure doesn't match expected formats for type reconstruction
-