Bring other network types up to speed with EnergyNetwork

This commit is contained in:
Ben Spiers 2013-08-04 01:41:16 +01:00
parent 837689a061
commit 8ee17b2a03
9 changed files with 452 additions and 152 deletions

View file

@ -21,14 +21,33 @@ import cpw.mods.fml.common.FMLCommonHandler;
public class GasNetwork implements ITransmitterNetwork
{
public Set<IPressurizedTube> tubes = new HashSet<IPressurizedTube>();
public HashSet<IPressurizedTube> tubes = new HashSet<IPressurizedTube>();
public Set<IGasAcceptor> possibleAcceptors = new HashSet<IGasAcceptor>();
public Map<IGasAcceptor, ForgeDirection> acceptorDirections = new HashMap<IGasAcceptor, ForgeDirection>();
private int ticksSinceCreate = 0;
private boolean fixed = false;
public GasNetwork(IPressurizedTube... varPipes)
{
tubes.addAll(Arrays.asList(varPipes));
register();
}
public GasNetwork(Set<GasNetwork> networks)
{
for(GasNetwork net : networks)
{
if(net != null)
{
addAllTubes(net.tubes);
net.deregister();
}
}
refresh();
register();
}
public int emit(int gasToSend, EnumGas transferType, TileEntity emitter)
@ -92,7 +111,8 @@ public class GasNetwork implements ITransmitterNetwork
public void refresh()
{
Iterator it = tubes.iterator();
Set<IPressurizedTube> iterTubes = (Set<IPressurizedTube>) tubes.clone();
Iterator<IPressurizedTube> it = iterTubes.iterator();
possibleAcceptors.clear();
acceptorDirections.clear();
@ -101,13 +121,10 @@ public class GasNetwork implements ITransmitterNetwork
{
IPressurizedTube conductor = (IPressurizedTube)it.next();
if(conductor == null)
{
it.remove();
}
else if(((TileEntity)conductor).isInvalid())
if(conductor == null || ((TileEntity)conductor).isInvalid())
{
it.remove();
tubes.remove(conductor);
}
else {
conductor.setNetwork(this);
@ -133,20 +150,27 @@ public class GasNetwork implements ITransmitterNetwork
{
if(network != null && network != this)
{
GasNetwork newNetwork = new GasNetwork();
newNetwork.tubes.addAll(tubes);
newNetwork.tubes.addAll(network.tubes);
Set<GasNetwork> networks = new HashSet();
networks.add(this);
networks.add(network);
GasNetwork newNetwork = new GasNetwork(networks);
newNetwork.refresh();
}
}
public void addAllTubes(Set<IPressurizedTube> newTubes)
{
tubes.addAll(newTubes);
}
public void split(IPressurizedTube splitPoint)
{
if(splitPoint instanceof TileEntity)
{
tubes.remove(splitPoint);
removeTube(splitPoint);
TileEntity[] connectedBlocks = new TileEntity[6];
boolean[] dealtWith = {false, false, false, false, false, false};
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
{
@ -162,68 +186,109 @@ public class GasNetwork implements ITransmitterNetwork
{
TileEntity connectedBlockA = connectedBlocks[countOne];
if(connectedBlockA instanceof IPressurizedTube)
if(connectedBlockA instanceof IPressurizedTube && !dealtWith[countOne])
{
for(int countTwo = 0; countTwo < connectedBlocks.length; countTwo++)
NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, Object3D.get(connectedBlockA), Object3D.get((TileEntity)splitPoint));
List<Object3D> partNetwork = finder.exploreNetwork();
for(int countTwo = countOne + 1; countTwo < connectedBlocks.length; countTwo++)
{
TileEntity connectedBlockB = connectedBlocks[countTwo];
if(connectedBlockA != connectedBlockB && connectedBlockB instanceof IPressurizedTube)
if(connectedBlockB instanceof IPressurizedTube && !dealtWith[countTwo])
{
NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, Object3D.get(connectedBlockB), Object3D.get((TileEntity)splitPoint));
if(finder.foundTarget(Object3D.get(connectedBlockA)))
if(partNetwork.contains(Object3D.get(connectedBlockB)))
{
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IPressurizedTube)
{
if(nodeTile != splitPoint)
{
((IPressurizedTube)nodeTile).setNetwork(this);
}
}
}
}
else {
GasNetwork newNetwork = new GasNetwork();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IPressurizedTube)
{
if(nodeTile != splitPoint)
{
newNetwork.tubes.add((IPressurizedTube)nodeTile);
}
}
}
newNetwork.refresh();
dealtWith[countTwo] = true;
}
}
}
GasNetwork newNetwork = new GasNetwork();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IPressurizedTube)
{
if(nodeTile != splitPoint)
{
newNetwork.tubes.add((IPressurizedTube)nodeTile);
}
}
}
newNetwork.refresh();
}
}
deregister();
}
}
public void fixMessedUpNetwork(IPressurizedTube tube)
{
if(tube instanceof TileEntity)
{
NetworkFinder finder = new NetworkFinder(((TileEntity)tube).getWorldObj(), Object3D.get((TileEntity)tube), null);
List<Object3D> partNetwork = finder.exploreNetwork();
Set<IPressurizedTube> newTubes = new HashSet<IPressurizedTube>();
for(Object3D node : partNetwork)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)tube).worldObj);
if(nodeTile instanceof IPressurizedTube)
{
((IPressurizedTube) nodeTile).removeFromNetwork();
newTubes.add((IPressurizedTube)nodeTile);
}
}
GasNetwork newNetwork = new GasNetwork(newTubes.toArray(new IPressurizedTube[0]));
newNetwork.refresh();
newNetwork.fixed = true;
deregister();
}
}
public void removeTube(IPressurizedTube tube)
{
tubes.remove(tube);
if(tubes.size() == 0)
{
deregister();
}
}
public void register()
{
IPressurizedTube aTube = tubes.iterator().next();
if(aTube instanceof TileEntity && !((TileEntity)aTube).worldObj.isRemote)
{
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
}
}
public void deregister()
{
tubes.clear();
TransmitterNetworkRegistry.getInstance().removeNetwork(this);
}
public static class NetworkFinder
{
public World worldObj;
public Object3D toFind;
public Object3D start;
public List<Object3D> iterated = new ArrayList<Object3D>();
public List<Object3D> toIgnore = new ArrayList<Object3D>();
public NetworkFinder(World world, Object3D target, Object3D... ignore)
public NetworkFinder(World world, Object3D location, Object3D... ignore)
{
worldObj = world;
toFind = target;
start = location;
if(ignore != null)
{
@ -231,13 +296,11 @@ public class GasNetwork implements ITransmitterNetwork
}
}
public void loopThrough(Object3D location)
public void loopAll(Object3D location)
{
iterated.add(location);
if(iterated.contains(toFind))
if(location.getTileEntity(worldObj) instanceof IPressurizedTube)
{
return;
iterated.add(location);
}
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
@ -246,25 +309,21 @@ public class GasNetwork implements ITransmitterNetwork
if(!iterated.contains(obj) && !toIgnore.contains(obj))
{
TileEntity tileEntity = location.getTileEntity(worldObj);
TileEntity sideTile = obj.getTileEntity(worldObj);
TileEntity tileEntity = obj.getTileEntity(worldObj);
if(sideTile instanceof IPressurizedTube && ((IPressurizedTube)sideTile).canTransferGas())
if(tileEntity instanceof IPressurizedTube)
{
if(((IPressurizedTube)sideTile).canTransferGasToTube(tileEntity) && ((IPressurizedTube)tileEntity).canTransferGasToTube(sideTile))
{
loopThrough(obj);
}
loopAll(obj);
}
}
}
}
public boolean foundTarget(Object3D start)
public List<Object3D> exploreNetwork()
{
loopThrough(start);
loopAll(start);
return iterated.contains(toFind);
return iterated;
}
}
@ -312,6 +371,16 @@ public class GasNetwork implements ITransmitterNetwork
public void tick()
{
//Fix weird behaviour periodically.
if(!fixed)
{
++ticksSinceCreate;
if(ticksSinceCreate > 1200)
{
ticksSinceCreate = 0;
fixMessedUpNetwork(tubes.iterator().next());
}
}
}
@Override

