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 36c64440c2..0b56c1e958 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java @@ -21,21 +21,26 @@ import com.plotsquared.core.location.Location; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.function.UnaryOperator; /** * 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 UnaryOperator locationTransformer; + /** * PlayerTeleportToPlotEvent: Called when a player teleports to a plot * * @param player That was teleported - * @param from Start location + * @param from The origin location, from where the teleport was triggered (players location most likely) * @param plot Plot to which the player was teleported * @param cause Why the teleport is being completed * @since 6.1.0 @@ -57,7 +62,8 @@ public TeleportCause getCause() { } /** - * 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 +71,27 @@ public Location getFrom() { return this.from; } + /** + * Gets the currently applied {@link UnaryOperator transformer} or null, if none was set + * + * @return LocationTransformer + * @since TODO + */ + public @Nullable UnaryOperator getLocationTransformer() { + return this.locationTransformer; + } + + /** + * Sets the {@link UnaryOperator transformer} to mutate the location where the player will be teleported to. + * May be {@code null}, if any previous set transformations should be discarded. + * + * @param locationTransformer The new transformer + * @since TODO + */ + public void setLocationTransformer(@Nullable UnaryOperator locationTransformer) { + this.locationTransformer = locationTransformer; + } + @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 90e3393082..8103b96044 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.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,8 +2615,9 @@ public void teleportPlayer(final PlotPlayer player, Consumer result) */ 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) { + + PlayerTeleportToPlotEvent event = this.eventDispatcher.callTeleport(player, player.getLocation(), plot, cause); + if (event.getEventResult() == Result.DENY) { player.sendMessage( TranslatableCaption.of("events.event_denied"), TagResolver.resolver("value", Tag.inserting(Component.text("Teleport"))) @@ -2623,7 +2625,10 @@ public void teleportPlayer(final PlotPlayer player, TeleportCause cause, Cons resultConsumer.accept(false); return; } - final Consumer locationConsumer = location -> { + + final Consumer locationConsumer = calculatedLocation -> { + Location location = event.getLocationTransformer() == null ? calculatedLocation : + Objects.requireNonNullElse(event.getLocationTransformer().apply(calculatedLocation), calculatedLocation); if (Settings.Teleport.DELAY == 0 || player.hasPermission("plots.teleport.delay.bypass")) { player.sendMessage(TranslatableCaption.of("teleport.teleported_to_plot")); player.teleport(location, cause);