Skip to content

Commit

Permalink
fix: appliedResourcePacks in api is not ordered
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchen5209 committed Aug 25, 2024
1 parent 19c1992 commit 5038ff7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
13 changes: 11 additions & 2 deletions src/main/kotlin/one/oktw/galaxy/proxy/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@ 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()
private val appliedPacks: MutableMap<Player, List<ResourcePackInfo>> = mutableMapOf()

var skipFurtherCheck = false
val packsToQueue = mutableListOf<ResourcePackInfo>()
val packsToRemove = mutableListOf<ResourcePackInfo>()
fun updatePlayerResourcePacks(player: Player, galaxy: String) {
val targetResourcePacks = main.config.galaxies[galaxy]?.ResourcePacks?.distinct()?.mapNotNull { main.config.resourcePacks[it]?.packInfo() } ?: return
val appliedResourcePacks = this.appliedPacks.getOrPut(player) { listOf() }

for (index in 0..max(targetResourcePacks.size, appliedResourcePacks.size)) {
if (targetResourcePacks.getOrNull(index)?.hash.contentEquals(appliedResourcePacks.getOrNull(index)?.hash) && !skipFurtherCheck) continue
var skipFurtherCheck = false
val packsToQueue = mutableListOf<ResourcePackInfo>()
val packsToRemove = mutableListOf<ResourcePackInfo>()

skipFurtherCheck = true
if (index < appliedResourcePacks.size) packsToRemove.add(appliedResourcePacks[index])
if (index < targetResourcePacks.size) packsToQueue.add(targetResourcePacks[index])
}
for (index in 0..max(targetResourcePacks.size, appliedResourcePacks.size)) {
if (targetResourcePacks.getOrNull(index)?.id == appliedResourcePacks.getOrNull(index)?.id && !skipFurtherCheck) continue

packsToRemove.forEach { pack -> player.removeResourcePacks(pack) }
packsToQueue.forEach { pack -> player.sendResourcePacks(pack) }
skipFurtherCheck = true
if (index < appliedResourcePacks.size) packsToRemove.add(appliedResourcePacks[index])
if (index < targetResourcePacks.size) packsToQueue.add(targetResourcePacks[index])
}

packsToRemove.forEach { pack -> player.removeResourcePacks(pack) }
packsToQueue.forEach { pack -> player.sendResourcePacks(pack) }
appliedPacks[player] = targetResourcePacks.toList()
}

fun removePlayer(player: Player) {
appliedPacks.remove(player)
}
}

0 comments on commit 5038ff7

Please sign in to comment.