/execute @a ~ ~ ~ detect ~ ~-1 ~ minecraft:stonebrick 1 /tp @p @e{type=Villager]
When ANY player steps on mossy stonebrick (minecraft:stonebirck 1) it teleports the nearest player to the command block to the villager regardless if he step on the mossy cobble.
The /execute
command modifies the sender of the command to that of its target. Any subsequent selectors in commands being run are going to use the executor as the sender.
This comes into play with sender bias, which forces a target selector to target the entity that ran the command. The command you've presented already uses it. The executing player will be the one to be teleported, so long as they are alive.
The @a
selector is the only target selector capable of targeting dead players. If a player dies while on top of stonebrick, they will teleport the nearest living player to them rather than themselves, because @p
cannot target dead players.
The fix for that in particular is to use the @a
selector while reducing the count (via c
parameter) to 1, which also enforces a sender bias. The command itself is still functional as you need it without having to have changed anything else.
/execute @a ~ ~ ~ detect ~ ~-1 ~ minecraft:stonebrick 1 /tp @a[c=1] @e[type=Villager,c=1]
/execute
is used to change the sender and origin of commands to run. /tp
is being run through the player, not by the command block. @p
will be in reference to the executing player's location, and due to sender bias, @p
will always target that player so long as that player is alive. Your command was originally working fine apart from the dead targeting flaw.
This is a good place to use scoreboard tags. This will allow you to tag any player that has diamond and use that tag to target them within the tp command.
Use 3 command blocks in a chain. The first command block removes the tag from all players. The second command block tags any players who have a diamond in their inventory. The third command block teleports any player within the boundaries of the target selector who do not have the tag.
These command blocks have to remain loaded. I recommend putting them in the spawn chunk. I also recommend stopping command block output to prevent chat spam for op players. Use command:
/gamerule commandBlockOutput false
The first command block is RepeatUnconditionalAlways Active with command:
scoreboard players tag @a remove Obtained
The second command block is ChainUnconditionalAlways Active with command:
scoreboard players tag @a add Obtained {Inventory:[{id:"minecraft:diamond"}]}
The Third command block is ChainUnconditionalAlways Active with command:
tp @a[x=X,y=Y,z=Z,r=R,tag=!Obtained] ~ ~ ~2
Replace X, Y, and Z with the coordinates of your gate area. Replace R with the desired radius for that area.
Best Answer
Summen the villager like that (He will be in "god mode" and will not move)
To teleport the villager to the Player put this cmd into an repeating cmd block
The problem is, that the villeger is exactly in the player. To soolve this, we'll add a kind of delay.
Create a scoreboard:
Then a repeating cmd block chain:
The player will then follow the player but will not go nearby the player (2 blocks radius)
Generator for summon commands: https://mcstacker.bimbimma.com/
Command selectors: http://minecraft.gamepedia.com/Commands#Target_selectors
Plugin for visible Commands: CommandDescriptor by SimonMeusel: https://www.spigotmc.org/resources/commanddesciptor.23870/