fixed generic fluid pumping
This commit is contained in:
parent
ebbc8d2146
commit
b3dac23866
2 changed files with 86 additions and 61 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<Integer, Deque<BlockIndex>> pumpLayerQueues = new TreeMap<Integer, Deque<BlockIndex>>();
|
||||
|
@ -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<BlockIndex> 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<BlockIndex> getLayerQueue(int layer) {
|
||||
Deque<BlockIndex> pumpQueue = pumpLayerQueues.get(layer);
|
||||
|
||||
if (pumpQueue == null) {
|
||||
pumpQueue = new LinkedList<BlockIndex>();
|
||||
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<BlockIndex> visitedBlocks = new HashSet<BlockIndex>();
|
||||
Deque<BlockIndex> fluidsFound = new LinkedList<BlockIndex>();
|
||||
|
@ -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<BlockIndex> visitedBlocks, Deque<BlockIndex> 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
|
||||
|
|
Loading…
Reference in a new issue