Last active
March 21, 2022 18:58
-
-
Save Lothrazar/eca2bb1779d9fe36631f168407b11925 to your computer and use it in GitHub Desktop.
Extracts fluids from the world in minecraft: cauldron, waterlogged block, water source in-world. Fills the given tank one buckets worth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PUBLIC DOMAIN | |
public static void extractFluidFromBlock(Level level, BlockPos posTarget, IFluidHandler tank) { | |
//fills always gonna be one bucket but we dont know what type yet | |
//test if its a source block, or a waterlogged block | |
BlockState targetState = level.getBlockState(posTarget); | |
FluidState fluidState = level.getFluidState(posTarget); | |
if (targetState.hasProperty(BlockStateProperties.WATERLOGGED) && targetState.getValue(BlockStateProperties.WATERLOGGED) == true) { | |
//for waterlogged it is hardcoded to water | |
int simFill = tank.fill(new FluidStack(new FluidStack(Fluids.WATER, FluidAttributes.BUCKET_VOLUME), FluidAttributes.BUCKET_VOLUME), FluidAction.SIMULATE); | |
if (simFill == FluidAttributes.BUCKET_VOLUME | |
&& level.setBlockAndUpdate(posTarget, targetState.setValue(BlockStateProperties.WATERLOGGED, false))) { | |
tank.fill(new FluidStack(Fluids.WATER, FluidAttributes.BUCKET_VOLUME), FluidAction.EXECUTE); | |
} | |
} | |
else if (targetState.getBlock() == Blocks.WATER_CAULDRON) { | |
int simFill = tank.fill(new FluidStack(new FluidStack(Fluids.WATER, FluidAttributes.BUCKET_VOLUME), FluidAttributes.BUCKET_VOLUME), FluidAction.SIMULATE); | |
if (simFill == FluidAttributes.BUCKET_VOLUME | |
&& level.setBlockAndUpdate(posTarget, Blocks.CAULDRON.defaultBlockState())) { | |
tank.fill(new FluidStack( | |
new FluidStack(Fluids.WATER, FluidAttributes.BUCKET_VOLUME), FluidAttributes.BUCKET_VOLUME), FluidAction.EXECUTE); | |
} | |
} | |
else if (targetState.getBlock() == Blocks.LAVA_CAULDRON) { | |
//copypasta of water cauldron code | |
int simFill = tank.fill(new FluidStack(new FluidStack(Fluids.LAVA, FluidAttributes.BUCKET_VOLUME), FluidAttributes.BUCKET_VOLUME), FluidAction.SIMULATE); | |
if (simFill == FluidAttributes.BUCKET_VOLUME | |
&& level.setBlockAndUpdate(posTarget, Blocks.CAULDRON.defaultBlockState())) { | |
tank.fill(new FluidStack( | |
new FluidStack(Fluids.LAVA, FluidAttributes.BUCKET_VOLUME), FluidAttributes.BUCKET_VOLUME), FluidAction.EXECUTE); | |
} | |
} | |
else if (fluidState != null && fluidState.isSource() && fluidState.getType() != null) { // from ze world | |
//not just water. any fluid source block | |
int simFill = tank.fill(new FluidStack(new FluidStack(fluidState.getType(), FluidAttributes.BUCKET_VOLUME), FluidAttributes.BUCKET_VOLUME), FluidAction.SIMULATE); | |
if (simFill == FluidAttributes.BUCKET_VOLUME | |
&& level.setBlockAndUpdate(posTarget, Blocks.AIR.defaultBlockState())) { | |
tank.fill(new FluidStack(fluidState.getType(), FluidAttributes.BUCKET_VOLUME), FluidAction.EXECUTE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment