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

Avoid creating a whole new claim when resizing #461

Merged
merged 1 commit into from
Jan 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 13 additions & 37 deletions src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ static Long getChunkHash(Location location)
*/
synchronized public CreateClaimResult createClaim(World world, int x1, int x2, int y1, int y2, int z1, int z2, UUID ownerID, Claim parent, Long id, Player creatingPlayer)
{
return createClaim(world,x1,x2,y1,y2,z1,z2,ownerID,parent,id,creatingPlayer,true);
return createClaim(world,x1,x2,y1,y2,z1,z2,ownerID,parent,id,creatingPlayer,false);
}
//creates a claim.
//if the new claim would overlap an existing claim, returns a failure along with a reference to the existing claim
Expand All @@ -790,7 +790,7 @@ synchronized public CreateClaimResult createClaim(World world, int x1, int x2, i
//does NOT check a player has permission to create a claim, or enough claim blocks.
//does NOT check minimum claim size constraints
//does NOT visualize the new claim for any players
synchronized public CreateClaimResult createClaim(World world, int x1, int x2, int y1, int y2, int z1, int z2, UUID ownerID, Claim parent, Long id, Player creatingPlayer, Boolean isNew)
synchronized public CreateClaimResult createClaim(World world, int x1, int x2, int y1, int y2, int z1, int z2, UUID ownerID, Claim parent, Long id, Player creatingPlayer, boolean dryRun)
{
CreateClaimResult result = new CreateClaimResult();

Expand Down Expand Up @@ -887,7 +887,12 @@ synchronized public CreateClaimResult createClaim(World world, int x1, int x2, i
return result;
}
}
if (isNew) {
if (dryRun) {
// since this is a dry run, just return the unsaved claim as is.
result.succeeded = true;
result.claim = newClaim;
return result;
}
ClaimCreatedEvent event = new ClaimCreatedEvent(newClaim, creatingPlayer);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
Expand All @@ -896,10 +901,6 @@ synchronized public CreateClaimResult createClaim(World world, int x1, int x2, i
return result;

}
} else {
ClaimModifiedEvent event = new ClaimModifiedEvent(newClaim, creatingPlayer);
Bukkit.getPluginManager().callEvent(event);
}
//otherwise add this new claim to the data store to make it effective
this.addClaim(newClaim, true);

Expand Down Expand Up @@ -1236,45 +1237,20 @@ synchronized public void deleteClaimsForPlayer(UUID playerID, boolean releasePet
synchronized public CreateClaimResult resizeClaim(Claim claim, int newx1, int newx2, int newy1, int newy2, int newz1, int newz2, Player resizingPlayer)
{
//try to create this new claim, ignoring the original when checking for overlap
CreateClaimResult result = this.createClaim(claim.getLesserBoundaryCorner().getWorld(), newx1, newx2, newy1, newy2, newz1, newz2, claim.ownerID, claim.parent, claim.id, resizingPlayer);
CreateClaimResult result = this.createClaim(claim.getLesserBoundaryCorner().getWorld(), newx1, newx2, newy1, newy2, newz1, newz2, claim.ownerID, claim.parent, claim.id, resizingPlayer, true);

//if succeeded
if(result.succeeded)
{
//copy permissions from old claim
ArrayList<String> builders = new ArrayList<String>();
ArrayList<String> containers = new ArrayList<String>();
ArrayList<String> accessors = new ArrayList<String>();
ArrayList<String> managers = new ArrayList<String>();
claim.getPermissions(builders, containers, accessors, managers);

for(int i = 0; i < builders.size(); i++)
result.claim.setPermission(builders.get(i), ClaimPermission.Build);

for(int i = 0; i < containers.size(); i++)
result.claim.setPermission(containers.get(i), ClaimPermission.Inventory);

for(int i = 0; i < accessors.size(); i++)
result.claim.setPermission(accessors.get(i), ClaimPermission.Access);

for(int i = 0; i < managers.size(); i++)
{
result.claim.managers.add(managers.get(i));
}

//restore subdivisions
for(Claim subdivision : claim.children)
{
subdivision.parent = result.claim;
result.claim.children.add(subdivision);
}
// copy the boundary from the claim created in the dry run of createClaim() to our existing claim
claim.lesserBoundaryCorner = result.claim.lesserBoundaryCorner;
claim.greaterBoundaryCorner = result.claim.greaterBoundaryCorner;
result.claim = claim;

//save those changes
this.saveClaim(result.claim);
ClaimModifiedEvent event = new ClaimModifiedEvent(result.claim,resizingPlayer);
Bukkit.getPluginManager().callEvent(event);
//make original claim ineffective (it's still in the hash map, so let's make it ignored)
claim.inDataStore = false;
}

return result;
Expand Down