net.minecraftforge.common.util.INBTSerializable Java Examples

The following examples show how to use net.minecraftforge.common.util.INBTSerializable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NbtUtils.java    From WearableBackpacks with MIT License 6 votes vote down vote up
/** 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);
}
 
Example #2
Source File: CapabilityDispatcher.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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]);
}
 
Example #3
Source File: ModificationTableTileEntity.java    From MiningGadgets with MIT License 5 votes vote down vote up
@Override
public CompoundNBT write(CompoundNBT tag) {
    handler.ifPresent(h -> {
        CompoundNBT compound = ((INBTSerializable<CompoundNBT>) h).serializeNBT();
        tag.put("inv", compound);
    });
    return super.write(tag);
}
 
Example #4
Source File: FluidTankList.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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);
        }
    }
}
 
Example #5
Source File: NbtUtils.java    From WearableBackpacks with MIT License 5 votes vote down vote up
/** 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;
}
 
Example #6
Source File: NbtUtils.java    From WearableBackpacks with MIT License 5 votes vote down vote up
/** 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());
}
 
Example #7
Source File: ModificationTableTileEntity.java    From MiningGadgets with MIT License 4 votes vote down vote up
@Override
public void read(CompoundNBT tag) {
    CompoundNBT invTag = tag.getCompound("inv");
    handler.ifPresent(h -> ((INBTSerializable<CompoundNBT>) h).deserializeNBT(invTag));
    super.read(tag);
}