From 847fe95ece5dd3e8fe2403e469f045aa286f72e1 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Fri, 12 Aug 2022 20:41:59 +0200 Subject: [PATCH 1/5] feat: add PlotMoveEvent and PostPlotMoveEvent --- .../com/plotsquared/core/command/Move.java | 14 +++++- .../core/events/PlotMoveEvent.java | 45 +++++++++++++++++++ .../core/events/post/PostPlotMoveEvent.java | 33 ++++++++++++++ .../core/util/EventDispatcher.java | 12 +++++ Core/src/main/resources/lang/messages_en.json | 1 + 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java create mode 100644 Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java diff --git a/Core/src/main/java/com/plotsquared/core/command/Move.java b/Core/src/main/java/com/plotsquared/core/command/Move.java index 161d129913..b093be8a61 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Move.java +++ b/Core/src/main/java/com/plotsquared/core/command/Move.java @@ -20,12 +20,15 @@ import com.google.inject.Inject; import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.events.Result; import com.plotsquared.core.location.Location; import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.plot.PlotId; import com.plotsquared.core.plot.world.PlotAreaManager; +import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; import net.kyori.adventure.text.Component; @@ -43,10 +46,12 @@ public class Move extends SubCommand { private final PlotAreaManager plotAreaManager; + private final EventDispatcher eventDispatcher; @Inject - public Move(final @NonNull PlotAreaManager plotAreaManager) { + public Move(final @NonNull PlotAreaManager plotAreaManager, final @NonNull EventDispatcher eventDispatcher) { this.plotAreaManager = plotAreaManager; + this.eventDispatcher = eventDispatcher; } @Override @@ -97,7 +102,13 @@ public CompletableFuture execute( return CompletableFuture.completedFuture(false); } + if (this.eventDispatcher.callMove(player, plot1, plot2).getEventResult() == Result.DENY) { + player.sendMessage(TranslatableCaption.of("move.event_cancelled")); + return CompletableFuture.completedFuture(false); + } + // Set strings here as the plot objects are mutable (the PlotID changes after being moved). + PlotId oldPlotId = plot1.getId().copy(); String p1 = plot1.toString(); String p2 = plot2.toString(); @@ -111,6 +122,7 @@ public CompletableFuture execute( .tag("target", Tag.inserting(Component.text(p2))) .build() ); + this.eventDispatcher.callPostMove(player, oldPlotId, plot1); return true; } else { player.sendMessage(TranslatableCaption.of("move.requires_unowned")); diff --git a/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java new file mode 100644 index 0000000000..338a41f7a0 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java @@ -0,0 +1,45 @@ +package com.plotsquared.core.events; + +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; + +import java.util.Objects; + +/** + * Called when a {@link PlotPlayer} attempts to move a {@link Plot} to another {@link Plot}. + * The Event-Result {@link Result#FORCE} does have no effect on the outcome. Only supported results are {@link Result#DENY} and + * {@link Result#ACCEPT}. + *
+ *
    + *
  • {@link #getPlotPlayer()} is the initiator of the move action (most likely the command executor)
  • + *
  • {@link #getPlot()} is the plot to be moved
  • + *
  • {@link #destination()} is the plot, where the plot will be moved to.
  • + *
+ * + * @since TODO + */ +public class PlotMoveEvent extends PlotPlayerEvent implements CancellablePlotEvent { + + private final Plot destination; + private Result result = Result.ACCEPT; + + public PlotMoveEvent(final PlotPlayer initiator, final Plot plot, final Plot destination) { + super(initiator, plot); + this.destination = destination; + } + + public Plot destination() { + return destination; + } + + @Override + public Result getEventResult() { + return result; + } + + @Override + public void setEventResult(Result eventResult) { + this.result = Objects.requireNonNull(eventResult); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java b/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java new file mode 100644 index 0000000000..c9c9cf55e0 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java @@ -0,0 +1,33 @@ +package com.plotsquared.core.events.post; + +import com.plotsquared.core.events.PlotEvent; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotId; + +/** + * Called after a plot move was performed and succeeded. + * + * @see com.plotsquared.core.events.PlotMoveEvent + * @since TODO + */ +public class PostPlotMoveEvent extends PlotEvent { + + private final PlotPlayer initiator; + private final PlotId oldPlot; + + public PostPlotMoveEvent(final PlotPlayer initiator, final PlotId oldPlot, final Plot plot) { + super(plot); + this.initiator = initiator; + this.oldPlot = oldPlot; + } + + public PlotPlayer initiator() { + return initiator; + } + + public PlotId oldPlot() { + return oldPlot; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index e20957b28b..a406b4da2f 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -45,6 +45,7 @@ import com.plotsquared.core.events.PlotFlagAddEvent; import com.plotsquared.core.events.PlotFlagRemoveEvent; import com.plotsquared.core.events.PlotMergeEvent; +import com.plotsquared.core.events.PlotMoveEvent; import com.plotsquared.core.events.PlotRateEvent; import com.plotsquared.core.events.PlotUnlinkEvent; import com.plotsquared.core.events.RemoveRoadEntityEvent; @@ -55,6 +56,7 @@ import com.plotsquared.core.events.post.PostPlotClearEvent; import com.plotsquared.core.events.post.PostPlotDeleteEvent; import com.plotsquared.core.events.post.PostPlotMergeEvent; +import com.plotsquared.core.events.post.PostPlotMoveEvent; import com.plotsquared.core.events.post.PostPlotUnlinkEvent; import com.plotsquared.core.listener.PlayerBlockEventType; import com.plotsquared.core.location.Direction; @@ -336,6 +338,16 @@ public void callPostPlayerBuyPlot(PlotPlayer player, OfflinePlotPlayer previo eventBus.post(new PostPlayerBuyPlotEvent(player, previousOwner, plot, price)); } + public PlotMoveEvent callMove(PlotPlayer initiator, Plot from, Plot destination) { + PlotMoveEvent event = new PlotMoveEvent(initiator, from, destination); + callEvent(event); + return event; + } + + public void callPostMove(PlotPlayer initiator, PlotId old, Plot destination) { + callEvent(new PostPlotMoveEvent(initiator, old, destination)); + } + public void doJoinTask(final PlotPlayer player) { if (player == null) { return; //possible future warning message to figure out where we are retrieving null diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index 5ea33d6a6f..ac2742ad7a 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -6,6 +6,7 @@ "move.move_merged": "Merged plots may not be moved. Please unmerge the plot before performing the move.", "move.copy_success": "Successfully copied plots -> ", "move.requires_unowned": "The location specified is already occupied.", + "move.event_cancelled": "Move was cancelled by an external plugin", "debug.requires_unmerged": "The plot cannot be merged.", "debug.debug_header": " Debug Information\n", "debug.debug_section": ">> ", From f2a50741356bdf2e850f4ec4711f4a039e2e7e3a Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Fri, 12 Aug 2022 20:47:16 +0200 Subject: [PATCH 2/5] chore: license headers and documentation --- .../core/events/PlotMoveEvent.java | 23 ++++++++++++++++ .../core/events/post/PostPlotMoveEvent.java | 26 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java index 338a41f7a0..c0a62c8c07 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * Copyright (C) IntellectualSites team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.plotsquared.core.events; import com.plotsquared.core.player.PlotPlayer; @@ -28,10 +46,15 @@ public PlotMoveEvent(final PlotPlayer initiator, final Plot plot, final Plot this.destination = destination; } + /** + * @return The destination for the plot to be moved to. + * @since TODO + */ public Plot destination() { return destination; } + @Override public Result getEventResult() { return result; diff --git a/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java b/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java index c9c9cf55e0..1e1067760d 100644 --- a/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * Copyright (C) IntellectualSites team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.plotsquared.core.events.post; import com.plotsquared.core.events.PlotEvent; @@ -22,10 +40,18 @@ public PostPlotMoveEvent(final PlotPlayer initiator, final PlotId oldPlot, fi this.oldPlot = oldPlot; } + /** + * @return The player who initiated the plot move. + * @since TODO + */ public PlotPlayer initiator() { return initiator; } + /** + * @return The id of the old plot location. + * @since TODO + */ public PlotId oldPlot() { return oldPlot; } From 49afd0dbe3734bfe83fc18537265b930c99099d1 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Sun, 11 Aug 2024 20:05:10 +0200 Subject: [PATCH 3/5] chore: properly implement events --- .../com/plotsquared/core/command/Move.java | 30 +++++++++++-------- .../core/events/PlotMoveEvent.java | 29 ++++++++++++++++-- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/Move.java b/Core/src/main/java/com/plotsquared/core/command/Move.java index b093be8a61..ff9c5be99b 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Move.java +++ b/Core/src/main/java/com/plotsquared/core/command/Move.java @@ -20,6 +20,7 @@ import com.google.inject.Inject; import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.events.PlotMoveEvent; import com.plotsquared.core.events.Result; import com.plotsquared.core.location.Location; import com.plotsquared.core.permissions.Permission; @@ -73,46 +74,49 @@ public CompletableFuture execute( boolean override = false; if (args.length == 2 && args[1].equalsIgnoreCase("-f")) { args = new String[]{args[0]}; - override = true; + override = player.hasPermission(Permission.PERMISSION_ADMIN); // Only allow force with admin permission } if (args.length != 1) { sendUsage(player); return CompletableFuture.completedFuture(false); } PlotArea area = this.plotAreaManager.getPlotAreaByString(args[0]); - Plot plot2; + Plot tmpTargetPlot; if (area == null) { - plot2 = Plot.getPlotFromString(player, args[0], true); - if (plot2 == null) { + tmpTargetPlot = Plot.getPlotFromString(player, args[0], true); + if (tmpTargetPlot == null) { return CompletableFuture.completedFuture(false); } } else { - plot2 = area.getPlotAbs(plot1.getId()); + tmpTargetPlot = area.getPlotAbs(plot1.getId()); } - if (plot1.equals(plot2)) { + final PlotMoveEvent moveEvent = this.eventDispatcher.callMove(player, plot1, tmpTargetPlot); + final Plot targetPlot = moveEvent.destination(); + + if (plot1.equals(targetPlot)) { player.sendMessage(TranslatableCaption.of("invalid.origin_cant_be_target")); return CompletableFuture.completedFuture(false); } - if (!plot1.getArea().isCompatible(plot2.getArea()) && (!override || !player.hasPermission(Permission.PERMISSION_ADMIN))) { + if (!plot1.getArea().isCompatible(targetPlot.getArea()) && (!override || moveEvent.getEventResult() != Result.FORCE)) { player.sendMessage(TranslatableCaption.of("errors.plotworld_incompatible")); return CompletableFuture.completedFuture(false); } - if (plot1.isMerged() || plot2.isMerged()) { + if (plot1.isMerged() || targetPlot.isMerged()) { player.sendMessage(TranslatableCaption.of("move.move_merged")); return CompletableFuture.completedFuture(false); } - if (this.eventDispatcher.callMove(player, plot1, plot2).getEventResult() == Result.DENY) { + if (moveEvent.getEventResult() == Result.DENY) { player.sendMessage(TranslatableCaption.of("move.event_cancelled")); return CompletableFuture.completedFuture(false); } // Set strings here as the plot objects are mutable (the PlotID changes after being moved). - PlotId oldPlotId = plot1.getId().copy(); + PlotId oldPlotId = PlotId.of(plot1.getId().getX(), plot1.getId().getY()); String p1 = plot1.toString(); - String p2 = plot2.toString(); + String p2 = targetPlot.toString(); - return plot1.getPlotModificationManager().move(plot2, player, () -> { + return plot1.getPlotModificationManager().move(targetPlot, player, () -> { }, false).thenApply(result -> { if (result) { player.sendMessage( @@ -122,7 +126,7 @@ public CompletableFuture execute( .tag("target", Tag.inserting(Component.text(p2))) .build() ); - this.eventDispatcher.callPostMove(player, oldPlotId, plot1); + this.eventDispatcher.callPostMove(player, oldPlotId, targetPlot); return true; } else { player.sendMessage(TranslatableCaption.of("move.requires_unowned")); diff --git a/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java index c0a62c8c07..002f8388c1 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java @@ -20,13 +20,14 @@ import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotId; +import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Objects; /** * Called when a {@link PlotPlayer} attempts to move a {@link Plot} to another {@link Plot}. - * The Event-Result {@link Result#FORCE} does have no effect on the outcome. Only supported results are {@link Result#DENY} and - * {@link Result#ACCEPT}. + * *
*
    *
  • {@link #getPlotPlayer()} is the initiator of the move action (most likely the command executor)
  • @@ -38,7 +39,7 @@ */ public class PlotMoveEvent extends PlotPlayerEvent implements CancellablePlotEvent { - private final Plot destination; + private Plot destination; private Result result = Result.ACCEPT; public PlotMoveEvent(final PlotPlayer initiator, final Plot plot, final Plot destination) { @@ -54,6 +55,28 @@ public Plot destination() { return destination; } + /** + * Set the new {@link Plot} to where the plot should be moved to. + * + * @param destination The plot representing the new location. + * @since TODO + */ + public void setDestination(@NonNull final Plot destination) { + this.destination = Objects.requireNonNull(destination); + } + + /** + * Set the new destination based off their X and Y coordinates. Calls {@link #setDestination(Plot)} while using the + * {@link com.plotsquared.core.plot.PlotArea} provided by the current {@link #destination()}. + *

    + * Note: the coordinates are not minecraft world coordinates, but the underlying {@link PlotId}s coordinates. + * + * @param x The X coordinate of the {@link PlotId} + * @param y The Y coordinate of the {@link PlotId} + */ + public void setDestination(final int x, final int y) { + this.destination = Objects.requireNonNull(this.destination.getArea()).getPlot(PlotId.of(x, y)); + } @Override public Result getEventResult() { From c4da114e26ba83635e2b725cad74280479e51617 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Sun, 11 Aug 2024 21:17:17 +0200 Subject: [PATCH 4/5] chore: add swap events, cleanup code --- .../com/plotsquared/core/command/Move.java | 17 ++- .../com/plotsquared/core/command/Swap.java | 44 ++++--- .../core/events/PlotMoveEvent.java | 44 +++++-- .../core/events/PlotSwapEvent.java | 107 ++++++++++++++++++ .../core/events/post/PostPlotMoveEvent.java | 16 +-- .../core/events/post/PostPlotSwapEvent.java | 65 +++++++++++ .../core/util/EventDispatcher.java | 12 ++ Core/src/main/resources/lang/messages_en.json | 1 + 8 files changed, 265 insertions(+), 41 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java create mode 100644 Core/src/main/java/com/plotsquared/core/events/post/PostPlotSwapEvent.java diff --git a/Core/src/main/java/com/plotsquared/core/command/Move.java b/Core/src/main/java/com/plotsquared/core/command/Move.java index ff9c5be99b..0a4029889a 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Move.java +++ b/Core/src/main/java/com/plotsquared/core/command/Move.java @@ -92,12 +92,22 @@ public CompletableFuture execute( } final PlotMoveEvent moveEvent = this.eventDispatcher.callMove(player, plot1, tmpTargetPlot); final Plot targetPlot = moveEvent.destination(); + if (!override) { + override = moveEvent.getEventResult() == Result.FORCE; + } + + if (moveEvent.getEventResult() == Result.DENY) { + if (moveEvent.sendErrorMessage()) { + player.sendMessage(TranslatableCaption.of("move.event_cancelled")); + } + return CompletableFuture.completedFuture(false); + } if (plot1.equals(targetPlot)) { player.sendMessage(TranslatableCaption.of("invalid.origin_cant_be_target")); return CompletableFuture.completedFuture(false); } - if (!plot1.getArea().isCompatible(targetPlot.getArea()) && (!override || moveEvent.getEventResult() != Result.FORCE)) { + if (!plot1.getArea().isCompatible(targetPlot.getArea()) && !override) { player.sendMessage(TranslatableCaption.of("errors.plotworld_incompatible")); return CompletableFuture.completedFuture(false); } @@ -106,11 +116,6 @@ public CompletableFuture execute( return CompletableFuture.completedFuture(false); } - if (moveEvent.getEventResult() == Result.DENY) { - player.sendMessage(TranslatableCaption.of("move.event_cancelled")); - return CompletableFuture.completedFuture(false); - } - // Set strings here as the plot objects are mutable (the PlotID changes after being moved). PlotId oldPlotId = PlotId.of(plot1.getId().getX(), plot1.getId().getY()); String p1 = plot1.toString(); diff --git a/Core/src/main/java/com/plotsquared/core/command/Swap.java b/Core/src/main/java/com/plotsquared/core/command/Swap.java index e56def26f3..332a6975a4 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Swap.java +++ b/Core/src/main/java/com/plotsquared/core/command/Swap.java @@ -18,11 +18,15 @@ */ package com.plotsquared.core.command; +import com.google.inject.Inject; import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.events.PlotSwapEvent; +import com.plotsquared.core.events.Result; import com.plotsquared.core.location.Location; import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; import net.kyori.adventure.text.Component; @@ -38,6 +42,9 @@ requiredType = RequiredType.PLAYER) public class Swap extends SubCommand { + @Inject + private EventDispatcher eventDispatcher; + @Override public CompletableFuture execute( PlotPlayer player, String[] args, @@ -45,41 +52,49 @@ public CompletableFuture execute( RunnableVal2 whenDone ) { Location location = player.getLocation(); - Plot plot1 = location.getPlotAbs(); - if (plot1 == null) { + Plot plot = location.getPlotAbs(); + if (plot == null) { player.sendMessage(TranslatableCaption.of("errors.not_in_plot")); return CompletableFuture.completedFuture(false); } - if (!plot1.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN)) { - player.sendMessage(TranslatableCaption.of("permission.no_plot_perms")); - return CompletableFuture.completedFuture(false); - } if (args.length != 1) { sendUsage(player); return CompletableFuture.completedFuture(false); } - Plot plot2 = Plot.getPlotFromString(player, args[0], true); - if (plot2 == null) { + final Plot plotArg = Plot.getPlotFromString(player, args[0], true); + if (plotArg == null) { + return CompletableFuture.completedFuture(false); + } + final PlotSwapEvent event = this.eventDispatcher.callSwap(player, plot, plotArg); + if (event.getEventResult() == Result.DENY) { + if (event.sendErrorMessage()) { + player.sendMessage(TranslatableCaption.of("swap.event_cancelled")); + } + return CompletableFuture.completedFuture(false); + } + if (!plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN) && event.getEventResult() != Result.FORCE) { + player.sendMessage(TranslatableCaption.of("permission.no_plot_perms")); return CompletableFuture.completedFuture(false); } - if (plot1.equals(plot2)) { + final Plot target = event.target(); + if (plot.equals(target)) { player.sendMessage(TranslatableCaption.of("invalid.origin_cant_be_target")); return CompletableFuture.completedFuture(false); } - if (!plot1.getArea().isCompatible(plot2.getArea())) { + if (!plot.getArea().isCompatible(target.getArea())) { player.sendMessage(TranslatableCaption.of("errors.plotworld_incompatible")); return CompletableFuture.completedFuture(false); } - if (plot1.isMerged() || plot2.isMerged()) { + if (plot.isMerged() || target.isMerged()) { player.sendMessage(TranslatableCaption.of("swap.swap_merged")); return CompletableFuture.completedFuture(false); } // Set strings here as the plot objects are mutable (the PlotID changes after being moved). - String p1 = plot1.toString(); - String p2 = plot2.toString(); + String p1 = plot.toString(); + String p2 = target.toString(); - return plot1.getPlotModificationManager().move(plot2, player, () -> { + return plot.getPlotModificationManager().move(target, player, () -> { }, true).thenApply(result -> { if (result) { player.sendMessage( @@ -89,6 +104,7 @@ public CompletableFuture execute( .tag("target", Tag.inserting(Component.text(p2))) .build() ); + this.eventDispatcher.callPostSwap(player, plot, target); return true; } else { player.sendMessage(TranslatableCaption.of("swap.swap_overlap")); diff --git a/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java index 002f8388c1..40a52860c6 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java @@ -35,11 +35,13 @@ *

  • {@link #destination()} is the plot, where the plot will be moved to.
  • *
* + * @see com.plotsquared.core.command.Move * @since TODO */ public class PlotMoveEvent extends PlotPlayerEvent implements CancellablePlotEvent { private Plot destination; + private boolean sendErrorMessage = true; private Result result = Result.ACCEPT; public PlotMoveEvent(final PlotPlayer initiator, final Plot plot, final Plot destination) { @@ -47,14 +49,6 @@ public PlotMoveEvent(final PlotPlayer initiator, final Plot plot, final Plot this.destination = destination; } - /** - * @return The destination for the plot to be moved to. - * @since TODO - */ - public Plot destination() { - return destination; - } - /** * Set the new {@link Plot} to where the plot should be moved to. * @@ -73,16 +67,50 @@ public void setDestination(@NonNull final Plot destination) { * * @param x The X coordinate of the {@link PlotId} * @param y The Y coordinate of the {@link PlotId} + * @since TODO */ public void setDestination(final int x, final int y) { this.destination = Objects.requireNonNull(this.destination.getArea()).getPlot(PlotId.of(x, y)); } + /** + * Set whether to send a generic message to the user ({@code Move was cancelled by an external plugin}). If set to {@code + * false}, make sure to send a message to the player yourself to avoid confusion. + * + * @param sendErrorMessage {@code true} if PlotSquared should send a generic error message to the player. + * @since TODO + */ + public void setSendErrorMessage(final boolean sendErrorMessage) { + this.sendErrorMessage = sendErrorMessage; + } + + /** + * @return The destination for the plot to be moved to. + * @since TODO + */ + public Plot destination() { + return destination; + } + + /** + * @return {@code true} if PlotSquared should send a generic error message to the player. + * @since TODO + */ + public boolean sendErrorMessage() { + return sendErrorMessage; + } + + /** + * {@inheritDoc} + */ @Override public Result getEventResult() { return result; } + /** + * {@inheritDoc} + */ @Override public void setEventResult(Result eventResult) { this.result = Objects.requireNonNull(eventResult); diff --git a/Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java new file mode 100644 index 0000000000..3ce56c4937 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java @@ -0,0 +1,107 @@ +package com.plotsquared.core.events; + +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotId; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + +/** + * Called when a player swaps {@link #getPlot() their Plot} with {@link #target() another Plot}. + * + * @see com.plotsquared.core.command.Swap + * @since TODO + */ +public class PlotSwapEvent extends PlotPlayerEvent implements CancellablePlotEvent { + + private Plot target; + private boolean sendErrorMessage = true; + private Result result = Result.ACCEPT; + + public PlotSwapEvent(final PlotPlayer plotPlayer, final Plot plot, final Plot target) { + super(plotPlayer, plot); + this.target = target; + } + + /** + * Set the plot which should be swapped with {@link #getPlot()}. + * + * @param target The target swapping plot. + * @since TODO + */ + public void setTarget(@NonNull final Plot target) { + this.target = Objects.requireNonNull(target); + } + + /** + * Set the new destination based off their X and Y coordinates. Calls {@link #setTarget(Plot)} while using the + * {@link com.plotsquared.core.plot.PlotArea} provided by the current {@link #target()}. + *

+ * Note: the coordinates are not minecraft world coordinates, but the underlying {@link PlotId}s coordinates. + * + * @param x The X coordinate of the {@link PlotId} + * @param y The Y coordinate of the {@link PlotId} + * @since TODO + */ + public void setTarget(final int x, final int y) { + this.target = Objects.requireNonNull(this.target.getArea()).getPlot(PlotId.of(x, y)); + } + + /** + * Set whether to send a generic message to the user ({@code Swap was cancelled by an external plugin}). If set to {@code + * false}, make sure to send a message to the player yourself to avoid confusion. + * + * @param sendErrorMessage {@code true} if PlotSquared should send a generic error message to the player. + * @since TODO + */ + public void setSendErrorMessage(final boolean sendErrorMessage) { + this.sendErrorMessage = sendErrorMessage; + } + + /** + * The target plot to swap with. + * + * @return The plot. + * @since TODO + */ + public Plot target() { + return target; + } + + /** + * @return {@code true} if PlotSquared should send a generic error message to the player. + * @since TODO + */ + public boolean sendErrorMessage() { + return sendErrorMessage; + } + + /** + * The plot issuing the swap with {@link #target()}. + * + * @return The plot. + */ + @Override + public Plot getPlot() { + return super.getPlot(); // delegate for overriding the documentation + } + + /** + * {@inheritDoc} + */ + @Override + public @Nullable Result getEventResult() { + return this.result; + } + + /** + * {@inheritDoc} + */ + @Override + public void setEventResult(@Nullable final Result eventResult) { + this.result = eventResult; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java b/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java index 1e1067760d..bc525ce612 100644 --- a/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java @@ -18,7 +18,7 @@ */ package com.plotsquared.core.events.post; -import com.plotsquared.core.events.PlotEvent; +import com.plotsquared.core.events.PlotPlayerEvent; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotId; @@ -29,25 +29,15 @@ * @see com.plotsquared.core.events.PlotMoveEvent * @since TODO */ -public class PostPlotMoveEvent extends PlotEvent { +public class PostPlotMoveEvent extends PlotPlayerEvent { - private final PlotPlayer initiator; private final PlotId oldPlot; public PostPlotMoveEvent(final PlotPlayer initiator, final PlotId oldPlot, final Plot plot) { - super(plot); - this.initiator = initiator; + super(initiator, plot); this.oldPlot = oldPlot; } - /** - * @return The player who initiated the plot move. - * @since TODO - */ - public PlotPlayer initiator() { - return initiator; - } - /** * @return The id of the old plot location. * @since TODO diff --git a/Core/src/main/java/com/plotsquared/core/events/post/PostPlotSwapEvent.java b/Core/src/main/java/com/plotsquared/core/events/post/PostPlotSwapEvent.java new file mode 100644 index 0000000000..9783bbf0d2 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/post/PostPlotSwapEvent.java @@ -0,0 +1,65 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * Copyright (C) IntellectualSites team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.events.post; + +import com.plotsquared.core.events.PlotPlayerEvent; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; + +/** + * Called after a plot swap was performed and succeeded. + *

+ * Note: {@link Plot#getId()} of the {@link #target() target Plot} will be the {@link com.plotsquared.core.plot.PlotId} + * of the {@link #getPlot() issuing plot} at this event stage (as the swap already happened). If you need the PlotId of the + * origin plot (where the player was standing on when issuing the command, e.g.) before the swap, use the {@link #target()} + * Plot, as the IDs in the plot objects were mutated. (The target plot is now the present origin plot) + * + * @see com.plotsquared.core.events.PlotSwapEvent + * @since TODO + */ +public class PostPlotSwapEvent extends PlotPlayerEvent { + + private final Plot target; + + public PostPlotSwapEvent(final PlotPlayer initiator, final Plot plot, final Plot target) { + super(initiator, plot); + this.target = target; + } + + /** + * The plot that initiated the plot swap. + * + * @return The plot. + */ + @Override + public Plot getPlot() { + return super.getPlot(); // delegate for overriding the documentation + } + + /** + * The plot that was swapped with {@link #getPlot()}. (The command argument) + * + * @return The plot. + * @since TODO + */ + public Plot target() { + return target; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index a406b4da2f..e5a8d4b106 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -47,6 +47,7 @@ import com.plotsquared.core.events.PlotMergeEvent; import com.plotsquared.core.events.PlotMoveEvent; import com.plotsquared.core.events.PlotRateEvent; +import com.plotsquared.core.events.PlotSwapEvent; import com.plotsquared.core.events.PlotUnlinkEvent; import com.plotsquared.core.events.RemoveRoadEntityEvent; import com.plotsquared.core.events.TeleportCause; @@ -57,6 +58,7 @@ import com.plotsquared.core.events.post.PostPlotDeleteEvent; import com.plotsquared.core.events.post.PostPlotMergeEvent; import com.plotsquared.core.events.post.PostPlotMoveEvent; +import com.plotsquared.core.events.post.PostPlotSwapEvent; import com.plotsquared.core.events.post.PostPlotUnlinkEvent; import com.plotsquared.core.listener.PlayerBlockEventType; import com.plotsquared.core.location.Direction; @@ -348,6 +350,16 @@ public void callPostMove(PlotPlayer initiator, PlotId old, Plot destination) callEvent(new PostPlotMoveEvent(initiator, old, destination)); } + public PlotSwapEvent callSwap(PlotPlayer initiator, Plot plot, Plot target) { + PlotSwapEvent event = new PlotSwapEvent(initiator, plot, target); + callEvent(event); + return event; + } + + public void callPostSwap(PlotPlayer initiator, Plot plot, Plot target) { + callEvent(new PostPlotSwapEvent(initiator, plot, target)); + } + public void doJoinTask(final PlotPlayer player) { if (player == null) { return; //possible future warning message to figure out where we are retrieving null diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index ac2742ad7a..73682c91eb 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -66,6 +66,7 @@ "swap.swap_overlap": "The proposed areas are not allowed to overlap.", "swap.swap_success": "Successfully swapped plots -> ", "swap.swap_merged": "Merged plots may not be swapped. Please unmerge the plots before performing the swap.", + "swap.event_cancelled": "Swap was cancelled by an external plugin", "swap.progress_region1_copy": "Current region 1 copy progress: %", "swap.progress_region2_copy": "Current region 2 copy progress: %", "swap.progress_region1_paste": "Current region 1 paste progress: %", From ef288b777f27e460389015b511f9b4053d69004f Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Sun, 11 Aug 2024 21:24:22 +0200 Subject: [PATCH 5/5] chore: add missing license header --- .../plotsquared/core/events/PlotSwapEvent.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java index 3ce56c4937..17d284c4b0 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * Copyright (C) IntellectualSites team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.plotsquared.core.events; import com.plotsquared.core.player.PlotPlayer;