Search in sources :

Example 1 with CustomEnergyStorage

use of de.canitzp.metalworks.CustomEnergyStorage in project Metalworks by canitzp.

the class TileBase method writeCapabilities.

private void writeCapabilities(NBTTagCompound nbt, @Nullable EnumFacing side) {
    if (hasEnergyFluidInv().getRight()) {
        IItemHandler inventory = this.getInventory(side);
        if (inventory instanceof IItemHandlerModifiable) {
            nbt.setTag("Inventory", Objects.requireNonNull(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.writeNBT(inventory, side)));
        }
    }
    if (hasEnergyFluidInv().getMiddle()) {
        IFluidHandler tank = getTank(side);
        if (tank instanceof IFluidTank) {
            nbt.setTag("FluidTank", Objects.requireNonNull(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.writeNBT(tank, side)));
        }
    }
    if (hasEnergyFluidInv().getLeft()) {
        IEnergyStorage energy = getEnergy(side);
        if (energy instanceof CustomEnergyStorage) {
            NBTTagCompound energyTag = new NBTTagCompound();
            energyTag.setInteger("Stored", energy.getEnergyStored());
            energyTag.setInteger("Capacity", energy.getMaxEnergyStored());
            energyTag.setInteger("MaxReceive", ((CustomEnergyStorage) energy).getReceiveTransfer());
            energyTag.setInteger("MaxExtract", ((CustomEnergyStorage) energy).getExtractTransfer());
            nbt.setTag("Energy", energyTag);
        }
    }
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) CustomEnergyStorage(de.canitzp.metalworks.CustomEnergyStorage) IItemHandler(net.minecraftforge.items.IItemHandler) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) IFluidTank(net.minecraftforge.fluids.IFluidTank)

Example 2 with CustomEnergyStorage

use of de.canitzp.metalworks.CustomEnergyStorage in project Metalworks by canitzp.

the class TileBase method readCapabilities.

private void readCapabilities(NBTTagCompound nbt, @Nullable EnumFacing side) {
    if (hasEnergyFluidInv().getRight()) {
        IItemHandler inventory = this.getInventory(side);
        if (inventory instanceof IItemHandlerModifiable && nbt.hasKey("Inventory")) {
            for (int i = 0; i < inventory.getSlots(); i++) {
                // clear the inventory, otherwise empty stacks doesn't get overridden while syncing. Forge Bug?
                ((IItemHandlerModifiable) inventory).setStackInSlot(i, ItemStack.EMPTY);
            }
            CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(inventory, side, nbt.getTag("Inventory"));
        }
    }
    if (hasEnergyFluidInv().getMiddle()) {
        IFluidHandler tank = getTank(side);
        if (tank instanceof IFluidTank && nbt.hasKey("FluidTank")) {
            CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.readNBT(tank, side, nbt.getCompoundTag("FluidTank"));
        }
    }
    if (hasEnergyFluidInv().getLeft()) {
        IEnergyStorage energy = getEnergy(side);
        if (energy instanceof CustomEnergyStorage && nbt.hasKey("Energy", Constants.NBT.TAG_COMPOUND)) {
            NBTTagCompound energyTag = nbt.getCompoundTag("Energy");
            ((CustomEnergyStorage) energy).setValues(energyTag.getInteger("Stored"), energyTag.getInteger("Capacity"), energyTag.getInteger("MaxReceive"), energyTag.getInteger("MaxExtract"));
        }
    }
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) CustomEnergyStorage(de.canitzp.metalworks.CustomEnergyStorage) IItemHandler(net.minecraftforge.items.IItemHandler) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) IFluidTank(net.minecraftforge.fluids.IFluidTank)

Example 3 with CustomEnergyStorage

use of de.canitzp.metalworks.CustomEnergyStorage in project Metalworks by canitzp.

the class TileBattery method onLoad.

@Override
public void onLoad() {
    Block block = world.getBlockState(this.getPos()).getBlock();
    if (block instanceof BlockBattery) {
        int oldEnergy = this.normal.getEnergyStored();
        BlockBattery.Type batType = ((BlockBattery) block).getBatteryType();
        this.normal = new CustomEnergyStorage(batType.maxEnergy, batType.maxTransfer, batType.maxTransfer, oldEnergy).setTile(this);
        this.input = new SlaveEnergyStorage(this.normal).set(true, false);
        this.output = new SlaveEnergyStorage(this.normal).set(false, true);
    }
}
Also used : CustomEnergyStorage(de.canitzp.metalworks.CustomEnergyStorage) Block(net.minecraft.block.Block) SlaveEnergyStorage(de.canitzp.metalworks.SlaveEnergyStorage)

Aggregations

CustomEnergyStorage (de.canitzp.metalworks.CustomEnergyStorage)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)2 IFluidTank (net.minecraftforge.fluids.IFluidTank)2 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)2 IItemHandler (net.minecraftforge.items.IItemHandler)2 IItemHandlerModifiable (net.minecraftforge.items.IItemHandlerModifiable)2 SlaveEnergyStorage (de.canitzp.metalworks.SlaveEnergyStorage)1 Block (net.minecraft.block.Block)1