diff --git a/src/minecraft/net/machinemuse/powersuits/item/ItemUtils.java b/src/minecraft/net/machinemuse/powersuits/item/ItemUtils.java index 302b09b..012ae85 100644 --- a/src/minecraft/net/machinemuse/powersuits/item/ItemUtils.java +++ b/src/minecraft/net/machinemuse/powersuits/item/ItemUtils.java @@ -202,6 +202,22 @@ public class ItemUtils { return slots; } + public static List findInInventoryForCost(List workingUpgradeCost, + InventoryPlayer inventory) { + List slots = new LinkedList(); + for (ItemStack stackInCost : workingUpgradeCost) { + int found = 0; + for (int i = 0; i < inventory.getSizeInventory() && found < stackInCost.stackSize; i++) { + ItemStack stackInInventory = inventory.getStackInSlot(i); + if (isSameItem(stackInInventory, stackInCost)) { + found += stackInInventory.stackSize; + slots.add(i); + } + } + } + return slots; + } + /** * Checks the given NBTTag and returns the value if it exists, otherwise 0. */ diff --git a/src/minecraft/net/machinemuse/powersuits/network/MusePacketInstallModuleRequest.java b/src/minecraft/net/machinemuse/powersuits/network/MusePacketInstallModuleRequest.java index bccd3d8..65d3bcf 100644 --- a/src/minecraft/net/machinemuse/powersuits/network/MusePacketInstallModuleRequest.java +++ b/src/minecraft/net/machinemuse/powersuits/network/MusePacketInstallModuleRequest.java @@ -7,6 +7,9 @@ import java.io.DataInputStream; import java.io.IOException; import java.util.List; +import universalelectricity.core.implement.IItemElectric; + +import net.machinemuse.powersuits.common.Config; import net.machinemuse.powersuits.item.ItemUtils; import net.machinemuse.powersuits.powermodule.PowerModule; import net.machinemuse.powersuits.powermodule.ModuleManager; @@ -74,13 +77,24 @@ public class MusePacketInstallModuleRequest extends MusePacket { int entityId = playerEntity.entityId; PowerModule moduleType = ModuleManager.getModule(moduleName); List cost = moduleType.getInstallCost(); - + if (ItemUtils.hasInInventory(cost, playerEntity.inventory)) { - List slots = ItemUtils.deleteFromInventory( + List slots = ItemUtils.findInInventoryForCost(cost, playerEntity.inventory); + double amps=0; + double volts = ItemUtils.getAsModular(stack.getItem()).getVoltage(); + for(Integer slot : slots) { + ItemStack stackInSlot = playerEntity.inventory.getStackInSlot(slot); + if(stackInSlot != null && stackInSlot.getItem() instanceof IItemElectric) { + IItemElectric electricItem = (IItemElectric)stackInSlot.getItem(); + amps = electricItem.getJoules(stackInSlot)/volts; + } + } + List slotsToUpdate = ItemUtils.deleteFromInventory( cost, inventory); ItemUtils.itemAddModule(stack, moduleType); slots.add(this.itemSlot); - for (Integer slotiter : slots) { + ItemUtils.getAsModular(stack.getItem()).onReceive(amps, volts, stack); + for (Integer slotiter : slotsToUpdate) { MusePacket reply = new MusePacketInventoryRefresh( player, slotiter, inventory.getStackInSlot(slotiter));