2013-08-27 00:49:32 +02:00
|
|
|
package mekanism.api.gas;
|
2013-03-11 18:49:01 +01:00
|
|
|
|
2013-12-20 22:09:09 +01:00
|
|
|
import mekanism.api.Coord4D;
|
2013-12-21 01:12:33 +01:00
|
|
|
import mekanism.api.transmitters.IGridTransmitter;
|
2013-08-27 00:49:32 +02:00
|
|
|
import mekanism.api.transmitters.TransmissionType;
|
2013-11-30 18:37:47 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2013-03-11 18:49:01 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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-08-19 03:32:47 +02:00
|
|
|
for(ForgeDirection orientation : ForgeDirection.VALID_DIRECTIONS)
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
TileEntity tube = Coord4D.get(tileEntity).getFromSide(orientation).getTileEntity(tileEntity.worldObj);
|
2013-08-19 03:32:47 +02:00
|
|
|
|
2013-08-25 07:34:45 +02:00
|
|
|
if(TransmissionType.checkTransmissionType(tube, TransmissionType.GAS, tileEntity))
|
2013-08-19 03:32:47 +02:00
|
|
|
{
|
|
|
|
tubes[orientation.ordinal()] = tube;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2013-12-12 22:54:55 +01:00
|
|
|
public static IGasHandler[] getConnectedAcceptors(TileEntity tileEntity)
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-12-12 22:54:55 +01:00
|
|
|
IGasHandler[] acceptors = new IGasHandler[] {null, null, null, null, null, null};
|
2013-08-19 13:55:47 +02:00
|
|
|
|
|
|
|
for(ForgeDirection orientation : ForgeDirection.VALID_DIRECTIONS)
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
TileEntity acceptor = Coord4D.get(tileEntity).getFromSide(orientation).getTileEntity(tileEntity.worldObj);
|
2013-08-19 13:55:47 +02:00
|
|
|
|
2013-12-12 22:54:55 +01:00
|
|
|
if(acceptor instanceof IGasHandler)
|
2013-08-19 13:55:47 +02:00
|
|
|
{
|
2013-12-12 22:54:55 +01:00
|
|
|
acceptors[orientation.ordinal()] = (IGasHandler)acceptor;
|
2013-08-19 13:55:47 +02:00
|
|
|
}
|
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-08-19 13:55:47 +02:00
|
|
|
|
|
|
|
for(ForgeDirection orientation : ForgeDirection.VALID_DIRECTIONS)
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
TileEntity connection = Coord4D.get(tileEntity).getFromSide(orientation).getTileEntity(tileEntity.worldObj);
|
2013-08-19 13:55:47 +02:00
|
|
|
|
2013-12-19 22:32:00 +01:00
|
|
|
if(canConnect(connection, orientation))
|
2013-08-19 13:55:47 +02:00
|
|
|
{
|
|
|
|
connections[orientation.ordinal()] = (ITubeConnection)connection;
|
|
|
|
}
|
2013-03-11 18:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return connections;
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Whether or not a TileEntity can connect to a specified tile on a specified side.
|
|
|
|
* @param tileEntity - TileEntity to attempt connection to
|
|
|
|
* @param side - side to attempt connection on
|
|
|
|
* @return if this tile and side are connectable
|
|
|
|
*/
|
2013-12-19 22:32:00 +01:00
|
|
|
public static boolean canConnect(TileEntity tileEntity, ForgeDirection side)
|
|
|
|
{
|
|
|
|
if(tileEntity instanceof ITubeConnection && (!(tileEntity instanceof IGasTransmitter) || TransmissionType.checkTransmissionType(tileEntity, TransmissionType.GAS, tileEntity)))
|
|
|
|
{
|
|
|
|
if(((ITubeConnection)tileEntity).canTubeConnect(side.getOpposite()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
/**
|
|
|
|
* 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
|
2013-11-27 02:11:26 +01:00
|
|
|
* @return gas sent
|
2013-03-11 18:49:01 +01:00
|
|
|
*/
|
2013-11-27 02:11:26 +01:00
|
|
|
public static int emitGasToNetwork(GasStack stack, TileEntity sender, ForgeDirection facing)
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2013-12-20 22:09:09 +01:00
|
|
|
TileEntity pointer = Coord4D.get(sender).getFromSide(facing).getTileEntity(sender.worldObj);
|
2013-03-11 18:49:01 +01:00
|
|
|
|
2013-08-25 07:34:45 +02:00
|
|
|
if(TransmissionType.checkTransmissionType(pointer, TransmissionType.GAS, sender))
|
2013-03-25 17:00:45 +01:00
|
|
|
{
|
2013-12-21 01:12:33 +01:00
|
|
|
return ((IGridTransmitter<GasNetwork>)pointer).getTransmitterNetwork().emit(stack);
|
2013-03-25 17:00:45 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
return 0;
|
2013-03-11 18:49:01 +01:00
|
|
|
}
|
2013-11-30 18:37:47 +01:00
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Removes a specified amount of gas from an IGasItem.
|
|
|
|
* @param itemStack - ItemStack of the IGasItem
|
|
|
|
* @param type - type of gas to remove from the IGasItem, null if it doesn't matter
|
|
|
|
* @param amount - amount of gas to remove from the ItemStack
|
|
|
|
* @return the GasStack removed by the IGasItem
|
|
|
|
*/
|
2013-11-30 18:37:47 +01:00
|
|
|
public static GasStack removeGas(ItemStack itemStack, Gas type, int amount)
|
|
|
|
{
|
|
|
|
if(itemStack != null && itemStack.getItem() instanceof IGasItem)
|
|
|
|
{
|
|
|
|
IGasItem item = (IGasItem)itemStack.getItem();
|
|
|
|
|
|
|
|
if(type != null && item.getGas(itemStack) != null && item.getGas(itemStack).getGas() != type || !item.canProvideGas(itemStack, type))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.removeGas(itemStack, amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:06:22 +01:00
|
|
|
/**
|
|
|
|
* Adds a specified amount of gas to an IGasItem.
|
|
|
|
* @param itemStack - ItemStack of the IGasItem
|
|
|
|
* @param stack - stack to add to the IGasItem
|
|
|
|
* @return amount of gas accepted by the IGasItem
|
|
|
|
*/
|
2013-11-30 18:37:47 +01:00
|
|
|
public static int addGas(ItemStack itemStack, GasStack stack)
|
|
|
|
{
|
|
|
|
if(itemStack != null && itemStack.getItem() instanceof IGasItem && ((IGasItem)itemStack.getItem()).canReceiveGas(itemStack, stack.getGas()))
|
|
|
|
{
|
|
|
|
return ((IGasItem)itemStack.getItem()).addGas(itemStack, stack.copy());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-11 18:49:01 +01:00
|
|
|
}
|