Minecraft – How to target a player after detecting an item in their inventory

minecraft-commandsminecraft-java-edition

If I detect a player having a nether star in his inventory, and it detected let's say "XPhotoCrafterX", how can I make it so it automatically effects him with strength 3?

I've tried to search on google but it didn't work.

Best Answer

You will need to assign a label to the player based on their NBT data first and then target based on their label. /testfor will not be useful as it does not label the players that were found.

1.9

No objectives needed.

/scoreboard players tag @a[tag=HasItem] remove HasItem
/scoreboard players tag @a[tag=!HasItem] add HasItem {Inventory:[{id:"minecraft:nether_star"}]}

And then you'd target them with the tag parameter.

/effect @a[tag=HasItem] minecraft:strength 1 3

1.8

Objective required.

/scoreboard objectives add HasItem dummy

Run on a clock in presented order.

/scoreboard players set @a HasItem 0
/scoreboard players set @a HasItem 1 {Inventory:[{id:"minecraft:nether_star"}]}

Players will have their "HasItem" score set to 1 if they have a nether star in their inventory. You can then target them based on their score.

/effect @a[score_HasItem_min=1] minecraft:strength 1 3