Java tutorial
package buildcraftAdditions.tileEntities.Bases; import io.netty.buffer.ByteBuf; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; import cofh.api.energy.IEnergyReceiver; /** * Copyright (c) 2014-2015, AEnterprise * http://buildcraftadditions.wordpress.com/ * Eureka is distributed under the terms of GNU GPL v3.0 * Please check the contents of the license located in * http://buildcraftadditions.wordpress.com/wiki/licensing-stuff/ */ public abstract class TileMachineBase extends TileBase implements IEnergyReceiver { protected final int capacity, maxTransfer; protected int energy; protected TileMachineBase(int capacity, int maxTransfer) { this.capacity = capacity; this.maxTransfer = maxTransfer; } @Override public void readFromNBT(NBTTagCompound tag) { super.readFromNBT(tag); energy = tag.getInteger("energy"); } @Override public void writeToNBT(NBTTagCompound tag) { super.writeToNBT(tag); tag.setInteger("energy", energy); } @Override public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) { int energyReceived = Math.min(capacity - energy, Math.min(maxTransfer, maxReceive)); if (!simulate) energy += energyReceived; return energyReceived; } @Override public int getEnergyStored(ForgeDirection from) { return energy; } @Override public int getMaxEnergyStored(ForgeDirection from) { return capacity; } @Override public boolean canConnectEnergy(ForgeDirection from) { return true; } @Override public void writeToByteBuff(ByteBuf buf) { buf.writeInt(energy); } @Override public void readFromByteBuff(ByteBuf buf) { energy = buf.readInt(); } }