Work on cell implementation of Battery

(really signing off now)
This commit is contained in:
Aidan Brady 2013-08-04 04:04:02 -04:00
parent ca44ee4cf4
commit 4ddb716101
5 changed files with 103 additions and 16 deletions

View file

@ -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 <V> Set<V> inverse(Set<V> set)
{
Set<V> toReturn = new HashSet<V>();
List list = Arrays.asList(set.toArray());
for(int i = list.size()-1; i >= 0; i--)
{
toReturn.add((V)list.get(i));
}
return toReturn;
}
}

View file

@ -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<Integer, BatteryCache> dynamicInventories = new HashMap<Integer, BatteryCache>();
@ -36,7 +37,7 @@ public class BatteryManager implements ITickHandler
if(tileEntity != null)
{
tileEntity.inventory = new HashSet<ItemStack>();
tileEntity.cachedInventory = new HashSet<ItemStack>();
tileEntity.inventoryID = WILDCARD;
}
}
@ -157,7 +158,7 @@ public class BatteryManager implements ITickHandler
if(battery != null)
{
battery.inventory = new HashSet<ItemStack>();
battery.cachedInventory = new HashSet<ItemStack>();
battery.inventoryID = WILDCARD;
}
}

View file

@ -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<ItemStack> newInventory = new HashSet<ItemStack>();
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 {

View file

@ -25,6 +25,11 @@ public class SynchronizedBatteryData
return length*width*height;
}
public int getMaxCells()
{
return getVolume()*BatteryManager.CELLS_PER_BATTERY;
}
@Override
public int hashCode()
{

View file

@ -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<ItemStack> inventory = new HashSet<ItemStack>();
public Set<ItemStack> cachedInventory = new HashSet<ItemStack>();
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<ItemStack>();
cachedInventory = new HashSet<ItemStack>();
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)