Minecraft Command Blocks: /testfor command for wearing a player head, then tp [duplicate]

Solution 1:

So I see a few issue with what you're doing. First, make the second command block a chain command block. What you're doing right now is pretty close to undefined behaviour (I think it is defined, but it's definitely not what you're intending).

Secondly, your /tp command is not going to play nicely with multiple players. This is why I strongly encourage people learning commands for the first time to stay away from /testfor, and learn the scoreboard instead.

With these bits of advice, we can get to a full solution. You're going to need 3 command blocks in a chain, the first being a repeating command block followed by 2 chain command blocks. In this instance, you're going to want the chain command blocks to be always active (you can get into some much more advanced stuff later with bigger command block contraptions with them set to need redstone, but that's well out of the scope of this problem), but you can leave the repeating command block as needs redstone in order for you to be able to toggle it.

The commands you're going to need are as follows:

/scoreboard players tag @a remove TrainingHelmet
/scoreboard players tag @a add TrainingHelmet {Inventory:[{Slot:103b,id:"minecraft:skull",tag:{display:{Name:"Training Helmet"}}}]}
/tp @a[tag=TrainingHelmet] -160 102 60

So what do these commands do? Let's work from the end. The last command teleports all players that have the TrainingHelmet tag. The second command adds the tag to the appropriate players, by testing if the item type and name match (testing against the owner is a little difficult). Since we only want to teleport players when they have the helmet on, we remove the tag from everyone in the first command.

This introduces a problem though: when they have the hat on, they'll be continuously teleported to the training location. That might not be what you want. There's a few ways around this. One is to restrict the /tp command to not run on players in a certain area, for instance changing the target selector to @a[tag=TrainingHelmet,x=-160,y=102,z=60,rm=20] will exclude anyone within 20 blocks (straight line) of the destination. Another is to remove the hat from their inventory after they're teleported with the following command:

/clear @a[tag=TrainingHelmet] minecraft:skull -1 -1 {display:{Name:"Training Helmet"}}