Implement MVTeleportDestinationEvent event

This commit is contained in:
Ben Woo 2024-11-23 21:24:14 +08:00
parent 668856dccc
commit b1afd8f786
3 changed files with 26 additions and 19 deletions

View File

@ -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;

View File

@ -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<Void, TeleportResult.Failure> 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));

View File

@ -15,5 +15,6 @@ public class TeleportResult {
TELEPORT_FAILED,
TELEPORT_FAILED_EXCEPTION,
PLAYER_OFFLINE,
EVENT_CANCELLED,
}
}