Skip to content

Commit

Permalink
Merge pull request #481 from ferriarnus/drainchanges
Browse files Browse the repository at this point in the history
  • Loading branch information
Rover656 authored Oct 8, 2023
2 parents cf42ae7 + dc0f07f commit 431f25d
Showing 1 changed file with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,44 +101,48 @@ protected boolean isActive() {
}

public void drainFluids() {
if (currentIndex >= positions.size()) {
currentIndex--;
}
BlockPos pos = positions.get(currentIndex);

//Skip, as this is the last checked block
if (pos.equals(worldPosition.below()) && positions.size() != 1) {
currentIndex++;
return;
}
int stop = Math.min(currentIndex + range, positions.size());
while (currentIndex < stop) {
if (currentIndex >= positions.size()) {
currentIndex--;
}
BlockPos pos = positions.get(currentIndex);

//Last block, so reset
if (currentIndex + 1 == positions.size()) {
if (!fluidFound) {
pos = worldPosition.below(); //No fluids found, so consume the last block under the drain
} else {
currentIndex = 0;
fluidFound = false;
//Skip, as this is the last checked block
if (pos.equals(worldPosition.below()) && positions.size() != 1) {
currentIndex++;
continue;
}
}

//Not a valid fluid
FluidState fluidState = level.getFluidState(pos);
if (fluidState.isEmpty() || !fluidState.isSource() || !getFluidTankNN().isFluidValid(new FluidStack(fluidState.getType(),1))) {
currentIndex++;
return;
}
//Last block, so reset
if (currentIndex + 1 == positions.size()) {
if (!fluidFound) {
pos = worldPosition.below(); //No fluids found, so consume the last block under the drain
} else {
currentIndex = 0;
fluidFound = false;
}
}

//Fluid found, try to consume it
fluidFound = true;
if (getFluidTankNN().fill(new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME) {
if (consumed >= ENERGY_PER_BUCKET) {
level.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL);
getFluidTankNN().fill(new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE);
consumed -= ENERGY_PER_BUCKET;
//Not a valid fluid
FluidState fluidState = level.getFluidState(pos);
if (fluidState.isEmpty() || !fluidState.isSource() || !getFluidTankNN().isFluidValid(new FluidStack(fluidState.getType(),1))) {
currentIndex++;
} else {
consumed += getEnergyStorage().consumeEnergy(ENERGY_PER_BUCKET - consumed, false);
continue;
}

//Fluid found, try to consume it
fluidFound = true;
if (getFluidTankNN().fill(new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME) {
if (consumed >= ENERGY_PER_BUCKET) {
level.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL);
getFluidTankNN().fill(new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE);
consumed -= ENERGY_PER_BUCKET;
currentIndex++;
} else {
consumed += getEnergyStorage().consumeEnergy(ENERGY_PER_BUCKET - consumed, false);
}
return;
}
}
}
Expand Down

0 comments on commit 431f25d

Please sign in to comment.