From b44ea67b46e10243d653582208a883d7d8e09236 Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Mon, 5 Aug 2013 02:08:37 -0400 Subject: [PATCH] Lists > Sets in this situation --- src/resonantinduction/base/ListUtil.java | 167 ++++++++++++++ src/resonantinduction/base/SetUtil.java | 210 ------------------ .../battery/BatteryUpdateProtocol.java | 12 +- .../battery/SynchronizedBatteryData.java | 22 +- .../battery/TileEntityBattery.java | 30 ++- 5 files changed, 205 insertions(+), 236 deletions(-) create mode 100644 src/resonantinduction/base/ListUtil.java delete mode 100644 src/resonantinduction/base/SetUtil.java diff --git a/src/resonantinduction/base/ListUtil.java b/src/resonantinduction/base/ListUtil.java new file mode 100644 index 00000000..1db6cf4e --- /dev/null +++ b/src/resonantinduction/base/ListUtil.java @@ -0,0 +1,167 @@ +package resonantinduction.base; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +public class ListUtil +{ + public static List inverse(List list) + { + List toReturn = new ArrayList(); + + for(int i = list.size(); i >= 0; i--) + { + toReturn.add((V)list.get(i)); + } + + return toReturn; + } + + public static List cap(List list, int cap) + { + List toReturn = new ArrayList(); + + if(list.size() <= cap) + { + toReturn = copy(list); + } + else { + int count = 0; + + for(V obj : list) + { + count++; + + toReturn.add(obj); + + if(count == cap) + { + break; + } + } + } + + return toReturn; + } + + public static List copy(List list) + { + List toReturn = new ArrayList(); + + for(V obj : list) + { + toReturn.add(obj); + } + + return toReturn; + } + + public static List merge(List listOne, List listTwo) + { + List newList = new ArrayList(); + + for(V obj : listOne) + { + newList.add(obj); + } + + for(V obj : listTwo) + { + newList.add(obj); + } + + return newList; + } + + public static List capRemains(List list, int cap) + { + List toReturn = new ArrayList(); + + if(list.size() <= cap) + { + return toReturn; + } + else { + List inverse = inverse(list); + + int iterNeeded = list.size()-cap; + int count = 0; + + for(V obj : list) + { + count++; + + toReturn.add(obj); + + if(count == iterNeeded) + { + break; + } + } + + return toReturn; + } + } + + public static ArrayList> split(List list, int divide) + { + int remain = list.size()%divide; + int size = (list.size()/divide)-remain; + + ArrayList> toReturn = new ArrayList>(); + + for(int i = 0; i < divide; i++) + { + toReturn.add(i, new ArrayList()); + } + + for(List iterSet : toReturn) + { + List removed = new ArrayList(); + + int toAdd = size; + + if(remain > 0) + { + remain--; + toAdd++; + } + + for(V obj : list) + { + if(toAdd == 0) + { + break; + } + + iterSet.add(obj); + removed.add(obj); + toAdd--; + } + + for(V obj : removed) + { + list.remove(obj); + } + } + + return toReturn; + } + + public static V getTop(List list) + { + for(V obj : list) + { + return obj; + } + + return null; + } + + public static List asList(Set set) + { + return (List)Arrays.asList(set.toArray()); + } +} diff --git a/src/resonantinduction/base/SetUtil.java b/src/resonantinduction/base/SetUtil.java deleted file mode 100644 index 724cbac4..00000000 --- a/src/resonantinduction/base/SetUtil.java +++ /dev/null @@ -1,210 +0,0 @@ -package resonantinduction.base; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -public class SetUtil -{ - /** - * Returns a new set with the reverse of the provided set's order. - * @param set - set to perform the operation on - * @return new set with the previous set's order reversed - */ - public static Set inverse(Set set) - { - Set toReturn = new HashSet(); - List list = Arrays.asList(set.toArray()); - - for(int i = list.size(); i >= 0; i--) - { - toReturn.add((V)list.get(i)); - } - - return toReturn; - } - - /** - * Returns a copy of the provided set, removing all objects after the defined cap index. - * @param set - set to perform the operation on - * @param cap - maximum amount of objects this set can store - * @return new set with previous set's objects other than those after the cap - */ - public static Set cap(Set set, int cap) - { - Set toReturn = new HashSet(); - - if(set.size() <= cap) - { - toReturn = copy(set); - } - else { - int count = 0; - - for(V obj : set) - { - count++; - - toReturn.add(obj); - - if(count == cap) - { - break; - } - } - } - - return toReturn; - } - - /** - * Copies a set. - * @param set - set to copy - * @return copied set - */ - public static Set copy(Set set) - { - Set toReturn = new HashSet(); - - for(V obj : set) - { - toReturn.add(obj); - } - - return toReturn; - } - - /** - * Returns a new set created by merging two others together. - * @param setOne - a set to be used in the merge - * @param setTwo - a set to be used in the merge - * @return new merged set - */ - 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 Set capRemains(Set set, int cap) - { - Set toReturn = new HashSet(); - - if(set.size() <= cap) - { - return toReturn; - } - else { - Set inverse = inverse(set); - - int iterNeeded = set.size()-cap; - int count = 0; - - for(V obj : set) - { - count++; - - toReturn.add(obj); - - if(count == iterNeeded) - { - break; - } - } - - return toReturn; - } - } - - /** - * Splits a set into the defined amount of new sets, and returns them in an ArrayList. - * @param set - set to split - * @param divide - how many new sets should be created - * @return sets split by the defined amount - */ - public static ArrayList> split(Set set, int divide) - { - int remain = set.size()%divide; - int size = (set.size()/divide)-remain; - - ArrayList> toReturn = new ArrayList>(); - - for(int i = 0; i < divide; i++) - { - toReturn.add(i, new HashSet()); - } - - for(Set iterSet : toReturn) - { - Set removed = new HashSet(); - - int toAdd = size; - - if(remain > 0) - { - remain--; - toAdd++; - } - - for(V obj : set) - { - if(toAdd == 0) - { - break; - } - - iterSet.add(obj); - removed.add(obj); - toAdd--; - } - - for(V obj : removed) - { - set.remove(obj); - } - } - - return toReturn; - } - - /** - * Gets the top object in a set. - * @param set - set to get the object from - * @return top object in the set - */ - public static V getTop(Set set) - { - for(V obj : set) - { - if(obj != null) - { - return obj; - } - } - - return null; - } - - /** - * Returns a List of a Set, while maintaining all it's objects. - * @param set - set to turn into a list - * @return list with defined set's objects - */ - public static List asList(Set set) - { - return (List)Arrays.asList(set.toArray()); - } -} diff --git a/src/resonantinduction/battery/BatteryUpdateProtocol.java b/src/resonantinduction/battery/BatteryUpdateProtocol.java index 80c53f41..fb9f7035 100644 --- a/src/resonantinduction/battery/BatteryUpdateProtocol.java +++ b/src/resonantinduction/battery/BatteryUpdateProtocol.java @@ -9,7 +9,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; -import resonantinduction.base.SetUtil; +import resonantinduction.base.ListUtil; import resonantinduction.base.Vector3; public class BatteryUpdateProtocol @@ -220,10 +220,10 @@ public class BatteryUpdateProtocol int maxCells = iteratedNodes.size()*BatteryManager.CELLS_PER_BATTERY; //TODO eject these - Set rejected = SetUtil.capRemains(oldStructure.inventory, maxCells); + List rejected = ListUtil.capRemains(oldStructure.inventory, maxCells); - ArrayList> inventories = SetUtil.split(SetUtil.cap(oldStructure.inventory, maxCells), iteratedNodes.size()); - List iterList = SetUtil.asList(iteratedNodes); + ArrayList> inventories = ListUtil.split(ListUtil.cap(oldStructure.inventory, maxCells), iteratedNodes.size()); + List iterList = ListUtil.asList(iteratedNodes); boolean didVisibleInventory = false; @@ -270,7 +270,7 @@ public class BatteryUpdateProtocol { TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj); - structureFound.inventory = SetUtil.merge(structureFound.inventory, tileEntity.structure.inventory); + structureFound.inventory = ListUtil.merge(structureFound.inventory, tileEntity.structure.inventory); if(tileEntity.structure.hasVisibleInventory()) { @@ -280,7 +280,7 @@ public class BatteryUpdateProtocol tileEntity.structure = structureFound; } - structureFound.inventory = SetUtil.cap(structureFound.inventory, structureFound.getMaxCells()); + structureFound.inventory = ListUtil.cap(structureFound.inventory, structureFound.getMaxCells()); } else { disperseCells(); diff --git a/src/resonantinduction/battery/SynchronizedBatteryData.java b/src/resonantinduction/battery/SynchronizedBatteryData.java index 9e4d8f4a..c47db973 100644 --- a/src/resonantinduction/battery/SynchronizedBatteryData.java +++ b/src/resonantinduction/battery/SynchronizedBatteryData.java @@ -1,18 +1,20 @@ package resonantinduction.battery; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import net.minecraft.item.ItemStack; import resonantinduction.api.IBattery; -import resonantinduction.base.SetUtil; +import resonantinduction.base.ListUtil; import resonantinduction.base.Vector3; public class SynchronizedBatteryData { public Set locations = new HashSet(); - public Set inventory = new HashSet(); + public List inventory = new ArrayList(); /** * Slot 0: Cell input slot @@ -47,7 +49,7 @@ public class SynchronizedBatteryData public void sortInventory() { - Object[] array = SetUtil.copy(inventory).toArray(); + Object[] array = ListUtil.copy(inventory).toArray(); ItemStack[] toSort = new ItemStack[array.length]; @@ -63,19 +65,19 @@ public class SynchronizedBatteryData { cont = false; - for(int j = 0; j < toSort.length-1; j++) + for(int i = 0; i < toSort.length-1; i++) { - if(((IBattery)toSort[j].getItem()).getEnergyStored(toSort[j]) < ((IBattery)toSort[j+1].getItem()).getEnergyStored(toSort[j+1])) + if(((IBattery)toSort[i].getItem()).getEnergyStored(toSort[i]) < ((IBattery)toSort[i+1].getItem()).getEnergyStored(toSort[i+1])) { - temp = toSort[j]; - toSort[j] = toSort[j+1]; - toSort[j+1] = temp; + temp = toSort[i]; + toSort[i] = toSort[i+1]; + toSort[i+1] = temp; cont = true; } } } - inventory = new HashSet(); + inventory = new ArrayList(); for(ItemStack itemStack : toSort) { @@ -96,7 +98,7 @@ public class SynchronizedBatteryData return false; } - public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity, Set inventory) + public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity, List inventory) { SynchronizedBatteryData structure = getBase(tileEntity); structure.inventory = inventory; diff --git a/src/resonantinduction/battery/TileEntityBattery.java b/src/resonantinduction/battery/TileEntityBattery.java index a8f2a907..126d5c40 100644 --- a/src/resonantinduction/battery/TileEntityBattery.java +++ b/src/resonantinduction/battery/TileEntityBattery.java @@ -4,8 +4,7 @@ package resonantinduction.battery; import java.util.ArrayList; -import java.util.HashSet; -import java.util.Set; +import java.util.List; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; @@ -15,7 +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.ListUtil; import resonantinduction.base.TileEntityBase; import com.google.common.io.ByteArrayDataInput; @@ -38,8 +37,6 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver @Override public void updateEntity() { - //DO NOT SUPER, CALCLAVIA! - ticks++; if(!worldObj.isRemote) @@ -49,6 +46,18 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver PacketHandler.sendTileEntityPacketToClients(this, getNetworkedData(new ArrayList()).toArray()); } + if(ListUtil.getTop(structure.inventory) != null) + { + System.out.println("-----"); + + for(ItemStack stack : structure.inventory) + { + System.out.println(((IBattery)stack.getItem()).getEnergyStored(stack)); + } + + System.out.println("----"); + } + if(ticks == 5 && !structure.isMultiblock) { update(); @@ -92,12 +101,12 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver if(nbtTags.hasKey("Items")) { NBTTagList tagList = nbtTags.getTagList("Items"); - structure.inventory = new HashSet(); + structure.inventory = new ArrayList(); for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) { NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount); - + System.out.println("Yup!"); structure.inventory.add(ItemStack.loadItemStackFromNBT(tagCompound)); } } @@ -135,6 +144,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { if(itemStack != null) { + System.out.println("YESTYSET"); NBTTagCompound tagCompound = new NBTTagCompound(); itemStack.writeToNBT(tagCompound); tagList.appendTag(tagCompound); @@ -190,7 +200,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver */ public float removeEnergy(float amount) { - Set inverse = SetUtil.inverse(structure.inventory); + List inverse = ListUtil.inverse(structure.inventory); //go from bottom to top return 0; } @@ -292,7 +302,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { if(!worldObj.isRemote) { - return SetUtil.getTop(structure.inventory); + return ListUtil.getTop(structure.inventory); } else { return structure.tempStack; @@ -351,7 +361,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver { if(!worldObj.isRemote) { - structure.inventory.remove(SetUtil.getTop(structure.inventory)); + structure.inventory.remove(ListUtil.getTop(structure.inventory)); } else { structure.tempStack = null;