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

View file

@ -3,6 +3,7 @@ package resonantinduction.battery;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
@ -248,7 +249,29 @@ public class BatteryUpdateProtocol
idFound = BatteryManager.getUniqueInventoryID(); 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) for(Vector3 obj : structureFound.locations)
{ {
@ -256,7 +279,7 @@ public class BatteryUpdateProtocol
tileEntity.inventoryID = idFound; tileEntity.inventoryID = idFound;
tileEntity.structure = structureFound; tileEntity.structure = structureFound;
tileEntity.inventory = cache.inventory; tileEntity.cachedInventory = newInventory;
} }
} }
else { else {

View file

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

View file

@ -14,6 +14,7 @@ import net.minecraft.nbt.NBTTagList;
import resonantinduction.PacketHandler; import resonantinduction.PacketHandler;
import resonantinduction.api.IBattery; import resonantinduction.api.IBattery;
import resonantinduction.base.IPacketReceiver; import resonantinduction.base.IPacketReceiver;
import resonantinduction.base.SetUtil;
import resonantinduction.base.TileEntityBase; import resonantinduction.base.TileEntityBase;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
@ -25,11 +26,11 @@ import com.google.common.io.ByteArrayDataInput;
*/ */
public class TileEntityBattery extends TileEntityBase implements IPacketReceiver 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 SynchronizedBatteryData structure;
public boolean prevStructure; public SynchronizedBatteryData prevStructure;
public boolean clientHasStructure; public boolean clientHasStructure;
@ -48,8 +49,6 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
{ {
structure = new SynchronizedBatteryData(); structure = new SynchronizedBatteryData();
} }
prevStructure = clientHasStructure;
} }
if(playersUsing.size() > 0 && ((worldObj.isRemote && !clientHasStructure) || (!worldObj.isRemote && structure == null))) 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) if(inventoryID != -1 && structure == null)
{ {
BatteryManager.updateCache(inventoryID, inventory, this); BatteryManager.updateCache(inventoryID, cachedInventory, this);
} }
if(structure == null && ticks == 5) if(structure == null && ticks == 5)
@ -72,12 +71,12 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
update(); update();
} }
if(prevStructure != (structure != null)) if(prevStructure != structure)
{ {
PacketHandler.sendTileEntityPacketToClients(this, getNetworkedData(new ArrayList())); PacketHandler.sendTileEntityPacketToClients(this, getNetworkedData(new ArrayList()));
} }
prevStructure = structure != null; prevStructure = structure;
if(structure != null) if(structure != null)
{ {
@ -87,7 +86,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
{ {
BatteryManager.updateCache(inventoryID, structure.inventory, this); 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) if(inventoryID != BatteryManager.WILDCARD)
{ {
NBTTagList tagList = nbtTags.getTagList("Items"); NBTTagList tagList = nbtTags.getTagList("Items");
inventory = new HashSet<ItemStack>(); cachedInventory = new HashSet<ItemStack>();
for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++) for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++)
{ {
NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(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(); NBTTagList tagList = new NBTTagList();
for(ItemStack itemStack : inventory) for(ItemStack itemStack : cachedInventory)
{ {
if(itemStack != null) if(itemStack != null)
{ {
@ -152,11 +151,30 @@ 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() public float getMaxEnergyStored()
{ {
float max = 0; float max = 0;
for (ItemStack itemStack : inventory) for (ItemStack itemStack : cachedInventory)
{ {
if (itemStack != null) if (itemStack != null)
{ {
@ -170,6 +188,24 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
return max; 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 @Override
public void handle(ByteArrayDataInput input) public void handle(ByteArrayDataInput input)
{ {