View file

@ -24,11 +24,20 @@ public interface IPressurizedTube
public void onTransfer(EnumGas type);
/**
* Gets the GasNetwork currently in use by this cable segment.
* Gets the GasNetwork currently in use by this tube segment.
* @return GasNetwork this cable is using
*/
public GasNetwork getNetwork();
/**
* Gets the GasNetwork currently in use by this tube segment.
* @param createIfNull - If true, the tube will try and connect to an
* adjacent network, merging several if necessary, or creating a new one
* if none is available
* @return GasNetwork this cable is using
*/
public GasNetwork getNetwork(boolean createIfNull);
/**
* Sets this cable segment's GasNetwork to a new value.
* @param network - GasNetwork to set to
@ -36,7 +45,18 @@ public interface IPressurizedTube
public void setNetwork(GasNetwork network);
/**
* Refreshes the cable's GasNetwork.
* Refreshes the tube's GasNetwork.
*/
public void refreshNetwork();
/**
* Remove a tube from its network.
*/
public void removeFromNetwork();
/**
* Call this if you're worried a tube's network is messed up and you want
* it to try and fix itself.
*/
public void fixNetwork();
}

View file

@ -40,7 +40,7 @@ public class EnergyNetwork implements ITransmitterNetwork
public EnergyNetwork(IUniversalCable... varCables)
{
cables.addAll(Arrays.asList(varCables));
EnergyNetworkRegistry.getInstance().registerNetwork(this);
register();
}
public EnergyNetwork(Set<EnergyNetwork> networks)
@ -55,7 +55,7 @@ public class EnergyNetwork implements ITransmitterNetwork
}
refresh();
EnergyNetworkRegistry.getInstance().registerNetwork(this);
register();
}
public double getEnergyNeeded(ArrayList<TileEntity> ignored)
@ -216,7 +216,7 @@ public class EnergyNetwork implements ITransmitterNetwork
{
if(network != null && network != this)
{
Set<EnergyNetwork> networks = new HashSet();
Set<EnergyNetwork> networks = new HashSet<EnergyNetwork>();
networks.add(this);
networks.add(network);
EnergyNetwork newNetwork = new EnergyNetwork(networks);
@ -270,8 +270,7 @@ public class EnergyNetwork implements ITransmitterNetwork
}
}
EnergyNetwork newNetwork = new EnergyNetwork();
Set<IUniversalCable> newNetCables= new HashSet<IUniversalCable>();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
@ -280,11 +279,12 @@ public class EnergyNetwork implements ITransmitterNetwork
{
if(nodeTile != splitPoint)
{
newNetwork.cables.add((IUniversalCable)nodeTile);
newNetCables.add((IUniversalCable)nodeTile);
}
}
}
EnergyNetwork newNetwork = new EnergyNetwork(newNetCables.toArray(new IUniversalCable[0]));
newNetwork.refresh();
}
}
@ -328,10 +328,19 @@ public class EnergyNetwork implements ITransmitterNetwork
}
}
public void register()
{
IUniversalCable aCable = cables.iterator().next();
if(aCable instanceof TileEntity && !((TileEntity)aCable).worldObj.isRemote)
{
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
}
}
public void deregister()
{
cables.clear();
EnergyNetworkRegistry.getInstance().removeNetwork(this);
TransmitterNetworkRegistry.getInstance().removeNetwork(this);
}
public static class NetworkFinder
@ -413,7 +422,6 @@ public class EnergyNetwork implements ITransmitterNetwork
return "[EnergyNetwork] " + cables.size() + " cables, " + possibleAcceptors.size() + " acceptors.";
}
public void tick()
{
clearJoulesTransmitted();
@ -425,7 +433,7 @@ public class EnergyNetwork implements ITransmitterNetwork
if(ticksSinceCreate > 1200)
{
ticksSinceCreate = 0;
fixMessedUpNetwork(cables.toArray(new IUniversalCable[0])[0]);
fixMessedUpNetwork(cables.iterator().next());
}
}
}

