From db28aaa1195bacb2522bd1ea2ed01e7b5fb79d03 Mon Sep 17 00:00:00 2001 From: jimchen5209 Date: Sun, 25 Aug 2024 19:39:53 +0800 Subject: [PATCH] fix: appliedResourcePacks in api is not ordered --- src/main/kotlin/one/oktw/galaxy/proxy/Main.kt | 13 ++++- .../proxy/resourcepack/ResourcePackHelper.kt | 51 ++++++++++--------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/one/oktw/galaxy/proxy/Main.kt b/src/main/kotlin/one/oktw/galaxy/proxy/Main.kt index 2d6b869..a0be7fa 100644 --- a/src/main/kotlin/one/oktw/galaxy/proxy/Main.kt +++ b/src/main/kotlin/one/oktw/galaxy/proxy/Main.kt @@ -2,6 +2,7 @@ package one.oktw.galaxy.proxy import com.google.inject.Inject import com.velocitypowered.api.event.Subscribe +import com.velocitypowered.api.event.connection.DisconnectEvent import com.velocitypowered.api.event.player.KickedFromServerEvent import com.velocitypowered.api.event.player.ServerPostConnectEvent import com.velocitypowered.api.event.player.ServerPreConnectEvent @@ -53,6 +54,7 @@ class Main : CoroutineScope by CoroutineScope(Dispatchers.Default + SupervisorJo private set lateinit var manager: Manager private set + lateinit var resourcePackHelper: ResourcePackHelper @Inject fun init(proxy: ProxyServer, logger: Logger) { @@ -63,6 +65,7 @@ class Main : CoroutineScope by CoroutineScope(Dispatchers.Default + SupervisorJo this.config = ConfigManager() this.kubernetesClient = KubernetesClient() this.redisClient = RedisClient() + this.resourcePackHelper = ResourcePackHelper() manager = Manager(config.redisConfig.URI, config.redisConfig.PubSubPrefix) manager.subscribe(MESSAGE_TOPIC) @@ -113,14 +116,20 @@ class Main : CoroutineScope by CoroutineScope(Dispatchers.Default + SupervisorJo it.result = ServerPreConnectEvent.ServerResult.allowed(lobby) } + // Remove player on disconnect + proxy.eventManager.register(this, DisconnectEvent::class.java) { + this.resourcePackHelper.removePlayer(it.player) + } + + // Update resourcepacks @Suppress("UnstableApiUsage") proxy.eventManager.register(this, ServerPostConnectEvent::class.java) { // TODO: Get Galaxy Type if (it.player.currentServer.get().serverInfo.name == "galaxy-lobby") { - ResourcePackHelper.updatePlayerResourcePacks(it.player, "lobby") + this.resourcePackHelper.updatePlayerResourcePacks(it.player, "lobby") } else { if (it.previousServer?.serverInfo?.name != "galaxy-lobby") return@register - ResourcePackHelper.updatePlayerResourcePacks(it.player, "normal_galaxy") + this.resourcePackHelper.updatePlayerResourcePacks(it.player, "normal_galaxy") } } diff --git a/src/main/kotlin/one/oktw/galaxy/proxy/resourcepack/ResourcePackHelper.kt b/src/main/kotlin/one/oktw/galaxy/proxy/resourcepack/ResourcePackHelper.kt index 917314e..3ce8ee6 100644 --- a/src/main/kotlin/one/oktw/galaxy/proxy/resourcepack/ResourcePackHelper.kt +++ b/src/main/kotlin/one/oktw/galaxy/proxy/resourcepack/ResourcePackHelper.kt @@ -6,30 +6,33 @@ import one.oktw.galaxy.proxy.Main.Companion.main import kotlin.math.max class ResourcePackHelper { - companion object { - fun updatePlayerResourcePacks(player: Player, galaxy: String) { - val targetResourcePacks = main.config.galaxies[galaxy]?.ResourcePacks?.distinct()?.mapNotNull { main.config.resourcePacks[it]?.packInfo() } ?: return - val appliedResourcePacks = player.appliedResourcePacks.toList().filterNotNull() - - var skipFurtherCheck = false - val packsToQueue = mutableListOf() - val packsToRemove = mutableListOf() - - for (index in 0..max(targetResourcePacks.size, appliedResourcePacks.size)) { - if (targetResourcePacks.getOrNull(index)?.hash.contentEquals(appliedResourcePacks.getOrNull(index)?.hash) && !skipFurtherCheck) continue - - skipFurtherCheck = true - if (index < appliedResourcePacks.size) packsToRemove.add(appliedResourcePacks[index]) - if (index < targetResourcePacks.size) packsToQueue.add(targetResourcePacks[index]) - } - - main.logger.info(appliedResourcePacks.map { it.url }.toString()) - main.logger.info(appliedResourcePacks.map { it.id }.toString()) - main.logger.info(targetResourcePacks.map { it.url }.toString()) - main.logger.info(targetResourcePacks.map { it.id }.toString()) - main.logger.info("Removing {} packs, Sending {} packs", packsToRemove.size, packsToQueue.size) - packsToRemove.forEach { pack -> player.removeResourcePacks(pack) } - packsToQueue.forEach { pack -> player.sendResourcePacks(pack) } + private val appliedPacks: MutableMap> = mutableMapOf() + + fun updatePlayerResourcePacks(player: Player, galaxy: String) { + val targetResourcePacks = main.config.galaxies[galaxy]?.ResourcePacks?.distinct()?.mapNotNull { main.config.resourcePacks[it] } ?: return + val appliedResourcePacks = this.appliedPacks.getOrPut(player) { listOf() } + + var skipFurtherCheck = false + val packsToQueue = mutableListOf() + val packsToRemove = mutableListOf() + + for (index in 0..max(targetResourcePacks.size, appliedResourcePacks.size)) { + if (targetResourcePacks.getOrNull(index)?.hash.contentEquals(appliedResourcePacks.getOrNull(index)?.hash) && !skipFurtherCheck) continue + + skipFurtherCheck = true + if (index < appliedResourcePacks.size) packsToRemove.add(appliedResourcePacks[index].packInfo()) + if (index < targetResourcePacks.size) packsToQueue.add(targetResourcePacks[index].packInfo()) } + + main.logger.debug(appliedResourcePacks.map { it.packInfo().id }.toString()) + main.logger.debug(targetResourcePacks.map { it.packInfo().id }.toString()) + main.logger.info("Removing {} packs, Sending {} packs", packsToRemove.size, packsToQueue.size) + packsToRemove.forEach { pack -> player.removeResourcePacks(pack) } + packsToQueue.forEach { pack -> player.sendResourcePacks(pack) } + appliedPacks[player] = appliedResourcePacks.toList() + } + + fun removePlayer(player: Player) { + appliedPacks.remove(player) } }