How could I check if a player is exposed to rain?

When you want to detect this on a multiplayer server, I would create a plugin, whichs checks, if it's raining (by getting the weather of the world of the player) and if the weather is actually affecting the biome at the players location (e.g. there is no rain in the desert). If this all is given and the block below the player is also the highest block at the players location, then the player is 'affected' of the rain.


The simpliest solution:

/execute as [selector] at @s if blocks ~ ~1 ~ ~ 255 ~ x y z all run scoreboard players set @s [name] 1
/execute as [selector] at @s unless blocks ~ ~1 ~ ~ 255 ~ x y z all run scoreboard players set @s [name] 0

Where [name] stands for the scoreboard name of your choice, [selector] is the player or entity you are looking to test for and x y z is a coordinate of the world where it should be all air(only the block of that coordinate and all above). 1 means he is exposed and 0 means he is in cover.


I had to do it again, and came back here since I found a better solution since it uses recursion, is cheap and doesn't require any pre empty space. This only works with datapacks

In short, what it does is:

  • Set player's Y score to his Y pos
  • Add player's Y score by 1
  • Check if block above him is air (Using positioned). If so, Go back to step 2

Note here that using positioned will slowly move the referencial pos up, instead of using the player's initial pos.

Let us create the scoreboard:

/scoreboard objectives add y dummy

In a file, let us call it main.mcfunction, do:

## Reset tag
tag @s remove exposed

## Do the recursive function
# Save player's y pos into the score
execute store result score @s y run data get entity @s Pos[1]
execute as @s at @s run function <namespace:our_rec_function>

# Tag player if is exposed to sky
execute if score @s y_trace matches 255 run tag @s add exposed

And our <our_rec_function.mcfunction> file will have:

scoreboard players add @s y 1
execute if score @s y matches ..254 positioned ~ ~1 ~ if block ~ ~ ~ air run function <namespace:our_rec_function>

Really clever solution, and really compact. Love it!

Simply change the function <namespace:function> to the appropriate names based on your file structure.