refactor: Move teleport queue into seperate class

This commit is contained in:
Ben Woo 2023-03-28 11:32:04 +08:00
parent 2f834ebfd6
commit 985f2e8823
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
5 changed files with 63 additions and 35 deletions

View File

@ -362,35 +362,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
return this.saveMVConfig() && worldManagerProvider.get().saveWorldsConfig();
}
//TODO: REMOVE THIS STATIC CRAP - START
private static final Map<String, String> teleportQueue = new HashMap<String, String>();
/**
* 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
// For testing purposes only //

View File

@ -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.MVCoreConfigProvider;
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<MVPermissions> mvPermsProvider;
private final SafeTTeleporter safeTTeleporter;
private final Server server;
private final TeleportQueue teleportQueue;
private final Map<String, String> playerWorld = new ConcurrentHashMap<String, String>();
@ -62,7 +65,8 @@ public class MVPlayerListener implements InjectableListener {
PermissionTools permissionTools,
Provider<MVPermissions> mvPermsProvider,
SafeTTeleporter safeTTeleporter,
Server server
Server server,
TeleportQueue teleportQueue
) {
this.plugin = plugin;
this.configProvider = configProvider;
@ -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<String> 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 '"

View File

@ -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()) {

View File

@ -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<String, String> 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<String> popFromQueue(String playerName) {
if (teleportQueue.containsKey(playerName)) {
String teleportee = teleportQueue.get(playerName);
teleportQueue.remove(playerName);
return Optional.of(teleportee);
}
return Optional.empty();
}
}

View File

@ -21,6 +21,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
@ -65,6 +66,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))