Skip to content

Commit

Permalink
Replace unnecessarily expensive method getting chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelien30000 committed Jan 22, 2023
1 parent 65e0f70 commit 6159047
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface IWorldGuardManager {

IOwnedLand getRegion(String name);

IOwnedLand getRegion(World world, int chunkX, int chunkZ);

Set<IOwnedLand> getRegions(UUID id, World world);

Set<IOwnedLand> getRegions(UUID id);
Expand Down Expand Up @@ -69,9 +71,9 @@ public interface IWorldGuardManager {

IOwnedLand[] getSurroundingsOwner(IOwnedLand land, UUID owner);

Map<Chunk, IOwnedLand> getNearbyLands(Chunk chunk, int offsetX, int offsetZ);
Map<Long, IOwnedLand> getNearbyLands(Chunk chunk, int offsetX, int offsetZ);

Map<Chunk, IOwnedLand> getNearbyLands(Location loc, int offsetX, int offsetZ);
Map<Long, IOwnedLand> getNearbyLands(Location loc, int offsetX, int offsetZ);

int getRegionCount(UUID id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Map;
import java.util.Set;

/**
* Project: LandLord
Expand Down Expand Up @@ -409,7 +410,7 @@ private boolean isGapBetweenLands(Player player, Chunk chunk) {
int radius = plugin.getConfig().getInt("CommandSettings.Claim.customGapRadius");

boolean differentOwner = false;
Map<Chunk, IOwnedLand> nearbyLands = wg.getNearbyLands(chunk, radius, radius);
Map<Long, IOwnedLand> nearbyLands = wg.getNearbyLands(chunk, radius, radius);
for (IOwnedLand adjLand : nearbyLands.values()) {
if (adjLand != null) {
if (!adjLand.isOwner(player.getUniqueId())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import biz.princeps.landlord.api.ILandLord;
import biz.princeps.landlord.api.IOwnedLand;
import biz.princeps.landlord.api.IWorldGuardManager;
import biz.princeps.landlord.util.JavaUtils;
import biz.princeps.landlord.util.MapConstants;
import biz.princeps.landlord.util.SimpleScoreboard;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -200,7 +201,7 @@ private String[] buildMap(Player p) {
String[][] mapBoard = getMapDir(p);
String[] mapRows = new String[mapBoard.length + 3];

Map<Chunk, IOwnedLand> nearby = wg.getNearbyLands(p.getLocation(), radius, radius);
Map<Long, IOwnedLand> nearby = wg.getNearbyLands(p.getLocation(), radius, radius);

for (int z = 0; z < mapBoard.length; z++) {
StringBuilder row = new StringBuilder();
Expand All @@ -209,7 +210,7 @@ private String[] buildMap(Player p) {
int xx = x - radius;
int zz = z - radius;

IOwnedLand land = nearby.get(p.getWorld().getChunkAt(xx + (p.getLocation().getBlockX() >> 4), zz + (p.getLocation().getBlockZ() >> 4)));
IOwnedLand land = nearby.get(JavaUtils.getChunkKey(xx + (p.getLocation().getBlockX() >> 4), zz + (p.getLocation().getBlockZ() >> 4)));

String currSpot = mapBoard[z][x];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import biz.princeps.landlord.api.IOwnedLand;
import biz.princeps.landlord.api.IWorldGuardManager;
import biz.princeps.landlord.api.tuple.Pair;
import biz.princeps.landlord.util.JavaUtils;
import biz.princeps.lib.PrincepsLib;
import org.bukkit.ChatColor;
import org.bukkit.Chunk;
Expand Down Expand Up @@ -105,21 +106,21 @@ public boolean isLLRegion(String name) {
}

@Override
public Map<Chunk, IOwnedLand> getNearbyLands(Chunk chunk, int offsetX, int offsetZ) {
Map<Chunk, IOwnedLand> lands = new HashMap<>();
public Map<Long, IOwnedLand> getNearbyLands(Chunk chunk, int offsetX, int offsetZ) {
Map<Long, IOwnedLand> lands = new HashMap<>();
World world = chunk.getWorld();
int xCoord = chunk.getX();
int zCoord = chunk.getZ();
for (int x = xCoord - offsetX; x <= xCoord + offsetX; x++) {
for (int z = zCoord - offsetZ; z <= zCoord + offsetZ; z++) {
Chunk chunkA = chunk.getWorld().getChunkAt(x, z);
lands.put(chunkA, this.getRegion(chunkA));
lands.put(JavaUtils.getChunkKey(x, z), this.getRegion(world, x, z));
}
}
return lands;
}

@Override
public Map<Chunk, IOwnedLand> getNearbyLands(Location loc, int offsetX, int offsetZ) {
public Map<Long, IOwnedLand> getNearbyLands(Location loc, int offsetX, int offsetZ) {
return getNearbyLands(loc.getChunk(), offsetX, offsetZ);
}

Expand Down Expand Up @@ -265,12 +266,23 @@ public IOwnedLand getRegion(Location loc) {
name += "_";

// x coord
int x = loc.getBlockX() >> 4;
name += x;
name += loc.getBlockX() >> 4;
name += "_";
// z coord
name += loc.getBlockZ() >> 4;
return getRegion(name);
}

@Override
public IOwnedLand getRegion(World world, int chunkX, int chunkZ) {
String name = world.getName().toLowerCase().replace(" ", "_");
name += "_";

// x coord
name += chunkX;
name += "_";
// z coord
int z = loc.getBlockZ() >> 4;
name += z;
name += chunkZ;
return getRegion(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ public static BlockFace getBlockFace(float yaw) {
return BLOCK_FACES[(Math.round(yaw / 90f) & 0x3)];
}

/**
* + * @param x X Coordinate
* + * @param z Z Coordinate
* + * @return Chunk coordinates packed into a long
* +
*/
public static long getChunkKey(int x, int z) {
return (long) x & 0xffffffffL | ((long) z & 0xffffffffL) << 32;
}

}

0 comments on commit 6159047

Please sign in to comment.