Skip to content

Commit

Permalink
chore: add swap events, cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreSchwang committed Aug 11, 2024
1 parent 49afd0d commit c4da114
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 41 deletions.
17 changes: 11 additions & 6 deletions Core/src/main/java/com/plotsquared/core/command/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,22 @@ public CompletableFuture<Boolean> 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);
}
Expand All @@ -106,11 +116,6 @@ public CompletableFuture<Boolean> 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();
Expand Down
44 changes: 30 additions & 14 deletions Core/src/main/java/com/plotsquared/core/command/Swap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,48 +42,59 @@
requiredType = RequiredType.PLAYER)
public class Swap extends SubCommand {

@Inject
private EventDispatcher eventDispatcher;

@Override
public CompletableFuture<Boolean> execute(
PlotPlayer<?> player, String[] args,
RunnableVal3<Command, Runnable, Runnable> confirm,
RunnableVal2<Command, CommandResult> 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(
Expand All @@ -89,6 +104,7 @@ public CompletableFuture<Boolean> 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"));
Expand Down
44 changes: 36 additions & 8 deletions Core/src/main/java/com/plotsquared/core/events/PlotMoveEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,20 @@
* <li>{@link #destination()} is the plot, where the plot will be moved to.</li>
* </ul>
*
* @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) {
super(initiator, 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.
*
Expand All @@ -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);
Expand Down
107 changes: 107 additions & 0 deletions Core/src/main/java/com/plotsquared/core/events/PlotSwapEvent.java
Original file line number Diff line number Diff line change
@@ -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()}.
* <p>
* <b>Note:</b> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Loading

0 comments on commit c4da114

Please sign in to comment.