Randomize minecraft spawnpoint for every player in the server

So, I'm trying to find a way to make every player have a randomized spawn point. I know I can try by using /spreadplayers and then make everyone set their spawn point to where they landed and, other than maybe non-uniform distribution of /spreadplayers, that would be it.

The thing is, is there a way to make this so any new player that enters gets a randomized spawn point automatically? I was thinking of doing it manually (aka, tp the new player to a random location and then set their spawnpoint by command line) or maybe having some kind of structure in the spawn with command blocks, but I'm curious as to see if there may be other ways.


Solution 1:

Yes. If I recall correctly, there is no native way to detect player world join, so we can test for a player without a tag and at the last line give them the line:

execute as @a[tag=!has_spawn] run spreadplayers ...
execute as @a[tag=!has_spawn] at @s run spawnpoint @s ~ ~ ~
execute as @a[tag=!has_spawn] run tag @s add has_spawn

The order of the commands is important, especially the last one.

You don't need this running in the background all the time to work, but having it all the time will make the process automatic. If you want non uniform distribution, you can run the spreadplayer individually (like it is done in the second line).

You can't really have uniformity since you won't be using spreadplayer on all players at the same time. You could have a much more complex system to test and make sure no one landed nearby (have an armorstand sit on top of the player's initial spawnpoint and test if the new player landed nearby).