Batteries are less buggy and more efficient
This commit is contained in:
parent
3ec8a3bdb1
commit
57b1343311
5 changed files with 206 additions and 230 deletions
|
@ -1,5 +1,6 @@
|
|||
package mekanism.induction.client.gui;
|
||||
|
||||
import mekanism.induction.common.BatteryManager;
|
||||
import mekanism.induction.common.MekanismInduction;
|
||||
import mekanism.induction.common.inventory.container.ContainerBattery;
|
||||
import mekanism.induction.common.tileentity.TileEntityBattery;
|
||||
|
@ -33,7 +34,7 @@ public class GuiBattery extends GuiContainer
|
|||
|
||||
fontRenderer.drawString("Battery", 43, 6, 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(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);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package mekanism.induction.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
@ -31,7 +32,7 @@ public class BatteryUpdateProtocol
|
|||
|
||||
private void loopThrough(TileEntity tile)
|
||||
{
|
||||
if (structureFound == null)
|
||||
if(structureFound == null)
|
||||
{
|
||||
World worldObj = tile.worldObj;
|
||||
|
||||
|
@ -46,14 +47,14 @@ public class BatteryUpdateProtocol
|
|||
|
||||
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;
|
||||
}
|
||||
|
||||
if (isCorner)
|
||||
if(isCorner)
|
||||
{
|
||||
if (isBattery(origX + 1, origY, origZ))
|
||||
if(isBattery(origX + 1, origY, origZ))
|
||||
{
|
||||
xmin = 0;
|
||||
|
||||
|
@ -64,8 +65,7 @@ public class BatteryUpdateProtocol
|
|||
|
||||
xmax = x;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
xmax = 0;
|
||||
|
||||
while (isBattery(origX + x - 1, origY, origZ))
|
||||
|
@ -76,22 +76,21 @@ public class BatteryUpdateProtocol
|
|||
xmin = x;
|
||||
}
|
||||
|
||||
if (isBattery(origX, origY + 1, origZ))
|
||||
if(isBattery(origX, origY + 1, origZ))
|
||||
{
|
||||
ymin = 0;
|
||||
|
||||
while (isBattery(origX, origY + y + 1, origZ))
|
||||
while(isBattery(origX, origY + y + 1, origZ))
|
||||
{
|
||||
y++;
|
||||
}
|
||||
|
||||
ymax = y;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
ymax = 0;
|
||||
|
||||
while (isBattery(origX, origY + y - 1, origZ))
|
||||
while(isBattery(origX, origY + y - 1, origZ))
|
||||
{
|
||||
y--;
|
||||
}
|
||||
|
@ -99,19 +98,18 @@ public class BatteryUpdateProtocol
|
|||
ymin = y;
|
||||
}
|
||||
|
||||
if (isBattery(origX, origY, origZ + 1))
|
||||
if(isBattery(origX, origY, origZ + 1))
|
||||
{
|
||||
zmin = 0;
|
||||
|
||||
while (isBattery(origX, origY, origZ + z + 1))
|
||||
while(isBattery(origX, origY, origZ + z + 1))
|
||||
{
|
||||
z++;
|
||||
}
|
||||
|
||||
zmax = z;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
zmax = 0;
|
||||
|
||||
while (isBattery(origX, origY, origZ + z - 1))
|
||||
|
@ -122,52 +120,48 @@ public class BatteryUpdateProtocol
|
|||
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;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
locations.add(new Vector3(tile).translate(new Vector3(x, y, z)));
|
||||
}
|
||||
}
|
||||
|
||||
if (!rightBlocks)
|
||||
if(!rightBlocks)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rightBlocks)
|
||||
if(!rightBlocks)
|
||||
{
|
||||
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();
|
||||
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;
|
||||
}
|
||||
|
||||
if (structure.locations.contains(new Vector3(pointer)))
|
||||
if(structure.locations.contains(new Vector3(pointer)))
|
||||
{
|
||||
structureFound = structure;
|
||||
}
|
||||
|
@ -177,13 +171,13 @@ public class BatteryUpdateProtocol
|
|||
|
||||
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);
|
||||
|
||||
if (tileEntity instanceof TileEntityBattery)
|
||||
if(tileEntity instanceof TileEntityBattery)
|
||||
{
|
||||
if (!iteratedNodes.contains(tileEntity))
|
||||
if(!iteratedNodes.contains(tileEntity))
|
||||
{
|
||||
loopThrough(tileEntity);
|
||||
}
|
||||
|
@ -193,7 +187,7 @@ public class BatteryUpdateProtocol
|
|||
|
||||
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;
|
||||
}
|
||||
|
@ -203,55 +197,70 @@ public class BatteryUpdateProtocol
|
|||
|
||||
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;
|
||||
break;
|
||||
visibleInvs.add(data.visibleInventory);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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++)
|
||||
if(!visibleInvs.isEmpty())
|
||||
{
|
||||
TileEntityBattery tile = iterList.get(i);
|
||||
tile.structure = SynchronizedBatteryData.getBase(tile, inventories.get(i));
|
||||
|
||||
if (!didVisibleInventory)
|
||||
{
|
||||
tile.structure.visibleInventory = oldStructure.visibleInventory;
|
||||
didVisibleInventory = true;
|
||||
}
|
||||
tile.structure.visibleInventory = visibleInvs.get(0);
|
||||
visibleInvs.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
if(!visibleInvs.isEmpty())
|
||||
{
|
||||
for(ItemStack[] inv : visibleInvs)
|
||||
{
|
||||
ejectItems(Arrays.asList(inv), new Vector3(pointer));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ejectItems(List<ItemStack> items, Vector3 vec)
|
||||
{
|
||||
for (ItemStack itemStack : items)
|
||||
for(ItemStack itemStack : items)
|
||||
{
|
||||
float motion = 0.7F;
|
||||
double motionX = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
||||
double motionY = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
||||
double motionZ = (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);
|
||||
|
||||
pointer.worldObj.spawnEntityInWorld(entityItem);
|
||||
if(itemStack != null)
|
||||
{
|
||||
float motion = 0.7F;
|
||||
double motionX = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
||||
double motionY = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
||||
double motionZ = (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);
|
||||
|
||||
pointer.worldObj.spawnEntityInWorld(entityItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -259,27 +268,36 @@ public class BatteryUpdateProtocol
|
|||
{
|
||||
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();
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
|
@ -290,8 +308,7 @@ public class BatteryUpdateProtocol
|
|||
|
||||
structureFound.inventory = ListUtil.cap(structureFound.inventory, structureFound.getMaxCells());
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
disperseCells();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,12 +21,6 @@ public class SynchronizedBatteryData
|
|||
*/
|
||||
public ItemStack[] visibleInventory = new ItemStack[3];
|
||||
|
||||
public int length;
|
||||
|
||||
public int width;
|
||||
|
||||
public int height;
|
||||
|
||||
public ItemStack tempStack;
|
||||
|
||||
public boolean isMultiblock;
|
||||
|
@ -35,16 +29,16 @@ public class SynchronizedBatteryData
|
|||
|
||||
public boolean wroteInventory;
|
||||
|
||||
public int getVolume()
|
||||
{
|
||||
return length * width * height;
|
||||
}
|
||||
|
||||
public int getMaxCells()
|
||||
{
|
||||
return getVolume() * BatteryManager.CELLS_PER_BATTERY;
|
||||
}
|
||||
|
||||
public int getVolume()
|
||||
{
|
||||
return locations.size();
|
||||
}
|
||||
|
||||
public boolean addCell(ItemStack cell)
|
||||
{
|
||||
if (this.inventory.size() < this.getMaxCells())
|
||||
|
@ -119,9 +113,6 @@ public class SynchronizedBatteryData
|
|||
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;
|
||||
|
@ -132,9 +123,6 @@ public class SynchronizedBatteryData
|
|||
{
|
||||
int code = 1;
|
||||
code = 31 * locations.hashCode();
|
||||
code = 31 * length;
|
||||
code = 31 * width;
|
||||
code = 31 * height;
|
||||
return code;
|
||||
}
|
||||
|
||||
|
@ -153,11 +141,6 @@ public class SynchronizedBatteryData
|
|||
return false;
|
||||
}
|
||||
|
||||
if (data.length != length || data.width != width || data.height != height)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ import mekanism.induction.common.tileentity.TileEntityBattery;
|
|||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.ITileEntityProvider;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.client.renderer.texture.IconRegister;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
|
@ -125,13 +124,13 @@ public class BlockBattery extends Block implements ITileEntityProvider
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@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)
|
||||
{
|
||||
TileEntityBattery battery = (TileEntityBattery) world.getBlockTileEntity(x, y, z);
|
||||
TileEntityBattery battery = (TileEntityBattery)world.getBlockTileEntity(x, y, z);
|
||||
|
||||
battery.update();
|
||||
}
|
||||
|
|
|
@ -55,6 +55,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
public float clientEnergy;
|
||||
public int clientCells;
|
||||
public float clientMaxEnergy;
|
||||
public int clientVolume;
|
||||
|
||||
private EnumSet inputSides = EnumSet.allOf(ForgeDirection.class);
|
||||
|
||||
|
@ -63,18 +64,18 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
{
|
||||
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.visibleInventory[0] = null;
|
||||
|
@ -89,25 +90,25 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
*/
|
||||
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:
|
||||
for (Entity entity : entities)
|
||||
{
|
||||
if (entity instanceof EntityPlayer)
|
||||
if(entity instanceof EntityPlayer)
|
||||
{
|
||||
IInventory inventory = ((EntityPlayer) entity).inventory;
|
||||
for (int i = 0; i < inventory.getSizeInventory(); 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;
|
||||
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();
|
||||
|
||||
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;
|
||||
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;
|
||||
IItemElectric battery = (IItemElectric) itemStack.getItem();
|
||||
|
||||
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));
|
||||
battery.recharge(itemStack, provideElectricity(toGive, true).getWatts(), true);
|
||||
}
|
||||
|
||||
if (structure.visibleInventory[2] != null)
|
||||
if(structure.visibleInventory[2] != null)
|
||||
{
|
||||
ItemStack itemStack = structure.visibleInventory[2];
|
||||
IItemElectric battery = (IItemElectric) itemStack.getItem();
|
||||
|
||||
float energyNeeded = getMaxEnergyStored() - getEnergyStored();
|
||||
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);
|
||||
}
|
||||
|
||||
if (prevStructure != structure)
|
||||
if(prevStructure != structure)
|
||||
{
|
||||
for (EntityPlayer player : playersUsing)
|
||||
{
|
||||
|
@ -172,28 +173,28 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
updateClient();
|
||||
}
|
||||
|
||||
this.prevStructure = structure;
|
||||
prevStructure = structure;
|
||||
|
||||
this.structure.wroteInventory = false;
|
||||
this.structure.didTick = false;
|
||||
structure.wroteInventory = false;
|
||||
structure.didTick = false;
|
||||
|
||||
if (this.playersUsing.size() > 0)
|
||||
if(playersUsing.size() > 0)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
this.produce();
|
||||
produce();
|
||||
}
|
||||
}
|
||||
|
||||
public float getTransferThreshhold()
|
||||
{
|
||||
return this.structure.getVolume() * 50;
|
||||
return structure.getVolume() * 50;
|
||||
}
|
||||
|
||||
public void updateClient()
|
||||
|
@ -227,7 +228,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
super.readFromNBT(nbtTags);
|
||||
|
||||
// Main inventory
|
||||
if (nbtTags.hasKey("Items"))
|
||||
if(nbtTags.hasKey("Items"))
|
||||
{
|
||||
NBTTagList tagList = nbtTags.getTagList("Items");
|
||||
structure.inventory = new ArrayList<ItemStack>();
|
||||
|
@ -241,7 +242,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
}
|
||||
|
||||
// Visible inventory
|
||||
if (nbtTags.hasKey("VisibleItems"))
|
||||
if(nbtTags.hasKey("VisibleItems"))
|
||||
{
|
||||
NBTTagList tagList = nbtTags.getTagList("VisibleItems");
|
||||
structure.visibleInventory = new ItemStack[3];
|
||||
|
@ -251,9 +252,9 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount);
|
||||
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));
|
||||
}
|
||||
|
@ -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");
|
||||
|
||||
|
@ -273,10 +274,10 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
{
|
||||
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(tagCount);
|
||||
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
|
||||
|
@ -284,16 +285,16 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
{
|
||||
super.writeToNBT(nbt);
|
||||
|
||||
if (!structure.wroteInventory)
|
||||
if(!structure.wroteInventory)
|
||||
{
|
||||
// Inventory
|
||||
if (structure.inventory != null)
|
||||
if(structure.inventory != null)
|
||||
{
|
||||
NBTTagList tagList = new NBTTagList();
|
||||
|
||||
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();
|
||||
tagCompound.setInteger("Slot", slotCount);
|
||||
|
@ -306,18 +307,18 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
}
|
||||
|
||||
// Visible inventory
|
||||
if (structure.visibleInventory != null)
|
||||
if(structure.visibleInventory != null)
|
||||
{
|
||||
NBTTagList tagList = new NBTTagList();
|
||||
|
||||
for (int slotCount = 0; slotCount < structure.visibleInventory.length; slotCount++)
|
||||
{
|
||||
if (slotCount > 0)
|
||||
if(slotCount > 0)
|
||||
{
|
||||
slotCount++;
|
||||
}
|
||||
|
||||
if (getStackInSlot(slotCount) != null)
|
||||
if(getStackInSlot(slotCount) != null)
|
||||
{
|
||||
NBTTagCompound tagCompound = new NBTTagCompound();
|
||||
tagCompound.setByte("Slot", (byte) slotCount);
|
||||
|
@ -335,13 +336,13 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
* Save the input sides.
|
||||
*/
|
||||
NBTTagList tagList = new NBTTagList();
|
||||
Iterator<ForgeDirection> it = this.inputSides.iterator();
|
||||
Iterator<ForgeDirection> it = inputSides.iterator();
|
||||
|
||||
while (it.hasNext())
|
||||
{
|
||||
ForgeDirection dir = it.next();
|
||||
|
||||
if (dir != ForgeDirection.UNKNOWN)
|
||||
if(dir != ForgeDirection.UNKNOWN)
|
||||
{
|
||||
NBTTagCompound tagCompound = new NBTTagCompound();
|
||||
tagCompound.setByte("side", (byte) dir.ordinal());
|
||||
|
@ -355,11 +356,11 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
|
||||
public void update()
|
||||
{
|
||||
if (!worldObj.isRemote && (structure == null || !structure.didTick))
|
||||
if(!worldObj.isRemote && (structure == null || !structure.didTick))
|
||||
{
|
||||
new BatteryUpdateProtocol(this).updateBatteries();
|
||||
|
||||
if (structure != null)
|
||||
if(structure != null)
|
||||
{
|
||||
structure.didTick = true;
|
||||
}
|
||||
|
@ -374,21 +375,21 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
|
||||
for (ItemStack itemStack : structure.inventory)
|
||||
{
|
||||
if (itemStack.getItem() instanceof IItemElectric)
|
||||
if(itemStack.getItem() instanceof IItemElectric)
|
||||
{
|
||||
IItemElectric battery = (IItemElectric) itemStack.getItem();
|
||||
|
||||
float needed = amount - added;
|
||||
float itemAdd = Math.min(battery.getMaxElectricityStored(itemStack) - battery.getElectricityStored(itemStack), needed);
|
||||
|
||||
if (doAdd)
|
||||
if(doAdd)
|
||||
{
|
||||
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) + itemAdd);
|
||||
}
|
||||
|
||||
added += itemAdd;
|
||||
|
||||
if (amount == added)
|
||||
if(amount == added)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
@ -408,42 +409,42 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
float removed = 0;
|
||||
for (ItemStack itemStack : inverse)
|
||||
{
|
||||
if (itemStack.getItem() instanceof IItemElectric)
|
||||
if(itemStack.getItem() instanceof IItemElectric)
|
||||
{
|
||||
IItemElectric battery = (IItemElectric) itemStack.getItem();
|
||||
|
||||
float needed = amount - removed;
|
||||
float itemRemove = Math.min(battery.getElectricityStored(itemStack), needed);
|
||||
|
||||
if (doRemove)
|
||||
if(doRemove)
|
||||
{
|
||||
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) - itemRemove);
|
||||
}
|
||||
|
||||
removed += itemRemove;
|
||||
|
||||
if (amount == removed)
|
||||
if(amount == removed)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ElectricityPack.getFromWatts(removed, this.getVoltage());
|
||||
return ElectricityPack.getFromWatts(removed, getVoltage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMaxEnergyStored()
|
||||
{
|
||||
if (!this.worldObj.isRemote)
|
||||
if(!worldObj.isRemote)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
@ -452,24 +453,23 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
|
||||
return max;
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.clientMaxEnergy;
|
||||
else {
|
||||
return clientMaxEnergy;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getEnergyStored()
|
||||
{
|
||||
if (!this.worldObj.isRemote)
|
||||
if(!worldObj.isRemote)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
@ -478,8 +478,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
|
||||
return energy;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
return clientEnergy;
|
||||
}
|
||||
}
|
||||
|
@ -487,21 +486,12 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
@Override
|
||||
public void handlePacketData(ByteArrayDataInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
structure.isMultiblock = input.readBoolean();
|
||||
structure.isMultiblock = input.readBoolean();
|
||||
|
||||
clientEnergy = input.readFloat();
|
||||
clientCells = input.readInt();
|
||||
clientMaxEnergy = input.readFloat();
|
||||
|
||||
structure.height = input.readInt();
|
||||
structure.length = input.readInt();
|
||||
structure.width = input.readInt();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
clientEnergy = input.readFloat();
|
||||
clientCells = input.readInt();
|
||||
clientMaxEnergy = input.readFloat();
|
||||
clientVolume = input.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -512,10 +502,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
data.add(getEnergyStored());
|
||||
data.add(structure.inventory.size());
|
||||
data.add(getMaxEnergyStored());
|
||||
|
||||
data.add(structure.height);
|
||||
data.add(structure.length);
|
||||
data.add(structure.width);
|
||||
data.add(structure.getVolume());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
@ -529,23 +516,21 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
@Override
|
||||
public ItemStack getStackInSlot(int i)
|
||||
{
|
||||
if (i == 0)
|
||||
if(i == 0)
|
||||
{
|
||||
return structure.visibleInventory[0];
|
||||
}
|
||||
else if (i == 1)
|
||||
else if(i == 1)
|
||||
{
|
||||
if (!worldObj.isRemote)
|
||||
if(!worldObj.isRemote)
|
||||
{
|
||||
return ListUtil.getTop(structure.inventory);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
return structure.tempStack;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
return structure.visibleInventory[i - 1];
|
||||
}
|
||||
}
|
||||
|
@ -553,21 +538,20 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
@Override
|
||||
public ItemStack decrStackSize(int slotID, int amount)
|
||||
{
|
||||
if (getStackInSlot(slotID) != null)
|
||||
if(getStackInSlot(slotID) != null)
|
||||
{
|
||||
ItemStack tempStack;
|
||||
|
||||
if (getStackInSlot(slotID).stackSize <= amount)
|
||||
if(getStackInSlot(slotID).stackSize <= amount)
|
||||
{
|
||||
tempStack = getStackInSlot(slotID);
|
||||
setInventorySlotContents(slotID, null);
|
||||
return tempStack;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
tempStack = getStackInSlot(slotID).splitStack(amount);
|
||||
|
||||
if (getStackInSlot(slotID).stackSize == 0)
|
||||
if(getStackInSlot(slotID).stackSize == 0)
|
||||
{
|
||||
setInventorySlotContents(slotID, null);
|
||||
}
|
||||
|
@ -575,8 +559,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
return tempStack;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -590,33 +573,30 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
@Override
|
||||
public void setInventorySlotContents(int i, ItemStack itemstack)
|
||||
{
|
||||
if (i == 0)
|
||||
if(i == 0)
|
||||
{
|
||||
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));
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
structure.tempStack = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (worldObj.isRemote)
|
||||
else {
|
||||
if(worldObj.isRemote)
|
||||
{
|
||||
structure.tempStack = itemstack;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
structure.visibleInventory[i - 1] = itemstack;
|
||||
}
|
||||
}
|
||||
|
@ -646,14 +626,10 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
}
|
||||
|
||||
@Override
|
||||
public void openChest()
|
||||
{
|
||||
}
|
||||
public void openChest() {}
|
||||
|
||||
@Override
|
||||
public void closeChest()
|
||||
{
|
||||
}
|
||||
public void closeChest() {}
|
||||
|
||||
@Override
|
||||
public boolean isItemValidForSlot(int i, ItemStack itemsSack)
|
||||
|
@ -664,19 +640,20 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
|
@ -685,13 +662,13 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
|
|||
@Override
|
||||
public EnumSet<ForgeDirection> getInputDirections()
|
||||
{
|
||||
return this.inputSides;
|
||||
return inputSides;
|
||||
}
|
||||
|
||||
@Override
|
||||
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)
|
||||
{
|
||||
if (this.inputSides.contains(orientation))
|
||||
if(inputSides.contains(orientation))
|
||||
{
|
||||
this.inputSides.remove(orientation);
|
||||
inputSides.remove(orientation);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.inputSides.add(orientation);
|
||||
else {
|
||||
inputSides.add(orientation);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue