Skip to content

Commit

Permalink
Added cache for Bukkit worlds in ChunkPosition (#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Jul 20, 2023
1 parent 5675309 commit 097618f
Showing 1 changed file with 16 additions and 5 deletions.
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.World;
import org.bukkit.block.Block;

import javax.annotation.Nullable;
import java.util.Objects;

public class ChunkPosition {
Expand All @@ -16,6 +17,8 @@ public class ChunkPosition {
private final int z;

private long pairedXZ = -1;
@Nullable
private World cachedBukkitWorld;

private ChunkPosition(WorldInfo worldInfo, int x, int z) {
this.worldInfo = worldInfo;
Expand All @@ -24,27 +27,30 @@ private ChunkPosition(WorldInfo worldInfo, int x, int z) {
}

public static ChunkPosition of(Block block) {
return of(WorldInfo.of(block.getWorld()), block.getX() >> 4, block.getZ() >> 4);
World world = block.getWorld();
return of(WorldInfo.of(world), block.getX() >> 4, block.getZ() >> 4).withBukkitWorld(world);
}

public static ChunkPosition of(Location location) {
return of(WorldInfo.of(location.getWorld()), location.getBlockX() >> 4, location.getBlockZ() >> 4);
World world = location.getWorld();
return of(WorldInfo.of(world), location.getBlockX() >> 4, location.getBlockZ() >> 4).withBukkitWorld(world);
}

public static ChunkPosition of(Chunk chunk) {
return of(WorldInfo.of(chunk.getWorld()), chunk.getX(), chunk.getZ());
World world = chunk.getWorld();
return of(WorldInfo.of(world), chunk.getX(), chunk.getZ()).withBukkitWorld(world);
}

public static ChunkPosition of(World world, int x, int z) {
return of(WorldInfo.of(world), x, z);
return of(WorldInfo.of(world), x, z).withBukkitWorld(world);
}

public static ChunkPosition of(WorldInfo worldInfo, int x, int z) {
return new ChunkPosition(worldInfo, x, z);
}

public World getWorld() {
return Bukkit.getWorld(getWorldName());
return this.cachedBukkitWorld == null ? (this.cachedBukkitWorld = Bukkit.getWorld(getWorldName())) : this.cachedBukkitWorld;
}

public WorldInfo getWorldsInfo() {
Expand Down Expand Up @@ -95,4 +101,9 @@ public String toString() {
return worldInfo.getName() + ", " + x + ", " + z;
}

private ChunkPosition withBukkitWorld(World world) {
this.cachedBukkitWorld = world;
return this;
}

}

0 comments on commit 097618f

Please sign in to comment.