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"));
}
}
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;
}
};
}
Aggregations