View file

@ -26,14 +26,34 @@ import cpw.mods.fml.common.FMLCommonHandler;
public class FluidNetwork implements ITransmitterNetwork
{
public Set<IMechanicalPipe> pipes = new HashSet<IMechanicalPipe>();
public HashSet<IMechanicalPipe> pipes = new HashSet<IMechanicalPipe>();
public Set<IFluidHandler> possibleAcceptors = new HashSet<IFluidHandler>();
public Map<IFluidHandler, ForgeDirection> acceptorDirections = new HashMap<IFluidHandler, ForgeDirection>();
private int ticksSinceCreate = 0;
private int ticksSinceSecond = 0;
private boolean fixed = false;
public FluidNetwork(IMechanicalPipe... varPipes)
{
pipes.addAll(Arrays.asList(varPipes));
register();
}
public FluidNetwork(Set<FluidNetwork> networks)
{
for(FluidNetwork net : networks)
{
if(net != null)
{
addAllPipes(net.pipes);
net.deregister();
}
}
refresh();
register();
}
public int emit(FluidStack fluidToSend, boolean doTransfer, TileEntity emitter)
@ -95,7 +115,8 @@ public class FluidNetwork implements ITransmitterNetwork
public void refresh()
{
Iterator it = pipes.iterator();
Set<IMechanicalPipe> iterPipes = (Set<IMechanicalPipe>) pipes.clone();
Iterator it = iterPipes.iterator();
possibleAcceptors.clear();
acceptorDirections.clear();
@ -104,20 +125,17 @@ public class FluidNetwork implements ITransmitterNetwork
{
IMechanicalPipe conductor = (IMechanicalPipe)it.next();
if(conductor == null)
{
it.remove();
}
else if(((TileEntity)conductor).isInvalid())
if(conductor == null || ((TileEntity)conductor).isInvalid())
{
it.remove();
pipes.remove(conductor);
}
else {
conductor.setNetwork(this);
}
}
for(IMechanicalPipe pipe : pipes)
for(IMechanicalPipe pipe : iterPipes)
{
IFluidHandler[] acceptors = PipeUtils.getConnectedAcceptors((TileEntity)pipe);
@ -136,20 +154,27 @@ public class FluidNetwork implements ITransmitterNetwork
{
if(network != null && network != this)
{
FluidNetwork newNetwork = new FluidNetwork();
newNetwork.pipes.addAll(pipes);
newNetwork.pipes.addAll(network.pipes);
Set<FluidNetwork> networks = new HashSet<FluidNetwork>();
networks.add(this);
networks.add(network);
FluidNetwork newNetwork = new FluidNetwork(networks);
newNetwork.refresh();
}
}
public void addAllPipes(Set<IMechanicalPipe> newPipes)
{
pipes.addAll(newPipes);
}
public void split(IMechanicalPipe splitPoint)
{
if(splitPoint instanceof TileEntity)
{
pipes.remove(splitPoint);
removePipe(splitPoint);
TileEntity[] connectedBlocks = new TileEntity[6];
boolean[] dealtWith = {false, false, false, false, false, false};
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
{
@ -165,56 +190,97 @@ public class FluidNetwork implements ITransmitterNetwork
{
TileEntity connectedBlockA = connectedBlocks[countOne];
if(connectedBlockA instanceof IMechanicalPipe)
if(connectedBlockA instanceof IMechanicalPipe && !dealtWith[countOne])
{
for(int countTwo = 0; countTwo < connectedBlocks.length; countTwo++)
NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, Object3D.get(connectedBlockA), Object3D.get((TileEntity)splitPoint));
List<Object3D> partNetwork = finder.exploreNetwork();
for(int countTwo = countOne + 1; countTwo < connectedBlocks.length; countTwo++)
{
TileEntity connectedBlockB = connectedBlocks[countTwo];
if(connectedBlockA != connectedBlockB && connectedBlockB instanceof IMechanicalPipe)
if(connectedBlockB instanceof IMechanicalPipe && !dealtWith[countTwo])
{
NetworkFinder finder = new NetworkFinder(((TileEntity)splitPoint).worldObj, Object3D.get(connectedBlockB), Object3D.get((TileEntity)splitPoint));
if(finder.foundTarget(Object3D.get(connectedBlockA)))
if(partNetwork.contains(Object3D.get(connectedBlockB)))
{
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IMechanicalPipe)
{
if(nodeTile != splitPoint)
{
((IMechanicalPipe)nodeTile).setNetwork(this);
}
}
}
}
else {
FluidNetwork newNetwork = new FluidNetwork();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IMechanicalPipe)
{
if(nodeTile != splitPoint)
{
newNetwork.pipes.add((IMechanicalPipe)nodeTile);
}
}
}
newNetwork.refresh();
dealtWith[countTwo] = true;
}
}
}
Set<IMechanicalPipe> newNetPipes= new HashSet<IMechanicalPipe>();
for(Object3D node : finder.iterated)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)splitPoint).worldObj);
if(nodeTile instanceof IMechanicalPipe)
{
if(nodeTile != splitPoint)
{
newNetPipes.add((IMechanicalPipe)nodeTile);
}
}
}
FluidNetwork newNetwork = new FluidNetwork(newNetPipes.toArray(new IMechanicalPipe[0]));
newNetwork.refresh();
}
}
deregister();
}
}
public void fixMessedUpNetwork(IMechanicalPipe pipe)
{
if(pipe instanceof TileEntity)
{
NetworkFinder finder = new NetworkFinder(((TileEntity)pipe).getWorldObj(), Object3D.get((TileEntity)pipe), null);
List<Object3D> partNetwork = finder.exploreNetwork();
Set<IMechanicalPipe> newPipes = new HashSet<IMechanicalPipe>();
for(Object3D node : partNetwork)
{
TileEntity nodeTile = node.getTileEntity(((TileEntity)pipe).worldObj);
if(nodeTile instanceof IMechanicalPipe)
{
((IMechanicalPipe) nodeTile).removeFromNetwork();
newPipes.add((IMechanicalPipe)nodeTile);
}
}
FluidNetwork newNetwork = new FluidNetwork(newPipes.toArray(new IMechanicalPipe[0]));
newNetwork.refresh();
newNetwork.fixed = true;
deregister();
}
}
public void removePipe(IMechanicalPipe pipe)
{
pipes.remove(pipe);
if(pipes.size() == 0)
{
deregister();
}
}
public void register()
{
IMechanicalPipe aPipe = pipes.iterator().next();
if(aPipe instanceof TileEntity && !((TileEntity)aPipe).worldObj.isRemote)
{
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
}
}
public void deregister()
{
pipes.clear();
TransmitterNetworkRegistry.getInstance().removeNetwork(this);
}
public static class NetworkFinder
{
public World worldObj;
@ -223,24 +289,22 @@ public class FluidNetwork implements ITransmitterNetwork
public List<Object3D> iterated = new ArrayList<Object3D>();
public List<Object3D> toIgnore = new ArrayList<Object3D>();
public NetworkFinder(World world, Object3D target, Object3D... ignore)
public NetworkFinder(World world, Object3D location, Object3D... ignore)
{
worldObj = world;
start = target;
start = location;
if(ignore != null)
{
toIgnore = Arrays.asList(ignore);
}
}
public void loopThrough(Object3D location)
public void loopAll(Object3D location)
{
iterated.add(location);
if(iterated.contains(start))
if(location.getTileEntity(worldObj) instanceof IMechanicalPipe)
{
return;
iterated.add(location);
}
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
@ -253,17 +317,17 @@ public class FluidNetwork implements ITransmitterNetwork
if(tileEntity instanceof IMechanicalPipe)
{
loopThrough(obj);
loopAll(obj);
}
}
}
}
public boolean foundTarget(Object3D start)
public List<Object3D> exploreNetwork()
{
loopThrough(start);
loopAll(start);
return iterated.contains(start);
return iterated;
}
}
@ -305,6 +369,16 @@ public class FluidNetwork implements ITransmitterNetwork
public void tick()
{
//Fix weird behaviour periodically.
if(!fixed)
{
++ticksSinceCreate;
if(ticksSinceCreate > 1200)
{
ticksSinceCreate = 0;
fixMessedUpNetwork(pipes.iterator().next());
}
}
}
@Override

