From 9bbbdd42f7da048cc538218e37a7ba6d7624f479 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 22 Mar 2024 22:09:10 +0100 Subject: [PATCH] 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. --- .../eu/cloudapio/deathswap/DeathSwapGame.java | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main/java/eu/cloudapio/deathswap/DeathSwapGame.java b/src/main/java/eu/cloudapio/deathswap/DeathSwapGame.java index 8b5f6ce..b94e4b5 100644 --- a/src/main/java/eu/cloudapio/deathswap/DeathSwapGame.java +++ b/src/main/java/eu/cloudapio/deathswap/DeathSwapGame.java @@ -20,10 +20,9 @@ import org.bukkit.entity.Player; public class DeathSwapGame { ArrayList players; - WorldCreator wc; World world; + World world_nether; Random randomizer; - String worldName; boolean isPlayingGame = false; @@ -34,12 +33,34 @@ public class DeathSwapGame { 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() { randomizer = new Random(System.currentTimeMillis()); players = new ArrayList(); - worldName = "death-swap"; - wc = new WorldCreator(worldName); + + world = createWorld("death-swap", false); + world_nether = createWorld("death-swap-nether", true); getMainWorld().setPVP(false); } @@ -132,26 +153,12 @@ public class DeathSwapGame { 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 spawnLocations = new ArrayList(players.size()); 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++)