Skip to content

Commit

Permalink
fix: cache resized claims
Browse files Browse the repository at this point in the history
Also only returns distinct parent claims in `getParentClaimsOverlapping`
  • Loading branch information
WiIIiam278 committed May 30, 2024
1 parent 30eb9f4 commit 80f0b6f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ && getPlugin().getClaimBlocks(claim.getOwner().get()) < neededBlocks) {
throw new IllegalArgumentException("Owner does not have enough claim blocks to resize claim");
}

// Update the claim
// Update the claim, resizing it in the claim world context
getPlugin().removeMappedClaim(claim, world);
claim.setRegion(newRegion);
world.resizeClaim(claim, newRegion);
getDatabase().updateClaimWorld(world);
getPlugin().addMappedClaim(claim, world);
getPlugin().invalidateClaimListCache(claim.getOwner().orElse(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public void addClaim(@NotNull Claim claim) {
* @since 1.0
*/
public void removeClaim(@NotNull Claim claim) {
if (claim.isChildClaim()) {
throw new IllegalArgumentException("Cannot remove a child claim directly");
}

final UUID owner = claim.getOwner().orElse(ADMIN_CLAIM);
final Set<Claim> ownedClaims = userClaims.get(owner);
if (ownedClaims != null) {
Expand All @@ -164,6 +168,40 @@ public void removeClaim(@NotNull Claim claim) {
});
}

/**
* Resize a claim in the ClaimWorld, caching the new chunks.
* <p>
* It is important that this method is used to resize claims, instead of simply just setting the new claim region,
* as this method ensures that the claim's chunks are correctly cached.
*
* @param claim the claim to resize
* @param newRegion the new region of the claim
* @since 1.3.1
*/
public void resizeClaim(@NotNull Claim claim, @NotNull Region newRegion) {
if (claim.isChildClaim()) {
throw new IllegalArgumentException("Cannot resize a child claim in a world context");
}

// Clear old region chunks
final Region oldRegion = claim.getRegion();
oldRegion.getChunks().forEach(chunk -> {
final long asLong = ((long) chunk[0] << 32) | (chunk[1] & 0xffffffffL);
final Set<Claim> chunkClaims = cachedClaims.get(asLong);
if (chunkClaims != null) {
chunkClaims.remove(claim);
}
});

// Set new region, cache new chunks
claim.setRegion(newRegion);
newRegion.getChunks().forEach(chunk -> {
final long asLong = ((long) chunk[0] << 32) | (chunk[1] & 0xffffffffL);
final Set<Claim> chunkClaims = cachedClaims.computeIfAbsent(asLong, k -> Sets.newConcurrentHashSet());
chunkClaims.add(claim);
});
}

/**
* Returns the name of the ClaimWorld associated with this instance.
*
Expand Down Expand Up @@ -322,7 +360,7 @@ public List<Claim> getParentClaimsOverlapping(@NotNull Region region) {
.filter(claim -> claim.getRegion().overlaps(region))
.forEach(overlappingClaims::add);
});
return overlappingClaims;
return overlappingClaims.stream().distinct().toList();
}

/**
Expand Down Expand Up @@ -428,6 +466,11 @@ private void cacheOwnedClaim(@NotNull Claim claim) {

// Cache a claim in the world
private void cacheClaim(@NotNull Claim claim) {
if (claim.isChildClaim()) {
throw new IllegalArgumentException("Cannot cache a child claim in a world context");
}

// Set parents
claim.getChildren().forEach(c -> c.setParent(claim));
cacheOwnedClaim(claim);

Expand Down

0 comments on commit 80f0b6f

Please sign in to comment.