Jump to content

[SOLVED] [1.20] detecting player click on water


Capricocious

Recommended Posts

Hi, I'm trying to detect if a block is water or not however I'm getting results for the block that is highlighted in the crosshair, not the water blocks on top/around it. Here is the method I'm testing this out in so far: is there a method or function that I can use that will treat water as a solid block instead of allowing it to be clicked through? Thanks!

public static void onKeyInput(InputEvent.Key event) {
            if (KeyBinding.DRINK.consumeClick()) {
                double x = Minecraft.getInstance().player.getViewVector(1.0F).x() + Minecraft.getInstance().player.blockPosition().getX();
                double y = Minecraft.getInstance().player.getViewVector(1.0F).y() + Minecraft.getInstance().player.blockPosition().getY();
                double z = Minecraft.getInstance().player.getViewVector(1.0F).z() + Minecraft.getInstance().player.blockPosition().getZ();

                Minecraft.getInstance().player.sendSystemMessage(Component.literal("X: " + x + ", Y: " + y + ", Z: " + z));

                BlockPos blockpos = new BlockPos((int) Math.round(x), (int) Math.round(y), (int) Math.round(z));
                FluidState isWater = Minecraft.getInstance().level.getFluidState(blockpos);

                Minecraft.getInstance().player.sendSystemMessage(Component.literal(isWater.toString()));
	}
}

Update: I've also had a look at the vanilla bucket item class, but I can't seem to figure out where I'm supposed to be looking. The BlockHitResult function only returns MISS, ENTITY or BLOCK. I'm guessing interaction result pass means to do nothing if the his result is a miss or not a block, but where in the final else statement does it determine that there is water?

Edited by Capricocious
solved the issue, reflected in post name and comment.

I'm the CEO of breaking things 

Link to comment
Share on other sites

public static void onKeyInput(InputEvent.Key event) {
            if (KeyBinding.DRINK.consumeClick()) {

                Player player = Minecraft.getInstance().player;
                Level level = Minecraft.getInstance().level;

                final Vec3 vec3 = player.getEyePosition(1.0F);
                final Vec3 vec31 = player.getViewVector(1.0F);
                final int reach = (int) Math.round(player.getBlockReach());

                for (int i = 1; i <= reach; i++)
                {
                    Vec3 vec32 = vec3.add(vec31.x * i, vec31.y * i, vec31.z * i);
                    BlockPos blockpos = new BlockPos((int) vec32.x, (int) vec32.y, (int) vec32.z);
                    FluidState blockstate = level.getBlockState(blockpos).getFluidState();
                    if (blockstate.getType() == Fluids.WATER) {
                        player.sendSystemMessage(Component.literal(blockstate.toString()));
                    }
                }
            }
 }

This solution appears to be working for now, I receive a message when I click on water. Help from the final comment on this post, and also looking and the vanilla lilypad block net.minecraft.world.level.block.waterlilyblock.

I'm the CEO of breaking things 

Link to comment
Share on other sites

  • Capricocious changed the title to [SOLVED] [1.20] detecting player click on water

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.