From 2f3394343823910e2a2f142c6ae72f23195220e8 Mon Sep 17 00:00:00 2001 From: SirSengir Date: Sun, 15 Jul 2012 23:00:35 +0200 Subject: [PATCH] Tank liquids drop down into empty tanks below. --- .../src/buildcraft/factory/TileTank.java | 190 +++++++++++------- 1 file changed, 117 insertions(+), 73 deletions(-) diff --git a/common/net/minecraft/src/buildcraft/factory/TileTank.java b/common/net/minecraft/src/buildcraft/factory/TileTank.java index 0d2b73ad..71a05ec6 100644 --- a/common/net/minecraft/src/buildcraft/factory/TileTank.java +++ b/common/net/minecraft/src/buildcraft/factory/TileTank.java @@ -33,64 +33,23 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer { public boolean hasUpdate = false; public SafeTimeTracker tracker = new SafeTimeTracker(); + /* UPDATING */ @Override - public int fill(Orientations from, int quantity, int id, boolean doFill) { - TileTank lastTank = this; - - for (int j = yCoord - 1; j > 1; --j) { - if (worldObj.getBlockTileEntity(xCoord, j, zCoord) instanceof TileTank) { - lastTank = (TileTank) worldObj.getBlockTileEntity(xCoord, j, zCoord); - } else { - break; - } + public void updateEntity() { + if (APIProxy.isServerSide() && hasUpdate && tracker.markTimeIfDelay(worldObj, 2 * BuildCraftCore.updateFactor)) { + sendNetworkUpdate(); + hasUpdate = false; } - - return lastTank.actualFill(from, quantity, id, doFill); - } - - private int actualFill(Orientations from, int quantity, int id, boolean doFill) { - if (stored != 0 && id != liquidId) { - return 0; - } - - liquidId = id; - - TileEntity above = worldObj.getBlockTileEntity(xCoord, yCoord + 1, zCoord); - - int used = 0; - - if (stored + quantity <= getTankCapacity()) { - if (doFill) { - stored += quantity; - hasUpdate = true; - } - - used = quantity; - } else if (stored <= getTankCapacity()) { - used = getTankCapacity() - stored; - - if (doFill) { - stored = getTankCapacity(); - hasUpdate = true; - } - } - - if (used < quantity && above instanceof TileTank) { - used = used + ((TileTank) above).actualFill(from, quantity - used, id, doFill); - } - - return used; - } - - @Override - public int getLiquidQuantity() { - return stored; - } - - public int getTankCapacity() { - return BuildCraftAPI.BUCKET_VOLUME * 16; + + if(APIProxy.isRemote()) + return; + + // Have liquid flow down into tanks below if any. + if(stored > 0) + moveLiquidBelow(); } + /* SAVING & LOADING */ @Override public void readFromNBT(NBTTagCompound nbttagcompound) { super.readFromNBT(nbttagcompound); @@ -111,22 +70,114 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer { nbttagcompound.setInteger("liquidId", liquidId); } - @Override - public int empty(int quantityMax, boolean doEmpty) { + /* HELPER FUNCTIONS */ + /** + * @return Last tank block below this one or this one if it is the last. + */ + public TileTank getBottomTank() { + TileTank lastTank = this; - for (int j = yCoord + 1; j <= DefaultProps.WORLD_HEIGHT; ++j) { - if (worldObj.getBlockTileEntity(xCoord, j, zCoord) instanceof TileTank) { - lastTank = (TileTank) worldObj.getBlockTileEntity(xCoord, j, zCoord); - } else { + while(true) { + TileTank below = getTankBelow(lastTank); + if(below != null) { + lastTank = below; + } else break; + } + + return lastTank; + } + + public TileTank getTankBelow(TileTank tile) { + TileEntity below = worldObj.getBlockTileEntity(tile.xCoord, tile.yCoord - 1, tile.zCoord); + if(below instanceof TileTank) + return(TileTank)below; + else + return null; + + } + + public TileTank getTankAbove(TileTank tile) { + TileEntity above = worldObj.getBlockTileEntity(tile.xCoord, tile.yCoord + 1, tile.zCoord); + if(above instanceof TileTank) + return(TileTank)above; + else + return null; + + } + + public void moveLiquidBelow() { + TileTank below = getTankBelow(this); + if(below == null) + return; + if(below.stored >= below.getTankCapacity()) + return; + if(below.liquidId > 0 + && below.liquidId != this.liquidId) + return; + + int toMove = Math.min(stored, 100); + int moved = Math.min(toMove, below.getTankCapacity() - below.stored); + stored -= moved; + below.liquidId = liquidId; + below.stored += moved; + + } + + /* ILIQUIDCONTAINER */ + @Override + public int fill(Orientations from, int quantity, int id, boolean doFill) { + return getBottomTank().actualFill(from, quantity, id, doFill); + } + + private int actualFill(Orientations from, int quantity, int id, boolean doFill) { + if (stored != 0 && id != liquidId) + return 0; + + liquidId = id; + int used = 0; + + TileTank above = getTankAbove(this); + + if (stored + quantity <= getTankCapacity()) { + if (doFill) { + stored += quantity; + hasUpdate = true; + } + + used = quantity; + } else if (stored <= getTankCapacity()) { + used = getTankCapacity() - stored; + + if (doFill) { + stored = getTankCapacity(); + hasUpdate = true; } } - return lastTank.actualEmtpy(quantityMax, doEmpty); + if (used < quantity && above != null) + used = used + above.actualFill(from, quantity - used, id, doFill); + + return used; + } + + @Override + public int getLiquidQuantity() { + return stored; + } + + public int getTankCapacity() { + return BuildCraftAPI.BUCKET_VOLUME * 16; + } + + @Override + public int empty(int quantityMax, boolean doEmpty) { + return getBottomTank().actualEmtpy(quantityMax, doEmpty); } private int actualEmtpy(int quantityMax, boolean doEmpty) { + if (stored >= quantityMax) { if (doEmpty) { stored -= quantityMax; @@ -134,6 +185,7 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer { } return quantityMax; + } else { int result = stored; @@ -142,11 +194,10 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer { hasUpdate = true; } - TileEntity under = worldObj.getBlockTileEntity(xCoord, yCoord - 1, zCoord); + TileTank below = getTankBelow(this); - if (under instanceof TileTank) { - result += ((TileTank) under).actualEmtpy(quantityMax - result, doEmpty); - } + if (below != null) + result += below.actualEmtpy(quantityMax - result, doEmpty); return result; } @@ -157,14 +208,6 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer { return liquidId; } - @Override - public void updateEntity() { - if (APIProxy.isServerSide() && hasUpdate && tracker.markTimeIfDelay(worldObj, 2 * BuildCraftCore.updateFactor)) { - sendNetworkUpdate(); - hasUpdate = false; - } - } - @Override public LiquidSlot[] getLiquidSlots() { int resultLiquidId = 0; @@ -210,4 +253,5 @@ public class TileTank extends TileBuildCraft implements ILiquidContainer { return new LiquidSlot[] { new LiquidSlot(resultLiquidId, resultLiquidQty, resultCapacity) }; } + }