From 0e0594a69bf4f926030fb89362fd690c9d8b4ae3 Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Sun, 17 Nov 2013 19:56:16 -0500 Subject: [PATCH] Electric Chest charges when in inventory --- .../inventory/InventoryElectricChest.java | 46 +++++++---- .../common/item/ItemBlockMachine.java | 80 ++++++++++++------- 2 files changed, 85 insertions(+), 41 deletions(-) diff --git a/common/mekanism/common/inventory/InventoryElectricChest.java b/common/mekanism/common/inventory/InventoryElectricChest.java index 5f1eee2b6..9e1bca772 100644 --- a/common/mekanism/common/inventory/InventoryElectricChest.java +++ b/common/mekanism/common/inventory/InventoryElectricChest.java @@ -11,6 +11,8 @@ import net.minecraft.nbt.NBTTagList; public class InventoryElectricChest extends InventoryBasic { public EntityPlayer entityPlayer; + public ItemStack itemStack; + public boolean reading; public InventoryElectricChest(EntityPlayer player) @@ -21,6 +23,14 @@ public class InventoryElectricChest extends InventoryBasic read(); } + public InventoryElectricChest(ItemStack stack) + { + super("Electric Chest", false, 55); + itemStack = stack; + + read(); + } + @Override public void onInventoryChanged() { @@ -36,14 +46,14 @@ public class InventoryElectricChest extends InventoryBasic public void openChest() { read(); - ((IElectricChest)getItemStack().getItem()).setOpen(getItemStack(), true); + ((IElectricChest)getStack().getItem()).setOpen(getStack(), true); } @Override public void closeChest() { write(); - ((IElectricChest)getItemStack().getItem()).setOpen(getItemStack(), false); + ((IElectricChest)getStack().getItem()).setOpen(getStack(), false); } public void write() @@ -61,31 +71,39 @@ public class InventoryElectricChest extends InventoryBasic } } - ((ISustainedInventory)getItemStack().getItem()).setInventory(tagList, getItemStack()); + ((ISustainedInventory)getStack().getItem()).setInventory(tagList, getStack()); } public void read() { + if(reading) + { + return; + } + reading = true; - NBTTagList tagList = ((ISustainedInventory)getItemStack().getItem()).getInventory(getItemStack()); + NBTTagList tagList = ((ISustainedInventory)getStack().getItem()).getInventory(getStack()); - for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) + if(tagList != null) { - NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount); - byte slotID = tagCompound.getByte("Slot"); - - if(slotID >= 0 && slotID < getSizeInventory()) - { - setInventorySlotContents(slotID, ItemStack.loadItemStackFromNBT(tagCompound)); - } + for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) + { + NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount); + byte slotID = tagCompound.getByte("Slot"); + + if(slotID >= 0 && slotID < getSizeInventory()) + { + setInventorySlotContents(slotID, ItemStack.loadItemStackFromNBT(tagCompound)); + } + } } reading = false; } - public ItemStack getItemStack() + public ItemStack getStack() { - return entityPlayer.getCurrentEquippedItem(); + return itemStack != null ? itemStack : entityPlayer.getCurrentEquippedItem(); } } diff --git a/common/mekanism/common/item/ItemBlockMachine.java b/common/mekanism/common/item/ItemBlockMachine.java index 778dabb69..abf5b486e 100644 --- a/common/mekanism/common/item/ItemBlockMachine.java +++ b/common/mekanism/common/item/ItemBlockMachine.java @@ -1,5 +1,7 @@ package mekanism.common.item; +import ic2.api.item.ElectricItem; +import ic2.api.item.IElectricItem; import ic2.api.item.IElectricItemManager; import ic2.api.item.ISpecialElectricItem; @@ -8,6 +10,7 @@ import java.util.List; import mekanism.api.EnumColor; import mekanism.api.IConfigurable; import mekanism.api.IUpgradeManagement; +import mekanism.api.energy.EnergizedItemManager; import mekanism.api.energy.IEnergizedItem; import mekanism.common.IElectricChest; import mekanism.common.IFactory; @@ -30,6 +33,7 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -42,6 +46,7 @@ import org.lwjgl.input.Keyboard; import universalelectricity.core.electricity.ElectricityDisplay; import universalelectricity.core.electricity.ElectricityDisplay.ElectricUnit; +import universalelectricity.core.item.ElectricItemHelper; import universalelectricity.core.item.IItemElectric; import cofh.api.energy.IEnergyContainerItem; import cpw.mods.fml.relauncher.Side; @@ -247,33 +252,54 @@ public class ItemBlockMachine extends ItemBlock implements IEnergizedItem, IItem { if(isElectricChest(itemstack)) { - setPrevLidAngle(itemstack, getLidAngle(itemstack)); - float increment = 0.1F; - - if((!getOpen(itemstack) && getLidAngle(itemstack) > 0.0F) || (getOpen(itemstack) && getLidAngle(itemstack) < 1.0F)) - { - float angle = getLidAngle(itemstack); - - if(getOpen(itemstack)) - { - setLidAngle(itemstack, getLidAngle(itemstack)+increment); - } - else { - setLidAngle(itemstack, getLidAngle(itemstack)-increment); - } - - if(getLidAngle(itemstack) > 1.0F) - { - setLidAngle(itemstack, 1.0F); - } - - float split = 0.5F; - - if(getLidAngle(itemstack) < 0.0F) - { - setLidAngle(itemstack, 0.0F); - } - } + if(!world.isRemote) + { + InventoryElectricChest inv = new InventoryElectricChest(itemstack); + + if(inv.getStackInSlot(54) != null && getEnergy(itemstack) < getMaxEnergy(itemstack)) + { + if(inv.getStackInSlot(54).getItem() instanceof IEnergizedItem) + { + setEnergy(itemstack, getEnergy(itemstack) + EnergizedItemManager.discharge(inv.getStackInSlot(54), getMaxEnergy(itemstack) - getEnergy(itemstack))); + } + else if(inv.getStackInSlot(54).getItem() instanceof IItemElectric) + { + setEnergy(itemstack, getEnergy(itemstack) + ElectricItemHelper.dischargeItem(inv.getStackInSlot(54), (float)((getMaxEnergy(itemstack) - getEnergy(itemstack))*Mekanism.TO_UE))); + } + else if(Mekanism.hooks.IC2Loaded && inv.getStackInSlot(54).getItem() instanceof IElectricItem) + { + IElectricItem item = (IElectricItem)inv.getStackInSlot(54).getItem(); + + if(item.canProvideEnergy(inv.getStackInSlot(54))) + { + double gain = ElectricItem.manager.discharge(inv.getStackInSlot(54), (int)((getMaxEnergy(itemstack) - getEnergy(itemstack))*Mekanism.TO_IC2), 3, false, false)*Mekanism.FROM_IC2; + setEnergy(itemstack, getEnergy(itemstack) + gain); + } + } + else if(inv.getStackInSlot(54).getItem() instanceof IEnergyContainerItem) + { + ItemStack itemStack = inv.getStackInSlot(54); + IEnergyContainerItem item = (IEnergyContainerItem)inv.getStackInSlot(54).getItem(); + + int itemEnergy = (int)Math.round(Math.min(Math.sqrt(item.getMaxEnergyStored(itemStack)), item.getEnergyStored(itemStack))); + int toTransfer = (int)Math.round(Math.min(itemEnergy, ((getMaxEnergy(itemstack) - getEnergy(itemstack))*Mekanism.TO_TE))); + + setEnergy(itemstack, getEnergy(itemstack) + (item.extractEnergy(itemStack, toTransfer, false)*Mekanism.FROM_TE)); + } + else if(inv.getStackInSlot(54).itemID == Item.redstone.itemID && getEnergy(itemstack)+Mekanism.ENERGY_PER_REDSTONE <= getMaxEnergy(itemstack)) + { + setEnergy(itemstack, getEnergy(itemstack) + Mekanism.ENERGY_PER_REDSTONE); + inv.getStackInSlot(54).stackSize--; + + if(inv.getStackInSlot(54).stackSize <= 0) + { + inv.setInventorySlotContents(54, null); + } + } + + inv.write(); + } + } } }