Search code examples
javaminecraftminecraft-forge

check if block is any liquid (minecraft forge 1.18.1)


I have a function that is being called for every block. And I want that the function returns true when it is a fluid (not only Minecraft fluids aka other mod fluids example: oil)

This is my code:

public boolean shouldDestroy(BlockPos pos) {
    Block block = this.getLevel().getBlockState(pos).getBlock();
    boolean isDestructable = block == Blocks.BEDROCK || block == Blocks.AIR;

    // liquids
    boolean isDrainable = false; // set to true if block is a liquid

    return !isDestructable && isDrainable;
}

I searched, and I didn't found anything that could help me


Solution

  • You can check if the block is instance of a fluid one like that:

    boolean isLiquid = block instanceof BlockLiquid || block instance IFluidBlock;
    

    Source

    It's also possible that you can have block.getMaterial().isLiquid(). This method change over the time and sometimes isn't present, I don't know specially for 1.18.