public static void transferItem( TileEntityItemStorableMachineBase inventory, int start, int end, int requireEnergy) { for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) { if (inventory.getStored() < requireEnergy) { break; } TileEntity tileEntity = inventory.worldObj.getBlockTileEntity( inventory.xCoord + dir.offsetX, inventory.yCoord + dir.offsetY, inventory.zCoord + dir.offsetZ); if (tileEntity != null && tileEntity instanceof IInventory) { for (int i = start; i < end; i++) { ItemStack target = inventory.getStackInSlot(i); if (target != null) { ItemStack result = Util.insertItem((IInventory) tileEntity, target, false); inventory.setInventorySlotContents(i, result); if (result == null || target.stackSize != result.stackSize) { inventory.useEnergy(requireEnergy); } } } } } }
public static void injectEnergy(TileEntityMachineBase tileEntity, ForgeDirection dir) { TileEntity connectedTileEntity = tileEntity.worldObj.getBlockTileEntity( tileEntity.xCoord + dir.offsetX, tileEntity.yCoord + dir.offsetY, tileEntity.zCoord + dir.offsetZ); if (connectedTileEntity != null) { if (connectedTileEntity instanceof ILargeEnergyStorage) { ILargeEnergyStorage storage = (ILargeEnergyStorage) connectedTileEntity; if (tileEntity instanceof ILargeEnergyStorage) { ILargeEnergyStorage source = (ILargeEnergyStorage) tileEntity; BigInteger demand = source .getAllStoredEnergy() .min(storage.getFullCapacity().subtract(storage.getAllStoredEnergy())); storage.setAllEnergy(storage.getAllStoredEnergy().add(demand)); source.useLargeEnergy(demand); } else { BigInteger demand = Util.toBigInteger(tileEntity.getStored()) .min(storage.getFullCapacity().subtract(storage.getAllStoredEnergy())); storage.setAllEnergy(storage.getAllStoredEnergy().add(demand)); tileEntity.useEnergy(demand.intValue()); } } else if (connectedTileEntity instanceof IEnergyStorage) { IEnergyStorage storage = (IEnergyStorage) connectedTileEntity; int demand = Math.min(tileEntity.getStored(), storage.getCapacity() - storage.getStored()); storage.setStored(storage.getStored() + demand); tileEntity.useEnergy(demand); } else if (connectedTileEntity instanceof IEnergySink) { IEnergySink sink = (IEnergySink) connectedTileEntity; int demand = 0; int prevStoredEnergy = 0; while ((demand = Math.min(sink.getMaxSafeInput() - 1, tileEntity.getStored())) > 0 && tileEntity.getStored() != prevStoredEnergy) { prevStoredEnergy = tileEntity.getStored(); sink.injectEnergy(DIRECTION_MAP.get(dir.getOpposite()), demand); tileEntity.useEnergy(demand); } } } }