下面列出了怎么用net.minecraftforge.common.util.INBTSerializable的API类实例代码及写法,或者点击链接到github查看源代码。
/** Creates and returns a primitive NBT tag from a value.
* If the value already is an NBT tag, it is returned instead. */
public static NBTBase createTag(Object value) {
if (value == null)
throw new IllegalArgumentException("value is null");
if (value instanceof NBTBase) return (NBTBase)value;
if (value instanceof INBTSerializable)
return ((INBTSerializable<?>)value).serializeNBT();
if (value instanceof Collection) return ((Collection<?>)value).stream()
.map(NbtUtils::createTag).collect(toList());
if (value instanceof Byte) return new NBTTagByte((Byte)value);
if (value instanceof Short) return new NBTTagShort((Short)value);
if (value instanceof Integer) return new NBTTagInt((Integer)value);
if (value instanceof Long) return new NBTTagLong((Long)value);
if (value instanceof Float) return new NBTTagFloat((Float)value);
if (value instanceof Double) return new NBTTagDouble((Double)value);
if (value instanceof String) return new NBTTagString((String)value);
if (value instanceof byte[]) return new NBTTagByteArray((byte[])value);
if (value instanceof int[]) return new NBTTagIntArray((int[])value);
throw new IllegalArgumentException("Can't create an NBT tag of value: " + value);
}
@SuppressWarnings("unchecked")
public CapabilityDispatcher(Map<Identifier, ICapabilityProvider> capabilities, List<Runnable> listeners, @Nullable ICapabilityProvider parent) {
List<ICapabilityProvider> providers = Lists.newArrayList();
List<INBTSerializable<Tag>> writers = Lists.newArrayList();
List<String> names = Lists.newArrayList();
// Parents go first!
if (parent != null) {
providers.add(parent);
if (parent instanceof INBTSerializable) {
writers.add((INBTSerializable<Tag>) parent);
names.add("Parent");
}
}
for (Map.Entry<Identifier, ICapabilityProvider> entry : capabilities.entrySet()) {
ICapabilityProvider provider = entry.getValue();
providers.add(provider);
if (provider instanceof INBTSerializable) {
writers.add((INBTSerializable<Tag>) provider);
names.add(entry.getKey().toString());
}
}
this.listeners = listeners;
this.providers = providers.toArray(new ICapabilityProvider[0]);
this.writers = writers.toArray(new INBTSerializable[0]);
this.names = names.toArray(new String[0]);
}
@Override
public CompoundNBT write(CompoundNBT tag) {
handler.ifPresent(h -> {
CompoundNBT compound = ((INBTSerializable<CompoundNBT>) h).serializeNBT();
tag.put("inv", compound);
});
return super.write(tag);
}
@Override
public void deserializeNBT(NBTTagCompound nbt) {
NBTTagList tanks = nbt.getTagList("Tanks", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < Math.min(fluidTanks.size(), nbt.getInteger("TankAmount")); i++) {
NBTBase nbtTag = tanks.get(i);
IFluidTank fluidTank = fluidTanks.get(i);
if (fluidTank instanceof FluidTank) {
((FluidTank) fluidTank).readFromNBT((NBTTagCompound) nbtTag);
} else if (fluidTank instanceof INBTSerializable) {
((INBTSerializable) fluidTank).deserializeNBT(nbtTag);
}
}
}
/** Returns the specified NBT serializable value
* instance deserialized from the specified NBT tag. */
public static <N extends NBTBase, T extends INBTSerializable<N>> T getTagValue(N tag, T value) {
if (tag == null) throw new IllegalArgumentException("tag is null");
if (value == null) throw new IllegalArgumentException("value is null");
value.deserializeNBT(tag);
return value;
}
/** Returns a list of NBT serializable values instantiated
* using the value supplier from the specified NBT list. */
@SuppressWarnings("unchecked")
public static <N extends NBTBase, T extends INBTSerializable<N>> List<T> getTagList(
NBTTagList list, Supplier<T> valueSupplier) {
return stream(list)
.map(tag -> getTagValue((N)tag, valueSupplier.get()))
.collect(Collectors.toList());
}
@Override
public void read(CompoundNBT tag) {
CompoundNBT invTag = tag.getCompound("inv");
handler.ifPresent(h -> ((INBTSerializable<CompoundNBT>) h).deserializeNBT(invTag));
super.read(tag);
}