I"m make the efforts to make a command block that spawns an object in former of girlfriend if you room holding things with a details name. I"ve acquired these two working scripts;

/testfor
p SelectedItemSlot:0,Inventory:­but i can"t gain them together. The block must only look for an item in the energetic slot.

You are watching: How to test if a player is holding an item


*

p SelectedItem:id:"minecraft:diamond_sword",tag:display:Name:"Item Name"This will return an calculation if the player is hold a called diamond sword in the selected slot.

However, the over command has actually been fully deprecated in 1.13. Testfor is currently Bedrock only, and also the selector has actually been changed. You additionally can"t really carry out anything through this testfor anyway, every it walk is offer a redstone output. A much better and updated variation for Java edition 1.13 would be something follow me the adhering to lines:

/execute at
p operation This command executes at the position of a player within 10 blocks that matches the SelectedItem NBT.


*

To incorporate the two dataTags you require to move the tag link from an initial command right into the list item link in the second:

/testfor
p SelectedItemSlot:0,Inventory:Note: This will just test true if the article is in slot 0 and also it is additionally the selected item.

You deserve to use the SelectedItem tag instead of the SelectedItemSlot tag. This will allow you come target any player who at this time has the specified item selected no issue which slot the is in:

/testfor
p SelectedItem:id:"minecraft:diamond_sword",tag:display:Name:"Item Name" together of 1.9 you can use scoreboard include tag command come tag the player hold the details item.

scoreboard football player tag
a add SelectedItem:id:"minecraft:diamond_sword",tag:display:Name:"Item Name"This permits you come target the football player within an additional command. Allows make the players through the selected knife say hello:

/execute
a ~ ~ ~ say helloYou can use this tags in the items summon commands.

Then to eliminate the tags from all player:

scoreboard players tag
a remove If you want to check for any type of player hold any item v the exactly name, merely omit the id part of the dataTag:

/testfor

*

If you are using a data pack, for better performance, a predicate have to be supplied to recognize holding things in the main hand.

Predicates are much more efficient 보다 /execute if entity
s … due to the fact that NBT checks on football player are hefty on performance.

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

See more: If A Snowball Melts So That Its Surface Area Decreases At A Rate Of 1 Cm^2/Min Find The Rate

A sample predicate for this purpose would look favor this:

"condition": "minecraft:entity_properties", "entity": "this", "predicate": "equipment": "mainhand": "items": <"minecraft:diamond_sword">, "nbt": "CustomItemTag:1b"

*

p SelectedItem:id:"minecraft:diamond_sword",tag:display:Name:"Item Name" would work but a much better way to execute it would certainly be to use scoreboard missions with stats zb

/scoreboard objectives add ItemHold stat.useItem.minecraft.diamond.sword I have tested it and it works so try that...


*

Highly energetic question. Knife 10 reputation (not counting the combination bonus) in order come answer this question. The reputation need helps safeguard this question from spam and also non-answer activity.

Not the price you're spring for? Browse various other questions tagged minecraft-java-edition minecraft-commands or questioning your own question.


How carry out I make it so as soon as a player is stop a details item in your hotbar or offhand, that a potion effect is provided to them?
site design / logo © 2021 ridge Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.10.8.40416


your privacy

By click “Accept every cookies”, girlfriend agree stack Exchange can store cookies on your device and disclose details in accordance with our Cookie Policy.