Batteries are less buggy and more efficient

This commit is contained in:
Aidan C. Brady 2013-11-16 16:55:20 -05:00
parent 3ec8a3bdb1
commit 57b1343311
5 changed files with 206 additions and 230 deletions

View file

@ -1,5 +1,6 @@
package mekanism.induction.client.gui; package mekanism.induction.client.gui;
import mekanism.induction.common.BatteryManager;
import mekanism.induction.common.MekanismInduction; import mekanism.induction.common.MekanismInduction;
import mekanism.induction.common.inventory.container.ContainerBattery; import mekanism.induction.common.inventory.container.ContainerBattery;
import mekanism.induction.common.tileentity.TileEntityBattery; import mekanism.induction.common.tileentity.TileEntityBattery;
@ -33,7 +34,7 @@ public class GuiBattery extends GuiContainer
fontRenderer.drawString("Battery", 43, 6, 0x404040); fontRenderer.drawString("Battery", 43, 6, 0x404040);
fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 2, 0x404040); fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 2, 0x404040);
fontRenderer.drawString("Cells: " + tileEntity.clientCells + " / " + tileEntity.structure.getMaxCells(), 62, 23, 0x404040); fontRenderer.drawString("Cells: " + tileEntity.clientCells + " / " + (tileEntity.clientVolume*BatteryManager.CELLS_PER_BATTERY), 62, 23, 0x404040);
fontRenderer.drawString("Energy: ", 62, 33, 0x404040); fontRenderer.drawString("Energy: ", 62, 33, 0x404040);
fontRenderer.drawString(ElectricityDisplay.getDisplay(this.tileEntity.getEnergyStored(), ElectricUnit.JOULES, 4, true), 62, 43, 0x404040); fontRenderer.drawString(ElectricityDisplay.getDisplay(this.tileEntity.getEnergyStored(), ElectricUnit.JOULES, 4, true), 62, 43, 0x404040);
fontRenderer.drawString("Max: " + ElectricityDisplay.getDisplayShort(this.tileEntity.getMaxEnergyStored(), ElectricUnit.JOULES), 62, 53, 0x404040); fontRenderer.drawString("Max: " + ElectricityDisplay.getDisplayShort(this.tileEntity.getMaxEnergyStored(), ElectricUnit.JOULES), 62, 53, 0x404040);

View file

