Work on new (hopefully more efficient) multiblock system

This commit is contained in:
Aidan Brady 2013-08-04 18:56:59 -04:00
parent a2d955d6a3
commit 1834bc09d8
5 changed files with 99 additions and 242 deletions

View file

@ -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 <V> Set<V> inverse(Set<V> set)
@ -49,12 +48,29 @@ public class SetUtil
return toReturn;
}
public static <V> Set<V>[] split(Set<V> set, int divide)
public static <V> Set<V> merge(Set<V> setOne, Set<V> setTwo)
{
Set<V> newSet = new HashSet<V>();
for(V obj : setOne)
{
newSet.add(obj);
}
for(V obj : setTwo)
{
newSet.add(obj);
}
return newSet;
}
public static <V> ArrayList<Set<V>> split(Set<V> set, int divide)
{
int remain = set.size()%divide;
int size = (set.size()/divide)-remain;
Set<V>[] toReturn = new HashSet[divide];
ArrayList<Set<V>> toReturn = new ArrayList<Set<V>>(divide);
for(Set<V> iterSet : toReturn)
{
@ -88,4 +104,9 @@ public class SetUtil
return toReturn;
}
public static <V> ArrayList<V> asList(Set<V> set)
{
return (ArrayList<V>)Arrays.asList(set.toArray());
}
}

View file

@ -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<Integer, BatteryCache> dynamicInventories = new HashMap<Integer, BatteryCache>();
/**
* 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<ItemStack>();
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<ItemStack> 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<TickType> type, Object... tickData)
{
@ -105,68 +27,7 @@ public class BatteryManager implements ITickHandler
@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData)
{
if(tickData[0] instanceof World)
{
ArrayList<Integer> idsToKill = new ArrayList<Integer>();
HashMap<Integer, HashSet<Vector3>> tilesToKill = new HashMap<Integer, HashSet<Vector3>>();
World world = (World)tickData[0];
if(!world.isRemote)
{
for(Map.Entry<Integer, BatteryCache> 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<Vector3>());
}
tilesToKill.get(inventoryID).add(obj);
}
}
}
if(entry.getValue().locations.isEmpty())
{
idsToKill.add(inventoryID);
}
}
for(Map.Entry<Integer, HashSet<Vector3>> 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<ItemStack>();
battery.inventoryID = WILDCARD;
}
}
dynamicInventories.remove(inventoryID);
}
}
}
}
@Override
@ -180,12 +41,4 @@ public class BatteryManager implements ITickHandler
{
return "BatteryMultiblockManager";
}
public static class BatteryCache
{
public Set<ItemStack> inventory = new HashSet<ItemStack>();
public int dimensionId;
public Set<Vector3> locations = new HashSet<Vector3>();
}
}

View file

@ -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
{
@ -162,6 +162,11 @@ public class BatteryUpdateProtocol
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)))
{
structureFound = structure;
@ -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<Set<ItemStack>> inventories = SetUtil.split(oldStructure.inventory, iteratedNodes.size());
ArrayList<TileEntityBattery> 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<ItemStack> 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();
}
}
}

View file

@ -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<ItemStack> 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()
{

View file

@ -28,14 +28,10 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
{
public Set<ItemStack> cachedInventory = new HashSet<ItemStack>();
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<ItemStack>();
for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++)
{
inventoryID = nbtTags.getInteger("inventoryID");
NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount);
if(inventoryID != BatteryManager.WILDCARD)
{
NBTTagList tagList = nbtTags.getTagList("Items");
cachedInventory = new HashSet<ItemStack>();
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();