From d4ba0f414e85b71146721af07482a2dfd8721177 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Mon, 2 Oct 2023 18:04:39 +0200 Subject: [PATCH] feat: ability to overwrite spawn location for plot teleports --- .../events/PlayerTeleportToPlotEvent.java | 46 ++++++++++++++++--- .../java/com/plotsquared/core/plot/Plot.java | 22 +++++---- .../core/util/EventDispatcher.java | 5 +- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java index 36c64440c..c8936571b 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java @@ -21,28 +21,36 @@ package com.plotsquared.core.events; import com.plotsquared.core.location.Location; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.function.Consumer; /** * Called when a player teleports to a plot */ public class PlayerTeleportToPlotEvent extends PlotPlayerEvent implements CancellablePlotEvent { - private final Location from; private final TeleportCause cause; private Result eventResult; + private final Location from; + private Location location; + /** * PlayerTeleportToPlotEvent: Called when a player teleports to a plot * - * @param player That was teleported - * @param from Start location - * @param plot Plot to which the player was teleported - * @param cause Why the teleport is being completed + * @param player That was teleported + * @param from The origin location, from where the teleport was triggered (players location most likely) + * @param location The initial location where the player will be teleported to + * @param plot Plot to which the player was teleported + * @param cause Why the teleport is being completed * @since 6.1.0 */ - public PlayerTeleportToPlotEvent(PlotPlayer player, Location from, Plot plot, TeleportCause cause) { + public PlayerTeleportToPlotEvent(PlotPlayer player, Location from, Location location, Plot plot, TeleportCause cause) { super(player, plot); this.from = from; + this.location = location; this.cause = cause; } @@ -57,7 +65,8 @@ public class PlayerTeleportToPlotEvent extends PlotPlayerEvent implements Cancel } /** - * Get the from location + * Get the location, from where the teleport was triggered + * (the players current location when executing the home command for example) * * @return Location */ @@ -65,6 +74,29 @@ public class PlayerTeleportToPlotEvent extends PlotPlayerEvent implements Cancel return this.from; } + /** + * Get the current location where the player will be teleported to. + * Defaults to {@link Plot#getHome(Consumer)} or {@link Plot#getDefaultHome(boolean, Consumer)} + * + * @return Location + * @since TODO + */ + public Location getLocation() { + return this.location; + } + + /** + * Set the new location where the player should be teleported to. + * Must be not-null. If your intention is to cancel this teleport, see {@link #setEventResult(Result)} + * @param location The new location + * @since TODO + */ + public void setLocation(@NotNull Location location) { + Objects.requireNonNull(location, "PlayerTeleportToPlotEvent#location may never be null. " + + "If you intend to cancel the teleport, use PlayerTeleportToPlotEvent#setEventResult"); + this.location = location; + } + @Override public Result getEventResult() { return eventResult; diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index 90e339308..9bb040cdd 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -29,6 +29,7 @@ import com.plotsquared.core.configuration.caption.CaptionUtility; import com.plotsquared.core.configuration.caption.StaticCaption; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.database.DBFunc; +import com.plotsquared.core.events.PlayerTeleportToPlotEvent; import com.plotsquared.core.events.Result; import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.generator.ClassicPlotWorld; @@ -2614,16 +2615,17 @@ public class Plot { */ public void teleportPlayer(final PlotPlayer player, TeleportCause cause, Consumer resultConsumer) { Plot plot = this.getBasePlot(false); - Result result = this.eventDispatcher.callTeleport(player, player.getLocation(), plot, cause).getEventResult(); - if (result == Result.DENY) { - player.sendMessage( - TranslatableCaption.of("events.event_denied"), - TagResolver.resolver("value", Tag.inserting(Component.text("Teleport"))) - ); - resultConsumer.accept(false); - return; - } - final Consumer locationConsumer = location -> { + final Consumer locationConsumer = calculatedLocation -> { + PlayerTeleportToPlotEvent event = this.eventDispatcher.callTeleport(player, player.getLocation(), calculatedLocation, plot, cause); + if (event.getEventResult() == Result.DENY) { + player.sendMessage( + TranslatableCaption.of("events.event_denied"), + TagResolver.resolver("value", Tag.inserting(Component.text("Teleport"))) + ); + resultConsumer.accept(false); + return; + } + Location location = event.getLocation(); if (Settings.Teleport.DELAY == 0 || player.hasPermission("plots.teleport.delay.bypass")) { player.sendMessage(TranslatableCaption.of("teleport.teleported_to_plot")); player.teleport(location, cause); 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 4de989601..62133cf97 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -158,8 +158,9 @@ public class EventDispatcher { return event; } - public PlayerTeleportToPlotEvent callTeleport(PlotPlayer player, Location from, Plot plot, TeleportCause cause) { - PlayerTeleportToPlotEvent event = new PlayerTeleportToPlotEvent(player, from, plot, cause); + public PlayerTeleportToPlotEvent callTeleport(PlotPlayer player, Location from, Location location, Plot plot, + TeleportCause cause) { + PlayerTeleportToPlotEvent event = new PlayerTeleportToPlotEvent(player, from, location, plot, cause); callEvent(event); return event; }