2013-08-16 17:56:42 +02:00
|
|
|
package mekanism.api;
|
|
|
|
|
2013-08-22 17:36:31 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
2013-08-17 19:32:05 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
2013-08-22 17:36:31 +02:00
|
|
|
import java.util.List;
|
2013-08-17 19:32:05 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2013-08-22 17:36:31 +02:00
|
|
|
import mekanism.common.MekanismUtils;
|
2013-08-17 19:32:05 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2013-08-22 17:36:31 +02:00
|
|
|
import net.minecraft.world.World;
|
2013-08-17 19:32:05 +02:00
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public abstract class DynamicNetwork<A, N> implements ITransmitterNetwork<A, N>
|
2013-08-16 17:56:42 +02:00
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
public HashSet<ITransmitter<N>> transmitters = new HashSet<ITransmitter<N>>();
|
2013-08-17 19:32:05 +02:00
|
|
|
|
|
|
|
public Set<A> possibleAcceptors = new HashSet<A>();
|
|
|
|
public Map<A, ForgeDirection> acceptorDirections = new HashMap<A, ForgeDirection>();
|
|
|
|
|
2013-08-16 17:56:42 +02:00
|
|
|
protected int ticksSinceCreate = 0;
|
|
|
|
protected int ticksSinceSecond = 0;
|
|
|
|
|
|
|
|
protected boolean fixed = false;
|
2013-08-17 19:32:05 +02:00
|
|
|
|
2013-08-22 17:36:31 +02:00
|
|
|
protected abstract ITransmitterNetwork<A, N> create(ITransmitter<N>... varTransmitters);
|
|
|
|
|
|
|
|
protected abstract ITransmitterNetwork<A, N> create(Collection<ITransmitter<N>> collection);
|
|
|
|
|
|
|
|
protected abstract ITransmitterNetwork<A, N> create(Set<N> networks);
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public void addAllTransmitters(Set<ITransmitter<N>> newTransmitters)
|
2013-08-17 19:32:05 +02:00
|
|
|
{
|
|
|
|
transmitters.addAll(newTransmitters);
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-08-19 03:32:47 +02:00
|
|
|
public void removeTransmitter(ITransmitter<N> transmitter)
|
2013-08-17 19:32:05 +02:00
|
|
|
{
|
|
|
|
transmitters.remove(transmitter);
|
|
|
|
|
|
|
|
if(transmitters.size() == 0)
|
|
|
|
{
|
|
|
|
deregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-08-17 19:32:05 +02:00
|
|
|
public void register()
|
|
|
|
{
|
|
|
|
try {
|
2013-08-19 03:32:47 +02:00
|
|
|
ITransmitter<N> aTransmitter = transmitters.iterator().next();
|
2013-08-17 19:32:05 +02:00
|
|
|
|
|
|
|
if(aTransmitter instanceof TileEntity && !((TileEntity)aTransmitter).worldObj.isRemote)
|
|
|
|
{
|
|
|
|
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
|
|
|
|
}
|
|
|
|
} catch(NoSuchElementException e) {}
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-08-17 19:32:05 +02:00
|
|
|
public void deregister()
|
|
|
|
{
|
|
|
|
transmitters.clear();
|
|
|
|
TransmitterNetworkRegistry.getInstance().removeNetwork(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSize()
|
|
|
|
{
|
|
|
|
return transmitters.size();
|
|
|
|
}
|
2013-08-18 22:51:47 +02:00
|
|
|
|
2013-08-23 20:23:08 +02:00
|
|
|
@Override
|
|
|
|
public int getAcceptorSize()
|
|
|
|
{
|
|
|
|
return possibleAcceptors.size();
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
|
|
|
public void tick()
|
|
|
|
{
|
|
|
|
if(!fixed)
|
|
|
|
{
|
|
|
|
++ticksSinceCreate;
|
|
|
|
|
|
|
|
if(ticksSinceCreate > 1200)
|
|
|
|
{
|
|
|
|
ticksSinceCreate = 0;
|
|
|
|
fixMessedUpNetwork(transmitters.iterator().next());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 17:36:31 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fixMessedUpNetwork(ITransmitter<N> transmitter)
|
|
|
|
{
|
|
|
|
if(transmitter instanceof TileEntity)
|
|
|
|
{
|
2013-08-22 20:07:28 +02:00
|
|
|
NetworkFinder finder = new NetworkFinder(((TileEntity)transmitter).getWorldObj(), getTransmissionType(), Object3D.get((TileEntity)transmitter));
|
2013-08-22 17:36:31 +02:00
|
|
|
List<Object3D> partNetwork = finder.exploreNetwork();
|
|
|
|
Set<ITransmitter<N>> newTransporters = new HashSet<ITransmitter<N>>();
|
|
|
|
|
|
|
|
for(Object3D node : partNetwork)
|
|
|
|
{
|
|
|
|
TileEntity nodeTile = node.getTileEntity(((TileEntity)transmitter).worldObj);
|
|
|
|
|
2013-08-22 20:07:28 +02:00
|
|
|
if(MekanismUtils.checkTransmissionType(nodeTile, getTransmissionType()))
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
|
|
|
((ITransmitter<N>)nodeTile).removeFromNetwork();
|
|
|
|
newTransporters.add((ITransmitter<N>)nodeTile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ITransmitterNetwork<A, N> newNetwork = create(newTransporters);
|
|
|
|
newNetwork.refresh();
|
|
|
|
newNetwork.setFixed(true);
|
|
|
|
deregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFixed(boolean value)
|
|
|
|
{
|
|
|
|
fixed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class NetworkFinder
|
|
|
|
{
|
2013-08-22 20:07:28 +02:00
|
|
|
public TransmissionType transmissionType;
|
|
|
|
|
2013-08-22 17:36:31 +02:00
|
|
|
public World worldObj;
|
|
|
|
public Object3D start;
|
|
|
|
|
|
|
|
public List<Object3D> iterated = new ArrayList<Object3D>();
|
|
|
|
public List<Object3D> toIgnore = new ArrayList<Object3D>();
|
|
|
|
|
2013-08-22 20:07:28 +02:00
|
|
|
public NetworkFinder(World world, TransmissionType type, Object3D location, Object3D... ignore)
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
|
|
|
worldObj = world;
|
|
|
|
start = location;
|
|
|
|
|
2013-08-22 20:07:28 +02:00
|
|
|
transmissionType = type;
|
2013-08-22 17:36:31 +02:00
|
|
|
|
|
|
|
if(ignore != null)
|
|
|
|
{
|
|
|
|
toIgnore = Arrays.asList(ignore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loopAll(Object3D location)
|
|
|
|
{
|
2013-08-22 20:07:28 +02:00
|
|
|
if(MekanismUtils.checkTransmissionType(location.getTileEntity(worldObj), transmissionType))
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
|
|
|
iterated.add(location);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
Object3D obj = location.getFromSide(direction);
|
|
|
|
|
|
|
|
if(!iterated.contains(obj) && !toIgnore.contains(obj))
|
|
|
|
{
|
|
|
|
TileEntity tileEntity = obj.getTileEntity(worldObj);
|
|
|
|
|
2013-08-23 05:19:01 +02:00
|
|
|
if(MekanismUtils.checkTransmissionType(tileEntity, transmissionType))
|
2013-08-22 17:36:31 +02:00
|
|
|
{
|
|
|
|
loopAll(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Object3D> exploreNetwork()
|
|
|
|
{
|
|
|
|
loopAll(start);
|
|
|
|
|
|
|
|
return iterated;
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 17:56:42 +02:00
|
|
|
}
|