diff --git a/common/mekanism/api/gas/GasNetwork.java b/common/mekanism/api/gas/GasNetwork.java index b8ab96d85..ea8577746 100644 --- a/common/mekanism/api/gas/GasNetwork.java +++ b/common/mekanism/api/gas/GasNetwork.java @@ -44,12 +44,14 @@ public class GasNetwork extends DynamicNetwork public GasNetwork(IGridTransmitter... varPipes) { transmitters.addAll(Arrays.asList(varPipes)); + updateCapacity(); register(); } public GasNetwork(Collection> collection) { transmitters.addAll(collection); + updateCapacity(); register(); } @@ -94,6 +96,7 @@ public class GasNetwork extends DynamicNetwork gasScale = getScale(); + updateCapacity(); refresh(); register(); } @@ -300,6 +303,7 @@ public class GasNetwork extends DynamicNetwork { Set> iterTubes = (Set>)transmitters.clone(); Iterator> it = iterTubes.iterator(); + boolean networkChanged = false; possibleAcceptors.clear(); acceptorDirections.clear(); @@ -311,6 +315,7 @@ public class GasNetwork extends DynamicNetwork if(conductor == null || ((TileEntity)conductor).isInvalid()) { it.remove(); + networkChanged = true; transmitters.remove(conductor); } else { @@ -333,6 +338,10 @@ public class GasNetwork extends DynamicNetwork } } } + + if (networkChanged) { + updateCapacity(); + } } @Override diff --git a/common/mekanism/api/transmitters/DynamicNetwork.java b/common/mekanism/api/transmitters/DynamicNetwork.java index a9b00d972..ad34edf44 100644 --- a/common/mekanism/api/transmitters/DynamicNetwork.java +++ b/common/mekanism/api/transmitters/DynamicNetwork.java @@ -30,7 +30,10 @@ public abstract class DynamicNetwork> implemen private List updateQueue = new ArrayList(); protected int ticksSinceCreate = 0; - + + protected int capacity = 0; + protected double meanCapacity = 0; + protected boolean fixed = false; protected boolean needsUpdate = false; @@ -44,6 +47,7 @@ public abstract class DynamicNetwork> implemen public void addAllTransmitters(Set> newTransmitters) { transmitters.addAll(newTransmitters); + updateCapacity(); } public boolean isFirst(IGridTransmitter transmitter) @@ -55,7 +59,8 @@ public abstract class DynamicNetwork> implemen public void removeTransmitter(IGridTransmitter transmitter) { transmitters.remove(transmitter); - + updateCapacity(); + if(transmitters.size() == 0) { deregister(); @@ -107,20 +112,33 @@ public abstract class DynamicNetwork> implemen return possibleAcceptors.size(); } - public int getCapacity() - { - return (int)getMeanCapacity() * transmitters.size(); + protected synchronized void updateCapacity() { + updateMeanCapacity(); + capacity = (int)meanCapacity * transmitters.size(); } - /** - * Override this if things can have variable capacity along the network. - * @return An 'average' value of capacity. Calculate it how you will. - */ - public double getMeanCapacity() - { - return transmitters.size() > 0 ? transmitters.iterator().next().getCapacity() : 0; + /** + * Override this if things can have variable capacity along the network. + * @return An 'average' value of capacity. Calculate it how you will. + */ + protected synchronized void updateMeanCapacity() { + if (transmitters.size() > 0) { + meanCapacity = transmitters.iterator().next().getCapacity(); + } else { + meanCapacity = 0; + } } + + public int getCapacity() + { + return capacity; + } + public double getMeanCapacity() + { + return meanCapacity; + } + @Override public void tick() { diff --git a/common/mekanism/common/EnergyNetwork.java b/common/mekanism/common/EnergyNetwork.java index 921a7a5e8..dd95915ad 100644 --- a/common/mekanism/common/EnergyNetwork.java +++ b/common/mekanism/common/EnergyNetwork.java @@ -41,12 +41,14 @@ public class EnergyNetwork extends DynamicNetwork public EnergyNetwork(IGridTransmitter... varCables) { transmitters.addAll(Arrays.asList(varCables)); + updateCapacity(); register(); } public EnergyNetwork(Collection> collection) { transmitters.addAll(collection); + updateCapacity(); register(); } @@ -80,46 +82,46 @@ public class EnergyNetwork extends DynamicNetwork register(); } - @Override - public double getMeanCapacity() + @Override + protected synchronized void updateMeanCapacity() { - //Use the harmonic mean. Because we're mean. - int numCables = transmitters.size(); - double reciprocalSum = 0; + int numCables = transmitters.size(); + double reciprocalSum = 0; + + for(IGridTransmitter cable : transmitters) + { + reciprocalSum += 1.0/(double)cable.getCapacity(); + } - for(IGridTransmitter cable : transmitters) - { - reciprocalSum += 1.0/(double)cable.getCapacity(); - } - - return (double)numCables / reciprocalSum; + meanCapacity = (double)numCables / reciprocalSum; } - - @Override - public void onNetworksCreated(List networks) - { - if(FMLCommonHandler.instance().getEffectiveSide().isServer()) - { - double[] caps = new double[networks.size()]; - double cap = 0; - - for(EnergyNetwork network : networks) - { - caps[networks.indexOf(network)] = network.getCapacity(); - cap += network.getCapacity(); - } - - electricityStored = Math.min(cap, electricityStored); - - double[] percent = ListUtils.percent(caps); - - for(EnergyNetwork network : networks) - { - network.electricityStored = round(percent[networks.indexOf(network)]*electricityStored); - } - } - } - + + @Override + public void onNetworksCreated(List networks) + { + if(FMLCommonHandler.instance().getEffectiveSide().isServer()) + { + double[] caps = new double[networks.size()]; + double cap = 0; + + for(EnergyNetwork network : networks) + { + double networkCapacity = network.getCapacity(); + caps[networks.indexOf(network)] = networkCapacity; + cap += networkCapacity; + } + + electricityStored = Math.min(cap, electricityStored); + + double[] percent = ListUtils.percent(caps); + + for(EnergyNetwork network : networks) + { + network.electricityStored = round(percent[networks.indexOf(network)]*electricityStored); + } + } + } + public synchronized double getEnergyNeeded() { if(FMLCommonHandler.instance().getEffectiveSide().isClient()) @@ -312,7 +314,8 @@ public class EnergyNetwork extends DynamicNetwork { Set> iterCables = (Set>)transmitters.clone(); Iterator> it = iterCables.iterator(); - + boolean networkChanged = false; + possibleAcceptors.clear(); acceptorDirections.clear(); @@ -324,6 +327,7 @@ public class EnergyNetwork extends DynamicNetwork { it.remove(); transmitters.remove(conductor); + networkChanged = true; } else { conductor.setTransmitterNetwork(this); @@ -346,6 +350,10 @@ public class EnergyNetwork extends DynamicNetwork } } + if (networkChanged) { + this.updateCapacity(); + } + needsUpdate = true; } @@ -448,6 +456,7 @@ public class EnergyNetwork extends DynamicNetwork network.joulesTransmitted = joulesTransmitted; network.lastPowerScale = lastPowerScale; network.electricityStored += electricityStored; + network.updateCapacity(); return network; } diff --git a/common/mekanism/common/FluidNetwork.java b/common/mekanism/common/FluidNetwork.java index 46243285f..7c178f266 100644 --- a/common/mekanism/common/FluidNetwork.java +++ b/common/mekanism/common/FluidNetwork.java @@ -42,12 +42,14 @@ public class FluidNetwork extends DynamicNetwork public FluidNetwork(IGridTransmitter... varPipes) { transmitters.addAll(Arrays.asList(varPipes)); + updateCapacity(); register(); } public FluidNetwork(Collection> collection) { transmitters.addAll(collection); + updateCapacity(); register(); } @@ -92,6 +94,8 @@ public class FluidNetwork extends DynamicNetwork fluidScale = getScale(); + updateCapacity(); + refresh(); register(); } @@ -298,7 +302,8 @@ public class FluidNetwork extends DynamicNetwork { Set> iterPipes = (Set>)transmitters.clone(); Iterator it = iterPipes.iterator(); - + boolean networkChanged = false; + possibleAcceptors.clear(); acceptorDirections.clear(); @@ -309,6 +314,7 @@ public class FluidNetwork extends DynamicNetwork if(conductor == null || ((TileEntity)conductor).isInvalid()) { it.remove(); + networkChanged = true; transmitters.remove(conductor); } else { @@ -331,6 +337,10 @@ public class FluidNetwork extends DynamicNetwork } } } + + if(networkChanged) { + updateCapacity(); + } } @Override diff --git a/common/mekanism/common/miner/ThreadMinerSearch.java b/common/mekanism/common/miner/ThreadMinerSearch.java index 23932ad01..83e61683f 100644 --- a/common/mekanism/common/miner/ThreadMinerSearch.java +++ b/common/mekanism/common/miner/ThreadMinerSearch.java @@ -11,6 +11,7 @@ import mekanism.common.tile.TileEntityDigitalMiner; import mekanism.common.util.MekanismUtils; import net.minecraft.block.Block; import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; public class ThreadMinerSearch extends Thread { @@ -66,7 +67,8 @@ public class ThreadMinerSearch extends Thread continue; } - if(tileEntity.worldObj.getBlockTileEntity(x, y, z) instanceof IBoundingBlock) + TileEntity bte; + if ((bte = tileEntity.worldObj.getBlockTileEntity(x, y, z)) != null && bte instanceof IBoundingBlock) { continue; } diff --git a/common/mekanism/common/tile/TileEntityChargepad.java b/common/mekanism/common/tile/TileEntityChargepad.java index c0dbb1b47..64a36eb6f 100644 --- a/common/mekanism/common/tile/TileEntityChargepad.java +++ b/common/mekanism/common/tile/TileEntityChargepad.java @@ -53,9 +53,8 @@ public class TileEntityChargepad extends TileEntityElectricBlock implements IAct if(!worldObj.isRemote) { isActive = false; - - List entities = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord+1, yCoord+0.2, zCoord+1)); - + List entities = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord+1, yCoord+0.2, zCoord+1)); + for(EntityLivingBase entity : entities) { if(entity instanceof EntityPlayer || entity instanceof EntityRobit)