Minecraft player selectors based on NBT data [duplicate]
![Minecraft player selectors based on NBT data [duplicate] - Black Turntable Minecraft player selectors based on NBT data [duplicate] - Black Turntable](/assets/images/minecraft_player_selectors_based_on_nbt_data_duplicate_0.jpeg)
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?
Best Answer
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.
Pictures about "Minecraft player selectors based on NBT data [duplicate]"
![Minecraft player selectors based on NBT data [duplicate] - Grayscale Photography of Person Using Dj Controller Minecraft player selectors based on NBT data [duplicate] - Grayscale Photography of Person Using Dj Controller](/assets/images/minecraft_player_selectors_based_on_nbt_data_duplicate_1.jpeg)
![Minecraft player selectors based on NBT data [duplicate] - Person Near Vinyl Record Player on Brown Surface Minecraft player selectors based on NBT data [duplicate] - Person Near Vinyl Record Player on Brown Surface](/assets/images/minecraft_player_selectors_based_on_nbt_data_duplicate_2.jpeg)
![Minecraft player selectors based on NBT data [duplicate] - Retro cassette tape with small flower Minecraft player selectors based on NBT data [duplicate] - Retro cassette tape with small flower](/assets/images/minecraft_player_selectors_based_on_nbt_data_duplicate_3.jpeg)
How do you copy blocks using NBT data?
The @e target selector is used to target all entities in the game. This includes both players and mobs in the world. Notice that in this last example, we have used [type=enderman] to target only enderman. This value is called a target selector argument and allows us to filter the target.How do you target specific entities in Minecraft?
NBT tags are used in game commands such as: /summon, /data, /give, /fill, /setblock. The NBT tag is always surrounded in {} such as {NoAI:1}. If there is more than one NBT tag used in a command, the NBT tags are separated by a comma such as {NoAI:1, IsBaby:1}. Each entity (or mob) has its own unique set of NBT tags.How do you use NBT data on Minecraft?
The other target selectors are \u201c@a\u201d for all players, \u201c@r\u201d for a random player, and \u201c@e\u201d will target all entities. Entities include everything that isn't a block, like monsters, snowballs, animals, and arrows.Tout sur le Command Block - Partie VI : NBT Data Tags
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Dids, Isabella Mendes, Muffin Creatives, Elijah O'Donnell