private static double emit_do( TileEntityElectricBlock emitter, List<ForgeDirection> outputtingSides, double totalToSend, boolean tryAgain) { double remains = totalToSend % outputtingSides.size(); double splitSend = (totalToSend - remains) / outputtingSides.size(); double sent = 0; List<ForgeDirection> toRemove = new ArrayList<ForgeDirection>(); for (ForgeDirection side : outputtingSides) { TileEntity tileEntity = Coord4D.get(emitter).getFromSide(side).getTileEntity(emitter.getWorldObj()); double toSend = splitSend + remains; remains = 0; double prev = sent; sent += emit_do_do(emitter, tileEntity, side, toSend, tryAgain); if (sent - prev == 0) { toRemove.add(side); } } for (ForgeDirection side : toRemove) { outputtingSides.remove(side); } return sent; }
public static void emit(TileEntityElectricBlock emitter) { if (!emitter.getWorldObj().isRemote && MekanismUtils.canFunction(emitter)) { double energyToSend = Math.min(emitter.getEnergy(), emitter.getMaxOutput()); if (energyToSend > 0) { List<ForgeDirection> outputtingSides = new ArrayList<ForgeDirection>(); boolean[] connectable = getConnections(emitter, emitter.getOutputtingSides()); for (ForgeDirection side : emitter.getOutputtingSides()) { if (connectable[side.ordinal()]) { outputtingSides.add(side); } } if (outputtingSides.size() > 0) { double sent = 0; boolean tryAgain = false; do { tryAgain = false; double prev = sent; sent += emit_do(emitter, outputtingSides, energyToSend - sent, tryAgain); if (energyToSend - sent > 0 && sent - prev > 0) { tryAgain = true; } } while (tryAgain); emitter.setEnergy(emitter.getEnergy() - sent); } } } }
/** * Universally charges an item, and updates the TileEntity's energy level. * * @param slotID - ID of the slot of which to discharge * @param storer - TileEntity the item is being discharged in */ public static void charge(int slotID, TileEntityElectricBlock storer) { if (storer.inventory[slotID] != null && storer.getEnergy() > 0) { if (storer.inventory[slotID].getItem() instanceof IEnergizedItem) { storer.setEnergy( storer.getEnergy() - EnergizedItemManager.charge(storer.inventory[slotID], storer.getEnergy())); } else if (Mekanism.hooks.IC2Loaded && storer.inventory[slotID].getItem() instanceof IElectricItem) { double sent = ElectricItem.manager.charge( storer.inventory[slotID], (int) (storer.getEnergy() * Mekanism.TO_IC2), 4, true, false) * Mekanism.FROM_IC2; storer.setEnergy(storer.getEnergy() - sent); } else if (storer.inventory[slotID].getItem() instanceof IEnergyContainerItem) { ItemStack itemStack = storer.inventory[slotID]; IEnergyContainerItem item = (IEnergyContainerItem) storer.inventory[slotID].getItem(); int itemEnergy = (int) Math.round( Math.min( Math.sqrt(item.getMaxEnergyStored(itemStack)), item.getMaxEnergyStored(itemStack) - item.getEnergyStored(itemStack))); int toTransfer = (int) Math.round(Math.min(itemEnergy, (storer.getEnergy() * Mekanism.TO_TE))); storer.setEnergy( storer.getEnergy() - (item.receiveEnergy(itemStack, toTransfer, false) * Mekanism.FROM_TE)); } } }
/** * Universally discharges an item, and updates the TileEntity's energy level. * * @param slotID - ID of the slot of which to charge * @param storer - TileEntity the item is being charged in */ public static void discharge(int slotID, TileEntityElectricBlock storer) { if (storer.inventory[slotID] != null && storer.getEnergy() < storer.getMaxEnergy()) { if (storer.inventory[slotID].getItem() instanceof IEnergizedItem) { storer.setEnergy( storer.getEnergy() + EnergizedItemManager.discharge( storer.inventory[slotID], storer.getMaxEnergy() - storer.getEnergy())); } else if (Mekanism.hooks.IC2Loaded && storer.inventory[slotID].getItem() instanceof IElectricItem) { IElectricItem item = (IElectricItem) storer.inventory[slotID].getItem(); if (item.canProvideEnergy(storer.inventory[slotID])) { double gain = ElectricItem.manager.discharge( storer.inventory[slotID], (int) ((storer.getMaxEnergy() - storer.getEnergy()) * Mekanism.TO_IC2), 4, true, false) * Mekanism.FROM_IC2; storer.setEnergy(storer.getEnergy() + gain); } } else if (storer.inventory[slotID].getItem() instanceof IEnergyContainerItem) { ItemStack itemStack = storer.inventory[slotID]; IEnergyContainerItem item = (IEnergyContainerItem) storer.inventory[slotID].getItem(); int itemEnergy = (int) Math.round( Math.min( Math.sqrt(item.getMaxEnergyStored(itemStack)), item.getEnergyStored(itemStack))); int toTransfer = (int) Math.round( Math.min( itemEnergy, ((storer.getMaxEnergy() - storer.getEnergy()) * Mekanism.TO_TE))); storer.setEnergy( storer.getEnergy() + (item.extractEnergy(itemStack, toTransfer, false) * Mekanism.FROM_TE)); } else if (storer.inventory[slotID].itemID == Item.redstone.itemID && storer.getEnergy() + Mekanism.ENERGY_PER_REDSTONE <= storer.getMaxEnergy()) { storer.setEnergy(storer.getEnergy() + Mekanism.ENERGY_PER_REDSTONE); storer.inventory[slotID].stackSize--; if (storer.inventory[slotID].stackSize <= 0) { storer.inventory[slotID] = null; } } } }