Minecraft – How to teleport one player to a random other player using a potion as the trigger

minecraft-java-edition

I am making a MC map and need help. I want to make it that when someone uses a certain potion, that same person teleports to a random user on the map. I read another post like this, but I think it was unrelated. I can't use the @p thing because the other players might teleport instead. I know to use the /effect code on command blocks and some other commands.

Best Answer

This was a very interesting challenge for me to work out. There are many parts to this problem, which are a lot harder to accomplish than one might think. There's identifying the player with the status effect, and identifying a set of random players who are not them (since /tp Unionhawk Unionhawk is a valid result of /tp @p @r, and does nothing).

But, finally, here is my setup, which works for a bounded number of players on a server. Some of its flaws include only allowing one game to be played on a given server, and using experience level to identify players. As such, /gamerule doMobLoot false must be used, and /gamerule commandBlockOutput false is strongly advised.

enter image description here

So there's quite a lot of things going on here, as you can see. But this is all accomplished using 4 commands and a hopper clock.

The Group 1 command blocks are there to detect when a player has the specified potion effect, remove it, and proceed to the matching Group 2 command block. I used /effect @p[c=n] 4 0 for that one, which removes the mining fatigue effect from player n on the server (going down the list of players on the server). When that command evaluates, the Group 2 command blocks give that player one level of experience using /xp 1L @p[c=n].

From there, the next time the hopper clock pulses, the "Group" 3 (you only need one) command block teleports a player with 1 level or more to a random player without any levels using /tp @p[lm=1] @r[l=0]. When this command evaluates, the "Group" 4 command block clears the experience level of all players using /xp -9999L @a.