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); } } } }
public static Direction convertToDirection(ForgeDirection dir) { return DIRECTION_MAP.get(dir); }
static { for (Direction dir : Direction.values()) { DIRECTION_MAP.put(dir.toForgeDirection(), dir); } }