fix: Create and load extra worlds only on server startup

This saves time having to wait for the entire world being generated on every swap.
This commit is contained in:
apio 2024-03-22 22:09:10 +01:00
parent 91e3a43dc4
commit 9bbbdd42f7
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -20,10 +20,9 @@ import org.bukkit.entity.Player;
public class DeathSwapGame { public class DeathSwapGame {
ArrayList<Player> players; ArrayList<Player> players;
WorldCreator wc;
World world; World world;
World world_nether;
Random randomizer; Random randomizer;
String worldName;
boolean isPlayingGame = false; boolean isPlayingGame = false;
@ -34,12 +33,34 @@ public class DeathSwapGame {
long lastSwapTime; long lastSwapTime;
private World createWorld(String name, boolean isNether)
{
World world = Bukkit.getWorld(name);
Bukkit.getServer().unloadWorld(world, false);
deleteWorld(name);
WorldCreator wc = new WorldCreator(name);
wc.copy(Bukkit.getServer().getWorlds().get(0));
wc.seed(randomizer.nextLong());
if(isNether) wc.environment(Environment.NETHER);
world = wc.createWorld();
world.setKeepSpawnInMemory(false);
world.setDifficulty(Difficulty.NORMAL);
world.setPVP(false);
world.setGameRule(GameRule.DO_IMMEDIATE_RESPAWN, true);
return world;
}
public DeathSwapGame() public DeathSwapGame()
{ {
randomizer = new Random(System.currentTimeMillis()); randomizer = new Random(System.currentTimeMillis());
players = new ArrayList<Player>(); players = new ArrayList<Player>();
worldName = "death-swap";
wc = new WorldCreator(worldName); world = createWorld("death-swap", false);
world_nether = createWorld("death-swap-nether", true);
getMainWorld().setPVP(false); getMainWorld().setPVP(false);
} }
@ -132,26 +153,12 @@ public class DeathSwapGame {
isPlayingGame = true; isPlayingGame = true;
Bukkit.getServer().unloadWorld(world, false);
deleteWorld(worldName);
wc.copy(Bukkit.getServer().getWorlds().get(0));
wc.seed(randomizer.nextLong());
if(isNether)
{
wc.environment(Environment.NETHER);
}
world = wc.createWorld();
world.setKeepSpawnInMemory(false);
world.setDifficulty(Difficulty.NORMAL);
world.setPVP(false);
world.setGameRule(GameRule.DO_IMMEDIATE_RESPAWN, true);
ArrayList<Location> spawnLocations = new ArrayList<Location>(players.size()); ArrayList<Location> spawnLocations = new ArrayList<Location>(players.size());
for(int i = 0; i < players.size(); i++) for(int i = 0; i < players.size(); i++)
{ {
spawnLocations.add(randomSpawnLocation(world, isNether)); if(isNether)
spawnLocations.add(randomSpawnLocation(world_nether, true));
else spawnLocations.add(randomSpawnLocation(world, false));
} }
for(int i = 0; i < players.size(); i++) for(int i = 0; i < players.size(); i++)