chore/feat: migrate to LocationTransformer to resolve unnecessary chunk loads

This commit is contained in:
Pierre Maurice Schwang 2023-10-03 16:12:10 +02:00
parent d4ba0f414e
commit 9d2c632ca8
3 changed files with 45 additions and 37 deletions

View File

@ -21,10 +21,7 @@ package com.plotsquared.core.events;
import com.plotsquared.core.location.Location; import com.plotsquared.core.location.Location;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import org.jetbrains.annotations.NotNull; import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Objects;
import java.util.function.Consumer;
/** /**
* Called when a player teleports to a plot * Called when a player teleports to a plot
@ -34,7 +31,7 @@ public class PlayerTeleportToPlotEvent extends PlotPlayerEvent implements Cancel
private final TeleportCause cause; private final TeleportCause cause;
private Result eventResult; private Result eventResult;
private final Location from; private final Location from;
private Location location; private LocationTransformer locationTransformer;
/** /**
@ -42,15 +39,13 @@ public class PlayerTeleportToPlotEvent extends PlotPlayerEvent implements Cancel
* *
* @param player That was teleported * @param player That was teleported
* @param from The origin location, from where the teleport was triggered (players location most likely) * @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 plot Plot to which the player was teleported
* @param cause Why the teleport is being completed * @param cause Why the teleport is being completed
* @since 6.1.0 * @since 6.1.0
*/ */
public PlayerTeleportToPlotEvent(PlotPlayer<?> player, Location from, Location location, Plot plot, TeleportCause cause) { public PlayerTeleportToPlotEvent(PlotPlayer<?> player, Location from, Plot plot, TeleportCause cause) {
super(player, plot); super(player, plot);
this.from = from; this.from = from;
this.location = location;
this.cause = cause; this.cause = cause;
} }
@ -75,26 +70,24 @@ public class PlayerTeleportToPlotEvent extends PlotPlayerEvent implements Cancel
} }
/** /**
* Get the current location where the player will be teleported to. * Gets the currently applied {@link LocationTransformer} or null, if none was set
* Defaults to {@link Plot#getHome(Consumer)} or {@link Plot#getDefaultHome(boolean, Consumer)}
* *
* @return Location * @return LocationTransformer
* @since TODO * @since TODO
*/ */
public Location getLocation() { public @Nullable LocationTransformer getLocationTransformer() {
return this.location; return this.locationTransformer;
} }
/** /**
* Set the new location where the player should be teleported to. * Sets the {@link LocationTransformer} to mutate the location where the player will be teleported to.
* <b>Must</b> be not-null. If your intention is to cancel this teleport, see {@link #setEventResult(Result)} * May be {@code null}, if any previous set transformations should be discarded.
* @param location The new location *
* @param locationTransformer The new transformer
* @since TODO * @since TODO
*/ */
public void setLocation(@NotNull Location location) { public void setLocationTransformer(@Nullable LocationTransformer locationTransformer) {
Objects.requireNonNull(location, "PlayerTeleportToPlotEvent#location may never be null. " + this.locationTransformer = locationTransformer;
"If you intend to cancel the teleport, use PlayerTeleportToPlotEvent#setEventResult");
this.location = location;
} }
@Override @Override
@ -107,4 +100,17 @@ public class PlayerTeleportToPlotEvent extends PlotPlayerEvent implements Cancel
this.eventResult = e; this.eventResult = e;
} }
public interface LocationTransformer {
/**
* Transforms an input location {@code origin} to a new location
*
* @param origin The origin location which should be transformed
* @return The transformed location
* @since TODO
*/
Location transform(Location origin);
}
} }

View File

@ -2615,8 +2615,8 @@ public class Plot {
*/ */
public void teleportPlayer(final PlotPlayer<?> player, TeleportCause cause, Consumer<Boolean> resultConsumer) { public void teleportPlayer(final PlotPlayer<?> player, TeleportCause cause, Consumer<Boolean> resultConsumer) {
Plot plot = this.getBasePlot(false); Plot plot = this.getBasePlot(false);
final Consumer<Location> locationConsumer = calculatedLocation -> {
PlayerTeleportToPlotEvent event = this.eventDispatcher.callTeleport(player, player.getLocation(), calculatedLocation, plot, cause); PlayerTeleportToPlotEvent event = this.eventDispatcher.callTeleport(player, player.getLocation(), plot, cause);
if (event.getEventResult() == Result.DENY) { if (event.getEventResult() == Result.DENY) {
player.sendMessage( player.sendMessage(
TranslatableCaption.of("events.event_denied"), TranslatableCaption.of("events.event_denied"),
@ -2625,7 +2625,10 @@ public class Plot {
resultConsumer.accept(false); resultConsumer.accept(false);
return; return;
} }
Location location = event.getLocation();
final Consumer<Location> locationConsumer = calculatedLocation -> {
Location location = event.getLocationTransformer() == null ?
calculatedLocation : event.getLocationTransformer().transform(calculatedLocation);
if (Settings.Teleport.DELAY == 0 || player.hasPermission("plots.teleport.delay.bypass")) { if (Settings.Teleport.DELAY == 0 || player.hasPermission("plots.teleport.delay.bypass")) {
player.sendMessage(TranslatableCaption.of("teleport.teleported_to_plot")); player.sendMessage(TranslatableCaption.of("teleport.teleported_to_plot"));
player.teleport(location, cause); player.teleport(location, cause);

View File

@ -158,9 +158,8 @@ public class EventDispatcher {
return event; return event;
} }
public PlayerTeleportToPlotEvent callTeleport(PlotPlayer<?> player, Location from, Location location, Plot plot, public PlayerTeleportToPlotEvent callTeleport(PlotPlayer<?> player, Location from, Plot plot, TeleportCause cause) {
TeleportCause cause) { PlayerTeleportToPlotEvent event = new PlayerTeleportToPlotEvent(player, from, plot, cause);
PlayerTeleportToPlotEvent event = new PlayerTeleportToPlotEvent(player, from, location, plot, cause);
callEvent(event); callEvent(event);
return event; return event;
} }