/** * Whether or not a defined deemed-electrical ItemStack can be outputted out of a slot. This puts * into account whether or not that slot is used for charging or discharging. * * @param itemstack - ItemStack to perform the check on * @param chargeSlot - whether or not the outputting slot is for charging or discharging * @return if the ItemStack can be outputted */ public static boolean canBeOutputted(ItemStack itemstack, boolean chargeSlot) { if (itemstack.getItem() instanceof IEnergizedItem) { IEnergizedItem energized = (IEnergizedItem) itemstack.getItem(); if (chargeSlot) { return energized.getEnergy(itemstack) == energized.getMaxEnergy(itemstack); } else { return energized.getEnergy(itemstack) == 0; } } else if (itemstack.getItem() instanceof IEnergyContainerItem) { IEnergyContainerItem energyContainer = (IEnergyContainerItem) itemstack.getItem(); if (chargeSlot) { return energyContainer.receiveEnergy(itemstack, 1, true) > 0; } else { return energyContainer.extractEnergy(itemstack, 1, true) > 0; } } else if (itemstack.getItem() instanceof ISpecialElectricItem) { IElectricItemManager manager = ((ISpecialElectricItem) itemstack.getItem()).getManager(itemstack); if (manager != null) { if (chargeSlot) { return manager.charge(itemstack, 1, 3, true, true) > 0; } else { return manager.discharge(itemstack, 1, 3, true, true, true) > 0; } } } return true; }
/** * 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, IStrictEnergyStorage storer) { IInventory inv = (TileEntityContainerBlock) storer; if (inv.getStackInSlot(slotID) != null && storer.getEnergy() > 0) { if (inv.getStackInSlot(slotID).getItem() instanceof IEnergizedItem) { storer.setEnergy( storer.getEnergy() - EnergizedItemManager.charge(inv.getStackInSlot(slotID), storer.getEnergy())); } else if (MekanismUtils.useIC2() && inv.getStackInSlot(slotID).getItem() instanceof IElectricItem) { double sent = ElectricItem.manager.charge( inv.getStackInSlot(slotID), (int) (storer.getEnergy() * general.TO_IC2), 4, true, false) * general.FROM_IC2; storer.setEnergy(storer.getEnergy() - sent); } else if (MekanismUtils.useRF() && inv.getStackInSlot(slotID).getItem() instanceof IEnergyContainerItem) { ItemStack itemStack = inv.getStackInSlot(slotID); IEnergyContainerItem item = (IEnergyContainerItem) inv.getStackInSlot(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() * general.TO_RF))); storer.setEnergy( storer.getEnergy() - (item.receiveEnergy(itemStack, toTransfer, false) * general.FROM_RF)); } } }
/** * 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)); } } }