Skip to content

Commit c4da114

Browse files
committed
chore: add swap events, cleanup code
1 parent 49afd0d commit c4da114

File tree

8 files changed

+265
-41
lines changed

8 files changed

+265
-41
lines changed

Core/src/main/java/com/plotsquared/core/command/Move.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,22 @@ public CompletableFuture<Boolean> execute(
9292
}
9393
final PlotMoveEvent moveEvent = this.eventDispatcher.callMove(player, plot1, tmpTargetPlot);
9494
final Plot targetPlot = moveEvent.destination();
95+
if (!override) {
96+
override = moveEvent.getEventResult() == Result.FORCE;
97+
}
98+
99+
if (moveEvent.getEventResult() == Result.DENY) {
100+
if (moveEvent.sendErrorMessage()) {
101+
player.sendMessage(TranslatableCaption.of("move.event_cancelled"));
102+
}
103+
return CompletableFuture.completedFuture(false);
104+
}
95105

96106
if (plot1.equals(targetPlot)) {
97107
player.sendMessage(TranslatableCaption.of("invalid.origin_cant_be_target"));
98108
return CompletableFuture.completedFuture(false);
99109
}
100-
if (!plot1.getArea().isCompatible(targetPlot.getArea()) && (!override || moveEvent.getEventResult() != Result.FORCE)) {
110+
if (!plot1.getArea().isCompatible(targetPlot.getArea()) && !override) {
101111
player.sendMessage(TranslatableCaption.of("errors.plotworld_incompatible"));
102112
return CompletableFuture.completedFuture(false);
103113
}
@@ -106,11 +116,6 @@ public CompletableFuture<Boolean> execute(
106116
return CompletableFuture.completedFuture(false);
107117
}
108118

109-
if (moveEvent.getEventResult() == Result.DENY) {
110-
player.sendMessage(TranslatableCaption.of("move.event_cancelled"));
111-
return CompletableFuture.completedFuture(false);
112-
}
113-
114119
// Set strings here as the plot objects are mutable (the PlotID changes after being moved).
115120
PlotId oldPlotId = PlotId.of(plot1.getId().getX(), plot1.getId().getY());
116121
String p1 = plot1.toString();

Core/src/main/java/com/plotsquared/core/command/Swap.java

+30-14
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
*/
1919
package com.plotsquared.core.command;
2020

21+
import com.google.inject.Inject;
2122
import com.plotsquared.core.configuration.caption.TranslatableCaption;
23+
import com.plotsquared.core.events.PlotSwapEvent;
24+
import com.plotsquared.core.events.Result;
2225
import com.plotsquared.core.location.Location;
2326
import com.plotsquared.core.permissions.Permission;
2427
import com.plotsquared.core.player.PlotPlayer;
2528
import com.plotsquared.core.plot.Plot;
29+
import com.plotsquared.core.util.EventDispatcher;
2630
import com.plotsquared.core.util.task.RunnableVal2;
2731
import com.plotsquared.core.util.task.RunnableVal3;
2832
import net.kyori.adventure.text.Component;
@@ -38,48 +42,59 @@
3842
requiredType = RequiredType.PLAYER)
3943
public class Swap extends SubCommand {
4044

45+
@Inject
46+
private EventDispatcher eventDispatcher;
47+
4148
@Override
4249
public CompletableFuture<Boolean> execute(
4350
PlotPlayer<?> player, String[] args,
4451
RunnableVal3<Command, Runnable, Runnable> confirm,
4552
RunnableVal2<Command, CommandResult> whenDone
4653
) {
4754
Location location = player.getLocation();
48-
Plot plot1 = location.getPlotAbs();
49-
if (plot1 == null) {
55+
Plot plot = location.getPlotAbs();
56+
if (plot == null) {
5057
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
5158
return CompletableFuture.completedFuture(false);
5259
}
53-
if (!plot1.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN)) {
54-
player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
55-
return CompletableFuture.completedFuture(false);
56-
}
5760
if (args.length != 1) {
5861
sendUsage(player);
5962
return CompletableFuture.completedFuture(false);
6063
}
61-
Plot plot2 = Plot.getPlotFromString(player, args[0], true);
62-
if (plot2 == null) {
64+
final Plot plotArg = Plot.getPlotFromString(player, args[0], true);
65+
if (plotArg == null) {
66+
return CompletableFuture.completedFuture(false);
67+
}
68+
final PlotSwapEvent event = this.eventDispatcher.callSwap(player, plot, plotArg);
69+
if (event.getEventResult() == Result.DENY) {
70+
if (event.sendErrorMessage()) {
71+
player.sendMessage(TranslatableCaption.of("swap.event_cancelled"));
72+
}
73+
return CompletableFuture.completedFuture(false);
74+
}
75+
if (!plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN) && event.getEventResult() != Result.FORCE) {
76+
player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
6377
return CompletableFuture.completedFuture(false);
6478
}
65-
if (plot1.equals(plot2)) {
79+
final Plot target = event.target();
80+
if (plot.equals(target)) {
6681
player.sendMessage(TranslatableCaption.of("invalid.origin_cant_be_target"));
6782
return CompletableFuture.completedFuture(false);
6883
}
69-
if (!plot1.getArea().isCompatible(plot2.getArea())) {
84+
if (!plot.getArea().isCompatible(target.getArea())) {
7085
player.sendMessage(TranslatableCaption.of("errors.plotworld_incompatible"));
7186
return CompletableFuture.completedFuture(false);
7287
}
73-
if (plot1.isMerged() || plot2.isMerged()) {
88+
if (plot.isMerged() || target.isMerged()) {
7489
player.sendMessage(TranslatableCaption.of("swap.swap_merged"));
7590
return CompletableFuture.completedFuture(false);
7691
}
7792

7893
// Set strings here as the plot objects are mutable (the PlotID changes after being moved).
79-
String p1 = plot1.toString();
80-
String p2 = plot2.toString();
94+
String p1 = plot.toString();
95+
String p2 = target.toString();
8196

82-
return plot1.getPlotModificationManager().move(plot2, player, () -> {
97+
return plot.getPlotModificationManager().move(target, player, () -> {
8398
}, true).thenApply(result -> {
8499
if (result) {
85100
player.sendMessage(
@@ -89,6 +104,7 @@ public CompletableFuture<Boolean> execute(
89104
.tag("target", Tag.inserting(Component.text(p2)))
90105
.build()
91106
);
107+
this.eventDispatcher.callPostSwap(player, plot, target);
92108
return true;
93109
} else {
94110
player.sendMessage(TranslatableCaption.of("swap.swap_overlap"));

Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java

+36-8
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,20 @@
3535
* <li>{@link #destination()} is the plot, where the plot will be moved to.</li>
3636
* </ul>
3737
*
38+
* @see com.plotsquared.core.command.Move
3839
* @since TODO
3940
*/
4041
public class PlotMoveEvent extends PlotPlayerEvent implements CancellablePlotEvent {
4142

4243
private Plot destination;
44+
private boolean sendErrorMessage = true;
4345
private Result result = Result.ACCEPT;
4446

4547
public PlotMoveEvent(final PlotPlayer<?> initiator, final Plot plot, final Plot destination) {
4648
super(initiator, plot);
4749
this.destination = destination;
4850
}
4951

50-
/**
51-
* @return The destination for the plot to be moved to.
52-
* @since TODO
53-
*/
54-
public Plot destination() {
55-
return destination;
56-
}
57-
5852
/**
5953
* Set the new {@link Plot} to where the plot should be moved to.
6054
*
@@ -73,16 +67,50 @@ public void setDestination(@NonNull final Plot destination) {
7367
*
7468
* @param x The X coordinate of the {@link PlotId}
7569
* @param y The Y coordinate of the {@link PlotId}
70+
* @since TODO
7671
*/
7772
public void setDestination(final int x, final int y) {
7873
this.destination = Objects.requireNonNull(this.destination.getArea()).getPlot(PlotId.of(x, y));
7974
}
8075

76+
/**
77+
* Set whether to send a generic message to the user ({@code Move was cancelled by an external plugin}). If set to {@code
78+
* false}, make sure to send a message to the player yourself to avoid confusion.
79+
*
80+
* @param sendErrorMessage {@code true} if PlotSquared should send a generic error message to the player.
81+
* @since TODO
82+
*/
83+
public void setSendErrorMessage(final boolean sendErrorMessage) {
84+
this.sendErrorMessage = sendErrorMessage;
85+
}
86+
87+
/**
88+
* @return The destination for the plot to be moved to.
89+
* @since TODO
90+
*/
91+
public Plot destination() {
92+
return destination;
93+
}
94+
95+
/**
96+
* @return {@code true} if PlotSquared should send a generic error message to the player.
97+
* @since TODO
98+
*/
99+
public boolean sendErrorMessage() {
100+
return sendErrorMessage;
101+
}
102+
103+
/**
104+
* {@inheritDoc}
105+
*/
81106
@Override
82107
public Result getEventResult() {
83108
return result;
84109
}
85110

111+
/**
112+
* {@inheritDoc}
113+
*/
86114
@Override
87115
public void setEventResult(Result eventResult) {
88116
this.result = Objects.requireNonNull(eventResult);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.plotsquared.core.events;
2+
3+
import com.plotsquared.core.player.PlotPlayer;
4+
import com.plotsquared.core.plot.Plot;
5+
import com.plotsquared.core.plot.PlotId;
6+
import org.checkerframework.checker.nullness.qual.NonNull;
7+
import org.checkerframework.checker.nullness.qual.Nullable;
8+
9+
import java.util.Objects;
10+
11+
/**
12+
* Called when a player swaps {@link #getPlot() their Plot} with {@link #target() another Plot}.
13+
*
14+
* @see com.plotsquared.core.command.Swap
15+
* @since TODO
16+
*/
17+
public class PlotSwapEvent extends PlotPlayerEvent implements CancellablePlotEvent {
18+
19+
private Plot target;
20+
private boolean sendErrorMessage = true;
21+
private Result result = Result.ACCEPT;
22+
23+
public PlotSwapEvent(final PlotPlayer<?> plotPlayer, final Plot plot, final Plot target) {
24+
super(plotPlayer, plot);
25+
this.target = target;
26+
}
27+
28+
/**
29+
* Set the plot which should be swapped with {@link #getPlot()}.
30+
*
31+
* @param target The target swapping plot.
32+
* @since TODO
33+
*/
34+
public void setTarget(@NonNull final Plot target) {
35+
this.target = Objects.requireNonNull(target);
36+
}
37+
38+
/**
39+
* Set the new destination based off their X and Y coordinates. Calls {@link #setTarget(Plot)} while using the
40+
* {@link com.plotsquared.core.plot.PlotArea} provided by the current {@link #target()}.
41+
* <p>
42+
* <b>Note:</b> the coordinates are not minecraft world coordinates, but the underlying {@link PlotId}s coordinates.
43+
*
44+
* @param x The X coordinate of the {@link PlotId}
45+
* @param y The Y coordinate of the {@link PlotId}
46+
* @since TODO
47+
*/
48+
public void setTarget(final int x, final int y) {
49+
this.target = Objects.requireNonNull(this.target.getArea()).getPlot(PlotId.of(x, y));
50+
}
51+
52+
/**
53+
* Set whether to send a generic message to the user ({@code Swap was cancelled by an external plugin}). If set to {@code
54+
* false}, make sure to send a message to the player yourself to avoid confusion.
55+
*
56+
* @param sendErrorMessage {@code true} if PlotSquared should send a generic error message to the player.
57+
* @since TODO
58+
*/
59+
public void setSendErrorMessage(final boolean sendErrorMessage) {
60+
this.sendErrorMessage = sendErrorMessage;
61+
}
62+
63+
/**
64+
* The target plot to swap with.
65+
*
66+
* @return The plot.
67+
* @since TODO
68+
*/
69+
public Plot target() {
70+
return target;
71+
}
72+
73+
/**
74+
* @return {@code true} if PlotSquared should send a generic error message to the player.
75+
* @since TODO
76+
*/
77+
public boolean sendErrorMessage() {
78+
return sendErrorMessage;
79+
}
80+
81+
/**
82+
* The plot issuing the swap with {@link #target()}.
83+
*
84+
* @return The plot.
85+
*/
86+
@Override
87+
public Plot getPlot() {
88+
return super.getPlot(); // delegate for overriding the documentation
89+
}
90+
91+
/**
92+
* {@inheritDoc}
93+
*/
94+
@Override
95+
public @Nullable Result getEventResult() {
96+
return this.result;
97+
}
98+
99+
/**
100+
* {@inheritDoc}
101+
*/
102+
@Override
103+
public void setEventResult(@Nullable final Result eventResult) {
104+
this.result = eventResult;
105+
}
106+
107+
}

Core/src/main/java/com/plotsquared/core/events/post/PostPlotMoveEvent.java

+3-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package com.plotsquared.core.events.post;
2020

21-
import com.plotsquared.core.events.PlotEvent;
21+
import com.plotsquared.core.events.PlotPlayerEvent;
2222
import com.plotsquared.core.player.PlotPlayer;
2323
import com.plotsquared.core.plot.Plot;
2424
import com.plotsquared.core.plot.PlotId;
@@ -29,25 +29,15 @@
2929
* @see com.plotsquared.core.events.PlotMoveEvent
3030
* @since TODO
3131
*/
32-
public class PostPlotMoveEvent extends PlotEvent {
32+
public class PostPlotMoveEvent extends PlotPlayerEvent {
3333

34-
private final PlotPlayer<?> initiator;
3534
private final PlotId oldPlot;
3635

3736
public PostPlotMoveEvent(final PlotPlayer<?> initiator, final PlotId oldPlot, final Plot plot) {
38-
super(plot);
39-
this.initiator = initiator;
37+
super(initiator, plot);
4038
this.oldPlot = oldPlot;
4139
}
4240

43-
/**
44-
* @return The player who initiated the plot move.
45-
* @since TODO
46-
*/
47-
public PlotPlayer<?> initiator() {
48-
return initiator;
49-
}
50-
5141
/**
5242
* @return The id of the old plot location.
5343
* @since TODO

0 commit comments

Comments
 (0)