Skip to content

Commit

Permalink
Make all region loading async
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Sep 2, 2024
1 parent 6fe6e8b commit f71a0ec
Showing 1 changed file with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit f71a0ec

Please sign in to comment.