diff --git a/src/resonantinduction/base/SetUtil.java b/src/resonantinduction/base/SetUtil.java index a635c33b..5d1da023 100644 --- a/src/resonantinduction/base/SetUtil.java +++ b/src/resonantinduction/base/SetUtil.java @@ -1,12 +1,11 @@ package resonantinduction.base; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; -import net.minecraft.item.ItemStack; - public class SetUtil { public static Set inverse(Set set) @@ -49,12 +48,29 @@ public class SetUtil return toReturn; } - public static Set[] split(Set set, int divide) + public static Set merge(Set setOne, Set setTwo) + { + Set newSet = new HashSet(); + + for(V obj : setOne) + { + newSet.add(obj); + } + + for(V obj : setTwo) + { + newSet.add(obj); + } + + return newSet; + } + + public static ArrayList> split(Set set, int divide) { int remain = set.size()%divide; int size = (set.size()/divide)-remain; - Set[] toReturn = new HashSet[divide]; + ArrayList> toReturn = new ArrayList>(divide); for(Set iterSet : toReturn) { @@ -88,4 +104,9 @@ public class SetUtil return toReturn; } + + public static ArrayList asList(Set set) + { + return (ArrayList)Arrays.asList(set.toArray()); + } } diff --git a/src/resonantinduction/battery/BatteryManager.java b/src/resonantinduction/battery/BatteryManager.java index 7f6d3f44..ca4f9d76 100644 --- a/src/resonantinduction/battery/BatteryManager.java +++ b/src/resonantinduction/battery/BatteryManager.java @@ -16,86 +16,8 @@ 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(); - - /** - * Grabs an inventory from the world's caches, and removes all the world's references to it. - * @param world - world the cache is stored in - * @param id - inventory ID to pull - * @return correct Battery inventory cache - */ - public static BatteryCache pullInventory(World world, int id) - { - BatteryCache toReturn = dynamicInventories.get(id); - - for(Vector3 obj : dynamicInventories.get(id).locations) - { - TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(world); - - if(tileEntity != null) - { - tileEntity.cachedInventory = new HashSet(); - tileEntity.inventoryID = WILDCARD; - } - } - - dynamicInventories.remove(id); - - return toReturn; - } - - /** - * Updates a battery cache with the defined inventory ID with the parameterized values. - * @param inventoryID - inventory ID of the battery - * @param fluid - cached fluid of the battery - * @param inventory - inventory of the battery - * @param tileEntity - battery TileEntity - */ - public static void updateCache(int inventoryID, Set inventory, TileEntityBattery tileEntity) - { - if(!dynamicInventories.containsKey(inventoryID)) - { - BatteryCache cache = new BatteryCache(); - cache.inventory = inventory; - cache.dimensionId = tileEntity.worldObj.provider.dimensionId; - cache.locations.add(new Vector3(tileEntity)); - - dynamicInventories.put(inventoryID, cache); - - return; - } - - dynamicInventories.get(inventoryID).inventory = inventory; - - dynamicInventories.get(inventoryID).locations.add(new Vector3(tileEntity)); - } - - /** - * Grabs a unique inventory ID for a battery. - * @return unique inventory ID - */ - public static int getUniqueInventoryID() - { - int id = 0; - - while(true) - { - for(Integer i : dynamicInventories.keySet()) - { - if(id == i) - { - id++; - continue; - } - } - - return id; - } - } - @Override public void tickStart(EnumSet type, Object... tickData) { @@ -105,68 +27,7 @@ public class BatteryManager implements ITickHandler @Override public void tickEnd(EnumSet type, Object... tickData) { - if(tickData[0] instanceof World) - { - ArrayList idsToKill = new ArrayList(); - HashMap> tilesToKill = new HashMap>(); - - World world = (World)tickData[0]; - - if(!world.isRemote) - { - for(Map.Entry entry : dynamicInventories.entrySet()) - { - int inventoryID = entry.getKey(); - - if(entry.getValue().dimensionId == world.provider.dimensionId) - { - for(Vector3 obj : entry.getValue().locations) - { - TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(world); - - if(tileEntity == null || tileEntity.inventoryID != inventoryID) - { - if(!tilesToKill.containsKey(inventoryID)) - { - tilesToKill.put(inventoryID, new HashSet()); - } - - tilesToKill.get(inventoryID).add(obj); - } - } - } - - if(entry.getValue().locations.isEmpty()) - { - idsToKill.add(inventoryID); - } - } - - for(Map.Entry> entry : tilesToKill.entrySet()) - { - for(Vector3 obj : entry.getValue()) - { - dynamicInventories.get(entry.getKey()).locations.remove(obj); - } - } - - for(int inventoryID : idsToKill) - { - for(Vector3 obj : dynamicInventories.get(inventoryID).locations) - { - TileEntityBattery battery = (TileEntityBattery)obj.getTileEntity(world); - - if(battery != null) - { - battery.cachedInventory = new HashSet(); - battery.inventoryID = WILDCARD; - } - } - - dynamicInventories.remove(inventoryID); - } - } - } + } @Override @@ -180,12 +41,4 @@ public class BatteryManager implements ITickHandler { return "BatteryMultiblockManager"; } - - public static class BatteryCache - { - public Set inventory = new HashSet(); - public int dimensionId; - - public Set locations = new HashSet(); - } } diff --git a/src/resonantinduction/battery/BatteryUpdateProtocol.java b/src/resonantinduction/battery/BatteryUpdateProtocol.java index 7c7ac109..34c4da9f 100644 --- a/src/resonantinduction/battery/BatteryUpdateProtocol.java +++ b/src/resonantinduction/battery/BatteryUpdateProtocol.java @@ -1,5 +1,6 @@ package resonantinduction.battery; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; @@ -9,7 +10,6 @@ import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; import resonantinduction.base.SetUtil; import resonantinduction.base.Vector3; -import resonantinduction.battery.BatteryManager.BatteryCache; public class BatteryUpdateProtocol { @@ -161,6 +161,11 @@ public class BatteryUpdateProtocol structure.length = Math.abs(xmax-xmin)+1; structure.height = Math.abs(ymax-ymin)+1; structure.width = Math.abs(zmax-zmin)+1; + + if(structure.getVolume() > 1) + { + structure.isMultiblock = true; + } if(structure.locations.contains(new Vector3(pointer))) { @@ -196,6 +201,32 @@ public class BatteryUpdateProtocol return false; } + public void disperseCells() + { + SynchronizedBatteryData oldStructure = null; + + for(TileEntityBattery tile : iteratedNodes) + { + if(tile.structure.isMultiblock) + { + oldStructure = tile.structure; + break; + } + } + + if(oldStructure != null) + { + ArrayList> inventories = SetUtil.split(oldStructure.inventory, iteratedNodes.size()); + ArrayList iterList = SetUtil.asList(iteratedNodes); + + for(int i = 0; i < iterList.size(); i++) + { + TileEntityBattery tile = iterList.get(i); + tile.structure = SynchronizedBatteryData.getBase(tile, inventories.get(i)); + } + } + } + /** * Runs the protocol and updates all batteries that make a part of the multiblock battery. */ @@ -209,10 +240,7 @@ public class BatteryUpdateProtocol { if(!structureFound.locations.contains(new Vector3(tileEntity))) { - for(TileEntity tile : iteratedNodes) - { - ((TileEntityBattery)tileEntity).structure = null; - } + disperseCells(); return; } @@ -224,50 +252,18 @@ public class BatteryUpdateProtocol System.out.println("Width: " + structureFound.width); System.out.println("Volume: " + structureFound.locations.size()); - int idFound = BatteryManager.WILDCARD; - for(Vector3 obj : structureFound.locations) { TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj); - if(tileEntity.inventoryID != BatteryManager.WILDCARD) - { - idFound = tileEntity.inventoryID; - break; - } - } - - BatteryCache cache = new BatteryCache(); - - if(idFound != BatteryManager.WILDCARD) - { - if(BatteryManager.dynamicInventories.get(idFound) != null) - { - cache = BatteryManager.pullInventory(pointer.worldObj, idFound); - } - } - else { - idFound = BatteryManager.getUniqueInventoryID(); - } - - Set newInventory = SetUtil.cap(cache.inventory, structureFound.getMaxCells()); - - structureFound.inventory = newInventory; - - for(Vector3 obj : structureFound.locations) - { - TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj); - - tileEntity.inventoryID = idFound; + structureFound.inventory = SetUtil.merge(structureFound.inventory, tileEntity.structure.inventory); tileEntity.structure = structureFound; - tileEntity.cachedInventory = newInventory; } + + structureFound.inventory = SetUtil.cap(structureFound.inventory, structureFound.getMaxCells()); } else { - for(TileEntity tileEntity : iteratedNodes) - { - ((TileEntityBattery)tileEntity).structure = null; - } + disperseCells(); } } } diff --git a/src/resonantinduction/battery/SynchronizedBatteryData.java b/src/resonantinduction/battery/SynchronizedBatteryData.java index 058dd7bc..2d45e914 100644 --- a/src/resonantinduction/battery/SynchronizedBatteryData.java +++ b/src/resonantinduction/battery/SynchronizedBatteryData.java @@ -18,6 +18,8 @@ public class SynchronizedBatteryData public int height; + public boolean isMultiblock; + public boolean didTick; public int getVolume() @@ -30,6 +32,25 @@ public class SynchronizedBatteryData return getVolume()*BatteryManager.CELLS_PER_BATTERY; } + public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity, Set inventory) + { + SynchronizedBatteryData structure = getBase(tileEntity); + structure.inventory = inventory; + + return structure; + } + + public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity) + { + SynchronizedBatteryData structure = new SynchronizedBatteryData(); + structure.length = 1; + structure.width = 1; + structure.height = 1; + structure.locations.add(new Vector3(tileEntity)); + + return structure; + } + @Override public int hashCode() { diff --git a/src/resonantinduction/battery/TileEntityBattery.java b/src/resonantinduction/battery/TileEntityBattery.java index cb24cc59..2a4ddc44 100644 --- a/src/resonantinduction/battery/TileEntityBattery.java +++ b/src/resonantinduction/battery/TileEntityBattery.java @@ -28,13 +28,9 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { public Set cachedInventory = new HashSet(); - public SynchronizedBatteryData structure; + public SynchronizedBatteryData structure = SynchronizedBatteryData.getBase(this); public SynchronizedBatteryData prevStructure; - - public boolean clientHasStructure; - - public int inventoryID = BatteryManager.WILDCARD; @Override public void updateEntity() @@ -51,28 +47,20 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver } } - if(playersUsing.size() > 0 && ((worldObj.isRemote && !clientHasStructure) || (!worldObj.isRemote && structure == null))) - { - for(EntityPlayer player : playersUsing) - { - player.closeScreen(); - } - } - if(!worldObj.isRemote) { - if(inventoryID != -1 && structure == null) - { - BatteryManager.updateCache(inventoryID, cachedInventory, this); - } - - if(structure == null && ticks == 5) + if(ticks == 5) { update(); } if(prevStructure != structure) { + for(EntityPlayer player : playersUsing) + { + player.closeScreen(); + } + PacketHandler.sendTileEntityPacketToClients(this, getNetworkedData(new ArrayList())); } @@ -81,13 +69,6 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver if(structure != null) { structure.didTick = false; - - if(inventoryID != -1) - { - BatteryManager.updateCache(inventoryID, structure.inventory, this); - - cachedInventory = structure.inventory; - } } } } @@ -97,22 +78,14 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { super.readFromNBT(nbtTags); - if(structure == null) + NBTTagList tagList = nbtTags.getTagList("Items"); + cachedInventory = new HashSet(); + + for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) { - inventoryID = nbtTags.getInteger("inventoryID"); - - if(inventoryID != BatteryManager.WILDCARD) - { - NBTTagList tagList = nbtTags.getTagList("Items"); - cachedInventory = new HashSet(); + NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount); - for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) - { - NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount); - - cachedInventory.add(ItemStack.loadItemStackFromNBT(tagCompound)); - } - } + cachedInventory.add(ItemStack.loadItemStackFromNBT(tagCompound)); } } @@ -121,8 +94,6 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { super.writeToNBT(nbtTags); - nbtTags.setInteger("inventoryID", inventoryID); - NBTTagList tagList = new NBTTagList(); for(ItemStack itemStack : cachedInventory) @@ -210,14 +181,9 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver public void handle(ByteArrayDataInput input) { try { - if(structure == null) - { - structure = new SynchronizedBatteryData(); - } + structure.isMultiblock = input.readBoolean(); - clientHasStructure = input.readBoolean(); - - if(clientHasStructure) + if(structure.isMultiblock) { structure.height = input.readInt(); structure.length = input.readInt();