Search in sources :

Example 1 with INBTSerializable

use of net.minecraftforge.common.util.INBTSerializable in project minecolonies by Minecolonies.

the class AbstractCombinedItemHandler method deserializeNBT.

@SuppressWarnings({ RAWTYPES, UNCHECKED })
@Override
public void deserializeNBT(final NBTTagCompound nbt) {
    final NBTTagList handlerList = nbt.getTagList(NBT_KEY_NAME, Constants.NBT.TAG_COMPOUND);
    final NBTTagList indexList = nbt.getTagList(NBT_KEY_HANDLERS_INDEXLIST, Constants.NBT.TAG_INT);
    if (handlerList.tagCount() == handlers.length) {
        for (int i = 0; i < handlerList.tagCount(); i++) {
            final NBTTagCompound handlerCompound = handlerList.getCompoundTagAt(i);
            final IItemHandlerModifiable modifiable = handlers[indexList.getIntAt(i)];
            if (modifiable instanceof INBTSerializable) {
                final INBTSerializable serializable = (INBTSerializable) modifiable;
                serializable.deserializeNBT(handlerCompound);
            }
        }
    }
    setName(nbt.hasKey(NBT_KEY_NAME) ? nbt.getString(NBT_KEY_NAME) : null);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) INBTSerializable(net.minecraftforge.common.util.INBTSerializable)

Example 2 with INBTSerializable

use of net.minecraftforge.common.util.INBTSerializable in project minecolonies by Minecolonies.

the class AbstractCombinedItemHandler method serializeNBT.

@SuppressWarnings(RAWTYPES)
@Override
public NBTTagCompound serializeNBT() {
    final NBTTagCompound compound = new NBTTagCompound();
    int index = 0;
    final NBTTagList handlerList = new NBTTagList();
    final NBTTagList indexList = new NBTTagList();
    for (final IItemHandlerModifiable handlerModifiable : handlers) {
        if (handlerModifiable instanceof INBTSerializable) {
            final INBTSerializable serializable = (INBTSerializable) handlerModifiable;
            handlerList.appendTag(serializable.serializeNBT());
            indexList.appendTag(new NBTTagInt(index));
        }
        index++;
    }
    compound.setTag(NBT_KEY_HANDLERS, handlerList);
    if (hasCustomName()) {
        compound.setString(NBT_KEY_NAME, customName);
    }
    return compound;
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) NBTTagInt(net.minecraft.nbt.NBTTagInt) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) INBTSerializable(net.minecraftforge.common.util.INBTSerializable)

Aggregations

NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 NBTTagList (net.minecraft.nbt.NBTTagList)2 INBTSerializable (net.minecraftforge.common.util.INBTSerializable)2 IItemHandlerModifiable (net.minecraftforge.items.IItemHandlerModifiable)2 NBTTagInt (net.minecraft.nbt.NBTTagInt)1