Minecraft – How to determine the light level of a block with commands

minecraft-commandsminecraft-java-edition

I want to build a set of command blocks that will automatically place a torch at the foot of a player (or entity) when the light level falls below a certain threshold. So basically I need to be able to determine the light level at a player using command blocks.

I've looked the some of the wiki pages on NBT data to see if the light level is stored with the block, so I could do something like /execute @p ~ ~ ~ testforblock ~ ~ ~ minecraft:air 0 {dataTagForLight:3b}, but there doesn't seem to be any such data tag.

I've considered spawning a daylight detector, but I have a feeling it won't do what I want it to do while in caves, since as far as I can tell, in the primary mode, it's only affected by sunlight. Plus there's other issue with the plan, and it just feels like a kludge.

So, is there a way to determine the light level of a given block? If so, how?

Best Answer

This method is also a fairly bad kludge and a bit impractical, but it should be able to detect block light as well as sunlight.

There are a few of processes that only occur at certain light levels, sunlight or not:

Event            || Light Level
================================
Snow/ice melt    || >=12
Mushrooms uproot || >=13
Flowers uproot   || <=7
Plants grow      || <=9

With a very high /gamerule randomTickSpeed, these processes can happen almost immediately. Obvious disadvantage here is that the gamerule will also effect the processes throughout the entire world.

Different mobs also have different spawning light levels:

Mob              || Light Level
================================
Bats             || <=3
Most hostiles    || <=7
Blaze/silverfish || <=11
Most passives    || >=9

You can also create custom mob spawners will spawn a mob every tick by changing its MinSpawnDelay and MaxSpawnDelay.

Using either of these methods (or both), what you'd need to do is something like:

  1. Clone blocks around the block/player to a specific area
  2. Set the block/spawner in the middle of the newly cloned area
  3. Wait a few ticks
  4. Test if a mob has spawned, or a process occurred

You could also have the mob spawner (minecart) or test block at the actual player's location, rather than cloning it first, which would reduce lag a lot but cause the player to see flickering mobs/blocks.