From afb725ef7df8f21e75a68f8feea9e4e9d9648c6d Mon Sep 17 00:00:00 2001 From: LlmDl Date: Tue, 4 Jun 2024 13:55:52 -0500 Subject: [PATCH] - API: Add TownPreReclaimEvent. - A cancellable event thrown before a player can reclaim a town. --- .../towny/event/town/TownPreReclaimEvent.java | 63 +++++++++++++++++++ .../bukkit/towny/utils/TownRuinUtil.java | 2 + Towny/src/main/resources/ChangeLog.txt | 4 +- 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 Towny/src/main/java/com/palmergames/bukkit/towny/event/town/TownPreReclaimEvent.java diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/event/town/TownPreReclaimEvent.java b/Towny/src/main/java/com/palmergames/bukkit/towny/event/town/TownPreReclaimEvent.java new file mode 100644 index 0000000000..cf6f54576e --- /dev/null +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/event/town/TownPreReclaimEvent.java @@ -0,0 +1,63 @@ +package com.palmergames.bukkit.towny.event.town; + +import com.palmergames.bukkit.towny.event.CancellableTownyEvent; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.Town; + +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class TownPreReclaimEvent extends CancellableTownyEvent { + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private final Player player; + private final Resident resident; + private final Town town; + + /** + * Event thrown prior to a {@link Town} being reclaimed by a {@link Resident}. + * + * @param town The Town being reclaimed. + * @param resident The resident who would become mayor. + * @param player The Player who would become mayor. + */ + public TownPreReclaimEvent(Town town, Resident resident, Player player) { + this.town = town; + this.resident = resident; + this.player = player; + } + + /** + * @return the {@link Town} which will be reclaimed. + */ + @Nullable + public Town getTown() { + return town; + } + + /** + * @return the resident who will become mayor. + */ + public Resident getResident() { + return resident; + } + + /** + * @return the player who will become mayor. + */ + public Player getPlayer() { + return player; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } +} \ No newline at end of file diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownRuinUtil.java b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownRuinUtil.java index a1511bf7d3..9e596fb1dc 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownRuinUtil.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownRuinUtil.java @@ -11,6 +11,7 @@ import com.palmergames.bukkit.towny.confirmations.Confirmation; import com.palmergames.bukkit.towny.confirmations.ConfirmationTransaction; import com.palmergames.bukkit.towny.event.DeleteTownEvent; +import com.palmergames.bukkit.towny.event.town.TownPreReclaimEvent; import com.palmergames.bukkit.towny.event.town.TownReclaimedEvent; import com.palmergames.bukkit.towny.event.town.TownRuinedEvent; import com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException; @@ -165,6 +166,7 @@ public static void processRuinedTownReclaimRequest(Player player) { Confirmation.runOnAccept(() -> reclaimTown(resident, town)) .setCost(new ConfirmationTransaction(() -> townReclaimCost, resident, "Cost of town reclaim.", Translatable.of("msg_insuf_funds"))) .setTitle(Translatable.of("msg_confirm_purchase", TownyEconomyHandler.getFormattedBalance(townReclaimCost))) + .setCancellableEvent(new TownPreReclaimEvent(town, resident, player)) .sendTo(player); } catch (TownyException e) { TownyMessaging.sendErrorMsg(player, e.getMessage(player)); diff --git a/Towny/src/main/resources/ChangeLog.txt b/Towny/src/main/resources/ChangeLog.txt index fe96e16469..95d7abd09f 100644 --- a/Towny/src/main/resources/ChangeLog.txt +++ b/Towny/src/main/resources/ChangeLog.txt @@ -9799,4 +9799,6 @@ v0.92.0.11: - Bump me.clip:placeholderapi from 2.11.5 to 2.11.6. 0.100.2.13: - Improve a couple SQL operations, courtesy of xyzeva with PR #7438. (First-Time Contributor!) - - Fix the relational placeholder that showed conquered status not being used for the player which are conquered. \ No newline at end of file + - Fix the relational placeholder that showed conquered status not being used for the player which are conquered. + - API: Add TownPreReclaimEvent. + - A cancellable event thrown before a player can reclaim a town. \ No newline at end of file