View file

@ -16,19 +16,39 @@ public interface IMechanicalPipe
public void onTransfer(FluidStack fluidStack);
/**
* Gets the FluidNetwork currently in use by this cable segment.
* @return FluidNetwork this cable is using
* Gets the FluidNetwork currently in use by this pipe segment.
* @return FluidNetwork this pipe is using
*/
public FluidNetwork getNetwork();
/**
* Sets this cable segment's FluidNetwork to a new value.
* Gets the FluidNetwork currently in use by this pipe segment.
* @param createIfNull - If true, the pipe will try and connect to an
* adjacent network, merging several if necessary, or creating a new one
* if none is available
* @return FluidNetwork this pipe is using
*/
public FluidNetwork getNetwork(boolean createIfNull);
/**
* Sets this pipe segment's FluidNetwork to a new value.
* @param network - FluidNetwork to set to
*/
public void setNetwork(FluidNetwork network);
/**
* Refreshes the cable's FluidNetwork.
* Refreshes the pipe's FluidNetwork.
*/
public void refreshNetwork();
/**
* Remove a pipe from its network.
*/
public void removeFromNetwork();
/**
* Call this if you're worried a pipe's network is messed up and you want
* it to try and fix itself.
*/
public void fixNetwork();
}

View file

@ -6,6 +6,7 @@ import universalelectricity.core.electricity.ElectricityDisplay;
import universalelectricity.core.electricity.ElectricityDisplay.ElectricUnit;
import mekanism.api.EnumColor;
import mekanism.api.TransmitterNetworkRegistry;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
@ -46,7 +47,7 @@ public class ItemEnergyMeter extends ItemEnergized
}
if(player.isSneaking() && Mekanism.debug){
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + "---------- " + EnumColor.DARK_BLUE + "[Mekanism Debug]" + EnumColor.GREY + " ----------"));
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + " *Networks: " + EnumColor.DARK_GREY + EnergyNetworkRegistry.getInstance().toString()));
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + " *Networks: " + EnumColor.DARK_GREY + TransmitterNetworkRegistry.getInstance().toString()));
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(EnumColor.GREY + "------------- " + EnumColor.DARK_BLUE + "[=======]" + EnumColor.GREY + " -------------"));
}
}

