4

I want to detect if a player is standing on a chest using an execute command then if so detect what is in that chest and give an output. E.G Player is standing on a chest there is 64 stone in the chest if so give a redstone or score board output?

I have tried

execute @p ~ ~ ~ setblock ~ ~-2 ~ command_block 0 replace {command:"testforblock ~ ~1 ~ chest -1 {Items:[{id:minecraft:stone,Slot:0b,Count:64b}]}"

and

execute @p ~ ~ ~ testforblock ~ ~-1 ~ chest -1 {Items:[{id:minecraft:stone,Slot:0b,Count:64b}]}
1
  • Hmm... I'm actually not sure how you would attack this. /execute will fire a successful output if it's able to successfully execute iirc, so I'm not sure how you would be able to do this. There might be shenannegians you could do with /execute detect, but I don't know if that sort of command can be that sophisticated or not. – Unionhawk Mar 28 '16 at 1:51
2

In order to target that player afterwards, you must use CommandStats to track the success of a command.

Prerequisites:

Objective to hold the return value.

/scoreboard objectives add AboveChest dummy

Applying the "AffectedBlocks" trigger to all players, who will then set their own "AboveChest" score depending on the success of block-related commands. This may need to run on a clock if new players can join at any time.

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

In order for CommandStats to modify a target's score, that target must be tracked prior. This may also need to run on a clock.

/scoreboard players add @a AboveChest 0

Clock commands:

The following must be run on a clock in numerical order.

  1. Cause players to run a /testforblock to find the chest. If the command is successful, that player will have their "AboveChest" score set to 1. If unsuccessful, that player will have their score set to 0.

    /execute @a ~ ~ ~ testforblock ~ ~-1 ~ minecraft:chest -1 {Items:[{id:"minecraft:stone",Count:64b,Slot:0b}]}
    
  2. Target players based on their "AboveChest" score.

    /say @a[score_AboveChest_min=1] is above a chest containing a stack of 64 stone.
    
4
  • Still just outputs failed to execute testforblock ~ ~-1 ~ minecraft:chest -1 {Items:[{id:"minecraft:stone",Count:64b,Slot:0b}]} – Fluffkin 1 Mar 28 '16 at 6:56
  • @Fluffkin1 That would be because you don't have a chest with the first slot containing 64 stone blocks 1 block below the player. You have to ensure the chest exists first. – Skylinerw Mar 28 '16 at 13:14
  • "@Fluffkin1 That would be because you don't have a chest with the first slot containing 64 stone blocks 1 block below the player. You have to ensure the chest exists first" it does exist – Fluffkin 1 Mar 31 '16 at 5:42
  • Chests are not a full block's height. When a player stands on top of a chest, their feet are in the same block as the chest. Therefore, do not lower the Y by 1, this will target the block below the chest. Use the same block coordinates of the player. – ExpertCoder14 Sep 11 '20 at 2:10
0

1.13+ Update to Skylinerw's answer

1.13 has completely changed commands. Are you playing 1.13 and have the same question? This is the answer for you.

Prerequisites

  1. Objective:

    /scoreboard objectives add AboveChest dummy
    
  2. Set the player's initial score.

    /scoreboard players set @a AboveChest 0
    

/stats is deprecated, use /execute store instead.

Clock commands

  1. Find chest and store success/failure in scoreboard variable.

    /execute as @a at @s if block ~ ~ ~ chest{Items:[{id:"minecraft:stone",Count:64b,Slot:0b}]}
    
  2. Target players who have a score of 1. Use whatever command you want and this target selector to get them

    @e[scores={AboveChest=1}]
    

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.