From f71a0ec8e8a62d3689b384ebc4d3aa57bfeab923 Mon Sep 17 00:00:00 2001 From: Archy-X <63976867+Archy-X@users.noreply.github.com> Date: Sun, 28 Jul 2024 15:35:00 -0700 Subject: [PATCH] Make all region loading async --- .../bukkit/region/BukkitRegionManager.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/bukkit/src/main/java/dev/aurelium/auraskills/bukkit/region/BukkitRegionManager.java b/bukkit/src/main/java/dev/aurelium/auraskills/bukkit/region/BukkitRegionManager.java index f07d5a96b..4ad76faa8 100644 --- a/bukkit/src/main/java/dev/aurelium/auraskills/bukkit/region/BukkitRegionManager.java +++ b/bukkit/src/main/java/dev/aurelium/auraskills/bukkit/region/BukkitRegionManager.java @@ -65,19 +65,30 @@ public void handleBlockPlace(Block block) { public void addPlacedBlock(Block block) { Region region = getRegionFromBlock(block); // Create region if it does not exist - if (region == null) { - int regionX = (int) Math.floor((double) block.getChunk().getX() / 32.0); - int regionZ = (int) Math.floor((double) block.getChunk().getZ() / 32.0); + if (region == null || region.shouldReload()) { + addLoadRegionAsync(block); + } else { + addToRegion(block, region); + } + } - region = new Region(block.getWorld().getName(), regionX, regionZ); + private void addLoadRegionAsync(Block block) { + plugin.getScheduler().executeAsync(() -> { + Region region = getRegionFromBlock(block); + if (region == null) { + int regionX = (int) Math.floor((double) block.getChunk().getX() / 32.0); + int regionZ = (int) Math.floor((double) block.getChunk().getZ() / 32.0); + region = new Region(block.getWorld().getName(), regionX, regionZ); - RegionCoordinate regionCoordinate = new RegionCoordinate(block.getWorld().getName(), regionX, regionZ); - regions.put(regionCoordinate, region); - loadRegion(region); - } else if (region.shouldReload()) { + RegionCoordinate regionCoordinate = new RegionCoordinate(block.getWorld().getName(), regionX, regionZ); + regions.put(regionCoordinate, region); + } loadRegion(region); - } + addToRegion(block, region); + }); + } + private void addToRegion(Block block, Region region) { byte regionChunkX = (byte) (block.getChunk().getX() - region.getX() * 32); byte regionChunkZ = (byte) (block.getChunk().getZ() - region.getZ() * 32); ChunkData chunkData = region.getChunkData(new ChunkCoordinate(regionChunkX, regionChunkZ));