Minecraft – (1.16.2) What is the most efficient way to /spreadplayers into a different dimension

minecraft-commandsminecraft-java-edition

I am trying to have all entities that travel to the Nether be instantly teleported back to a random location in the Overworld.

The /spreadplayers command is perfect for the random location:

spreadplayers 0 0 0 45 under 30 false @s

And I can detect any entity in the nether with a predicate:

{
  "condition": "minecraft:entity_properties",
  "entity": "this",
  "predicate": {
    "location": {
      "dimension": "minecraft:the_nether"
    }
  }
}

Like so: spreadplayers 0 0 0 45 under 30 false @e[predicate=namespace:in_nether]

Unfortunately, this command spreads all entities randomly around 0x 0z in the Nether, not the Overworld.


I then tried:

execute in minecraft:overworld run spreadplayers 0 0 0 45 under 30 false @e[predicate=namespace:in_nether]

The result was the same.


I decided to do some testing, so I replaced the first command's /spreadplayers with /tp:

tp @e[predicate=namespace:in_nether] 0 1 0

It worked perfectly. All entities that traveled to the Nether were instantly teleported to Overworld 0x 1y 0z.


It seemed that /tp worked differently than /spreadplayers. So, my next attempt was to place the following commands into a function I called portal:

tp @s 0 1 0
spreadplayers 0 0 0 45 under 30 false @s

I ran this function every tick:

execute as @e[predicate=namespace:in_nether] run function namespace:portal

It worked- whenever my player traveled through the portal, it was instantly teleported to Overworld 0x 1y 0z and then spread randomly around Overworld 0x 0z. But then I found that it seemed to only work for my player.

When any other type of entity traveled to the Nether, they were teleported back to Overworld 0x 1y 0z, but not spread randomly.


I finally found a working solution: (And it created a client-side visual glitch)

tag @e[predicate=namespace:in_nether] add in_nether
tp @e[predicate=namespace:in_nether] 0 1 0
spreadplayers 0 0 0 45 under 30 false @e[tag=in_nether]
tag @e[tag=in_nether] remove in_nether

This solution is laggier and more complex than what I originally thought would work. I'm not sure why /spreadplayers acts this way, so to restate my question- What is the most efficient way to /spreadplayers into a different dimension? (And why did my other more elegant solutions not work?!) Thanks

Best Answer

Your idea may be the best you can do.

It seems like our friend Fabian Röling has pointed out a bug report about this issue. Because it has been confirmed as a bug, it means that /spreadplayers should not act this way.

Therefore, to answer your original question, the theoretical most effective way to /spreadplayers into a different dimension is one of your original trials:

execute in minecraft:overworld run spreadplayers 0 0 0 45 under 30 false @e[predicate=namespace:in_nether]

But because of the bug, it doesn’t work. And you had to find a workaround.