Refactored LiquidTank to be an interface, added fill and drain functions.

This commit is contained in:
SirSengir 2012-07-18 13:42:51 +02:00
parent 6ac8ec1db6
commit 4078a8367c
5 changed files with 105 additions and 11 deletions

View file

@ -0,0 +1,28 @@
package net.minecraft.src.buildcraft.api.liquids;
import net.minecraft.src.buildcraft.api.Orientations;
public interface ILiquidTank {
/**
* @return LiquidStack representing the liquid contained in the tank, null if empty.
*/
LiquidStack getLiquid();
void setLiquid(LiquidStack liquid);
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);
}

View file

@ -24,23 +24,23 @@ public interface ITankContainer {
/**
* Drains liquid out of internal tanks, distribution is left to the ITankContainer.
* @param from Orientation the liquid is drained to.
* @param maxEmpty Maximum amount of liquid to drain.
* @param maxDrain Maximum amount of liquid to drain.
* @param doDrain If false draining will only be simulated.
* @return LiquidStack representing the liquid and amount actually drained from the ITankContainer
*/
LiquidStack drain(Orientations from, int maxEmpty, boolean doDrain);
LiquidStack drain(Orientations from, int maxDrain, boolean doDrain);
/**
* Drains liquid out of the specified internal tank.
* @param from Orientation the liquid is drained to.
* @param maxEmpty Maximum amount of liquid to drain.
* @param maxDrain Maximum amount of liquid to drain.
* @param doDrain If false draining will only be simulated.
* @return LiquidStack representing the liquid and amount actually drained from the ITankContainer
*/
LiquidStack drain(int tankIndex, int maxEmpty, boolean doDrain);
LiquidStack drain(int tankIndex, int maxDrain, boolean doDrain);
/**
* @return Array of {@link LiquidTank}s contained in this ITankContainer
*/
LiquidTank[] getTanks();
ILiquidTank[] getTanks();
}

View file

@ -1,6 +1,9 @@
package net.minecraft.src.buildcraft.api.liquids;
public class LiquidTank {
/**
* Reference implementation of ILiquidTank. Use this or implement your own.
*/
public class LiquidTank implements ILiquidTank {
private LiquidStack liquid;
private int capacity;
@ -12,15 +15,76 @@ public class LiquidTank {
this.capacity = capacity;
}
@Override
public LiquidStack getLiquid() {
return this.liquid;
}
@Override
public void setLiquid(LiquidStack liquid) {
this.liquid = liquid;
}
@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)
this.liquid = resource.copy();
return resource.amount;
} else {
if(doFill) {
this.liquid = resource.copy();
this.liquid.amount = capacity;
}
return capacity;
}
}
if(!liquid.isLiquidEqual(resource))
return 0;
int space = capacity - liquid.amount;
if(resource.amount <= space) {
if(doFill)
this.liquid.amount += resource.amount;
return resource.amount;
} else {
if(doFill)
this.liquid.amount = capacity;
return space;
}
}
@Override
public LiquidStack drain(int maxDrain, boolean doDrain) {
if(liquid == null || liquid.itemID <= 0)
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;
}
}

View file

@ -26,6 +26,7 @@ import net.minecraft.src.buildcraft.api.PowerFramework;
import net.minecraft.src.buildcraft.api.PowerProvider;
import net.minecraft.src.buildcraft.api.SafeTimeTracker;
import net.minecraft.src.buildcraft.api.TileNetworkData;
import net.minecraft.src.buildcraft.api.liquids.ILiquidTank;
import net.minecraft.src.buildcraft.api.liquids.ITankContainer;
import net.minecraft.src.buildcraft.api.liquids.LiquidStack;
import net.minecraft.src.buildcraft.api.liquids.LiquidTank;
@ -520,8 +521,8 @@ public class TileRefinery extends TileMachine implements ILiquidContainer, ITank
}
@Override
public LiquidTank[] getTanks() {
return new LiquidTank[] {
public ILiquidTank[] getTanks() {
return new ILiquidTank[] {
new LiquidTank(slot1.liquidId, slot1.quantity, LIQUID_PER_SLOT),
new LiquidTank(slot2.liquidId, slot2.quantity, LIQUID_PER_SLOT),
new LiquidTank(result.liquidId, result.quantity, LIQUID_PER_SLOT),

View file

@ -20,6 +20,7 @@ import net.minecraft.src.buildcraft.api.LiquidSlot;
import net.minecraft.src.buildcraft.api.Orientations;
import net.minecraft.src.buildcraft.api.SafeTimeTracker;
import net.minecraft.src.buildcraft.api.TileNetworkData;
import net.minecraft.src.buildcraft.api.liquids.ILiquidTank;
import net.minecraft.src.buildcraft.api.liquids.ITankContainer;
import net.minecraft.src.buildcraft.api.liquids.LiquidStack;
import net.minecraft.src.buildcraft.api.liquids.LiquidTank;
@ -213,7 +214,7 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer, ITankC
@Override
public LiquidSlot[] getLiquidSlots() {
LiquidTank tank = getTanks()[0];
ILiquidTank tank = getTanks()[0];
return new LiquidSlot[] { new LiquidSlot(tank.getLiquid().itemID, tank.getLiquid().amount, tank.getCapacity()) };
}
@ -241,7 +242,7 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer, ITankC
}
@Override
public LiquidTank[] getTanks() {
public ILiquidTank[] getTanks() {
int resultLiquidId = 0;
int resultLiquidQty = 0;
int resultCapacity = 0;
@ -283,7 +284,7 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer, ITankC
resultCapacity += tank.getTankCapacity();
}
return new LiquidTank[] { new LiquidTank(resultLiquidId, resultLiquidQty, resultCapacity) };
return new ILiquidTank[] { new LiquidTank(resultLiquidId, resultLiquidQty, resultCapacity) };
}
}