Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace unnecessarily expensive method getting chunks #127

Draft
wants to merge 2 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -14,6 +14,8 @@
public class JavaUtils {

public static final BlockFace[] BLOCK_FACES = new BlockFace[]{BlockFace.SOUTH, BlockFace.WEST, BlockFace.NORTH, BlockFace.EAST};
private static final long CHUNK_KEY_MAX_VALUE = 0xffffffffL;
private static final short CHUNK_KEY_MAX_BITS = 32;

public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
Expand Down Expand Up @@ -64,4 +66,13 @@ public static BlockFace getBlockFace(float yaw) {
return BLOCK_FACES[(Math.round(yaw / 90f) & 0x3)];
}

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

}