2013-08-27 00:49:32 +02:00
|
|
|
package mekanism.api.gas;
|
2013-06-26 23:49:47 +02:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
2013-08-19 03:32:47 +02:00
|
|
|
import java.util.Collection;
|
2013-06-26 23:49:47 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2013-08-27 00:49:32 +02:00
|
|
|
import mekanism.api.transmitters.DynamicNetwork;
|
|
|
|
import mekanism.api.transmitters.ITransmitter;
|
|
|
|
import mekanism.api.transmitters.TransmissionType;
|
2013-06-26 23:49:47 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.Event;
|
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public class GasNetwork extends DynamicNetwork<IGasAcceptor, GasNetwork>
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
public GasNetwork(ITransmitter<GasNetwork>... varPipes)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-08-17 19:32:05 +02:00
|
|
|
transmitters.addAll(Arrays.asList(varPipes));
|
2013-08-04 02:41:16 +02:00
|
|
|
register();
|
|
|
|
}
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
public GasNetwork(Collection<ITransmitter<GasNetwork>> collection)
|
|
|
|
{
|
|
|
|
transmitters.addAll(collection);
|
|
|
|
register();
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:41:16 +02:00
|
|
|
public GasNetwork(Set<GasNetwork> networks)
|
|
|
|
{
|
|
|
|
for(GasNetwork net : networks)
|
|
|
|
{
|
|
|
|
if(net != null)
|
|
|
|
{
|
2013-08-17 19:32:05 +02:00
|
|
|
addAllTransmitters(net.transmitters);
|
2013-08-04 02:41:16 +02:00
|
|
|
net.deregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
register();
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized int emit(int gasToSend, EnumGas transferType, TileEntity emitter)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-08-18 22:51:47 +02:00
|
|
|
List availableAcceptors = Arrays.asList(getAcceptors(transferType).toArray());
|
2013-06-26 23:49:47 +02:00
|
|
|
|
|
|
|
Collections.shuffle(availableAcceptors);
|
|
|
|
|
|
|
|
int prevSending = gasToSend;
|
|
|
|
|
|
|
|
if(!availableAcceptors.isEmpty())
|
|
|
|
{
|
|
|
|
int divider = availableAcceptors.size();
|
|
|
|
int remaining = gasToSend % divider;
|
|
|
|
int sending = (gasToSend-remaining)/divider;
|
|
|
|
|
|
|
|
for(Object obj : availableAcceptors)
|
|
|
|
{
|
|
|
|
if(obj instanceof IGasAcceptor && obj != emitter)
|
|
|
|
{
|
|
|
|
IGasAcceptor acceptor = (IGasAcceptor)obj;
|
|
|
|
|
|
|
|
int currentSending = sending;
|
|
|
|
|
|
|
|
if(remaining > 0)
|
|
|
|
{
|
|
|
|
currentSending++;
|
|
|
|
remaining--;
|
|
|
|
}
|
|
|
|
|
|
|
|
gasToSend -= (currentSending - acceptor.transferGasToAcceptor(currentSending, transferType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(prevSending > gasToSend && FMLCommonHandler.instance().getEffectiveSide().isServer())
|
|
|
|
{
|
|
|
|
MinecraftForge.EVENT_BUS.post(new GasTransferEvent(this, transferType));
|
|
|
|
}
|
|
|
|
|
|
|
|
return gasToSend;
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized Set<IGasAcceptor> getAcceptors(Object... data)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-08-18 22:51:47 +02:00
|
|
|
EnumGas transferType = (EnumGas)data[0];
|
2013-06-26 23:49:47 +02:00
|
|
|
Set<IGasAcceptor> toReturn = new HashSet<IGasAcceptor>();
|
|
|
|
|
|
|
|
for(IGasAcceptor acceptor : possibleAcceptors)
|
|
|
|
{
|
|
|
|
if(acceptor.canReceiveGas(acceptorDirections.get(acceptor).getOpposite(), transferType))
|
|
|
|
{
|
|
|
|
if(!(acceptor instanceof IGasStorage) || (acceptor instanceof IGasStorage && (((IGasStorage)acceptor).getMaxGas(transferType) - ((IGasStorage)acceptor).getGas(transferType)) > 0))
|
|
|
|
{
|
|
|
|
toReturn.add(acceptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized void refresh()
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
Set<ITransmitter<GasNetwork>> iterTubes = (Set<ITransmitter<GasNetwork>>)transmitters.clone();
|
|
|
|
Iterator<ITransmitter<GasNetwork>> it = iterTubes.iterator();
|
2013-06-26 23:49:47 +02:00
|
|
|
|
|
|
|
possibleAcceptors.clear();
|
|
|
|
acceptorDirections.clear();
|
|
|
|
|
|
|
|
while(it.hasNext())
|
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
ITransmitter<GasNetwork> conductor = (ITransmitter<GasNetwork>)it.next();
|
2013-06-26 23:49:47 +02:00
|
|
|
|
2013-08-04 02:41:16 +02:00
|
|
|
if(conductor == null || ((TileEntity)conductor).isInvalid())
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
it.remove();
|
2013-08-17 19:32:05 +02:00
|
|
|
transmitters.remove(conductor);
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-08-27 01:04:00 +02:00
|
|
|
conductor.setTransmitterNetwork(this);
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 03:32:47 +02:00
|
|
|
for(ITransmitter<GasNetwork> pipe : transmitters)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
IGasAcceptor[] acceptors = GasTransmission.getConnectedAcceptors((TileEntity)pipe);
|
|
|
|
|
|
|
|
for(IGasAcceptor acceptor : acceptors)
|
|
|
|
{
|
2013-08-19 03:32:47 +02:00
|
|
|
if(acceptor != null && !(acceptor instanceof ITransmitter))
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
possibleAcceptors.add(acceptor);
|
|
|
|
acceptorDirections.put(acceptor, ForgeDirection.getOrientation(Arrays.asList(acceptors).indexOf(acceptor)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-18 22:51:47 +02:00
|
|
|
@Override
|
2013-10-20 06:04:11 +02:00
|
|
|
public synchronized void merge(GasNetwork network)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
|
|
|
if(network != null && network != this)
|
|
|
|
{
|
2013-08-04 02:41:16 +02:00
|
|
|
Set<GasNetwork> networks = new HashSet();
|
|
|
|
networks.add(this);
|
|
|
|
networks.add(network);
|
|
|
|
GasNetwork newNetwork = new GasNetwork(networks);
|
2013-06-26 23:49:47 +02:00
|
|
|
newNetwork.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class GasTransferEvent extends Event
|
|
|
|
{
|
|
|
|
public final GasNetwork gasNetwork;
|
|
|
|
|
|
|
|
public final EnumGas transferType;
|
|
|
|
|
|
|
|
public GasTransferEvent(GasNetwork network, EnumGas type)
|
|
|
|
{
|
|
|
|
gasNetwork = network;
|
|
|
|
transferType = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
2013-08-17 19:32:05 +02:00
|
|
|
return "[GasNetwork] " + transmitters.size() + " transmitters, " + possibleAcceptors.size() + " acceptors.";
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
2013-08-22 17:36:31 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GasNetwork create(ITransmitter<GasNetwork>... varTransmitters)
|
|
|
|
{
|
|
|
|
return new GasNetwork(varTransmitters);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GasNetwork create(Collection<ITransmitter<GasNetwork>> collection)
|
|
|
|
{
|
|
|
|
return new GasNetwork(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GasNetwork create(Set<GasNetwork> networks)
|
|
|
|
{
|
|
|
|
return new GasNetwork(networks);
|
|
|
|
}
|
2013-08-22 20:07:28 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public TransmissionType getTransmissionType()
|
|
|
|
{
|
|
|
|
return TransmissionType.GAS;
|
|
|
|
}
|
2013-08-23 20:23:08 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNeeded()
|
|
|
|
{
|
|
|
|
return "Undefined for Gas networks.";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFlow()
|
|
|
|
{
|
|
|
|
return "Not defined yet for Fluid networks";
|
|
|
|
}
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|