Skip to content

Commit

Permalink
added admin /re cancel command
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneDx committed May 15, 2020
1 parent 2fd872d commit b7eb449
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 24 deletions.
12 changes: 12 additions & 0 deletions src/me/EtienneDx/RealEstate/RECommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
}
bt.exitOffer = null;
claim.dropPermission(bt.buyer.toString());
claim.managers.remove(bt.buyer.toString());
GriefPrevention.instance.dataStore.saveClaim(claim);
bt.buyer = null;
bt.update();// eventual cancel is contained in here
Expand Down Expand Up @@ -399,6 +400,17 @@ else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
}
}

@Subcommand("cancel")
@Conditions("claimHasTransaction")
@CommandPermission("realestate.admin")
public static void cancelTransaction(Player player)
{
Location loc = player.getLocation();
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null);
Transaction t = RealEstate.transactionsStore.getTransaction(claim);
t.tryCancelTransaction(player, true);
}

@HelpCommand
public static void onHelp(CommandSender sender, CommandHelp help)
{
Expand Down
16 changes: 16 additions & 0 deletions src/me/EtienneDx/RealEstate/RealEstate.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ private void registerConditions()
}
throw new ConditionFailedException("You must stand inside of a claim to use this command!");
});
manager.getCommandConditions().addCondition("claimHasTransaction", (context) -> {
if(!context.getIssuer().isPlayer())
{
throw new ConditionFailedException("Only Players can perform this command!");
}
Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
if(c == null)
{
throw new ConditionFailedException("You must stand inside of a claim to use this command!");
}
Transaction tr = transactionsStore.getTransaction(c);
if(tr == null)
{
throw new ConditionFailedException("This claim has no ongoing transactions!");
}
});
manager.getCommandConditions().addCondition("inPendingTransactionClaim", (context) -> {
if(!context.getIssuer().isPlayer())
{
Expand Down
64 changes: 47 additions & 17 deletions src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
}
else
{
this.exitLease();
}
// no need to re update, since there's no sign
RealEstate.transactionsStore.saveData();
}

private void exitLease()
{
if(buyer != null)
{
OfflinePlayer buyerPlayer = Bukkit.getOfflinePlayer(buyer);
OfflinePlayer seller = owner == null ? null : Bukkit.getOfflinePlayer(owner);

Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);

String claimType = claim.parent == null ? "claim" : "subclaim";

if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer)
{
((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
Expand All @@ -214,13 +231,13 @@ else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", the transaction has been cancelled.");
}
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
{
User u = RealEstate.ess.getUser(this.buyer);
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.RED +
{
User u = RealEstate.ess.getUser(this.buyer);
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.RED +
"Couldn't pay the lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " +
sign.getBlockX() + ", Y: " +
sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", the transaction has been cancelled.");
}
}
if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner)
{
((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() +
Expand All @@ -230,30 +247,43 @@ else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
ChatColor.AQUA + ", the transaction has been cancelled");
}
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
{
User u = RealEstate.ess.getUser(this.owner);
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() +
{
User u = RealEstate.ess.getUser(this.owner);
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() +
ChatColor.AQUA + " couldn't pay lease for the " + claimType + " at " + ChatColor.BLUE + "[" +
sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " +
sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" +
ChatColor.AQUA + ", the transaction has been cancelled");
}
RealEstate.transactionsStore.cancelTransaction(this);
}

claim.managers.remove(buyer.toString());
claim.dropPermission(buyer.toString());
}
// no need to re update, since there's no sign
RealEstate.transactionsStore.saveData();
else
{
getHolder().breakNaturally();// the sign should still be there since the lease has netver begun
}
RealEstate.transactionsStore.cancelTransaction(this);
}

@Override
public boolean tryCancelTransaction(Player p)
public boolean tryCancelTransaction(Player p, boolean force)
{
if(buyer != null)
{
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);
if(p != null)
p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This " + (claim.parent == null ? "claim" : "subclaim") +
" is currently rented, you can't cancel the transaction!");
return false;
if(p.hasPermission("realestate.admin") && force == true)
{
this.exitLease();
return true;
}
else
{
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);
if(p != null)
p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This " + (claim.parent == null ? "claim" : "subclaim") +
" is currently leased, you can't cancel the transaction!");
return false;
}
}
else
{
Expand Down
21 changes: 15 additions & 6 deletions src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,24 @@ else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
}

@Override
public boolean tryCancelTransaction(Player p)
public boolean tryCancelTransaction(Player p, boolean force)
{
if(buyer != null)
{
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);
if(p != null)
p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This " + (claim.parent == null ? "claim" : "subclaim") +
" is currently rented, you can't cancel the transaction!");
return false;
if(p.hasPermission("realestate.admin") && force == true)
{
this.unRent(true);
RealEstate.transactionsStore.cancelTransaction(this);
return true;
}
else
{
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);
if(p != null)
p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This " + (claim.parent == null ? "claim" : "subclaim") +
" is currently rented, you can't cancel the transaction!");
return false;
}
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public boolean update()
}

@Override
public boolean tryCancelTransaction(Player p)
public boolean tryCancelTransaction(Player p, boolean force)
{
// nothing special here, this transaction can only be waiting for a buyer
RealEstate.transactionsStore.cancelTransaction(this);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ public UUID getOwner()
{
return owner;
}

@Override
public boolean tryCancelTransaction(Player p)
{
return this.tryCancelTransaction(p, false);
}
}
1 change: 1 addition & 0 deletions src/me/EtienneDx/RealEstate/Transactions/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public interface Transaction
public void preview(Player player);
public boolean update();
public boolean tryCancelTransaction(Player p);
public boolean tryCancelTransaction(Player p, boolean force);
public void msgInfo(CommandSender cs);
}

0 comments on commit b7eb449

Please sign in to comment.