From de07e4380f676407a5561cda71b1d5cd6e3c06af Mon Sep 17 00:00:00 2001 From: Warrior <50800980+Warriorrrr@users.noreply.github.com> Date: Wed, 19 Jun 2024 20:02:35 +0200 Subject: [PATCH] Add plot group creation/deletion events (#7476) --- .../bukkit/towny/command/PlotCommand.java | 42 ++++++++---- .../event/plot/group/PlotGroupAddEvent.java | 50 ++++++++++++++ .../plot/group/PlotGroupCreatedEvent.java | 24 +++++++ .../plot/group/PlotGroupDeletedEvent.java | 67 +++++++++++++++++++ .../bukkit/towny/utils/TownRuinUtil.java | 4 +- 5 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupAddEvent.java create mode 100644 Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupCreatedEvent.java create mode 100644 Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupDeletedEvent.java diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/command/PlotCommand.java b/Towny/src/main/java/com/palmergames/bukkit/towny/command/PlotCommand.java index d520b4a7f7..2096221ecc 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/command/PlotCommand.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/command/PlotCommand.java @@ -21,6 +21,9 @@ import com.palmergames.bukkit.towny.event.plot.PlotSetForSaleEvent; import com.palmergames.bukkit.towny.event.plot.PlotTrustAddEvent; import com.palmergames.bukkit.towny.event.plot.PlotTrustRemoveEvent; +import com.palmergames.bukkit.towny.event.plot.group.PlotGroupAddEvent; +import com.palmergames.bukkit.towny.event.plot.group.PlotGroupCreatedEvent; +import com.palmergames.bukkit.towny.event.plot.group.PlotGroupDeletedEvent; import com.palmergames.bukkit.towny.event.plot.toggle.PlotToggleExplosionEvent; import com.palmergames.bukkit.towny.event.plot.toggle.PlotToggleFireEvent; import com.palmergames.bukkit.towny.event.plot.toggle.PlotToggleMobsEvent; @@ -1318,42 +1321,53 @@ public void parsePlotGroupAdd(String[] split, TownBlock townBlock, Player player Confirmation.runOnAccept( ()-> { PlotGroup oldGroup = townBlock.getPlotObjectGroup(); oldGroup.removeTownBlock(townBlock); - if (oldGroup.getTownBlocks().isEmpty()) { + if (oldGroup.getTownBlocks().isEmpty() && !BukkitTools.isEventCancelled(new PlotGroupDeletedEvent(oldGroup, player, PlotGroupDeletedEvent.Cause.NO_TOWNBLOCKS))) { String oldName = oldGroup.getName(); town.removePlotGroup(oldGroup); TownyUniverse.getInstance().getDataSource().removePlotGroup(oldGroup); TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_deleted", oldName)); } else oldGroup.save(); - createOrAddOnToPlotGroup(townBlock, town, name); - resident.setPlotGroupName(name); - TownyMessaging.sendMsg(player, Translatable.of("msg_townblock_transferred_from_x_to_x_group", oldGroup.getName(), townBlock.getPlotObjectGroup().getName())); + + try { + createOrAddOnToPlotGroup(townBlock, town, player, name); + resident.setPlotGroupName(name); + TownyMessaging.sendMsg(player, Translatable.of("msg_townblock_transferred_from_x_to_x_group", oldGroup.getName(), townBlock.getPlotObjectGroup().getName())); + } catch (TownyException e) { + TownyMessaging.sendErrorMsg(player, e.getMessage(player)); + } }) .setTitle(Translatable.of("msg_plot_group_already_exists_did_you_want_to_transfer", townBlock.getPlotObjectGroup().getName(), split[1])) .sendTo(player); } else { // Create a brand new plot group. - createOrAddOnToPlotGroup(townBlock, town, plotGroupName); + createOrAddOnToPlotGroup(townBlock, town, player, plotGroupName); resident.setPlotGroupName(plotGroupName); TownyMessaging.sendMsg(player, Translatable.of("msg_plot_was_put_into_group_x", townBlock.getX(), townBlock.getZ(), townBlock.getPlotObjectGroup().getName())); } } - private void createOrAddOnToPlotGroup(TownBlock townBlock, Town town, String plotGroupName) { - PlotGroup newGroup = null; + private void createOrAddOnToPlotGroup(TownBlock townBlock, Town town, Player player, String plotGroupName) throws TownyException { + PlotGroup newGroup; // Don't add the group to the town data if it's already there. if (town.hasPlotGroupName(plotGroupName)) { newGroup = town.getPlotObjectGroupFromName(plotGroupName); + + BukkitTools.ifCancelledThenThrow(new PlotGroupAddEvent(newGroup, townBlock, player)); + townBlock.setPermissions(newGroup.getPermissions().toString()); townBlock.setChanged(!townBlock.getPermissions().toString().equals(town.getPermissions().toString())); - } else { + } else { // This is a brand new PlotGroup, register it. newGroup = new PlotGroup(UUID.randomUUID(), plotGroupName, town); - TownyUniverse.getInstance().registerGroup(newGroup); newGroup.setPermissions(townBlock.getPermissions()); newGroup.setTrustedResidents(townBlock.getTrustedResidents()); newGroup.setPermissionOverrides(townBlock.getPermissionOverrides()); + + BukkitTools.ifCancelledThenThrow(new PlotGroupCreatedEvent(newGroup, townBlock, player)); + + TownyUniverse.getInstance().registerGroup(newGroup); } // Add group to townblock, this also adds the townblock to the group. @@ -1378,9 +1392,11 @@ public void parsePlotGroupDelete(TownBlock townBlock, Player player, Town town) Confirmation.runOnAccept(()-> { String name = group.getName(); - town.removePlotGroup(group); - TownyUniverse.getInstance().getDataSource().removePlotGroup(group); - TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_deleted", name)); + if (!BukkitTools.isEventCancelled(new PlotGroupDeletedEvent(group, player, PlotGroupDeletedEvent.Cause.DELETED))) { + town.removePlotGroup(group); + TownyUniverse.getInstance().getDataSource().removePlotGroup(group); + TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_deleted", name)); + } }).sendTo(player); } @@ -1483,7 +1499,7 @@ public void parsePlotGroupRemove(TownBlock townBlock, Player player, Town town) townBlock.save(); TownyMessaging.sendMsg(player, Translatable.of("msg_plot_was_removed_from_group_x", townBlock.getX(), townBlock.getZ(), name)); - if (group.getTownBlocks().isEmpty()) { + if (group.getTownBlocks().isEmpty() && !BukkitTools.isEventCancelled(new PlotGroupDeletedEvent(group, player, PlotGroupDeletedEvent.Cause.NO_TOWNBLOCKS))) { town.removePlotGroup(group); TownyUniverse.getInstance().getDataSource().removePlotGroup(group); TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_empty_deleted", name)); diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupAddEvent.java b/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupAddEvent.java new file mode 100644 index 0000000000..cedd504103 --- /dev/null +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupAddEvent.java @@ -0,0 +1,50 @@ +package com.palmergames.bukkit.towny.event.plot.group; + +import com.palmergames.bukkit.towny.event.CancellableTownyEvent; +import com.palmergames.bukkit.towny.object.PlotGroup; +import com.palmergames.bukkit.towny.object.TownBlock; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * Called when a townblock is added into a plot group + */ +public class PlotGroupAddEvent extends CancellableTownyEvent { + private static final HandlerList HANDLER_LIST = new HandlerList(); + private final PlotGroup plotGroup; + private final TownBlock townBlock; + private final Player player; + + public PlotGroupAddEvent(final PlotGroup group, final TownBlock townBlock, final Player player) { + this.plotGroup = group; + this.townBlock = townBlock; + this.player = player; + } + + @NotNull + public PlotGroup getPlotGroup() { + return plotGroup; + } + + @NotNull + public TownBlock getTownBlock() { + return townBlock; + } + + @NotNull + public Player getPlayer() { + return player; + } + + @NotNull + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } +} diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupCreatedEvent.java b/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupCreatedEvent.java new file mode 100644 index 0000000000..3f9734a0ed --- /dev/null +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupCreatedEvent.java @@ -0,0 +1,24 @@ +package com.palmergames.bukkit.towny.event.plot.group; + +import com.palmergames.bukkit.towny.object.PlotGroup; +import com.palmergames.bukkit.towny.object.TownBlock; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +/** + * Called when a plot group is created. + */ +public class PlotGroupCreatedEvent extends PlotGroupAddEvent { + public PlotGroupCreatedEvent(PlotGroup group, TownBlock townBlock, Player player) { + super(group, townBlock, player); + } + + /** + * @return The initial townblock that this plot group is being created with. + */ + @Override + @NotNull + public TownBlock getTownBlock() { + return super.getTownBlock(); + } +} diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupDeletedEvent.java b/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupDeletedEvent.java new file mode 100644 index 0000000000..9ef62fa78e --- /dev/null +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/event/plot/group/PlotGroupDeletedEvent.java @@ -0,0 +1,67 @@ +package com.palmergames.bukkit.towny.event.plot.group; + +import com.palmergames.bukkit.towny.event.CancellableTownyEvent; +import com.palmergames.bukkit.towny.object.PlotGroup; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class PlotGroupDeletedEvent extends CancellableTownyEvent { + private static final HandlerList HANDLER_LIST = new HandlerList(); + private final PlotGroup plotGroup; + private final Player player; + private final Cause deletionCause; + + public PlotGroupDeletedEvent(@NotNull PlotGroup group, @Nullable Player player, @NotNull Cause deletionCause) { + this.plotGroup = group; + this.player = player; + this.deletionCause = deletionCause; + } + + /** + * @return The plot group that is being deleted. + */ + @NotNull + public PlotGroup getPlotGroup() { + return plotGroup; + } + + /** + * @return The player associated with the deletion, if applicable. + */ + @Nullable + public Player getPlayer() { + return player; + } + + public Cause getDeletionCause() { + return deletionCause; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public enum Cause { + UNKNOWN, + /** + * The plot group was deleted by a player via the /plot group delete command. + */ + DELETED, + /** + * The plot group was deleted because all of its townblocks were removed. + */ + NO_TOWNBLOCKS, + /** + * The plot group was deleted because the town it was in being deleted/ruined. + */ + TOWN_DELETED, + } +} 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 9e596fb1dc..0b41795ad0 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.plot.group.PlotGroupDeletedEvent; import com.palmergames.bukkit.towny.event.town.TownPreReclaimEvent; import com.palmergames.bukkit.towny.event.town.TownReclaimedEvent; import com.palmergames.bukkit.towny.event.town.TownRuinedEvent; @@ -124,7 +125,8 @@ public static void putTownIntoRuinedState(Town town) { // Unregister the now empty plotgroups. if (town.getPlotGroups() != null) for (PlotGroup group : new ArrayList<>(town.getPlotGroups())) - TownyUniverse.getInstance().getDataSource().removePlotGroup(group); + if (!BukkitTools.isEventCancelled(new PlotGroupDeletedEvent(group, null, PlotGroupDeletedEvent.Cause.TOWN_DELETED))) + TownyUniverse.getInstance().getDataSource().removePlotGroup(group); // Check if Town has more residents than it should be allowed (if it were the capital of a nation.) if (TownySettings.getMaxResidentsPerTown() > 0)