@ -1,6 +1,7 @@
package mekanism.induction.common; package mekanism.induction.common;
import java.util.ArrayList; import java.util.ArrayList;
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;
@ -31,7 +32,7 @@ public class BatteryUpdateProtocol
private void loopThrough(TileEntity tile) private void loopThrough(TileEntity tile)
{ {
if (structureFound == null) if(structureFound == null)
{ {
World worldObj = tile.worldObj; World worldObj = tile.worldObj;
@ -46,14 +47,14 @@ public class BatteryUpdateProtocol
int x = 0, y = 0, z = 0; int x = 0, y = 0, z = 0;
if ((isBattery(origX + 1, origY, origZ) && isBattery(origX - 1, origY, origZ)) || (isBattery(origX, origY + 1, origZ) && isBattery(origX, origY - 1, origZ)) || (isBattery(origX, origY, origZ + 1) && isBattery(origX, origY, origZ - 1))) if((isBattery(origX + 1, origY, origZ) && isBattery(origX - 1, origY, origZ)) || (isBattery(origX, origY + 1, origZ) && isBattery(origX, origY - 1, origZ)) || (isBattery(origX, origY, origZ + 1) && isBattery(origX, origY, origZ - 1)))
{ {
isCorner = false; isCorner = false;
} }
if (isCorner) if(isCorner)
{ {
if (isBattery(origX + 1, origY, origZ)) if(isBattery(origX + 1, origY, origZ))
{ {
xmin = 0; xmin = 0;
@ -64,8 +65,7 @@ public class BatteryUpdateProtocol
xmax = x; xmax = x;
} }
else else {
{
xmax = 0; xmax = 0;
while (isBattery(origX + x - 1, origY, origZ)) while (isBattery(origX + x - 1, origY, origZ))
@ -76,22 +76,21 @@ public class BatteryUpdateProtocol
xmin = x; xmin = x;
} }
if (isBattery(origX, origY + 1, origZ)) if(isBattery(origX, origY + 1, origZ))
{ {
ymin = 0; ymin = 0;
while (isBattery(origX, origY + y + 1, origZ)) while(isBattery(origX, origY + y + 1, origZ))
{ {
y++; y++;
} }
ymax = y; ymax = y;
} }
else else {
{
ymax = 0; ymax = 0;
while (isBattery(origX, origY + y - 1, origZ)) while(isBattery(origX, origY + y - 1, origZ))
{ {
y--; y--;
} }
@ -99,19 +98,18 @@ public class BatteryUpdateProtocol
ymin = y; ymin = y;
} }
if (isBattery(origX, origY, origZ + 1)) if(isBattery(origX, origY, origZ + 1))
{ {
zmin = 0; zmin = 0;
while (isBattery(origX, origY, origZ + z + 1)) while(isBattery(origX, origY, origZ + z + 1))
{ {
z++; z++;
} }
zmax = z; zmax = z;
} }
else else {
{
zmax = 0; zmax = 0;
while (isBattery(origX, origY, origZ + z - 1)) while (isBattery(origX, origY, origZ + z - 1))
@ -122,52 +120,48 @@ public class BatteryUpdateProtocol
zmin = z; zmin = z;
} }
for (x = xmin; x <= xmax; x++) for(x = xmin; x <= xmax; x++)
{ {
for (y = ymin; y <= ymax; y++) for(y = ymin; y <= ymax; y++)
{ {
for (z = zmin; z <= zmax; z++) for(z = zmin; z <= zmax; z++)
{ {
if (!isBattery(origX + x, origY + y, origZ + z)) if(!isBattery(origX + x, origY + y, origZ + z))
{ {
rightBlocks = false; rightBlocks = false;
break; break;
} }
else else {
{
locations.add(new Vector3(tile).translate(new Vector3(x, y, z))); locations.add(new Vector3(tile).translate(new Vector3(x, y, z)));
} }
} }
if (!rightBlocks) if(!rightBlocks)
{ {
break; break;
} }
} }
if (!rightBlocks) if(!rightBlocks)
{ {
break; break;
} }
} }
} }
if (locations.size() >= 1 && locations.size() < 512) if(locations.size() >= 1 && locations.size() < 512)
{ {
if (rightBlocks && isCorner) if(rightBlocks && isCorner)
{ {
SynchronizedBatteryData structure = new SynchronizedBatteryData(); SynchronizedBatteryData structure = new SynchronizedBatteryData();
structure.locations = locations; structure.locations = locations;
structure.length = Math.abs(xmax - xmin) + 1;
structure.height = Math.abs(ymax - ymin) + 1;
structure.width = Math.abs(zmax - zmin) + 1;
if (structure.getVolume() > 1) if(structure.getVolume() > 1)
{ {
structure.isMultiblock = true; structure.isMultiblock = true;
} }
if (structure.locations.contains(new Vector3(pointer))) if(structure.locations.contains(new Vector3(pointer)))
{ {
structureFound = structure; structureFound = structure;
} }
@ -177,13 +171,13 @@ public class BatteryUpdateProtocol
iteratedNodes.add((TileEntityBattery) tile); iteratedNodes.add((TileEntityBattery) tile);
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{ {
TileEntity tileEntity = new Vector3(tile).modifyPositionFromSide(side).getTileEntity(tile.worldObj); TileEntity tileEntity = new Vector3(tile).modifyPositionFromSide(side).getTileEntity(tile.worldObj);
if (tileEntity instanceof TileEntityBattery) if(tileEntity instanceof TileEntityBattery)
{ {
if (!iteratedNodes.contains(tileEntity)) if(!iteratedNodes.contains(tileEntity))
{ {
loopThrough(tileEntity); loopThrough(tileEntity);
} }
@ -193,7 +187,7 @@ public class BatteryUpdateProtocol
private boolean isBattery(int x, int y, int z) private boolean isBattery(int x, int y, int z)
{ {
if (pointer.worldObj.getBlockTileEntity(x, y, z) instanceof TileEntityBattery) if(pointer.worldObj.getBlockTileEntity(x, y, z) instanceof TileEntityBattery)
{ {
return true; return true;
} }
@ -203,55 +197,70 @@ public class BatteryUpdateProtocol
private void disperseCells() private void disperseCells()
{ {
SynchronizedBatteryData oldStructure = null; Set<SynchronizedBatteryData> structures = new HashSet<SynchronizedBatteryData>();
List<ItemStack> mergedInv = new ArrayList<ItemStack>();
List<ItemStack[]> visibleInvs = new ArrayList<ItemStack[]>();
for (TileEntityBattery tile : iteratedNodes) for(TileEntityBattery tile : iteratedNodes)
{ {
if (tile.structure.isMultiblock) structures.add(tile.structure);
}
for(SynchronizedBatteryData data : structures)
{
mergedInv = ListUtil.merge(mergedInv, data.inventory);
if(data.hasVisibleInventory())
{ {
oldStructure = tile.structure; visibleInvs.add(data.visibleInventory);
break;
} }
} }
if (oldStructure != null) int maxCells = iteratedNodes.size() * BatteryManager.CELLS_PER_BATTERY;
List<ItemStack> rejected = ListUtil.capRemains(mergedInv, maxCells);
ejectItems(rejected, new Vector3(pointer));
ArrayList<List<ItemStack>> inventories = ListUtil.split(ListUtil.cap(mergedInv, maxCells), iteratedNodes.size());
List<TileEntityBattery> iterList = ListUtil.asList(iteratedNodes);
for(int i = 0; i < iterList.size(); i++)
{ {
int maxCells = iteratedNodes.size() * BatteryManager.CELLS_PER_BATTERY; TileEntityBattery tile = iterList.get(i);
tile.structure = SynchronizedBatteryData.getBase(tile, inventories.get(i));
List<ItemStack> rejected = ListUtil.capRemains(oldStructure.inventory, maxCells); if(!visibleInvs.isEmpty())
ejectItems(rejected, new Vector3(pointer));
ArrayList<List<ItemStack>> inventories = ListUtil.split(ListUtil.cap(oldStructure.inventory, maxCells), iteratedNodes.size());
List<TileEntityBattery> iterList = ListUtil.asList(iteratedNodes);
boolean didVisibleInventory = false;
for (int i = 0; i < iterList.size(); i++)
{ {
TileEntityBattery tile = iterList.get(i); tile.structure.visibleInventory = visibleInvs.get(0);
tile.structure = SynchronizedBatteryData.getBase(tile, inventories.get(i)); visibleInvs.remove(0);
}
if (!didVisibleInventory) }
{
tile.structure.visibleInventory = oldStructure.visibleInventory; if(!visibleInvs.isEmpty())
didVisibleInventory = true; {
} for(ItemStack[] inv : visibleInvs)
{
ejectItems(Arrays.asList(inv), new Vector3(pointer));
} }
} }
} }
private void ejectItems(List<ItemStack> items, Vector3 vec) private void ejectItems(List<ItemStack> items, Vector3 vec)
{ {
for (ItemStack itemStack : items) for(ItemStack itemStack : items)
{ {
float motion = 0.7F; if(itemStack != null)
double motionX = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; {
double motionY = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; float motion = 0.7F;
double motionZ = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D; double motionX = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
double motionY = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
EntityItem entityItem = new EntityItem(pointer.worldObj, vec.x + motionX, vec.y + motionY, vec.z + motionZ, itemStack); double motionZ = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
pointer.worldObj.spawnEntityInWorld(entityItem); EntityItem entityItem = new EntityItem(pointer.worldObj, vec.x + motionX, vec.y + motionY, vec.z + motionZ, itemStack);
pointer.worldObj.spawnEntityInWorld(entityItem);
}
} }
} }
@ -259,27 +268,36 @@ public class BatteryUpdateProtocol
{ {
loopThrough(pointer); loopThrough(pointer);
if (structureFound != null) if(structureFound != null)
{ {
for (TileEntityBattery tileEntity : iteratedNodes) for(TileEntityBattery tileEntity : iteratedNodes)
{ {
if (!structureFound.locations.contains(new Vector3(tileEntity))) if(!structureFound.locations.contains(new Vector3(tileEntity)))
{ {
disperseCells(); disperseCells();
return; return;
} }
} }
boolean foundVisibleInv = false;
for (Vector3 obj : structureFound.locations) for(Vector3 obj : structureFound.locations)
{ {
TileEntityBattery tileEntity = (TileEntityBattery) obj.getTileEntity(pointer.worldObj); TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj);
structureFound.inventory = ListUtil.merge(structureFound.inventory, tileEntity.structure.inventory); structureFound.inventory = ListUtil.merge(structureFound.inventory, tileEntity.structure.inventory);
if (tileEntity.structure.hasVisibleInventory()) if(tileEntity.structure.hasVisibleInventory())
{ {
structureFound.visibleInventory = tileEntity.structure.visibleInventory; if(foundVisibleInv)
{
ejectItems(Arrays.asList(tileEntity.structure.visibleInventory), obj);
}
else {
structureFound.visibleInventory = tileEntity.structure.visibleInventory;
foundVisibleInv = true;
}
} }
tileEntity.structure = structureFound; tileEntity.structure = structureFound;
@ -290,8 +308,7 @@ public class BatteryUpdateProtocol
structureFound.inventory = ListUtil.cap(structureFound.inventory, structureFound.getMaxCells()); structureFound.inventory = ListUtil.cap(structureFound.inventory, structureFound.getMaxCells());
} }
else else {
{
disperseCells(); disperseCells();
} }
} }

View file

@ -21,12 +21,6 @@ public class SynchronizedBatteryData
*/ */
public ItemStack[] visibleInventory = new ItemStack[3]; public ItemStack[] visibleInventory = new ItemStack[3];
public int length;
public int width;
public int height;
public ItemStack tempStack; public ItemStack tempStack;
public boolean isMultiblock; public boolean isMultiblock;
@ -35,16 +29,16 @@ public class SynchronizedBatteryData
public boolean wroteInventory; public boolean wroteInventory;
public int getVolume()
{
return length * width * height;
}
public int getMaxCells() public int getMaxCells()
{ {
return getVolume() * BatteryManager.CELLS_PER_BATTERY; return getVolume() * BatteryManager.CELLS_PER_BATTERY;
} }
public int getVolume()
{
return locations.size();
}
public boolean addCell(ItemStack cell) public boolean addCell(ItemStack cell)
{ {
if (this.inventory.size() < this.getMaxCells()) if (this.inventory.size() < this.getMaxCells())
@ -119,9 +113,6 @@ public class SynchronizedBatteryData
public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity) public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity)
{ {
SynchronizedBatteryData structure = new SynchronizedBatteryData(); SynchronizedBatteryData structure = new SynchronizedBatteryData();
structure.length = 1;
structure.width = 1;
structure.height = 1;
structure.locations.add(new Vector3(tileEntity)); structure.locations.add(new Vector3(tileEntity));
return structure; return structure;
@ -132,9 +123,6 @@ public class SynchronizedBatteryData
{ {
int code = 1; int code = 1;
code = 31 * locations.hashCode(); code = 31 * locations.hashCode();
code = 31 * length;
code = 31 * width;
code = 31 * height;
return code; return code;
} }
@ -153,11 +141,6 @@ public class SynchronizedBatteryData
return false; return false;
} }
if (data.length != length || data.width != width || data.height != height)
{
return false;
}
return true; return true;
} }
} }

View file

@ -12,7 +12,6 @@ import mekanism.induction.common.tileentity.TileEntityBattery;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
@ -125,13 +124,13 @@ public class BlockBattery extends Block implements ITileEntityProvider
} }
} }
} }
@Override @Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityliving, ItemStack itemstack) public void onBlockAdded(World world, int x, int y, int z)
{ {
if (!world.isRemote) if (!world.isRemote)
{ {
TileEntityBattery battery = (TileEntityBattery) world.getBlockTileEntity(x, y, z); TileEntityBattery battery = (TileEntityBattery)world.getBlockTileEntity(x, y, z);
battery.update(); battery.update();
} }

View file

@ -55,6 +55,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
public float clientEnergy; public float clientEnergy;
public int clientCells; public int clientCells;
public float clientMaxEnergy; public float clientMaxEnergy;
public int clientVolume;
private EnumSet inputSides = EnumSet.allOf(ForgeDirection.class); private EnumSet inputSides = EnumSet.allOf(ForgeDirection.class);
@ -63,18 +64,18 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{ {
super.updateEntity(); super.updateEntity();
if (!this.worldObj.isRemote) if(!worldObj.isRemote)
{ {
if (this.ticks == 5 && !this.structure.isMultiblock) if(ticks == 5 && !structure.isMultiblock)
{ {
this.update(); update();
} }
if (this.structure.visibleInventory[0] != null) if(structure.visibleInventory[0] != null)
{ {
if (structure.inventory.size() < structure.getMaxCells()) if(structure.inventory.size() < structure.getMaxCells())
{ {
if (structure.visibleInventory[0].getItem() instanceof ICapacitor) if(structure.visibleInventory[0].getItem() instanceof ICapacitor)
{ {
structure.inventory.add(structure.visibleInventory[0]); structure.inventory.add(structure.visibleInventory[0]);
structure.visibleInventory[0] = null; structure.visibleInventory[0] = null;
@ -89,25 +90,25 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
*/ */
ItemStack chargeItem = null; ItemStack chargeItem = null;
if (this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord)) if(worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))
{ {
List<Entity> entities = this.worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(this.xCoord, this.yCoord + 1, this.zCoord, this.xCoord + 1, this.yCoord + 2, this.zCoord + 1)); List<Entity> entities = worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord + 1, zCoord, xCoord + 1, yCoord + 2, zCoord + 1));
electricItemLoop: electricItemLoop:
for (Entity entity : entities) for (Entity entity : entities)
{ {
if (entity instanceof EntityPlayer) if(entity instanceof EntityPlayer)
{ {
IInventory inventory = ((EntityPlayer) entity).inventory; IInventory inventory = ((EntityPlayer) entity).inventory;
for (int i = 0; i < inventory.getSizeInventory(); i++) for (int i = 0; i < inventory.getSizeInventory(); i++)
{ {
ItemStack checkStack = inventory.getStackInSlot(i); ItemStack checkStack = inventory.getStackInSlot(i);
if (checkStack != null) if(checkStack != null)
{ {
if (checkStack.getItem() instanceof IItemElectric) if(checkStack.getItem() instanceof IItemElectric)
{ {
if (((IItemElectric) checkStack.getItem()).recharge(checkStack, provideElectricity(this.getTransferThreshhold(), false).getWatts(), false) > 0) if(((IItemElectric) checkStack.getItem()).recharge(checkStack, provideElectricity(getTransferThreshhold(), false).getWatts(), false) > 0)
{ {
chargeItem = checkStack; chargeItem = checkStack;
break electricItemLoop; break electricItemLoop;
@ -116,15 +117,15 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
} }
} }
else if (entity instanceof EntityItem) else if(entity instanceof EntityItem)
{ {
ItemStack checkStack = ((EntityItem) entity).getEntityItem(); ItemStack checkStack = ((EntityItem) entity).getEntityItem();
if (checkStack != null) if(checkStack != null)
{ {
if (checkStack.getItem() instanceof IItemElectric) if(checkStack.getItem() instanceof IItemElectric)
{ {
if (((IItemElectric) checkStack.getItem()).recharge(checkStack, provideElectricity(this.getTransferThreshhold(), false).getWatts(), false) > 0) if(((IItemElectric) checkStack.getItem()).recharge(checkStack, provideElectricity(getTransferThreshhold(), false).getWatts(), false) > 0)
{ {
chargeItem = checkStack; chargeItem = checkStack;
break electricItemLoop; break electricItemLoop;
@ -135,34 +136,34 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
} }
if (chargeItem == null) if(chargeItem == null)
{ {
chargeItem = this.structure.visibleInventory[1]; chargeItem = structure.visibleInventory[1];
} }
if (chargeItem != null) if(chargeItem != null)
{ {
ItemStack itemStack = chargeItem; ItemStack itemStack = chargeItem;
IItemElectric battery = (IItemElectric) itemStack.getItem(); IItemElectric battery = (IItemElectric) itemStack.getItem();
float energyStored = getMaxEnergyStored(); float energyStored = getMaxEnergyStored();
float batteryNeeded = battery.recharge(itemStack, provideElectricity(this.getTransferThreshhold(), false).getWatts(), false); float batteryNeeded = battery.recharge(itemStack, provideElectricity(getTransferThreshhold(), false).getWatts(), false);
float toGive = Math.min(energyStored, Math.min(battery.getTransfer(itemStack), batteryNeeded)); float toGive = Math.min(energyStored, Math.min(battery.getTransfer(itemStack), batteryNeeded));
battery.recharge(itemStack, provideElectricity(toGive, true).getWatts(), true); battery.recharge(itemStack, provideElectricity(toGive, true).getWatts(), true);
} }
if (structure.visibleInventory[2] != null) if(structure.visibleInventory[2] != null)
{ {
ItemStack itemStack = structure.visibleInventory[2]; ItemStack itemStack = structure.visibleInventory[2];
IItemElectric battery = (IItemElectric) itemStack.getItem(); IItemElectric battery = (IItemElectric) itemStack.getItem();
float energyNeeded = getMaxEnergyStored() - getEnergyStored(); float energyNeeded = getMaxEnergyStored() - getEnergyStored();
float batteryStored = battery.getElectricityStored(itemStack); float batteryStored = battery.getElectricityStored(itemStack);
float toReceive = Math.min(energyNeeded, Math.min(this.getTransferThreshhold(), Math.min(battery.getTransfer(itemStack), batteryStored))); float toReceive = Math.min(energyNeeded, Math.min(getTransferThreshhold(), Math.min(battery.getTransfer(itemStack), batteryStored)));
battery.discharge(itemStack, receiveElectricity(toReceive, true), true); battery.discharge(itemStack, receiveElectricity(toReceive, true), true);
} }
if (prevStructure != structure) if(prevStructure != structure)
{ {
for (EntityPlayer player : playersUsing) for (EntityPlayer player : playersUsing)
{ {
@ -172,28 +173,28 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
updateClient(); updateClient();
} }
this.prevStructure = structure; prevStructure = structure;
this.structure.wroteInventory = false; structure.wroteInventory = false;
this.structure.didTick = false; structure.didTick = false;
if (this.playersUsing.size() > 0) if(playersUsing.size() > 0)
{ {
updateClient(); updateClient();
} }
for (EntityPlayer player : this.playersUsing) for (EntityPlayer player : playersUsing)
{ {
PacketHandler.sendPacket(Transmission.SINGLE_CLIENT, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())), player); PacketHandler.sendPacket(Transmission.SINGLE_CLIENT, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())), player);
} }
this.produce(); produce();
} }
} }
public float getTransferThreshhold() public float getTransferThreshhold()
{ {
return this.structure.getVolume() * 50; return structure.getVolume() * 50;
} }
public void updateClient() public void updateClient()
@ -227,7 +228,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
super.readFromNBT(nbtTags); super.readFromNBT(nbtTags);
// Main inventory // Main inventory
if (nbtTags.hasKey("Items")) if(nbtTags.hasKey("Items"))
{ {
NBTTagList tagList = nbtTags.getTagList("Items"); NBTTagList tagList = nbtTags.getTagList("Items");
structure.inventory = new ArrayList<ItemStack>(); structure.inventory = new ArrayList<ItemStack>();
@ -241,7 +242,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
// Visible inventory // Visible inventory
if (nbtTags.hasKey("VisibleItems")) if(nbtTags.hasKey("VisibleItems"))
{ {
NBTTagList tagList = nbtTags.getTagList("VisibleItems"); NBTTagList tagList = nbtTags.getTagList("VisibleItems");
structure.visibleInventory = new ItemStack[3]; structure.visibleInventory = new ItemStack[3];
@ -251,9 +252,9 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount); NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount);
byte slotID = tagCompound.getByte("Slot"); byte slotID = tagCompound.getByte("Slot");
if (slotID >= 0 && slotID < structure.visibleInventory.length) if(slotID >= 0 && slotID < structure.visibleInventory.length)
{ {
if (slotID == 0) if(slotID == 0)
{ {
setInventorySlotContents(slotID, ItemStack.loadItemStackFromNBT(tagCompound)); setInventorySlotContents(slotID, ItemStack.loadItemStackFromNBT(tagCompound));
} }
@ -265,7 +266,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
} }
this.inputSides = EnumSet.noneOf(ForgeDirection.class); inputSides = EnumSet.noneOf(ForgeDirection.class);
NBTTagList tagList = nbtTags.getTagList("inputSides"); NBTTagList tagList = nbtTags.getTagList("inputSides");
@ -273,10 +274,10 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{ {
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount); NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount);
byte side = tagCompound.getByte("side"); byte side = tagCompound.getByte("side");
this.inputSides.add(ForgeDirection.getOrientation(side)); inputSides.add(ForgeDirection.getOrientation(side));
} }
this.inputSides.remove(ForgeDirection.UNKNOWN); inputSides.remove(ForgeDirection.UNKNOWN);
} }
@Override @Override
@ -284,16 +285,16 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{ {
super.writeToNBT(nbt); super.writeToNBT(nbt);
if (!structure.wroteInventory) if(!structure.wroteInventory)
{ {
// Inventory // Inventory
if (structure.inventory != null) if(structure.inventory != null)
{ {
NBTTagList tagList = new NBTTagList(); NBTTagList tagList = new NBTTagList();
for (int slotCount = 0; slotCount < structure.inventory.size(); slotCount++) for (int slotCount = 0; slotCount < structure.inventory.size(); slotCount++)
{ {
if (structure.inventory.get(slotCount) != null) if(structure.inventory.get(slotCount) != null)
{ {
NBTTagCompound tagCompound = new NBTTagCompound(); NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setInteger("Slot", slotCount); tagCompound.setInteger("Slot", slotCount);
@ -306,18 +307,18 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
// Visible inventory // Visible inventory
if (structure.visibleInventory != null) if(structure.visibleInventory != null)
{ {
NBTTagList tagList = new NBTTagList(); NBTTagList tagList = new NBTTagList();
for (int slotCount = 0; slotCount < structure.visibleInventory.length; slotCount++) for (int slotCount = 0; slotCount < structure.visibleInventory.length; slotCount++)
{ {
if (slotCount > 0) if(slotCount > 0)
{ {
slotCount++; slotCount++;
} }
if (getStackInSlot(slotCount) != null) if(getStackInSlot(slotCount) != null)
{ {
NBTTagCompound tagCompound = new NBTTagCompound(); NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setByte("Slot", (byte) slotCount); tagCompound.setByte("Slot", (byte) slotCount);
@ -335,13 +336,13 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
* Save the input sides. * Save the input sides.
*/ */
NBTTagList tagList = new NBTTagList(); NBTTagList tagList = new NBTTagList();
Iterator<ForgeDirection> it = this.inputSides.iterator(); Iterator<ForgeDirection> it = inputSides.iterator();
while (it.hasNext()) while (it.hasNext())
{ {
ForgeDirection dir = it.next(); ForgeDirection dir = it.next();
if (dir != ForgeDirection.UNKNOWN) if(dir != ForgeDirection.UNKNOWN)
{ {
NBTTagCompound tagCompound = new NBTTagCompound(); NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setByte("side", (byte) dir.ordinal()); tagCompound.setByte("side", (byte) dir.ordinal());
@ -355,11 +356,11 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
public void update() public void update()
{ {
if (!worldObj.isRemote && (structure == null || !structure.didTick)) if(!worldObj.isRemote && (structure == null || !structure.didTick))
{ {
new BatteryUpdateProtocol(this).updateBatteries(); new BatteryUpdateProtocol(this).updateBatteries();
if (structure != null) if(structure != null)
{ {
structure.didTick = true; structure.didTick = true;
} }
@ -374,21 +375,21 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
for (ItemStack itemStack : structure.inventory) for (ItemStack itemStack : structure.inventory)
{ {
if (itemStack.getItem() instanceof IItemElectric) if(itemStack.getItem() instanceof IItemElectric)
{ {
IItemElectric battery = (IItemElectric) itemStack.getItem(); IItemElectric battery = (IItemElectric) itemStack.getItem();
float needed = amount - added; float needed = amount - added;
float itemAdd = Math.min(battery.getMaxElectricityStored(itemStack) - battery.getElectricityStored(itemStack), needed); float itemAdd = Math.min(battery.getMaxElectricityStored(itemStack) - battery.getElectricityStored(itemStack), needed);
if (doAdd) if(doAdd)
{ {
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) + itemAdd); battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) + itemAdd);
} }
added += itemAdd; added += itemAdd;
if (amount == added) if(amount == added)
{ {
break; break;
} }
@ -408,42 +409,42 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
float removed = 0; float removed = 0;
for (ItemStack itemStack : inverse) for (ItemStack itemStack : inverse)
{ {
if (itemStack.getItem() instanceof IItemElectric) if(itemStack.getItem() instanceof IItemElectric)
{ {
IItemElectric battery = (IItemElectric) itemStack.getItem(); IItemElectric battery = (IItemElectric) itemStack.getItem();
float needed = amount - removed; float needed = amount - removed;
float itemRemove = Math.min(battery.getElectricityStored(itemStack), needed); float itemRemove = Math.min(battery.getElectricityStored(itemStack), needed);
if (doRemove) if(doRemove)
{ {
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) - itemRemove); battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) - itemRemove);
} }
removed += itemRemove; removed += itemRemove;
if (amount == removed) if(amount == removed)
{ {
break; break;
} }
} }
} }
return ElectricityPack.getFromWatts(removed, this.getVoltage()); return ElectricityPack.getFromWatts(removed, getVoltage());
} }
@Override @Override
public float getMaxEnergyStored() public float getMaxEnergyStored()
{ {
if (!this.worldObj.isRemote) if(!worldObj.isRemote)
{ {
float max = 0; float max = 0;
for (ItemStack itemStack : this.structure.inventory) for (ItemStack itemStack : structure.inventory)
{ {
if (itemStack != null) if(itemStack != null)
{ {
if (itemStack.getItem() instanceof IItemElectric) if(itemStack.getItem() instanceof IItemElectric)
{ {
max += ((IItemElectric) itemStack.getItem()).getMaxElectricityStored(itemStack); max += ((IItemElectric) itemStack.getItem()).getMaxElectricityStored(itemStack);
} }
@ -452,24 +453,23 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
return max; return max;
} }
else else {
{ return clientMaxEnergy;
return this.clientMaxEnergy;
} }
} }
@Override @Override
public float getEnergyStored() public float getEnergyStored()
{ {
if (!this.worldObj.isRemote) if(!worldObj.isRemote)
{ {
float energy = 0; float energy = 0;
for (ItemStack itemStack : this.structure.inventory) for(ItemStack itemStack : structure.inventory)
{ {
if (itemStack != null) if(itemStack != null)
{ {
if (itemStack.getItem() instanceof IItemElectric) if(itemStack.getItem() instanceof IItemElectric)
{ {
energy += ((IItemElectric) itemStack.getItem()).getElectricityStored(itemStack); energy += ((IItemElectric) itemStack.getItem()).getElectricityStored(itemStack);
} }
@ -478,8 +478,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
return energy; return energy;
} }
else else {
{
return clientEnergy; return clientEnergy;
} }
} }
@ -487,21 +486,12 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
@Override @Override
public void handlePacketData(ByteArrayDataInput input) public void handlePacketData(ByteArrayDataInput input)
{ {
try structure.isMultiblock = input.readBoolean();
{
structure.isMultiblock = input.readBoolean();
clientEnergy = input.readFloat(); clientEnergy = input.readFloat();
clientCells = input.readInt(); clientCells = input.readInt();
clientMaxEnergy = input.readFloat(); clientMaxEnergy = input.readFloat();
clientVolume = input.readInt();
structure.height = input.readInt();
structure.length = input.readInt();
structure.width = input.readInt();
}
catch (Exception e)
{
}
} }
@Override @Override
@ -512,10 +502,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
data.add(getEnergyStored()); data.add(getEnergyStored());
data.add(structure.inventory.size()); data.add(structure.inventory.size());
data.add(getMaxEnergyStored()); data.add(getMaxEnergyStored());
data.add(structure.getVolume());
data.add(structure.height);
data.add(structure.length);
data.add(structure.width);
return data; return data;
} }
@ -529,23 +516,21 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
@Override @Override
public ItemStack getStackInSlot(int i) public ItemStack getStackInSlot(int i)
{ {
if (i == 0) if(i == 0)
{ {
return structure.visibleInventory[0]; return structure.visibleInventory[0];
} }
else if (i == 1) else if(i == 1)
{ {
if (!worldObj.isRemote) if(!worldObj.isRemote)
{ {
return ListUtil.getTop(structure.inventory); return ListUtil.getTop(structure.inventory);
} }
else else {
{
return structure.tempStack; return structure.tempStack;
} }
} }
else else {
{
return structure.visibleInventory[i - 1]; return structure.visibleInventory[i - 1];
} }
} }
@ -553,21 +538,20 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
@Override @Override
public ItemStack decrStackSize(int slotID, int amount) public ItemStack decrStackSize(int slotID, int amount)
{ {
if (getStackInSlot(slotID) != null) if(getStackInSlot(slotID) != null)
{ {
ItemStack tempStack; ItemStack tempStack;
if (getStackInSlot(slotID).stackSize <= amount) if(getStackInSlot(slotID).stackSize <= amount)
{ {
tempStack = getStackInSlot(slotID); tempStack = getStackInSlot(slotID);
setInventorySlotContents(slotID, null); setInventorySlotContents(slotID, null);
return tempStack; return tempStack;
} }
else else {
{
tempStack = getStackInSlot(slotID).splitStack(amount); tempStack = getStackInSlot(slotID).splitStack(amount);
if (getStackInSlot(slotID).stackSize == 0) if(getStackInSlot(slotID).stackSize == 0)
{ {
setInventorySlotContents(slotID, null); setInventorySlotContents(slotID, null);
} }
@ -575,8 +559,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
return tempStack; return tempStack;
} }
} }
else else {
{
return null; return null;
} }
} }
@ -590,33 +573,30 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
@Override @Override
public void setInventorySlotContents(int i, ItemStack itemstack) public void setInventorySlotContents(int i, ItemStack itemstack)
{ {
if (i == 0) if(i == 0)
{ {
structure.visibleInventory[0] = itemstack; structure.visibleInventory[0] = itemstack;
} }
else if (i == 1) else if(i == 1)
{ {
if (itemstack == null) if(itemstack == null)
{ {
if (!worldObj.isRemote) if(!worldObj.isRemote)
{ {
structure.inventory.remove(ListUtil.getTop(structure.inventory)); structure.inventory.remove(ListUtil.getTop(structure.inventory));
} }
else else {
{
structure.tempStack = null; structure.tempStack = null;
} }
} }
else else {
{ if(worldObj.isRemote)
if (worldObj.isRemote)
{ {
structure.tempStack = itemstack; structure.tempStack = itemstack;
} }
} }
} }
else else {
{
structure.visibleInventory[i - 1] = itemstack; structure.visibleInventory[i - 1] = itemstack;
} }
} }
@ -646,14 +626,10 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
} }
@Override @Override
public void openChest() public void openChest() {}
{
}
@Override @Override
public void closeChest() public void closeChest() {}
{
}
@Override @Override
public boolean isItemValidForSlot(int i, ItemStack itemsSack) public boolean isItemValidForSlot(int i, ItemStack itemsSack)
@ -664,19 +640,20 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
@Override @Override
public float getRequest(ForgeDirection direction) public float getRequest(ForgeDirection direction)
{ {
if (this.getInputDirections().contains(direction)) if(getInputDirections().contains(direction))
{ {
return Math.min(this.getMaxEnergyStored() - this.getEnergyStored(), this.getTransferThreshhold()); return Math.min(getMaxEnergyStored() - getEnergyStored(), getTransferThreshhold());
} }
return 0; return 0;
} }
@Override @Override
public float getProvide(ForgeDirection direction) public float getProvide(ForgeDirection direction)
{ {
if (this.getOutputDirections().contains(direction)) if(getOutputDirections().contains(direction))
{ {
return Math.min(this.getEnergyStored(), this.getTransferThreshhold()); return Math.min(getEnergyStored(), getTransferThreshhold());
} }
return 0; return 0;
@ -685,13 +662,13 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
@Override @Override
public EnumSet<ForgeDirection> getInputDirections() public EnumSet<ForgeDirection> getInputDirections()
{ {
return this.inputSides; return inputSides;
} }
@Override @Override
public EnumSet<ForgeDirection> getOutputDirections() public EnumSet<ForgeDirection> getOutputDirections()
{ {
return EnumSet.complementOf(this.inputSides); return EnumSet.complementOf(inputSides);
} }
/** /**
@ -699,14 +676,13 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
*/ */
public boolean toggleSide(ForgeDirection orientation) public boolean toggleSide(ForgeDirection orientation)
{ {
if (this.inputSides.contains(orientation)) if(inputSides.contains(orientation))
{ {
this.inputSides.remove(orientation); inputSides.remove(orientation);
return false; return false;
} }
else else {
{ inputSides.add(orientation);
this.inputSides.add(orientation);
return true; return true;
} }
} }