Skip to content

Commit

Permalink
feat(shulker-proxy-agent): expose player teleporting in API (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln authored Aug 25, 2024
1 parent a8e2e70 commit 111ca37
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ interface ProxyInterface {
fun prepareNetworkAdminsPermissions(playerIds: List<UUID>)

fun teleportPlayerOnServer(
playerName: String,
playerId: UUID,
serverName: String,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -12,15 +13,15 @@ class RedisPubSubAdapter(private val jedisPool: JedisPool) : PubSubAdapter {
}

override fun teleportPlayerOnServer(
playerId: String,
playerId: UUID,
serverName: String,
) {
this.jedisPool.resource.use { jedis ->
jedis.publish("shulker:teleport", "$playerId:$serverName")
}
}

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(
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class ShulkerProxyAPIImpl(private val agent: ShulkerProxyAgentCommon) : ShulkerP

override fun getServersByTag(tag: String): Set<String> = this.agent.serverDirectoryService.getServersByTag(tag)

override fun teleportPlayerOnServer(
playerId: UUID,
serverName: String,
) =
this.agent.proxyInterface.teleportPlayerOnServer(playerId, serverName)

override fun getPlayerPosition(playerId: UUID): Optional<PlayerPosition> =
this.agent.cache.getPlayerPosition(
playerId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerPosition> {
val playerPosition =
agent.cache.getPlayerIdFromName(playerName)
.flatMap { playerId -> agent.cache.getPlayerPosition(playerId) }
): Optional<Pair<UUID, PlayerPosition>> {
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()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.shulkermc.proxyagent.api;

import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
Expand All @@ -16,6 +15,7 @@ public abstract class ShulkerProxyAPI {

abstract public @NotNull Set<String> getServersByTag(@NotNull String tag);

abstract public void teleportPlayerOnServer(@NotNull UUID playerId, @NotNull String serverName);
abstract public @NotNull Optional<PlayerPosition> getPlayerPosition(@NotNull UUID playerId);
abstract public boolean isPlayerConnected(@NotNull UUID playerId);
abstract public int countOnlinePlayers();
Expand Down

0 comments on commit 111ca37

Please sign in to comment.