13
0

Staggered world reload

Dieser Commit ist enthalten in:
Lixfel 2020-10-28 11:15:12 +01:00
Ursprung 78d8b25ade
Commit 724bc117e6

Datei anzeigen

@ -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);
}
}
}
}
}