You can define custom interfaces extending NBTProxy to create type-safe wrappers around NBT data. The API interprets method names starting with has, get, or set as NBT operations.
Basic Proxy
Methods like hasKills() map to nbt.hasTag("kills"), setKills(int) maps to nbt.setInteger("kills", amount), and getKills() maps to nbt.getInteger("kills").
Advanced Mapping with @NBTTarget
Use the @NBTTarget annotation to specify the operation type (Type.GET, Type.SET, etc.) and the specific NBT key for a method. This allows a getter to return another NBTProxy interface nested under a specific key.
Custom Data Types and Handlers
To support complex types like ItemStack, you must override the init() method in your interface and register the appropriate handler from the NBTHandlers class.
// Basic Proxy
interface TestInterface extends NBTProxy {
boolean hasKills();
void setKills(int amount);
int getKills();
}
// Nested Proxy with @NBTTarget
interface TestInterface extends NBTProxy {
@NBTTarget(type = Type.GET, value = "other")
PointsInterface getOtherInterface();
}
interface PointsInterface extends NBTProxy {
int getPoints();
void setPoints(int points);
}
// Proxy with Custom Handlers
interface TestInterface extends NBTProxy {
@Override
default void init() {
registerHandler(ItemStack.class, NBTHandlers.ITEM_STACK);
registerHandler(ReadableNBT.class, NBTHandlers.STORE_READABLE_TAG);
registerHandler(ReadWriteNBT.class, NBTHandlers.STORE_READWRITE_TAG);
}
ItemStack getItem();
void setItem(ItemStack item);
}