Minecraft – How to kill people who are standing in the sun

minecraft-commandsminecraft-java-edition

I'm trying to find a way to make it so when someone is outside during the day they get killed. I've searched for a long time and I've tried many different things, but they end up killing you even if you're safe.

Best Answer

It might seem like the best idea to use a daylight sensor for this, but that has lots of little problems, like being visible when placed at the player's location, potentially overwriting blocks (like grass), not being able to check its output level directly, …

Instead I would suggest simply checking for air in a column from the player's head to the build limit.

First make sure that you have a column of air at a known position, for example 0 0 (preferably in the spawn chunks):

/fill 0 0 0 0 255 0 air

Whenever you now want to check if everything above the player is air, you just need to compare with partially relative coordinates:

execute if blocks ~ ~ ~ ~ 255 ~ 0 ~ 0 all run <command>

The downside of this is that all blocks except air protect a player, including glass, buttons, signs, …