From 111ca37f1985a2a0aa64f0d613d7736abb2d7d41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Levilain?= Date: Sun, 25 Aug 2024 15:36:03 +0200 Subject: [PATCH] feat(shulker-proxy-agent): expose player teleporting in API (#629) --- .../bungeecord/ProxyInterfaceBungeeCord.kt | 4 ++-- .../io/shulkermc/proxyagent/ProxyInterface.kt | 2 +- .../proxyagent/adapters/pubsub/PubSubAdapter.kt | 6 ++++-- .../adapters/pubsub/RedisPubSubAdapter.kt | 7 ++++--- .../shulkermc/proxyagent/api/ShulkerProxyAPIImpl.kt | 6 ++++++ .../proxyagent/commands/CommandHandlerHelper.kt | 13 +++++++------ .../proxyagent/commands/FindCommandHandler.kt | 2 +- .../proxyagent/commands/TeleportCommandHandler.kt | 8 ++++---- .../handlers/TeleportPlayerOnServerHandler.kt | 5 +++-- .../proxyagent/velocity/ProxyInterfaceVelocity.kt | 4 ++-- .../shulkermc/proxyagent/api/ShulkerProxyAPI.java | 2 +- 11 files changed, 35 insertions(+), 24 deletions(-) diff --git a/packages/shulker-proxy-agent/src/bungeecord/kotlin/io/shulkermc/proxyagent/bungeecord/ProxyInterfaceBungeeCord.kt b/packages/shulker-proxy-agent/src/bungeecord/kotlin/io/shulkermc/proxyagent/bungeecord/ProxyInterfaceBungeeCord.kt index 14a009e7..6d741e8e 100644 --- a/packages/shulker-proxy-agent/src/bungeecord/kotlin/io/shulkermc/proxyagent/bungeecord/ProxyInterfaceBungeeCord.kt +++ b/packages/shulker-proxy-agent/src/bungeecord/kotlin/io/shulkermc/proxyagent/bungeecord/ProxyInterfaceBungeeCord.kt @@ -181,13 +181,13 @@ class ProxyInterfaceBungeeCord( } override fun teleportPlayerOnServer( - playerName: String, + playerId: UUID, serverName: String, ) { val server = this.proxy.getServerInfo(serverName) if (server != null) { - this.proxy.getPlayer(playerName)?.connect(server) + this.proxy.getPlayer(playerId)?.connect(server) } } diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/ProxyInterface.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/ProxyInterface.kt index 4711bdb5..49ad8a90 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/ProxyInterface.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/ProxyInterface.kt @@ -55,7 +55,7 @@ interface ProxyInterface { fun prepareNetworkAdminsPermissions(playerIds: List) fun teleportPlayerOnServer( - playerName: String, + playerId: UUID, serverName: String, ) diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/PubSubAdapter.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/PubSubAdapter.kt index b99efa2e..6c321734 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/PubSubAdapter.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/PubSubAdapter.kt @@ -1,12 +1,14 @@ package io.shulkermc.proxyagent.adapters.pubsub +import java.util.UUID + interface PubSubAdapter { fun teleportPlayerOnServer( - playerId: String, + playerId: UUID, serverName: String, ) - fun onTeleportPlayerOnServer(callback: (playerId: String, serverName: String) -> Unit) + fun onTeleportPlayerOnServer(callback: (playerId: UUID, serverName: String) -> Unit) fun drainProxy(proxyName: String) diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/RedisPubSubAdapter.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/RedisPubSubAdapter.kt index ad9b4b87..1a77ce30 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/RedisPubSubAdapter.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/RedisPubSubAdapter.kt @@ -2,6 +2,7 @@ package io.shulkermc.proxyagent.adapters.pubsub import redis.clients.jedis.JedisPool import redis.clients.jedis.JedisPubSub +import java.util.UUID import java.util.concurrent.Executors class RedisPubSubAdapter(private val jedisPool: JedisPool) : PubSubAdapter { @@ -12,7 +13,7 @@ class RedisPubSubAdapter(private val jedisPool: JedisPool) : PubSubAdapter { } override fun teleportPlayerOnServer( - playerId: String, + playerId: UUID, serverName: String, ) { this.jedisPool.resource.use { jedis -> @@ -20,7 +21,7 @@ class RedisPubSubAdapter(private val jedisPool: JedisPool) : PubSubAdapter { } } - override fun onTeleportPlayerOnServer(callback: (playerId: String, serverName: String) -> Unit) { + override fun onTeleportPlayerOnServer(callback: (playerId: UUID, serverName: String) -> Unit) { this.executor.submit { this.jedisPool.resource.use { jedis -> jedis.subscribe( @@ -30,7 +31,7 @@ class RedisPubSubAdapter(private val jedisPool: JedisPool) : PubSubAdapter { message: String, ) { val (playerId, serverName) = message.split(":") - callback(playerId, serverName) + callback(UUID.fromString(playerId), serverName) } }, "shulker:teleport", diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/api/ShulkerProxyAPIImpl.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/api/ShulkerProxyAPIImpl.kt index 07407010..9b0b8387 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/api/ShulkerProxyAPIImpl.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/api/ShulkerProxyAPIImpl.kt @@ -11,6 +11,12 @@ class ShulkerProxyAPIImpl(private val agent: ShulkerProxyAgentCommon) : ShulkerP override fun getServersByTag(tag: String): Set = this.agent.serverDirectoryService.getServersByTag(tag) + override fun teleportPlayerOnServer( + playerId: UUID, + serverName: String, + ) = + this.agent.proxyInterface.teleportPlayerOnServer(playerId, serverName) + override fun getPlayerPosition(playerId: UUID): Optional = this.agent.cache.getPlayerPosition( playerId, diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/CommandHandlerHelper.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/CommandHandlerHelper.kt index c2859736..a13b2be7 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/CommandHandlerHelper.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/CommandHandlerHelper.kt @@ -6,21 +6,22 @@ import net.kyori.adventure.audience.Audience import net.kyori.adventure.text.Component import net.kyori.adventure.text.format.NamedTextColor import java.util.Optional +import java.util.UUID object CommandHandlerHelper { fun findPlayerOrMessage( agent: ShulkerProxyAgentCommon, source: Audience, playerName: String, - ): Optional { - val playerPosition = - agent.cache.getPlayerIdFromName(playerName) - .flatMap { playerId -> agent.cache.getPlayerPosition(playerId) } + ): Optional> { + val playerId = agent.cache.getPlayerIdFromName(playerName) + val playerPosition = playerId.flatMap { agent.cache.getPlayerPosition(it) } - if (playerPosition.isEmpty) { + if (playerId.isEmpty || playerPosition.isEmpty) { source.sendMessage(Component.text("Player $playerName not found", NamedTextColor.RED)) + return Optional.empty() } - return playerPosition + return Optional.of(Pair(playerId.get(), playerPosition.get())) } } diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/FindCommandHandler.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/FindCommandHandler.kt index ef60347d..391fbc7c 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/FindCommandHandler.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/FindCommandHandler.kt @@ -15,7 +15,7 @@ object FindCommandHandler { source: Audience, playerName: String, ) { - val playerPosition = CommandHandlerHelper.findPlayerOrMessage(agent, source, playerName).getOrNull() ?: return + val (_, playerPosition) = CommandHandlerHelper.findPlayerOrMessage(agent, source, playerName).getOrNull() ?: return source.sendMessage( Component.text( diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/TeleportCommandHandler.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/TeleportCommandHandler.kt index b8de8f84..43bca89d 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/TeleportCommandHandler.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/TeleportCommandHandler.kt @@ -15,10 +15,10 @@ object TeleportCommandHandler { source: Audience, playerName: String, ) { - val playerPosition = CommandHandlerHelper.findPlayerOrMessage(agent, source, playerName).getOrNull() ?: return + val (playerId, playerPosition) = CommandHandlerHelper.findPlayerOrMessage(agent, source, playerName).getOrNull() ?: return val server = playerPosition.serverName - agent.pubSub.teleportPlayerOnServer(playerName, server) + agent.pubSub.teleportPlayerOnServer(playerId, server) source.sendMessage(Component.text("Teleported to server $server", NamedTextColor.GREEN)) } @@ -28,9 +28,9 @@ object TeleportCommandHandler { playerName: String, serverName: String, ) { - CommandHandlerHelper.findPlayerOrMessage(agent, source, playerName).getOrNull() ?: return + val (playerId, _) = CommandHandlerHelper.findPlayerOrMessage(agent, source, playerName).getOrNull() ?: return - agent.pubSub.teleportPlayerOnServer(playerName, serverName) + agent.pubSub.teleportPlayerOnServer(playerId, serverName) source.sendMessage( Component.text("Teleported $playerName to server $serverName", NamedTextColor.GREEN), ) diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/handlers/TeleportPlayerOnServerHandler.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/handlers/TeleportPlayerOnServerHandler.kt index 54d11d55..51126263 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/handlers/TeleportPlayerOnServerHandler.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/handlers/TeleportPlayerOnServerHandler.kt @@ -1,12 +1,13 @@ package io.shulkermc.proxyagent.handlers import io.shulkermc.proxyagent.ShulkerProxyAgentCommon +import java.util.UUID class TeleportPlayerOnServerHandler(private val agent: ShulkerProxyAgentCommon) { fun handle( - playerName: String, + playerId: UUID, serverName: String, ) { - this.agent.proxyInterface.teleportPlayerOnServer(playerName, serverName) + this.agent.proxyInterface.teleportPlayerOnServer(playerId, serverName) } } diff --git a/packages/shulker-proxy-agent/src/velocity/kotlin/io/shulkermc/proxyagent/velocity/ProxyInterfaceVelocity.kt b/packages/shulker-proxy-agent/src/velocity/kotlin/io/shulkermc/proxyagent/velocity/ProxyInterfaceVelocity.kt index f8bfe0db..3c25ed0a 100644 --- a/packages/shulker-proxy-agent/src/velocity/kotlin/io/shulkermc/proxyagent/velocity/ProxyInterfaceVelocity.kt +++ b/packages/shulker-proxy-agent/src/velocity/kotlin/io/shulkermc/proxyagent/velocity/ProxyInterfaceVelocity.kt @@ -154,10 +154,10 @@ class ProxyInterfaceVelocity( } override fun teleportPlayerOnServer( - playerName: String, + playerId: UUID, serverName: String, ) { - this.proxy.getPlayer(playerName).ifPresent { player -> + this.proxy.getPlayer(playerId).ifPresent { player -> this.proxy.getServer(serverName).ifPresent { server -> player.createConnectionRequest(server).fireAndForget() } diff --git a/packages/shulker-proxy-api/src/main/java/io/shulkermc/proxyagent/api/ShulkerProxyAPI.java b/packages/shulker-proxy-api/src/main/java/io/shulkermc/proxyagent/api/ShulkerProxyAPI.java index 3edc1ed2..1061629d 100644 --- a/packages/shulker-proxy-api/src/main/java/io/shulkermc/proxyagent/api/ShulkerProxyAPI.java +++ b/packages/shulker-proxy-api/src/main/java/io/shulkermc/proxyagent/api/ShulkerProxyAPI.java @@ -1,6 +1,5 @@ package io.shulkermc.proxyagent.api; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import java.util.Optional; @@ -16,6 +15,7 @@ public abstract class ShulkerProxyAPI { abstract public @NotNull Set getServersByTag(@NotNull String tag); + abstract public void teleportPlayerOnServer(@NotNull UUID playerId, @NotNull String serverName); abstract public @NotNull Optional getPlayerPosition(@NotNull UUID playerId); abstract public boolean isPlayerConnected(@NotNull UUID playerId); abstract public int countOnlinePlayers();