From b7eb4491466715db0c00d0c0918da0c00f9f3a60 Mon Sep 17 00:00:00 2001 From: EtienneDx Date: Fri, 15 May 2020 11:12:06 +0200 Subject: [PATCH] added admin /re cancel command --- src/me/EtienneDx/RealEstate/RECommand.java | 12 ++++ src/me/EtienneDx/RealEstate/RealEstate.java | 16 +++++ .../RealEstate/Transactions/ClaimLease.java | 64 ++++++++++++++----- .../RealEstate/Transactions/ClaimRent.java | 21 ++++-- .../RealEstate/Transactions/ClaimSell.java | 3 +- .../Transactions/ClaimTransaction.java | 6 ++ .../RealEstate/Transactions/Transaction.java | 1 + 7 files changed, 99 insertions(+), 24 deletions(-) diff --git a/src/me/EtienneDx/RealEstate/RECommand.java b/src/me/EtienneDx/RealEstate/RECommand.java index df0d504..755a512 100644 --- a/src/me/EtienneDx/RealEstate/RECommand.java +++ b/src/me/EtienneDx/RealEstate/RECommand.java @@ -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 @@ -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) { diff --git a/src/me/EtienneDx/RealEstate/RealEstate.java b/src/me/EtienneDx/RealEstate/RealEstate.java index 082623d..e450f07 100644 --- a/src/me/EtienneDx/RealEstate/RealEstate.java +++ b/src/me/EtienneDx/RealEstate/RealEstate.java @@ -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()) { diff --git a/src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java b/src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java index 1f357ab..f7544b3 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java +++ b/src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java @@ -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 + @@ -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() + @@ -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 { diff --git a/src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java b/src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java index a674778..49cbab8 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java +++ b/src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java @@ -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 { diff --git a/src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java b/src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java index ea0d9f5..6cbc55d 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java +++ b/src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java @@ -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; } diff --git a/src/me/EtienneDx/RealEstate/Transactions/ClaimTransaction.java b/src/me/EtienneDx/RealEstate/Transactions/ClaimTransaction.java index 8c463d2..4fc3622 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/ClaimTransaction.java +++ b/src/me/EtienneDx/RealEstate/Transactions/ClaimTransaction.java @@ -68,4 +68,10 @@ public UUID getOwner() { return owner; } + + @Override + public boolean tryCancelTransaction(Player p) + { + return this.tryCancelTransaction(p, false); + } } diff --git a/src/me/EtienneDx/RealEstate/Transactions/Transaction.java b/src/me/EtienneDx/RealEstate/Transactions/Transaction.java index 87530cd..6e15b50 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/Transaction.java +++ b/src/me/EtienneDx/RealEstate/Transactions/Transaction.java @@ -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); }