5

I'm trying to make a command block that spawns an item in front of you if you are holding an item with a specific name. I've got these two working scripts;

/testfor @p[r=10] {Inventory:[{tag:{display:{Name:"Item Name"}}}]}

/testfor @p[r=10] {SelectedItemSlot:0,Inventory:[{Slot:0b,­id:"minecraft:diamond_sword"}]­}

but I can't get them together. The block should only look for an item in the active slot.

4 Answers 4

5

You can actually combine this into one command. Prior to 1.13, this would look like this:

/testfor @p[r=10] {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}}

This will return an output if the player is holding a named diamond sword in the selected slot.

However, the above command has been completely deprecated in 1.13. testfor is now Bedrock only, and the [r=] selector has been changed. You also can't really do anything with this testfor anyway, all it does is give a redstone output. A better and updated version for Java Edition 1.13 would be something along the following lines:

/execute at @p[distance=..10, nbt={SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}}] run <your item spawning command>

This command executes <your item spawning command> at the position of a player within 10 blocks that matches the SelectedItem NBT.

10
  • Thank you, but I'm looking for a script that only looks for the item name and only in the active slot. Commented Aug 17, 2015 at 14:27
  • 1
    1.13 syntax: /execute if entity @p[r=10,nbt={SelectedItem:...}]
    – pppery
    Commented Sep 6, 2018 at 11:20
  • 3
    ^^^no. they removed r= and replaced it with distance=, and in order to calculate anything before or after the specified number, you need to add .., respectively. (eg. to find someone within 10 blocks radius, execute if entity @p[distance=..10,nbt={SelectedItem:...}])
    – xTerrene
    Commented Apr 11, 2019 at 20:58
  • 1
    @Unionhawk What happened? :D
    – user143228
    Commented May 13, 2019 at 11:54
  • 1
    It should be execute ... run <your item spawning command>, not execute ... <your item spawning command>
    – pppery
    Commented Nov 9, 2019 at 13:44
1

To combine the two dataTags you need to move the tag compound from first command into the inventory item compound in the second:

/testfor @p[r=10] {SelectedItemSlot:0,Inventory:[{Slot:0b,­id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}]}

Note: This will only test true if the item is in slot 0 and it is also the selected item.

You can use the SelectedItem tag instead of the SelectedItemSlot tag. This will allow you to target any player who currently has the specified item selected no matter which slot it is in:

/testfor @p[r=10] {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}} 

As of 1.9 you can use scoreboard add tag command to tag the player holding the specific item.

scoreboard players tag @a add <tagName> {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}}

This allows you to target the players within another command. Lets make the players with the selected sword say hello:

/execute @a[tag=<tagName>] ~ ~ ~ say hello

You could use this tag in the item summon commands.

Then to remove the tag from all player:

scoreboard players tag @a remove <tagName> 

If you want to test for any player holding any item with the correct name, simply omit the id portion of the dataTag:

/testfor @p[r=10] {SelectedItem:{tag:{display:{Name:"Item Name"}}}}
0

If you are using a data pack, for better performance, a predicate should be used to detect holding an item in the main hand.

Predicates are more efficient than /execute if entity @s[nbt=…] or /execute if data entity @s {…} because NBT checks on players are heavy on performance.

Predicates go in the /data/<namespace>/predicates folder of your data pack.

A sample predicate for this purpose would look like this:

{
  "condition": "minecraft:entity_properties",
  "entity": "this",
  "predicate": {
    "equipment": {
      "mainhand": {
        "items": ["minecraft:diamond_sword"],
        "nbt": "{CustomItemTag:1b}"
      }
    }
  }
}
-1
/testfor @p[r=10] {SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"Item Name"}}}} 

Would work but a better way to do it would be to use scoreboard objectives with stats ex-

/scoreboard objectives add ItemHold stat.useItem.minecraft.diamond.sword 

I have tested it and it works so try that...

1
  • 1
    stat.useItem.minecraft.diamond.sword increases when you hit a mob/player with a diamond sword, not when you hold it. This answer also doesn't account for the item being named.
    – MrLemon
    Commented Dec 28, 2015 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .