From 4ddb71610196057b16c200d3c5bc6101f0ece6a1 Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Sun, 4 Aug 2013 04:04:02 -0400 Subject: [PATCH] Work on cell implementation of Battery (really signing off now) --- src/resonantinduction/base/SetUtil.java | 22 +++++++ .../battery/BatteryManager.java | 5 +- .../battery/BatteryUpdateProtocol.java | 27 ++++++++- .../battery/SynchronizedBatteryData.java | 5 ++ .../battery/TileEntityBattery.java | 60 +++++++++++++++---- 5 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 src/resonantinduction/base/SetUtil.java diff --git a/src/resonantinduction/base/SetUtil.java b/src/resonantinduction/base/SetUtil.java new file mode 100644 index 00000000..a1f0cd42 --- /dev/null +++ b/src/resonantinduction/base/SetUtil.java @@ -0,0 +1,22 @@ +package resonantinduction.base; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class SetUtil +{ + public static Set inverse(Set set) + { + Set toReturn = new HashSet(); + List list = Arrays.asList(set.toArray()); + + for(int i = list.size()-1; i >= 0; i--) + { + toReturn.add((V)list.get(i)); + } + + return toReturn; + } +} diff --git a/src/resonantinduction/battery/BatteryManager.java b/src/resonantinduction/battery/BatteryManager.java index 511b76e3..7f6d3f44 100644 --- a/src/resonantinduction/battery/BatteryManager.java +++ b/src/resonantinduction/battery/BatteryManager.java @@ -17,6 +17,7 @@ import cpw.mods.fml.common.TickType; public class BatteryManager implements ITickHandler { public static final int WILDCARD = -1; + public static final int CELLS_PER_BATTERY = 16; public static Map dynamicInventories = new HashMap(); @@ -36,7 +37,7 @@ public class BatteryManager implements ITickHandler if(tileEntity != null) { - tileEntity.inventory = new HashSet(); + tileEntity.cachedInventory = new HashSet(); tileEntity.inventoryID = WILDCARD; } } @@ -157,7 +158,7 @@ public class BatteryManager implements ITickHandler if(battery != null) { - battery.inventory = new HashSet(); + battery.cachedInventory = new HashSet(); battery.inventoryID = WILDCARD; } } diff --git a/src/resonantinduction/battery/BatteryUpdateProtocol.java b/src/resonantinduction/battery/BatteryUpdateProtocol.java index e92bd303..ef747dd6 100644 --- a/src/resonantinduction/battery/BatteryUpdateProtocol.java +++ b/src/resonantinduction/battery/BatteryUpdateProtocol.java @@ -3,6 +3,7 @@ package resonantinduction.battery; import java.util.HashSet; import java.util.Set; +import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; @@ -248,7 +249,29 @@ public class BatteryUpdateProtocol idFound = BatteryManager.getUniqueInventoryID(); } - structureFound.inventory = cache.inventory; + Set newInventory = new HashSet(); + + if(cache.inventory.size() <= structureFound.getMaxCells()) + { + newInventory = cache.inventory; + } + else { + int count = 0; + + for(ItemStack itemStack : cache.inventory) + { + count++; + + newInventory.add(itemStack); + + if(count == structureFound.getMaxCells()) + { + break; + } + } + } + + structureFound.inventory = newInventory; for(Vector3 obj : structureFound.locations) { @@ -256,7 +279,7 @@ public class BatteryUpdateProtocol tileEntity.inventoryID = idFound; tileEntity.structure = structureFound; - tileEntity.inventory = cache.inventory; + tileEntity.cachedInventory = newInventory; } } else { diff --git a/src/resonantinduction/battery/SynchronizedBatteryData.java b/src/resonantinduction/battery/SynchronizedBatteryData.java index f57be3bb..058dd7bc 100644 --- a/src/resonantinduction/battery/SynchronizedBatteryData.java +++ b/src/resonantinduction/battery/SynchronizedBatteryData.java @@ -25,6 +25,11 @@ public class SynchronizedBatteryData return length*width*height; } + public int getMaxCells() + { + return getVolume()*BatteryManager.CELLS_PER_BATTERY; + } + @Override public int hashCode() { diff --git a/src/resonantinduction/battery/TileEntityBattery.java b/src/resonantinduction/battery/TileEntityBattery.java index 682cb4e6..cb24cc59 100644 --- a/src/resonantinduction/battery/TileEntityBattery.java +++ b/src/resonantinduction/battery/TileEntityBattery.java @@ -14,6 +14,7 @@ import net.minecraft.nbt.NBTTagList; import resonantinduction.PacketHandler; import resonantinduction.api.IBattery; import resonantinduction.base.IPacketReceiver; +import resonantinduction.base.SetUtil; import resonantinduction.base.TileEntityBase; import com.google.common.io.ByteArrayDataInput; @@ -25,11 +26,11 @@ import com.google.common.io.ByteArrayDataInput; */ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { - public Set inventory = new HashSet(); + public Set cachedInventory = new HashSet(); public SynchronizedBatteryData structure; - public boolean prevStructure; + public SynchronizedBatteryData prevStructure; public boolean clientHasStructure; @@ -48,8 +49,6 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { structure = new SynchronizedBatteryData(); } - - prevStructure = clientHasStructure; } if(playersUsing.size() > 0 && ((worldObj.isRemote && !clientHasStructure) || (!worldObj.isRemote && structure == null))) @@ -64,7 +63,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { if(inventoryID != -1 && structure == null) { - BatteryManager.updateCache(inventoryID, inventory, this); + BatteryManager.updateCache(inventoryID, cachedInventory, this); } if(structure == null && ticks == 5) @@ -72,12 +71,12 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver update(); } - if(prevStructure != (structure != null)) + if(prevStructure != structure) { PacketHandler.sendTileEntityPacketToClients(this, getNetworkedData(new ArrayList())); } - prevStructure = structure != null; + prevStructure = structure; if(structure != null) { @@ -87,7 +86,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { BatteryManager.updateCache(inventoryID, structure.inventory, this); - inventory = structure.inventory; + cachedInventory = structure.inventory; } } } @@ -105,13 +104,13 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver if(inventoryID != BatteryManager.WILDCARD) { NBTTagList tagList = nbtTags.getTagList("Items"); - inventory = new HashSet(); + cachedInventory = new HashSet(); for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) { NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount); - inventory.add(ItemStack.loadItemStackFromNBT(tagCompound)); + cachedInventory.add(ItemStack.loadItemStackFromNBT(tagCompound)); } } } @@ -126,7 +125,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver NBTTagList tagList = new NBTTagList(); - for(ItemStack itemStack : inventory) + for(ItemStack itemStack : cachedInventory) { if(itemStack != null) { @@ -151,12 +150,31 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver } } } + + /** + * @return added energy + */ + public float addEnergy(float amount) + { + //go from top to bottom + return 0; + } + + /** + * @return removed energy + */ + public float removeEnergy(float amount) + { + Set inverse = SetUtil.inverse(structure.inventory); + //go from bottom to top + return 0; + } public float getMaxEnergyStored() { float max = 0; - for (ItemStack itemStack : inventory) + for (ItemStack itemStack : cachedInventory) { if (itemStack != null) { @@ -169,6 +187,24 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver return max; } + + public float getEnergyStored() + { + float energy = 0; + + for (ItemStack itemStack : cachedInventory) + { + if (itemStack != null) + { + if (itemStack.getItem() instanceof IBattery) + { + energy += ((IBattery) itemStack.getItem()).getEnergyStored(itemStack); + } + } + } + + return energy; + } @Override public void handle(ByteArrayDataInput input)