1

I am in Java 1.14.4 and I'm trying to create an item that gives the player water_breathing if they are underwater, but I can't find a good way to detect if the player is fully underwater.

execute as @a at @s if block ~ ~1.6 ~ water

Problem with this command is that there could be some other block above the player's head. There also doesn't seem to be any NBT data the player has to simply test for this.

scoreboard objectives add inWater minecraft.custom:minecraft.walk_under_water_one_cm

Doesn't work either since the player must actually be walking for this to occur.

1 Answer 1

3

It's fairly straightfoward to do this by exploiting the fact that, when a player is underwater, their air meter will begin decreasing, which can be measured by command blocks.

First, set up the necessary scoreboard objectives (running these commands once):

/scoreboard objectives add air air
/scoreboard objectives add difference dummy

Then, every tick, run these command blocks in order (using a repeating command block and a chain command block):

/execute as @a run scoreboard players operation @s difference -= @s air
<command goes here>
/execute as @a run scoreboard players operation @s difference = @s air

Between the two /execute commands, you can select underwater players using the selector @a[scores={difference=1..}].

This isn't 100% reliable for a few reasons, but it should work:

  1. It fails to detect players who are underwater with a water breathing effect (which doesn't matter at all if what your command does is give players water breathing). You can detect players who gained the water breathing effect while underwater (but not players who had water breathing beforehand) with @a[scores={difference=0..},nbt=!{Air:300s}].
  2. It fails to detect players the exact tick they take damage from drowning.
3
  • With your idea of air decreasing with the player underwater I was actually able to simplify this with: execute as @a[nbt=!{Air:300s}] at @s unless block ~ ~1 ~ air run [...] Since air is kept in a player's NBT data, I didn't think scores were necessary. Maybe this has drawbacks I haven't thought of yet, so I was looking for your thoughts.
    – Hydra
    Aug 12, 2019 at 0:35
  • @Hydra That doesn't work properly because the air meter takes a few seconds to recharge as of 1.13, meaning that if you emerge from the water and quickly move into a non-air non-full-block (ex, open doors), it will cause a false positive.
    – pppery
    Aug 12, 2019 at 0:48
  • I didn't think of that, but I don't have any doors in my map so I think I'm fine with this simplified version.
    – Hydra
    Aug 12, 2019 at 1:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .