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; package resonantinduction.base;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import net.minecraft.item.ItemStack;
public class SetUtil public class SetUtil
{ {
public static <V> Set<V> inverse(Set<V> set) public static <V> Set<V> inverse(Set<V> set)
@ -49,12 +48,29 @@ public class SetUtil
return toReturn; 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 remain = set.size()%divide;
int size = (set.size()/divide)-remain; 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) for(Set<V> iterSet : toReturn)
{ {
@ -88,4 +104,9 @@ public class SetUtil
return toReturn; 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 class BatteryManager implements ITickHandler
{ {
public static final int WILDCARD = -1;
public static final int CELLS_PER_BATTERY = 16; 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 @Override
public void tickStart(EnumSet<TickType> type, Object... tickData) public void tickStart(EnumSet<TickType> type, Object... tickData)
{ {
@ -105,68 +27,7 @@ public class BatteryManager implements ITickHandler
@Override @Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) 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 @Override
@ -180,12 +41,4 @@ public class BatteryManager implements ITickHandler
{ {
return "BatteryMultiblockManager"; 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; package resonantinduction.battery;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -9,7 +10,6 @@ import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import resonantinduction.base.SetUtil; import resonantinduction.base.SetUtil;
import resonantinduction.base.Vector3; import resonantinduction.base.Vector3;
import resonantinduction.battery.BatteryManager.BatteryCache;
public class BatteryUpdateProtocol public class BatteryUpdateProtocol
{ {
@ -161,6 +161,11 @@ public class BatteryUpdateProtocol
structure.length = Math.abs(xmax-xmin)+1; structure.length = Math.abs(xmax-xmin)+1;
structure.height = Math.abs(ymax-ymin)+1; structure.height = Math.abs(ymax-ymin)+1;
structure.width = Math.abs(zmax-zmin)+1; structure.width = Math.abs(zmax-zmin)+1;
if(structure.getVolume() > 1)
{
structure.isMultiblock = true;
}
if(structure.locations.contains(new Vector3(pointer))) if(structure.locations.contains(new Vector3(pointer)))
{ {
@ -196,6 +201,32 @@ public class BatteryUpdateProtocol
return false; 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. * 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))) if(!structureFound.locations.contains(new Vector3(tileEntity)))
{ {
for(TileEntity tile : iteratedNodes) disperseCells();
{
((TileEntityBattery)tileEntity).structure = null;
}
return; return;
} }
@ -224,50 +252,18 @@ public class BatteryUpdateProtocol
System.out.println("Width: " + structureFound.width); System.out.println("Width: " + structureFound.width);
System.out.println("Volume: " + structureFound.locations.size()); System.out.println("Volume: " + structureFound.locations.size());
int idFound = BatteryManager.WILDCARD;
for(Vector3 obj : structureFound.locations) for(Vector3 obj : structureFound.locations)
{ {
TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj); TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj);
if(tileEntity.inventoryID != BatteryManager.WILDCARD) structureFound.inventory = SetUtil.merge(structureFound.inventory, tileEntity.structure.inventory);
{
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;
tileEntity.structure = structureFound; tileEntity.structure = structureFound;
tileEntity.cachedInventory = newInventory;
} }
structureFound.inventory = SetUtil.cap(structureFound.inventory, structureFound.getMaxCells());
} }
else { else {
for(TileEntity tileEntity : iteratedNodes) disperseCells();
{
((TileEntityBattery)tileEntity).structure = null;
}
} }
} }
} }

View file

@ -18,6 +18,8 @@ public class SynchronizedBatteryData
public int height; public int height;
public boolean isMultiblock;
public boolean didTick; public boolean didTick;
public int getVolume() public int getVolume()
@ -30,6 +32,25 @@ public class SynchronizedBatteryData
return getVolume()*BatteryManager.CELLS_PER_BATTERY; 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 @Override
public int hashCode() public int hashCode()
{ {

View file

@ -28,13 +28,9 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
{ {
public Set<ItemStack> cachedInventory = new HashSet<ItemStack>(); public Set<ItemStack> cachedInventory = new HashSet<ItemStack>();
public SynchronizedBatteryData structure; public SynchronizedBatteryData structure = SynchronizedBatteryData.getBase(this);
public SynchronizedBatteryData prevStructure; public SynchronizedBatteryData prevStructure;
public boolean clientHasStructure;
public int inventoryID = BatteryManager.WILDCARD;
@Override @Override
public void updateEntity() 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(!worldObj.isRemote)
{ {
if(inventoryID != -1 && structure == null) if(ticks == 5)
{
BatteryManager.updateCache(inventoryID, cachedInventory, this);
}
if(structure == null && ticks == 5)
{ {
update(); update();
} }
if(prevStructure != structure) if(prevStructure != structure)
{ {
for(EntityPlayer player : playersUsing)
{
player.closeScreen();
}
PacketHandler.sendTileEntityPacketToClients(this, getNetworkedData(new ArrayList())); PacketHandler.sendTileEntityPacketToClients(this, getNetworkedData(new ArrayList()));
} }
@ -81,13 +69,6 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
if(structure != null) if(structure != null)
{ {
structure.didTick = false; 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); 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++) cachedInventory.add(ItemStack.loadItemStackFromNBT(tagCompound));
{
NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount);
cachedInventory.add(ItemStack.loadItemStackFromNBT(tagCompound));
}
}
} }
} }
@ -121,8 +94,6 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
{ {
super.writeToNBT(nbtTags); super.writeToNBT(nbtTags);
nbtTags.setInteger("inventoryID", inventoryID);
NBTTagList tagList = new NBTTagList(); NBTTagList tagList = new NBTTagList();
for(ItemStack itemStack : cachedInventory) for(ItemStack itemStack : cachedInventory)
@ -210,14 +181,9 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
public void handle(ByteArrayDataInput input) public void handle(ByteArrayDataInput input)
{ {
try { try {
if(structure == null) structure.isMultiblock = input.readBoolean();
{
structure = new SynchronizedBatteryData();
}
clientHasStructure = input.readBoolean(); if(structure.isMultiblock)
if(clientHasStructure)
{ {
structure.height = input.readInt(); structure.height = input.readInt();
structure.length = input.readInt(); structure.length = input.readInt();