From 724bc117e69bdc8eead785e33566c8c5bb1af7fe Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 28 Oct 2020 11:15:12 +0100 Subject: [PATCH] Staggered world reload --- .../spectatesystem/util/WorldLoader.java | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/de/steamwar/spectatesystem/util/WorldLoader.java b/src/de/steamwar/spectatesystem/util/WorldLoader.java index 1cd4373..f504b51 100644 --- a/src/de/steamwar/spectatesystem/util/WorldLoader.java +++ b/src/de/steamwar/spectatesystem/util/WorldLoader.java @@ -31,8 +31,6 @@ import org.bukkit.WorldCreator; import org.bukkit.craftbukkit.v1_15_R1.CraftChunk; import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; -import java.util.Objects; - public class WorldLoader { private WorldLoader(){} @@ -41,35 +39,39 @@ public class WorldLoader { public static void reloadWorld(){ Bukkit.getScheduler().runTask(SpectateSystem.get(), () -> { REntity.removeAll(); + World backupWorld = new WorldCreator(world.getName() + "-backup").createWorld(); - WorldServer cworld = ((CraftWorld)world).getHandle(); - WorldServer backup = ((CraftWorld) Objects.requireNonNull(new WorldCreator(world.getName() + "-backup").createWorld())).getHandle(); - + int delay = 0; for(int cX = Config.ArenaMinX / 16; cX <= Config.ArenaMaxX / 16; cX++){ for(int cZ = Config.ArenaMinZ / 16; cZ <= Config.ArenaMaxZ / 16; cZ++){ - long start = System.currentTimeMillis(); - int changedBlocks = 0; - net.minecraft.server.v1_15_R1.Chunk cchunk = ((CraftChunk) world.getChunkAt(cX, cZ)).getHandle(); - for(int y = 255; y >= 0; y--){ // y top down to reduce lightning calculations - for(int x = cX * 16; x < (cX + 1) * 16; x++){ - for(int z = cZ; z < (cZ + 1) * 16; z++){ - BlockPosition blockPos = new BlockPosition(x, y, z); - IBlockData backupBlock = backup.getType(blockPos); - if(backupBlock == cworld.getType(blockPos)) - continue; - - changedBlocks++; - cchunk.removeTileEntity(blockPos); - cworld.setTypeAndData(blockPos, backupBlock, 1042); - cworld.getChunkProvider().flagDirty(blockPos); - } - } - } - System.out.println("Resetted " + changedBlocks + " in " + (System.currentTimeMillis() - start) + "ms"); + int finalCX = cX; + int finalCZ = cZ; + Bukkit.getScheduler().runTaskLater(SpectateSystem.get(), () -> reloadChunk(backupWorld, finalCX, finalCZ), delay++); } } - Bukkit.unloadWorld(world.getName() + "-backup", false); + Bukkit.getScheduler().runTaskLater(SpectateSystem.get(), () -> Bukkit.unloadWorld(world.getName() + "-backup", false), delay); }); } + + public static void reloadChunk(World backupWorld, int cX, int cZ){ + WorldServer cworld = ((CraftWorld)world).getHandle(); + WorldServer backup = ((CraftWorld) backupWorld).getHandle(); + + net.minecraft.server.v1_15_R1.Chunk cchunk = ((CraftChunk) world.getChunkAt(cX, cZ)).getHandle(); + for(int y = 255; y >= 0; y--){ // y top down to reduce lightning calculations + for(int x = cX * 16; x < (cX + 1) * 16; x++){ + for(int z = cZ; z < (cZ + 1) * 16; z++){ + BlockPosition blockPos = new BlockPosition(x, y, z); + IBlockData backupBlock = backup.getType(blockPos); + if(backupBlock == cworld.getType(blockPos)) + continue; + + cchunk.removeTileEntity(blockPos); + cworld.setTypeAndData(blockPos, backupBlock, 1042); + cworld.getChunkProvider().flagDirty(blockPos); + } + } + } + } }