Skip to content

Commit

Permalink
Implemented claim transferring
Browse files Browse the repository at this point in the history
  • Loading branch information
MFHKiwi committed Jul 10, 2021
1 parent 9acbbb1 commit eb67143
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/main/java/me/MFHKiwi/KiwiClaims/KClaim.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public KClaim(Location min, Location max, String owner_name, UUID uuid) {
}
this.min = new Location(min.getWorld(), x1, min.getBlockY(), z1);
this.max = new Location(max.getWorld(), x2, max.getBlockY(), z2);
this.owner_name = owner_name;
this.owner_name = owner_name.toLowerCase();
this.uuid = uuid;
}

Expand Down Expand Up @@ -85,10 +85,19 @@ public Location getMax() {
return this.max;
}

public void setOwnerName(String name) {
this.owner_name = name.toLowerCase();
}

public String getOwnerName() {
return this.owner_name;
}

public boolean ownerEquals(String name) {
if (name.equalsIgnoreCase(this.owner_name)) return true;
return false;
}

public UUID getUUID() {
return this.uuid;
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/me/MFHKiwi/KiwiClaims/KClaimSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ public boolean removeTrusted(KClaim claim, String name) throws Exception {
return true;
}

public boolean setOwner(KClaim claim, String name) throws Exception {
int i = this.claims.indexOf(claim);
if (this.claims.get(i) == null) throw new Exception("Claim " + claim.getUUID().toString() + " not found");
if (claim.ownerEquals(name)) return false;
claim.setOwnerName(name);
saveClaim(claim, this.data_folder);
this.claims.set(i, claim);
return true;
}

public void saveClaims() {
int failed_claims = 0, failed_exclusions = 0;
if (!this.claims.isEmpty()) {
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/me/MFHKiwi/KiwiClaims/KCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class KCommandHandler implements CommandExecutor {
private final KiwiClaims plugin;
private final KPlayerListener listener;
private final String[] help_message = new String[9];
private final String[] owner_set = new String[2];
private final String incorrect_usage, plugin_info, not_player, claim_message, not_in_claim, unclaim_message, not_allowed,
trust_message, already_trusted, untrust_message, already_untrusted, internal_error, no_permission;
trust_message, already_trusted, untrust_message, already_untrusted, internal_error, no_permission, already_owner;

public KCommandHandler(KiwiClaims plugin, KPlayerListener listener) {
this.plugin = plugin;
Expand All @@ -50,17 +51,20 @@ public KCommandHandler(KiwiClaims plugin, KPlayerListener listener) {
this.claim_message = colour2 + "Select a claim by left and right clicking its opposite corners.";
this.not_in_claim = colour1 + "You must be standing in a claim to do that.";
this.unclaim_message = colour2 + "Claim removed.";
this.not_allowed = colour1 + "You must be owner of the claim to do that";
this.not_allowed = colour1 + "You must be owner of the claim to do that.";
this.trust_message = colour2 + "Player trusted.";
this.already_trusted = colour1 + "That player is already trusted in this claim.";
this.untrust_message = colour2 + "Player untrusted.";
this.already_untrusted = colour1 + "That player does not have trust in this claim.";
this.internal_error = colour1 + "Could not do this due to an internal plugin error. Contact staff about this.";
this.no_permission = colour1 + "You do not have permission to do that.";
this.already_owner = colour1 + "You already own this claim.";
this.owner_set[0] = colour2 + "Transferred claim to " + colour1;
this.owner_set[1] = colour2 + ".";
}

private boolean shouldPrevent(Player player, KClaim claim) {
if (!claim.getOwnerName().equals(player.getName()) && !player.hasPermission("kc.admin")) return true;
if (!claim.ownerEquals(player.getName()) && !player.hasPermission("kc.admin")) return true;
return false;
}

Expand Down Expand Up @@ -155,6 +159,29 @@ else if (shouldPrevent(player, claim)) {
}
return true;
}
if (subcommand.equalsIgnoreCase("transfer")) {
KClaim claim = plugin.getClaimSave().getClaimAt(player.getLocation());
if (args.length < 2) {
player.sendMessage(this.incorrect_usage);
}
else if (claim == null) {
player.sendMessage(this.not_in_claim);
}
else if (shouldPrevent(player, claim)) {
player.sendMessage(this.not_allowed);
}
else try {
if (!plugin.getClaimSave().setOwner(claim, args[1])) {
player.sendMessage(this.already_owner);
} else {
player.sendMessage(this.owner_set[0] + args[1] + this.owner_set[1]);
}
} catch (Exception e) {
plugin.log(e.getMessage());
player.sendMessage(this.internal_error);
}
return true;
}
if (!player.hasPermission("kc.admin")) {
player.sendMessage(this.no_permission);
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/MFHKiwi/KiwiClaims/KiwiClaims.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ChatColor getColour(int id) {
// Common methods that classes use. Not worth creating a class for, so I'm putting it in here.
public static boolean shouldPrevent(Player player, KClaim claim) {
String player_name = player.getName();
if (!player_name.equals(claim.getOwnerName()) &&
if (!claim.ownerEquals(player_name) &&
!claim.isTrusted(player_name) &&
!player.hasPermission("kc.admin")) return true;
else return false;
Expand Down

0 comments on commit eb67143

Please sign in to comment.