diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index bcc32c3b..c027d0de 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -372,35 +372,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore { && anchorManagerProvider.get().saveAnchors(); } - //TODO: REMOVE THIS STATIC CRAP - START - private static final Map teleportQueue = new HashMap(); - - /** - * This method is used to add a teleportation to the teleportQueue. - * - * @param teleporter The name of the player that initiated the teleportation. - * @param teleportee The name of the player that was teleported. - */ - public static void addPlayerToTeleportQueue(String teleporter, String teleportee) { - Logging.finest("Adding mapping '%s' => '%s' to teleport queue", teleporter, teleportee); - teleportQueue.put(teleportee, teleporter); - } - - /** - * This method is used to find out who is teleporting a player. - * @param playerName The teleported player (the teleportee). - * @return The player that teleported the other one (the teleporter). - */ - public static String getPlayerTeleporter(String playerName) { - if (teleportQueue.containsKey(playerName)) { - String teleportee = teleportQueue.get(playerName); - teleportQueue.remove(playerName); - return teleportee; - } - return null; - } - //TODO: REMOVE THIS STATIC CRAP - END - /** * Gets the best service from this plugin that implements the given contract or has the given implementation. diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 80d26546..e8f68dec 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -8,6 +8,7 @@ package com.onarandombox.MultiverseCore.listeners; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import com.dumptruckman.minecraft.util.Logging; @@ -18,6 +19,7 @@ import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.event.MVRespawnEvent; import com.onarandombox.MultiverseCore.inject.InjectableListener; +import com.onarandombox.MultiverseCore.teleportation.TeleportQueue; import com.onarandombox.MultiverseCore.utils.MVPermissions; import com.onarandombox.MultiverseCore.utils.PermissionTools; import jakarta.inject.Inject; @@ -51,6 +53,7 @@ public class MVPlayerListener implements InjectableListener { private final Provider mvPermsProvider; private final SafeTTeleporter safeTTeleporter; private final Server server; + private final TeleportQueue teleportQueue; private final Map playerWorld = new ConcurrentHashMap(); @@ -62,7 +65,8 @@ public class MVPlayerListener implements InjectableListener { PermissionTools permissionTools, Provider mvPermsProvider, SafeTTeleporter safeTTeleporter, - Server server + Server server, + TeleportQueue teleportQueue ) { this.plugin = plugin; this.config = config; @@ -71,6 +75,7 @@ public class MVPlayerListener implements InjectableListener { this.mvPermsProvider = mvPermsProvider; this.safeTTeleporter = safeTTeleporter; this.server = server; + this.teleportQueue = teleportQueue; } private MVWorldManager getWorldManager() { @@ -185,13 +190,13 @@ public class MVPlayerListener implements InjectableListener { } Player teleportee = event.getPlayer(); CommandSender teleporter = null; - String teleporterName = MultiverseCore.getPlayerTeleporter(teleportee.getName()); - if (teleporterName != null) { + Optional teleporterName = teleportQueue.popFromQueue(teleportee.getName()); + if (teleporterName.isPresent()) { if (teleporterName.equals("CONSOLE")) { Logging.finer("We know the teleporter is the console! Magical!"); teleporter = this.server.getConsoleSender(); } else { - teleporter = this.server.getPlayerExact(teleporterName); + teleporter = this.server.getPlayerExact(teleporterName.get()); } } Logging.finer("Inferred sender '" + teleporter + "' from name '" diff --git a/src/main/java/com/onarandombox/MultiverseCore/teleportation/SimpleSafeTTeleporter.java b/src/main/java/com/onarandombox/MultiverseCore/teleportation/SimpleSafeTTeleporter.java index 4b41fb94..a39aa2e2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/teleportation/SimpleSafeTTeleporter.java +++ b/src/main/java/com/onarandombox/MultiverseCore/teleportation/SimpleSafeTTeleporter.java @@ -37,16 +37,19 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { private final MultiverseCore plugin; private final LocationManipulation locationManipulation; private final BlockSafety blockSafety; + private final TeleportQueue teleportQueue; @Inject public SimpleSafeTTeleporter( MultiverseCore plugin, LocationManipulation locationManipulation, - BlockSafety blockSafety + BlockSafety blockSafety, + TeleportQueue teleportQueue ) { this.plugin = plugin; this.locationManipulation = locationManipulation; this.blockSafety = blockSafety; + this.teleportQueue = teleportQueue; } private static final Vector DEFAULT_VECTOR = new Vector(); @@ -220,7 +223,7 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { return TeleportResult.FAIL_INVALID; } - MultiverseCore.addPlayerToTeleportQueue(teleporter.getIssuer().getName(), teleporteePlayer.getName()); + teleportQueue.addToQueue(teleporter.getIssuer().getName(), teleporteePlayer.getName()); Location safeLoc = destination.getLocation(teleportee); if (destination.getDestination().checkTeleportSafety()) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/teleportation/TeleportQueue.java b/src/main/java/com/onarandombox/MultiverseCore/teleportation/TeleportQueue.java new file mode 100644 index 00000000..dc9e69e1 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/teleportation/TeleportQueue.java @@ -0,0 +1,43 @@ +package com.onarandombox.MultiverseCore.teleportation; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import com.dumptruckman.minecraft.util.Logging; +import org.jvnet.hk2.annotations.Service; + +@Service +public class TeleportQueue { + + private final Map teleportQueue; + + public TeleportQueue() { + teleportQueue = new HashMap<>(); + } + + /** + * This method is used to add a teleportation to the teleportQueue. + * + * @param teleporter The name of the player that initiated the teleportation. + * @param teleportee The name of the player that was teleported. + */ + public void addToQueue(String teleporter, String teleportee) { + Logging.finest("Adding mapping '%s' => '%s' to teleport queue", teleporter, teleportee); + teleportQueue.put(teleportee, teleporter); + } + + /** + * This method is used to find out who is teleporting a player. + * @param playerName The teleported player (the teleportee). + * @return The player that teleported the other one (the teleporter). + */ + public Optional popFromQueue(String playerName) { + if (teleportQueue.containsKey(playerName)) { + String teleportee = teleportQueue.get(playerName); + teleportQueue.remove(playerName); + return Optional.of(teleportee); + } + return Optional.empty(); + } +} diff --git a/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt b/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt index ca2b1ae4..68901a7e 100644 --- a/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt @@ -22,6 +22,7 @@ import com.onarandombox.MultiverseCore.listeners.MVWorldListener import com.onarandombox.MultiverseCore.teleportation.SimpleBlockSafety import com.onarandombox.MultiverseCore.teleportation.SimpleLocationManipulation import com.onarandombox.MultiverseCore.teleportation.SimpleSafeTTeleporter +import com.onarandombox.MultiverseCore.teleportation.TeleportQueue import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator import com.onarandombox.MultiverseCore.world.SimpleMVWorldManager @@ -66,6 +67,11 @@ class InjectionTest : TestWithMockBukkit() { assertNotNull(multiverseCore.getService(SimpleSafeTTeleporter::class.java)) } + @Test + fun `TeleportQueue is available as a service`() { + assertNotNull(multiverseCore.getService(TeleportQueue::class.java)) + } + @Test fun `UnsafeCallWrapper is available as a service`() { assertNotNull(multiverseCore.getService(UnsafeCallWrapper::class.java))