From b3dac2386694fd9722f75223c8f07129d3526000 Mon Sep 17 00:00:00 2001 From: SpaceToad Date: Fri, 14 Feb 2014 08:53:53 +0100 Subject: [PATCH] fixed generic fluid pumping --- common/buildcraft/core/utils/BlockUtil.java | 38 +++---- common/buildcraft/factory/TilePump.java | 109 +++++++++++++------- 2 files changed, 86 insertions(+), 61 deletions(-) diff --git a/common/buildcraft/core/utils/BlockUtil.java b/common/buildcraft/core/utils/BlockUtil.java index 507d8475..167f4c28 100644 --- a/common/buildcraft/core/utils/BlockUtil.java +++ b/common/buildcraft/core/utils/BlockUtil.java @@ -137,14 +137,7 @@ public class BlockUtil { } public static Fluid getFluid(Block block) { - if (block instanceof IFluidBlock) { - return ((IFluidBlock) block).getFluid(); - } else if (block == Blocks.water || block == Blocks.flowing_water) { - return FluidRegistry.WATER; - } else if (block == Blocks.lava || block == Blocks.flowing_lava) { - return FluidRegistry.LAVA; - } - return null; + return FluidRegistry.lookupFluidForBlock (block); } public static FluidStack drainBlock(World world, int x, int y, int z, boolean doDrain) { @@ -152,26 +145,23 @@ public class BlockUtil { } public static FluidStack drainBlock(Block block, World world, int x, int y, int z, boolean doDrain) { - if (block instanceof IFluidBlock) { - IFluidBlock fluidBlock = (IFluidBlock) block; - if (fluidBlock.canDrain(world, x, y, z)) - return fluidBlock.drain(world, x, y, z, doDrain); - } else if (block == Blocks.water || block == Blocks.flowing_water) { + Fluid fluid = FluidRegistry.lookupFluidForBlock(block); + + if (fluid != null) { int meta = world.getBlockMetadata(x, y, z); - if (meta != 0) + + if (meta != 0) { return null; - if (doDrain) + } + + if (doDrain) { world.setBlockToAir(x, y, z); - return new FluidStack(FluidRegistry.WATER, FluidContainerRegistry.BUCKET_VOLUME); - } else if (block == Blocks.lava || block == Blocks.lava) { - int meta = world.getBlockMetadata(x, y, z); - if (meta != 0) - return null; - if (doDrain) - world.setBlockToAir(x, y, z); - return new FluidStack(FluidRegistry.LAVA, FluidContainerRegistry.BUCKET_VOLUME); + } + + return new FluidStack(fluid, FluidContainerRegistry.BUCKET_VOLUME); + } else { + return null; } - return null; } /** diff --git a/common/buildcraft/factory/TilePump.java b/common/buildcraft/factory/TilePump.java index 153795c6..08f5f61d 100644 --- a/common/buildcraft/factory/TilePump.java +++ b/common/buildcraft/factory/TilePump.java @@ -55,7 +55,7 @@ import net.minecraftforge.fluids.IFluidHandler; public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor, IFluidHandler { - public static int REBUID_DELAY = 512; + public static final int REBUID_DELAY = 512; public static int MAX_LIQUID = FluidContainerRegistry.BUCKET_VOLUME * 16; EntityBlock tube; private TreeMap> pumpLayerQueues = new TreeMap>(); @@ -64,7 +64,7 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor int aimY = 0; private PowerHandler powerHandler; private TileBuffer[] tileBuffer = null; - private SafeTimeTracker timer = new SafeTimeTracker(); + private SafeTimeTracker timer = new SafeTimeTracker(REBUID_DELAY); private int tick = Utils.RANDOM.nextInt(); private int numFluidBlocksFound = 0; private boolean powered = false; @@ -86,19 +86,23 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor if (powered) { pumpLayerQueues.clear(); destroyTube(); - } else + } else { createTube(); + } - if (worldObj.isRemote) + if (worldObj.isRemote) { return; + } pushToConsumers(); - if(powered) + if (powered) { return; + } - if(tube == null) + if (tube == null) { return; + } if (tube.posY - aimY > 0.01) { tubeY = tube.posY - 0.01; @@ -108,17 +112,17 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor } tick++; - if (tick % 16 != 0) + + if (tick % 16 != 0) { return; + } BlockIndex index = getNextIndexToPump(false); FluidStack fluidToPump = index != null ? BlockUtil.drainBlock(worldObj, index.x, index.y, index.z, false) : null; if (fluidToPump != null) { if (isFluidAllowed(fluidToPump.getFluid()) && tank.fill(fluidToPump, false) == fluidToPump.amount) { - if (powerHandler.useEnergy(10, 10, true) == 10) { - if (fluidToPump.getFluid() != FluidRegistry.WATER || BuildCraftCore.consumeWaterSources || numFluidBlocksFound < 9) { index = getNextIndexToPump(true); BlockUtil.drainBlock(worldObj, index.x, index.y, index.z, true); @@ -130,7 +134,6 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor } else { if (tick % 128 == 0) { // TODO: improve that decision - rebuildQueue(); if (getNextIndexToPump(false) == null) { @@ -149,27 +152,35 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor public void onNeighborBlockChange(Block block) { boolean p = worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord); + if (powered != p) { powered = p; - if(!worldObj.isRemote) + + if(!worldObj.isRemote) { sendNetworkUpdate(); + } } } private boolean isBlocked(int x, int y, int z) { Material mat = worldObj.getBlock(x, y, z).getMaterial(); + return mat.blocksMovement(); } private void pushToConsumers() { - if (tileBuffer == null) + if (tileBuffer == null) { tileBuffer = TileBuffer.makeBuffer(worldObj, xCoord, yCoord, zCoord, false); + } + FluidUtils.pushFluidToConsumers(tank, 400, tileBuffer); } private TileEntity getTile(ForgeDirection side) { - if (tileBuffer == null) + if (tileBuffer == null) { tileBuffer = TileBuffer.makeBuffer(worldObj, xCoord, yCoord, zCoord, false); + } + return tileBuffer[side.ordinal()].getTile(); } @@ -193,8 +204,9 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor worldObj.spawnEntityInWorld(tube); - if (!worldObj.isRemote) + if (!worldObj.isRemote) { sendNetworkUpdate(); + } } } @@ -209,33 +221,39 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor private BlockIndex getNextIndexToPump(boolean remove) { if (pumpLayerQueues.isEmpty()) { - if (timer.markTimeIfDelay(worldObj, REBUID_DELAY)) { + if (timer.markTimeIfDelay(worldObj)) { rebuildQueue(); } + return null; } Deque topLayer = pumpLayerQueues.lastEntry().getValue(); if (topLayer != null) { - if (topLayer.isEmpty()) + if (topLayer.isEmpty()) { pumpLayerQueues.pollLastEntry(); + } + if (remove) { BlockIndex index = topLayer.pollLast(); return index; + } else { + return topLayer.peekLast(); } - return topLayer.peekLast(); + } else { + return null; } - - return null; } private Deque getLayerQueue(int layer) { Deque pumpQueue = pumpLayerQueues.get(layer); + if (pumpQueue == null) { pumpQueue = new LinkedList(); pumpLayerQueues.put(layer, pumpQueue); } + return pumpQueue; } @@ -244,13 +262,16 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor pumpLayerQueues.clear(); int x = xCoord; int y = aimY; - int z = zCoord; + int z = zCoord; Fluid pumpingFluid = BlockUtil.getFluid(worldObj.getBlock(x, y, z)); - if (pumpingFluid == null) + + if (pumpingFluid == null) { return; + } - if (pumpingFluid != tank.getAcceptedFluid() && tank.getAcceptedFluid() != null) + if (pumpingFluid != tank.getAcceptedFluid() && tank.getAcceptedFluid() != null) { return; + } Set visitedBlocks = new HashSet(); Deque fluidsFound = new LinkedList(); @@ -270,8 +291,11 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor queueForPumping(index.x, index.y, index.z + 1, visitedBlocks, fluidsFound, pumpingFluid); queueForPumping(index.x, index.y, index.z - 1, visitedBlocks, fluidsFound, pumpingFluid); - if (pumpingFluid == FluidRegistry.WATER && !BuildCraftCore.consumeWaterSources && numFluidBlocksFound >= 9) + if (pumpingFluid == FluidRegistry.WATER + && !BuildCraftCore.consumeWaterSources + && numFluidBlocksFound >= 9) { return; + } // if (System.nanoTime() > timeoutTime) // return; @@ -282,13 +306,16 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor public void queueForPumping(int x, int y, int z, Set visitedBlocks, Deque fluidsFound, Fluid pumpingFluid) { BlockIndex index = new BlockIndex(x, y, z); if (visitedBlocks.add(index)) { - if ((x - xCoord) * (x - xCoord) + (z - zCoord) * (z - zCoord) > 64 * 64) + if ((x - xCoord) * (x - xCoord) + (z - zCoord) * (z - zCoord) > 64 * 64) { return; + } Block block = worldObj.getBlock(x, y, z); + if (BlockUtil.getFluid(block) == pumpingFluid) { fluidsFound.add(index); } + if (canDrainBlock(block, x, y, z, pumpingFluid)) { getLayerQueue(y).add(index); numFluidBlocksFound++; @@ -298,24 +325,30 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor private boolean isPumpableFluid(int x, int y, int z) { Fluid fluid = BlockUtil.getFluid(worldObj.getBlock(x, y, z)); - if (fluid == null) + + if (fluid == null) { return false; - if (!isFluidAllowed(fluid)) + } else if (!isFluidAllowed(fluid)) { return false; - if (tank.getAcceptedFluid() != null && tank.getAcceptedFluid() != fluid) + } else if (tank.getAcceptedFluid() != null && tank.getAcceptedFluid() != fluid) { return false; - return true; + } else { + return true; + } } private boolean canDrainBlock(Block block, int x, int y, int z, Fluid fluid) { - if (!isFluidAllowed(fluid)) + if (!isFluidAllowed(fluid)) { return false; + } FluidStack fluidStack = BlockUtil.drainBlock(block, worldObj, x, y, z, false); - if (fluidStack == null || fluidStack.amount <= 0) + + if (fluidStack == null || fluidStack.amount <= 0) { return false; - - return fluidStack.getFluid() == fluid; + } else { + return fluidStack.getFluid() == fluid; + } } private boolean isFluidAllowed(Fluid fluid) { @@ -361,9 +394,9 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor if (next != null) { return isPumpableFluid(next.x, next.y, next.z); + } else { + return false; } - - return false; } @Override @@ -463,11 +496,13 @@ public class TilePump extends TileBuildCraft implements IMachine, IPowerReceptor @Override public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) { - if (resource == null) + if (resource == null) { return null; - if (!resource.isFluidEqual(tank.getFluid())) + } else if (!resource.isFluidEqual(tank.getFluid())) { return null; - return drain(from, resource.amount, doDrain); + } else { + return drain(from, resource.amount, doDrain); + } } @Override