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;
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);

View file

@ -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;
@ -64,8 +65,7 @@ public class BatteryUpdateProtocol
xmax = x;
}
else
{
else {
xmax = 0;
while (isBattery(origX + x - 1, origY, origZ))
@ -87,8 +87,7 @@ public class BatteryUpdateProtocol
ymax = y;
}
else
{
else {
ymax = 0;
while(isBattery(origX, origY + y - 1, origZ))
@ -110,8 +109,7 @@ public class BatteryUpdateProtocol
zmax = z;
}
else
{
else {
zmax = 0;
while (isBattery(origX, origY, origZ + z - 1))
@ -133,8 +131,7 @@ public class BatteryUpdateProtocol
rightBlocks = false;
break;
}
else
{
else {
locations.add(new Vector3(tile).translate(new Vector3(x, y, z)));
}
}
@ -158,9 +155,6 @@ public class BatteryUpdateProtocol
{
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)
{
@ -203,46 +197,60 @@ 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)
{
if (tile.structure.isMultiblock)
structures.add(tile.structure);
}
for(SynchronizedBatteryData data : structures)
{
oldStructure = tile.structure;
break;
mergedInv = ListUtil.merge(mergedInv, data.inventory);
if(data.hasVisibleInventory())
{
visibleInvs.add(data.visibleInventory);
}
}
if (oldStructure != null)
{
int maxCells = iteratedNodes.size() * BatteryManager.CELLS_PER_BATTERY;
List<ItemStack> rejected = ListUtil.capRemains(oldStructure.inventory, maxCells);
List<ItemStack> rejected = ListUtil.capRemains(mergedInv, maxCells);
ejectItems(rejected, new Vector3(pointer));
ArrayList<List<ItemStack>> inventories = ListUtil.split(ListUtil.cap(oldStructure.inventory, maxCells), iteratedNodes.size());
ArrayList<List<ItemStack>> inventories = ListUtil.split(ListUtil.cap(mergedInv, 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 = SynchronizedBatteryData.getBase(tile, inventories.get(i));
if (!didVisibleInventory)
if(!visibleInvs.isEmpty())
{
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)
{
if(itemStack != null)
{
float motion = 0.7F;
double motionX = (pointer.worldObj.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
@ -254,6 +262,7 @@ public class BatteryUpdateProtocol
pointer.worldObj.spawnEntityInWorld(entityItem);
}
}
}
public void updateBatteries()
{
@ -271,6 +280,8 @@ public class BatteryUpdateProtocol
}
}
boolean foundVisibleInv = false;
for(Vector3 obj : structureFound.locations)
{
TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj);
@ -279,7 +290,14 @@ public class BatteryUpdateProtocol
if(tileEntity.structure.hasVisibleInventory())
{
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();
}
}

View file

@ -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;
}
}

View file

@ -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;
@ -127,7 +126,7 @@ 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)
{

View file

@ -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,14 +64,14 @@ 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())
{
@ -89,9 +90,9 @@ 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)
@ -107,7 +108,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{
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;
@ -124,7 +125,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{
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;
@ -137,7 +138,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
if(chargeItem == null)
{
chargeItem = this.structure.visibleInventory[1];
chargeItem = structure.visibleInventory[1];
}
if(chargeItem != null)
@ -146,7 +147,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
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);
}
@ -158,7 +159,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
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);
}
@ -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()
@ -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
@ -335,7 +336,7 @@ 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())
{
@ -429,17 +430,17 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
}
}
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)
{
@ -452,20 +453,19 @@ 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)
{
@ -478,30 +478,20 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
return energy;
}
else
{
else {
return clientEnergy;
}
}
@Override
public void handlePacketData(ByteArrayDataInput input)
{
try
{
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)
{
}
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;
}
@ -539,13 +526,11 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{
return ListUtil.getTop(structure.inventory);
}
else
{
else {
return structure.tempStack;
}
}
else
{
else {
return structure.visibleInventory[i - 1];
}
}
@ -563,8 +548,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
setInventorySlotContents(slotID, null);
return tempStack;
}
else
{
else {
tempStack = getStackInSlot(slotID).splitStack(amount);
if(getStackInSlot(slotID).stackSize == 0)
@ -575,8 +559,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
return tempStack;
}
}
else
{
else {
return null;
}
}
@ -602,21 +585,18 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{
structure.inventory.remove(ListUtil.getTop(structure.inventory));
}
else
{
else {
structure.tempStack = null;
}
}
else
{
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;
}
}