3

This question already has an answer here:

So 1.8 brought the ability to test for whatever the player currently has selected.

For example (maybe not exactly, but something like this),

testfor @a {SelectedItem:{id:minecraft:stick}}

How do you go about selecting based on this? I'm looking for something like this:

tp @a[SelectedItem:{id=minecraft:stick}}] ~ ~5 ~

I've tried many different permutations of syntax but the Command Block still outputs errors. Any ideas?

marked as duplicate by Ivo Coumans, Wondercricket, Virusbomb, Malco, arghtype Apr 15 at 17:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I think the partial nbt test only works with /testfor, /testforblock, and /clear – ModDL Oct 6 '14 at 3:59
3

This can be done, but you'll need a few command blocks to do it. The use of the scoreboard feature is what allows this to work.

Let's say you want to teleport a player holding a nether star out of the world (I don't know why you would want to do that, but let's say you want to do that). First of all, you'll need to set up the scoreboard:

scoreboard objectives add holdingstar dummy

You only need to do that once. Now that that's done, you can set up your command blocks (you're going to want to set them up on a clock so that they run continuously). You're going to want to set this score to 1 for all players holding a nether star, and 0 for all players not holding a nether star. Unfortunately, there isn't a way to check if a player is not holding a specific item, so the solution becomes a little more kludgey. Running these two commands in sequence should do the trick though:

scoreboard players set @a holdingstar 0
scoreboard players set @a holdingstar 1 {SelectedItem:{id:minecraft:nether_star}}

This sets the score of holdingstar for all players to 0, and then sets the score of those holding a star to 1. Now that we have the scoreboard checking for what we want to check for, we can pass the score into a selector:

tp @a[score_holdingstar_min:1] ~ -1 ~

Bye bye, everyone who has a nether star selected. If you do happen to run this on a clock, keep in mind that it will keep running the command over and over again; there's no way to tell the system that it already performed this command on this player. This is fine for /effect commands and the like, but it complicates things for /tp commands. Just something to keep in mind.

  • You might be able to position the two scoreboard commands on a fill clock line in such a way that they run in sequence, but it's a little weird. fill clocks take "redstone quirks" to an entirely new level that I don't quite understand fully. – Unionhawk Oct 6 '14 at 4:17
  • The syntax for your selector is incorrect. It should contain a = and not a :. – Sumurai8 Nov 22 '15 at 14:16

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