2013-03-11 18:49:01 +01:00
|
|
|
package mekanism.api;
|
|
|
|
|
2013-06-26 23:49:47 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
2013-06-26 23:49:47 +02:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2013-03-11 18:49:01 +01:00
|
|
|
|
2013-04-01 01:12:10 +02:00
|
|
|
/**
|
|
|
|
* A handy class containing several utilities for efficient gas transfer.
|
|
|
|
* @author AidanBrady
|
|
|
|
*
|
|
|
|
*/
|
2013-04-18 04:40:11 +02:00
|
|
|
public final class GasTransmission
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-06-26 23:49:47 +02:00
|
|
|
/** Do not mess with this, used to only register NetworkLoader once. */
|
|
|
|
private static boolean networkLoader_loaded = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this method in your mod's init phase in order for the GasNetwork to function properly.
|
|
|
|
*/
|
|
|
|
public static void register()
|
|
|
|
{
|
|
|
|
if(!networkLoader_loaded)
|
|
|
|
{
|
|
|
|
MinecraftForge.EVENT_BUS.register(new GasNetwork.NetworkLoader());
|
|
|
|
networkLoader_loaded = true;
|
|
|
|
|
|
|
|
System.out.println("[Mekanism] Successfully loaded gas transmission NetworkLoader.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
/**
|
|
|
|
* Gets all the tubes around a tile entity.
|
|
|
|
* @param tileEntity - center tile entity
|
|
|
|
* @return array of TileEntities
|
|
|
|
*/
|
|
|
|
public static TileEntity[] getConnectedTubes(TileEntity tileEntity)
|
|
|
|
{
|
|
|
|
TileEntity[] tubes = new TileEntity[] {null, null, null, null, null, null};
|
|
|
|
|
2013-06-26 23:49:47 +02:00
|
|
|
if(!(tileEntity instanceof IPressurizedTube) || ((IPressurizedTube)tileEntity).canTransferGas())
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-06-26 23:49:47 +02:00
|
|
|
for(ForgeDirection orientation : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
TileEntity tube = Object3D.get(tileEntity).getFromSide(orientation).getTileEntity(tileEntity.worldObj);
|
|
|
|
|
2013-06-28 16:29:32 +02:00
|
|
|
if(tileEntity instanceof IPressurizedTube)
|
2013-06-26 23:49:47 +02:00
|
|
|
{
|
2013-06-28 16:29:32 +02:00
|
|
|
if(((IPressurizedTube)tileEntity).canTransferGasToTube(tube))
|
2013-06-27 21:05:56 +02:00
|
|
|
{
|
|
|
|
tubes[orientation.ordinal()] = tube;
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 16:29:32 +02:00
|
|
|
else if(tube instanceof IPressurizedTube && ((IPressurizedTube)tube).canTransferGas())
|
2013-06-27 21:05:56 +02:00
|
|
|
{
|
|
|
|
tubes[orientation.ordinal()] = tube;
|
2013-06-26 23:49:47 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 18:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return tubes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all the acceptors around a tile entity.
|
|
|
|
* @param tileEntity - center tile entity
|
|
|
|
* @return array of IGasAcceptors
|
|
|
|
*/
|
|
|
|
public static IGasAcceptor[] getConnectedAcceptors(TileEntity tileEntity)
|
|
|
|
{
|
|
|
|
IGasAcceptor[] acceptors = new IGasAcceptor[] {null, null, null, null, null, null};
|
|
|
|
|
2013-06-26 23:49:47 +02:00
|
|
|
if(!(tileEntity instanceof IPressurizedTube) || ((IPressurizedTube)tileEntity).canTransferGas())
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-06-26 23:49:47 +02:00
|
|
|
for(ForgeDirection orientation : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
TileEntity acceptor = Object3D.get(tileEntity).getFromSide(orientation).getTileEntity(tileEntity.worldObj);
|
|
|
|
|
|
|
|
if(acceptor instanceof IGasAcceptor)
|
|
|
|
{
|
|
|
|
acceptors[orientation.ordinal()] = (IGasAcceptor)acceptor;
|
|
|
|
}
|
|
|
|
}
|
2013-03-11 18:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return acceptors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all the tube connections around a tile entity.
|
|
|
|
* @param tileEntity - center tile entity
|
|
|
|
* @return array of ITubeConnections
|
|
|
|
*/
|
|
|
|
public static ITubeConnection[] getConnections(TileEntity tileEntity)
|
|
|
|
{
|
|
|
|
ITubeConnection[] connections = new ITubeConnection[] {null, null, null, null, null, null};
|
|
|
|
|
2013-06-26 23:49:47 +02:00
|
|
|
if(!(tileEntity instanceof IPressurizedTube) || ((IPressurizedTube)tileEntity).canTransferGas())
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-06-26 23:49:47 +02:00
|
|
|
for(ForgeDirection orientation : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
TileEntity connection = Object3D.get(tileEntity).getFromSide(orientation).getTileEntity(tileEntity.worldObj);
|
|
|
|
|
|
|
|
if(connection instanceof ITubeConnection)
|
|
|
|
{
|
|
|
|
connections[orientation.ordinal()] = (ITubeConnection)connection;
|
|
|
|
}
|
|
|
|
}
|
2013-03-11 18:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return connections;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emits a defined gas to the network.
|
|
|
|
* @param type - gas type to send
|
|
|
|
* @param amount - amount of gas to send
|
|
|
|
* @param sender - the sender of the gas
|
|
|
|
* @param facing - side the sender is outputting from
|
|
|
|
* @return rejected gas
|
|
|
|
*/
|
|
|
|
public static int emitGasToNetwork(EnumGas type, int amount, TileEntity sender, ForgeDirection facing)
|
|
|
|
{
|
2013-04-23 21:36:43 +02:00
|
|
|
TileEntity pointer = Object3D.get(sender).getFromSide(facing).getTileEntity(sender.worldObj);
|
2013-03-11 18:49:01 +01:00
|
|
|
|
2013-03-25 17:00:45 +01:00
|
|
|
if(pointer instanceof IPressurizedTube)
|
|
|
|
{
|
2013-06-26 23:49:47 +02:00
|
|
|
return ((IPressurizedTube)pointer).getNetwork().emit(amount, type, sender);
|
2013-03-25 17:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emits gas from all sides of a TileEntity.
|
|
|
|
* @param type - gas type to send
|
|
|
|
* @param amount - amount of gas to send
|
|
|
|
* @param pointer - sending TileEntity
|
|
|
|
* @return rejected gas
|
|
|
|
*/
|
|
|
|
public static int emitGasFromAllSides(EnumGas type, int amount, TileEntity pointer)
|
|
|
|
{
|
2013-03-11 18:49:01 +01:00
|
|
|
if(pointer != null)
|
|
|
|
{
|
2013-06-26 23:49:47 +02:00
|
|
|
Set<GasNetwork> networks = new HashSet<GasNetwork>();
|
|
|
|
int totalRemaining = 0;
|
|
|
|
|
|
|
|
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
TileEntity sideTile = Object3D.get(pointer).getFromSide(side).getTileEntity(pointer.worldObj);
|
|
|
|
|
|
|
|
if(sideTile instanceof IPressurizedTube)
|
|
|
|
{
|
|
|
|
networks.add(((IPressurizedTube)sideTile).getNetwork());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int remaining = amount%networks.size();
|
|
|
|
int splitGas = (amount-remaining)/networks.size();
|
|
|
|
|
|
|
|
for(GasNetwork network : networks)
|
|
|
|
{
|
|
|
|
totalRemaining += network.emit(splitGas+remaining, type, pointer);
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalRemaining;
|
2013-03-11 18:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
}
|