1

Currently the easiest or most compact way to test for a block, say grass, around the player is by using an execute detect command for every block in a radius that you want. That means you would need 27 to test for a 1 block radius (3x3x3), one for each possible X, Y, and Z combimation (ex. detect ~ ~ ~ grass detect ~ ~ ~-1 grass detect ~ ~ ~1 grass detect ~ ~1 ~ grass etc.). This is even more of a problem if you are detecting for more than one block, meaning you could have over 70 for just 3 blocks, and we're just talking about a 1 block radius here.

Is there a solution to this? an easier way, or a more compact way?

5

You can use a filtered /clone command that will copy blocks to their same position. The result is no difference visually, but the command can return a CommandStat value depending on its success.

Prerequisites:

Objective to hold the returned CommandStat value.

/scoreboard objectives add BLOCKS dummy

Applying the AffectedBlocks trigger to players, so that when they run commands, their own "BLOCKS" score will be set equal to the number of blocks affected. You may need this on a clock if new players can join at any time.

/stats entity @a set AffectedBlocks @a[c=1] BLOCKS

In order for CommandStats to modify a target's score, they must be tracked on the scoreboard prior. This may need to be on a clock as well.

/scoreboard players add @a BLOCKS 0

Clock commands:

The following must be run in order on a clock.

  1. Cause players to run a /clone command around them, looking for grass specifically. It will be cloned to the same position that they're already in. This will return either the number of grass blocks found or 0. The target coordinates should be the same as the first specified corner.

    /execute @a ~ ~ ~ /clone ~-10 ~-1 ~-10 ~10 ~10 ~10 ~-10 ~-1 ~-10 filtered force minecraft:grass
    
  2. Players with a "BLOCKS" score of 1+ has grass around them. You can target them based on this value.

    /say @a[score_BLOCKS_min=1] has grass blocks around them.
    /say @a[score_BLOCKS_min=10] has at least 10 grass blocks around them.
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.