Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PlotMoveEvent and PostPlotMoveEvent #3769

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Core/src/main/java/com/plotsquared/core/command/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.Permissions;
import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3;
Expand All @@ -42,10 +45,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
Expand Down Expand Up @@ -98,7 +103,13 @@ public CompletableFuture<Boolean> 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();

Expand All @@ -110,6 +121,7 @@ public CompletableFuture<Boolean> execute(
Template.of("origin", p1),
Template.of("target", p2)
);
this.eventDispatcher.callPostMove(player, oldPlotId, plot1);
return true;
} else {
player.sendMessage(TranslatableCaption.of("move.requires_unowned"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* 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 <https://www.gnu.org/licenses/>.
*/
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}.
* <br>
* <ul>
* <li>{@link #getPlotPlayer()} is the initiator of the move action (most likely the command executor)</li>
* <li>{@link #getPlot()} is the plot to be moved</li>
* <li>{@link #destination()} is the plot, where the plot will be moved to.</li>
* </ul>
*
* @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;
}

/**
* @return The destination for the plot to be moved to.
* @since TODO
*/
public Plot destination() {
return destination;
}


@Override
public Result getEventResult() {
return result;
}

@Override
public void setEventResult(Result eventResult) {
this.result = Objects.requireNonNull(eventResult);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* 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 <https://www.gnu.org/licenses/>.
*/
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;
}

/**
* @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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
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.TeleportCause;
import com.plotsquared.core.events.post.PostPlayerAutoPlotEvent;
import com.plotsquared.core.events.post.PostPlotChangeOwnerEvent;
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;
Expand Down Expand Up @@ -296,6 +298,16 @@ public PlotDoneEvent callDone(Plot plot) {
return event;
}

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
Expand Down
1 change: 1 addition & 0 deletions Core/src/main/resources/lang/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"move.move_merged": "<prefix><red>Merged plots may not be moved. Please unmerge the plot before performing the move.</red>",
"move.copy_success": "<prefix><dark_aqua>Successfully copied plots</dark_aqua> <gold><origin></gold><dark_aqua> -> </dark_aqua><gold><target></gold>",
"move.requires_unowned": "<prefix><red>The location specified is already occupied.</red>",
"move.event_cancelled": "<prefix><gray>Move was cancelled by an external plugin</gray>",
"debug.requires_unmerged": "<prefix><red>The plot cannot be merged.</red>",
"debug.debug_header": "<prefix> <gold>Debug Information</gold>\n",
"debug.debug_section": "<gray>>></gray> <gold><bold><val></bold></gold>",
Expand Down