From b1afd8f7860cbbe19f8386e00e6c068249ec8ec4 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sat, 23 Nov 2024 21:24:14 +0800 Subject: [PATCH] Implement MVTeleportDestinationEvent event --- ...t.java => MVTeleportDestinationEvent.java} | 25 ++++++------------- .../teleportation/AsyncSafetyTeleporter.java | 19 ++++++++++++-- .../core/teleportation/TeleportResult.java | 1 + 3 files changed, 26 insertions(+), 19 deletions(-) rename src/main/java/org/mvplugins/multiverse/core/event/{MVTeleportEvent.java => MVTeleportDestinationEvent.java} (76%) diff --git a/src/main/java/org/mvplugins/multiverse/core/event/MVTeleportEvent.java b/src/main/java/org/mvplugins/multiverse/core/event/MVTeleportDestinationEvent.java similarity index 76% rename from src/main/java/org/mvplugins/multiverse/core/event/MVTeleportEvent.java rename to src/main/java/org/mvplugins/multiverse/core/event/MVTeleportDestinationEvent.java index 196ddb59..46a6c272 100644 --- a/src/main/java/org/mvplugins/multiverse/core/event/MVTeleportEvent.java +++ b/src/main/java/org/mvplugins/multiverse/core/event/MVTeleportDestinationEvent.java @@ -9,6 +9,7 @@ package org.mvplugins.multiverse.core.event; import org.bukkit.Location; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; @@ -18,20 +19,18 @@ import org.mvplugins.multiverse.core.destination.DestinationInstance; import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter; /** - * Event that gets called when a player use the /mvtp command. + * Event that gets called when a player teleports to a {@link DestinationInstance} with {@link AsyncSafetyTeleporter}. */ -public class MVTeleportEvent extends Event implements Cancellable { - private Player teleportee; - private CommandSender teleporter; - private DestinationInstance dest; - private boolean useSafeTeleport; +public class MVTeleportDestinationEvent extends Event implements Cancellable { + private final Entity teleportee; + private final CommandSender teleporter; + private final DestinationInstance dest; private boolean isCancelled; - public MVTeleportEvent(DestinationInstance dest, Player teleportee, CommandSender teleporter, boolean safeTeleport) { + public MVTeleportDestinationEvent(DestinationInstance dest, Entity teleportee, CommandSender teleporter) { this.teleportee = teleportee; this.teleporter = teleporter; this.dest = dest; - this.useSafeTeleport = safeTeleport; } private static final HandlerList HANDLERS = new HandlerList(); @@ -57,7 +56,7 @@ public class MVTeleportEvent extends Event implements Cancellable { * * @return The player who will be teleported by this event. */ - public Player getTeleportee() { + public Entity getTeleportee() { return this.teleportee; } @@ -88,14 +87,6 @@ public class MVTeleportEvent extends Event implements Cancellable { return this.dest; } - /** - * Looks if this {@link MVTeleportEvent} is using the {@link AsyncSafetyTeleporter}. - * @return True if this {@link MVTeleportEvent} is using the {@link AsyncSafetyTeleporter}. - */ - public boolean isUsingSafeTTeleporter() { - return useSafeTeleport; - } - @Override public boolean isCancelled() { return this.isCancelled; diff --git a/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java b/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java index 9c9de783..4cfcb7f8 100644 --- a/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java +++ b/src/main/java/org/mvplugins/multiverse/core/teleportation/AsyncSafetyTeleporter.java @@ -9,12 +9,14 @@ import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jvnet.hk2.annotations.Service; import org.mvplugins.multiverse.core.api.BlockSafety; import org.mvplugins.multiverse.core.destination.DestinationInstance; +import org.mvplugins.multiverse.core.event.MVTeleportDestinationEvent; import org.mvplugins.multiverse.core.utils.result.Async; import org.mvplugins.multiverse.core.utils.result.AsyncAttempt; import org.mvplugins.multiverse.core.utils.result.Attempt; @@ -26,13 +28,16 @@ import org.mvplugins.multiverse.core.utils.result.Attempt; public class AsyncSafetyTeleporter { private final BlockSafety blockSafety; private final TeleportQueue teleportQueue; + private final PluginManager pluginManager; @Inject AsyncSafetyTeleporter( - BlockSafety blockSafety, - TeleportQueue teleportQueue) { + @NotNull BlockSafety blockSafety, + @NotNull TeleportQueue teleportQueue, + @NotNull PluginManager pluginManager) { this.blockSafety = blockSafety; this.teleportQueue = teleportQueue; + this.pluginManager = pluginManager; } public AsyncAttempt teleportSafely( @@ -57,6 +62,11 @@ public class AsyncSafetyTeleporter { if (destination == null) { return AsyncAttempt.failure(TeleportResult.Failure.NULL_DESTINATION); } + MVTeleportDestinationEvent event = new MVTeleportDestinationEvent(destination, teleportee, teleporter); + this.pluginManager.callEvent(event); + if (event.isCancelled()) { + return AsyncAttempt.failure(TeleportResult.Failure.EVENT_CANCELLED); + } return destination.getLocation(teleportee) .map(location -> destination.checkTeleportSafety() ? teleportSafely(teleporter, teleportee, location) @@ -105,6 +115,11 @@ public class AsyncSafetyTeleporter { if (destination == null) { return AsyncAttempt.failure(TeleportResult.Failure.NULL_DESTINATION); } + MVTeleportDestinationEvent event = new MVTeleportDestinationEvent(destination, teleportee, teleporter); + this.pluginManager.callEvent(event); + if (event.isCancelled()) { + return AsyncAttempt.failure(TeleportResult.Failure.EVENT_CANCELLED); + } return destination.getLocation(teleportee) .map(location -> teleport(teleporter, teleportee, location)) .getOrElse(AsyncAttempt.failure(TeleportResult.Failure.NULL_LOCATION)); diff --git a/src/main/java/org/mvplugins/multiverse/core/teleportation/TeleportResult.java b/src/main/java/org/mvplugins/multiverse/core/teleportation/TeleportResult.java index 93b37ff9..ef483599 100644 --- a/src/main/java/org/mvplugins/multiverse/core/teleportation/TeleportResult.java +++ b/src/main/java/org/mvplugins/multiverse/core/teleportation/TeleportResult.java @@ -15,5 +15,6 @@ public class TeleportResult { TELEPORT_FAILED, TELEPORT_FAILED_EXCEPTION, PLAYER_OFFLINE, + EVENT_CANCELLED, } }