Search in sources :

Example 6 with EnergyStorage

use of net.minecraftforge.energy.EnergyStorage in project Metalworks by canitzp.

the class TileBase method readCapabilities.

protected void readCapabilities(NBTTagCompound nbt, @Nullable EnumFacing side) {
    IItemHandler inventory = this.getInventory(side);
    if (inventory != null && inventory instanceof IItemHandlerModifiable && nbt.hasKey("Inventory")) {
        for (int i = 0; i < inventory.getSlots(); i++) {
            // clear the inventory, otherwise empty stacks doesn't get overriden while syncing. Forge Bug?
            ((IItemHandlerModifiable) inventory).setStackInSlot(i, ItemStack.EMPTY);
        }
        CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(inventory, side, nbt.getTag("Inventory"));
    }
    IFluidHandler tank = getTank(side);
    if (tank != null && tank instanceof IFluidTank && nbt.hasKey("FluidTank")) {
        CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.readNBT(tank, side, nbt.getCompoundTag("FluidTank"));
    }
    IEnergyStorage energy = getEnergy(side);
    if (energy != null && energy instanceof EnergyStorage && nbt.hasKey("Energy")) {
        CapabilityEnergy.ENERGY.readNBT(energy, side, nbt.getTag("Energy"));
    }
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) IItemHandler(net.minecraftforge.items.IItemHandler) EnergyStorage(net.minecraftforge.energy.EnergyStorage) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) IFluidTank(net.minecraftforge.fluids.IFluidTank)

Example 7 with EnergyStorage

use of net.minecraftforge.energy.EnergyStorage in project Metalworks by canitzp.

the class ItemBattery method initCapabilities.

@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
    return new ICapabilityProvider() {

        @Override
        public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
            return capability == CapabilityEnergy.ENERGY;
        }

        @Nullable
        @Override
        public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
            int maxTransfer = Math.round(maxEnergy / 50.0F);
            return capability == CapabilityEnergy.ENERGY ? CapabilityEnergy.ENERGY.cast(new EnergyStorage(maxEnergy, maxTransfer, maxTransfer, stack.getTagCompound() != null ? stack.getTagCompound().getInteger("Energy") : 0) {

                @Override
                public int receiveEnergy(int maxReceive, boolean simulate) {
                    int i = super.receiveEnergy(maxReceive, simulate);
                    setEnergy(energy);
                    return i;
                }

                @Override
                public int extractEnergy(int maxExtract, boolean simulate) {
                    int i = super.extractEnergy(maxExtract, simulate);
                    setEnergy(energy);
                    return i;
                }

                private void setEnergy(int energy) {
                    NBTTagCompound nbt = stack.getTagCompound() != null ? stack.getTagCompound() : new NBTTagCompound();
                    nbt.setInteger("Energy", energy);
                    stack.setTagCompound(nbt);
                }
            }) : null;
        }
    };
}
Also used : Capability(net.minecraftforge.common.capabilities.Capability) ICapabilityProvider(net.minecraftforge.common.capabilities.ICapabilityProvider) Nonnull(javax.annotation.Nonnull) EnumFacing(net.minecraft.util.EnumFacing) EnergyStorage(net.minecraftforge.energy.EnergyStorage) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Nullable(javax.annotation.Nullable)

Aggregations

EnergyStorage (net.minecraftforge.energy.EnergyStorage)7 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)6 Nonnull (javax.annotation.Nonnull)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 Nullable (javax.annotation.Nullable)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 EnumFacing (net.minecraft.util.EnumFacing)1 Capability (net.minecraftforge.common.capabilities.Capability)1 ICapabilityProvider (net.minecraftforge.common.capabilities.ICapabilityProvider)1