Mekanism-tilera-Edition/common/mekanism/api/ChemicalPair.java

117 lines
2.7 KiB
Java
Raw Normal View History

2013-12-13 02:35:37 +01:00
package mekanism.api;
import mekanism.api.gas.GasStack;
import mekanism.api.gas.GasTank;
/**
* An input of gasses for recipe use.
* @author aidancbrady
*
*/
2014-01-10 18:58:05 +01:00
public class ChemicalPair
{
/** The left gas of this chemical input */
public GasStack leftGas;
/** The right gas of this chemical input */
public GasStack rightGas;
/**
* Creates a chemical input with two defined gasses.
* @param left - left gas
* @param right - right gas
*/
2014-01-10 18:58:05 +01:00
public ChemicalPair(GasStack left, GasStack right)
{
leftGas = left;
rightGas = right;
}
/**
* If this is a valid
* @return
*/
public boolean isValid()
{
return leftGas != null && rightGas != null;
}
/**
* Whether or not the defined input contains the same gasses and at least the required amount of the defined gasses as this input.
* @param input - input to check
* @return if the input meets this input's requirements
*/
2014-01-10 18:58:05 +01:00
public boolean meetsInput(ChemicalPair input)
{
return meets(input) || meets(input.swap());
}
/**
* Swaps the right gas and left gas of this input.
* @return a swapped ChemicalInput
*/
2014-01-10 18:58:05 +01:00
public ChemicalPair swap()
{
2014-01-10 18:58:05 +01:00
return new ChemicalPair(rightGas, leftGas);
}
/**
* Draws the needed amount of gas from each tank.
* @param leftTank - left tank to draw from
* @param rightTank - right tank to draw from
*/
public void draw(GasTank leftTank, GasTank rightTank)
{
2014-01-10 18:58:05 +01:00
if(meets(new ChemicalPair(leftTank.getGas(), rightTank.getGas())))
2013-12-13 00:33:56 +01:00
{
leftTank.draw(leftGas.amount, true);
rightTank.draw(rightGas.amount, true);
2013-12-13 00:33:56 +01:00
}
2014-01-10 18:58:05 +01:00
else if(meets(new ChemicalPair(rightTank.getGas(), leftTank.getGas())))
2013-12-13 00:33:56 +01:00
{
leftTank.draw(rightGas.amount, true);
rightTank.draw(leftGas.amount, true);
2013-12-13 00:33:56 +01:00
}
}
2014-01-10 02:47:06 +01:00
/**
* Whether or not one of this ChemicalInput's GasStack entry's gas type is equal to the gas type of the given gas.
* @param stack - stack to check
* @return if the stack's gas type is contained in this ChemicalInput
*/
public boolean containsType(GasStack stack)
{
if(stack == null || stack.amount == 0)
{
return false;
}
return stack.isGasEqual(leftGas) || stack.isGasEqual(rightGas);
}
/**
* Actual implementation of meetsInput(), performs the checks.
* @param input - input to check
* @return if the input meets this input's requirements
*/
2014-01-10 18:58:05 +01:00
private boolean meets(ChemicalPair input)
{
if(input == null || !input.isValid())
{
return false;
}
if(input.leftGas.getGas() != leftGas.getGas() || input.rightGas.getGas() != rightGas.getGas())
{
return false;
}
return input.leftGas.amount >= leftGas.amount && input.rightGas.amount >= rightGas.amount;
}
2014-01-10 18:58:05 +01:00
public ChemicalPair copy()
{
2014-01-10 18:58:05 +01:00
return new ChemicalPair(leftGas.copy(), rightGas.copy());
}
}