Some new stuff on the liquid API for better RP2 interaction. Coming soon: blocks!

This commit is contained in:
Christian 2012-11-13 14:27:31 -05:00
parent 52ac3c1af8
commit 1072a8f0ae
4 changed files with 91 additions and 23 deletions

View File

@ -11,17 +11,27 @@ public interface ILiquidTank {
int getCapacity();
/**
*
*
* @param resource
* @param doFill
* @return Amount of liquid used for filling.
*/
int fill(LiquidStack resource, boolean doFill);
/**
*
*
* @param maxDrain
* @param doDrain
* @return Null if nothing was drained, otherwise a LiquidStack containing the drained.
*/
LiquidStack drain(int maxDrain, boolean doDrain);
/**
* Positive values indicate a positive liquid pressure (liquid wants to leave this tank)
* Negative values indicate a negative liquid pressure (liquid wants to fill this tank)
* Zero indicates no pressure
*
* @return a number indicating tank pressure
*/
public int getTankPressure();
}

View File

@ -40,8 +40,17 @@ public interface ITankContainer {
/**
* @param direction tank side: UNKNOWN for default tank set
* @return Array of {@link LiquidTank}s contained in this ITankContainer
* @return Array of {@link LiquidTank}s contained in this ITankContainer for this direction
*/
ILiquidTank[] getTanks(ForgeDirection direction);
/**
* Return the tank that this tank container desired to be used for the specified liquid type from the specified direction
*
* @param direction the direction
* @param type the liquid type, null is always an acceptable value
* @return a tank or null for no such tank
*/
ILiquidTank getTank(ForgeDirection direction, LiquidStack type);
}

View File

@ -3,6 +3,13 @@ package net.minecraftforge.liquids;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.Event;
import net.minecraftforge.oredict.OreDictionary.OreRegisterEvent;
import com.google.common.collect.ImmutableMap;
/**
* When creating liquids you should register them with this class.
*
@ -31,15 +38,16 @@ public abstract class LiquidDictionary
return existing.copy();
}
liquids.put(name, liquid.copy());
MinecraftForge.EVENT_BUS.post(new LiquidRegisterEvent(name, liquid));
return liquid;
}
/**
* Returns the liquid matching the name,
* if such a liquid exists.
*
*
* Can return null.
*
*
* @param name the name of the liquid
* @param amount the amout of liquid
* @return
@ -47,11 +55,40 @@ public abstract class LiquidDictionary
public static LiquidStack getLiquid(String name, int amount)
{
LiquidStack liquid = liquids.get(name);
if(liquid == null)
if(liquid == null)
return null;
liquid = liquid.copy();
liquid.amount = amount;
return liquid;
}
/**
* Get an immutable list of the liquids defined
*
* @return the defined liquids
*/
public Map<String, LiquidStack> getDefinedLiquids()
{
return ImmutableMap.copyOf(liquids);
}
/**
* Fired when a new liquid is created
*
* @author cpw
*
*/
public static class LiquidRegisterEvent extends Event
{
public final String Name;
public final LiquidStack Liquid;
public LiquidRegisterEvent(String name, LiquidStack liquid)
{
this.Name = name;
this.Liquid = liquid.copy();
}
}
}

View File

@ -6,11 +6,12 @@ package net.minecraftforge.liquids;
public class LiquidTank implements ILiquidTank {
private LiquidStack liquid;
private int capacity;
private int tankPressure;
public LiquidTank(int capacity) {
this(null, capacity);
}
public LiquidTank(int liquidId, int quantity, int capacity) {
this(new LiquidStack(liquidId, quantity), capacity);
}
@ -18,32 +19,32 @@ public class LiquidTank implements ILiquidTank {
this.liquid = liquid;
this.capacity = capacity;
}
@Override
public LiquidStack getLiquid() {
return this.liquid;
}
@Override
public void setLiquid(LiquidStack liquid) {
this.liquid = liquid;
}
@Override
public void setCapacity(int capacity) {
this.capacity = capacity;
}
@Override
public int getCapacity() {
return this.capacity;
}
@Override
public int fill(LiquidStack resource, boolean doFill) {
if(resource == null || resource.itemID <= 0)
return 0;
if(liquid == null || liquid.itemID <= 0) {
if(resource.amount <= capacity) {
if(doFill)
@ -57,10 +58,10 @@ public class LiquidTank implements ILiquidTank {
return capacity;
}
}
if(!liquid.isLiquidEqual(resource))
return 0;
int space = capacity - liquid.amount;
if(resource.amount <= space) {
if(doFill)
@ -80,21 +81,32 @@ public class LiquidTank implements ILiquidTank {
return null;
if(liquid.amount <= 0)
return null;
int used = maxDrain;
if(liquid.amount < used)
used = liquid.amount;
if(doDrain) {
liquid.amount -= used;
}
LiquidStack drained = new LiquidStack(liquid.itemID, used, liquid.itemMeta);
// Reset liquid if emptied
if(liquid.amount <= 0)
liquid = null;
return drained;
}
@Override
public int getTankPressure() {
return tankPressure;
}
public void setTankPressure(int pressure)
{
this.tankPressure = pressure;
}
}