View file

@ -2,8 +2,10 @@ package mekanism.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import mekanism.api.Object3D;
import mekanism.api.TransmitterNetworkRegistry;
import mekanism.common.PacketHandler.Transmission;
import mekanism.common.network.PacketDataRequest;
import net.minecraft.nbt.NBTTagCompound;
@ -64,6 +66,44 @@ public class TileEntityMechanicalPipe extends TileEntity implements IMechanicalP
return fluidNetwork;
}
@Override
public FluidNetwork getNetwork(boolean createIfNull)
{
if(fluidNetwork == null && createIfNull)
{
TileEntity[] adjacentCables = CableUtils.getConnectedCables(this);
HashSet<FluidNetwork> connectedNets = new HashSet<FluidNetwork>();
for(TileEntity pipe : adjacentCables)
{
if(pipe instanceof IMechanicalPipe && ((IMechanicalPipe)pipe).getNetwork(false) != null)
{
connectedNets.add(((IMechanicalPipe)pipe).getNetwork());
}
}
if(connectedNets.size() == 0 || worldObj.isRemote)
{
fluidNetwork = new FluidNetwork(this);
}
else if(connectedNets.size() == 1)
{
fluidNetwork = connectedNets.iterator().next();
fluidNetwork.pipes.add(this);
}
else {
fluidNetwork = new FluidNetwork(connectedNets);
fluidNetwork.pipes.add(this);
}
}
return fluidNetwork;
}
@Override
public void fixNetwork()
{
getNetwork().fixMessedUpNetwork(this);
}
@Override
public void invalidate()
{
@ -78,9 +118,22 @@ public class TileEntityMechanicalPipe extends TileEntity implements IMechanicalP
@Override
public void setNetwork(FluidNetwork network)
{
fluidNetwork = network;
if(network != fluidNetwork)
{
removeFromNetwork();
fluidNetwork = network;
}
}
@Override
public void removeFromNetwork()
{
if(fluidNetwork != null)
{
fluidNetwork.removePipe(this);
}
}
@Override
public void refreshNetwork()
{
@ -100,6 +153,13 @@ public class TileEntityMechanicalPipe extends TileEntity implements IMechanicalP
}
}
@Override
public void onChunkUnload()
{
invalidate();
TransmitterNetworkRegistry.getInstance().pruneEmptyNetworks();
}
@Override
public void updateEntity()
{

View file

@ -1,5 +1,7 @@
package mekanism.common;
import java.util.HashSet;
import mekanism.api.EnumGas;
import mekanism.api.GasNetwork;
import mekanism.api.IPressurizedTube;
@ -40,14 +42,47 @@ public class TileEntityPressurizedTube extends TileEntity implements IPressurize
@Override
public GasNetwork getNetwork()
{
if(gasNetwork == null)
return getNetwork(true);
}
@Override
public GasNetwork getNetwork(boolean createIfNull)
{
if(gasNetwork == null && createIfNull)
{
gasNetwork = new GasNetwork(this);
TileEntity[] adjacentPipes = PipeUtils.getConnectedPipes(this);
HashSet<GasNetwork> connectedNets = new HashSet<GasNetwork>();
for(TileEntity cable : adjacentPipes)
{
if(cable instanceof IPressurizedTube && ((IPressurizedTube)cable).getNetwork(false) != null)
{
connectedNets.add(((IPressurizedTube)cable).getNetwork());
}
}
if(connectedNets.size() == 0 || worldObj.isRemote)
{
gasNetwork = new GasNetwork(this);
}
else if(connectedNets.size() == 1)
{
gasNetwork = (GasNetwork)connectedNets.iterator().next();
gasNetwork.tubes.add(this);
}
else {
gasNetwork = new GasNetwork(connectedNets);
gasNetwork.tubes.add(this);
}
}
return gasNetwork;
}
@Override
public void fixNetwork()
{
getNetwork().fixMessedUpNetwork(this);
}
@Override
public void invalidate()
{
@ -62,7 +97,20 @@ public class TileEntityPressurizedTube extends TileEntity implements IPressurize
@Override
public void setNetwork(GasNetwork network)
{
gasNetwork = network;
if(network != gasNetwork)
{
removeFromNetwork();
gasNetwork = network;
}
}
@Override
public void removeFromNetwork()
{
if(gasNetwork != null)
{
gasNetwork.removeTube(this);
}
}
@Override

View file

@ -1,9 +1,9 @@
package mekanism.common;
import java.util.ArrayList;
import java.util.HashSet;
import mekanism.api.Object3D;
import mekanism.api.TransmitterNetworkRegistry;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
@ -60,13 +60,13 @@ public class TileEntityUniversalCable extends TileEntity implements IUniversalCa
connectedNets.add(((IUniversalCable)cable).getNetwork());
}
}
if(connectedNets.size() == 0)
if(connectedNets.size() == 0 || worldObj.isRemote)
{
energyNetwork = new EnergyNetwork(this);
}
else if(connectedNets.size() == 1)
{
energyNetwork = (EnergyNetwork)connectedNets.toArray()[0];
energyNetwork = connectedNets.iterator().next();
energyNetwork.cables.add(this);
}
else {
@ -159,6 +159,6 @@ public class TileEntityUniversalCable extends TileEntity implements IUniversalCa
public void onChunkUnload()
{
invalidate();
EnergyNetworkRegistry.getInstance().pruneEmptyNetworks();
TransmitterNetworkRegistry.getInstance().pruneEmptyNetworks();
}
}