From 2a299efdbf2bcb72498cb4d94eb8c00bdfadeab6 Mon Sep 17 00:00:00 2001 From: Lukas <113579672+byPixelTV@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:41:11 +0200 Subject: [PATCH] Added some testing for proxy resource packs, but I could not get it to work with my current knowledge, and it just crashes the server when using ProtocolLib on default fallback and throws an error when not using ProtocolLib --- .../de/bypixeltv/redivelocity/RediVelocity.kt | 5 +- .../listeners/PostLoginListener.kt | 2 +- .../listeners/ResourcePackListeners.kt | 62 +++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/de/bypixeltv/redivelocity/listeners/ResourcePackListeners.kt diff --git a/src/main/kotlin/de/bypixeltv/redivelocity/RediVelocity.kt b/src/main/kotlin/de/bypixeltv/redivelocity/RediVelocity.kt index daf4235..daf5ce5 100644 --- a/src/main/kotlin/de/bypixeltv/redivelocity/RediVelocity.kt +++ b/src/main/kotlin/de/bypixeltv/redivelocity/RediVelocity.kt @@ -8,10 +8,7 @@ import com.velocitypowered.api.plugin.Plugin import com.velocitypowered.api.proxy.ProxyServer import de.bypixeltv.redivelocity.commands.RediVelocityCommand import de.bypixeltv.redivelocity.config.ConfigLoader -import de.bypixeltv.redivelocity.listeners.DisconnectListener -import de.bypixeltv.redivelocity.listeners.PostLoginListener -import de.bypixeltv.redivelocity.listeners.ProxyPingListener -import de.bypixeltv.redivelocity.listeners.ServerSwitchListener +import de.bypixeltv.redivelocity.listeners.* import de.bypixeltv.redivelocity.managers.RedisController import de.bypixeltv.redivelocity.managers.UpdateManager import de.bypixeltv.redivelocity.utils.ProxyIdGenerator diff --git a/src/main/kotlin/de/bypixeltv/redivelocity/listeners/PostLoginListener.kt b/src/main/kotlin/de/bypixeltv/redivelocity/listeners/PostLoginListener.kt index cb8d8e9..29d023f 100644 --- a/src/main/kotlin/de/bypixeltv/redivelocity/listeners/PostLoginListener.kt +++ b/src/main/kotlin/de/bypixeltv/redivelocity/listeners/PostLoginListener.kt @@ -9,7 +9,7 @@ import de.bypixeltv.redivelocity.config.Config import de.bypixeltv.redivelocity.managers.RedisController import net.kyori.adventure.text.minimessage.MiniMessage -class PostLoginListener @Inject constructor(private val rediVelocity: RediVelocity, private val redisController: RedisController, private val config: Config, private val proxyId: String, private val proxy: ProxyServer) { +class PostLoginListener @Inject constructor(private val rediVelocity: RediVelocity, private val redisController: RedisController, private val config: Config, private val proxyId: String, private val proxy: ProxyServer) { private val miniMessage = MiniMessage.miniMessage() diff --git a/src/main/kotlin/de/bypixeltv/redivelocity/listeners/ResourcePackListeners.kt b/src/main/kotlin/de/bypixeltv/redivelocity/listeners/ResourcePackListeners.kt new file mode 100644 index 0000000..ef7ce73 --- /dev/null +++ b/src/main/kotlin/de/bypixeltv/redivelocity/listeners/ResourcePackListeners.kt @@ -0,0 +1,62 @@ +package de.bypixeltv.redivelocity.listeners + +import com.google.inject.Inject +import com.velocitypowered.api.event.EventTask +import com.velocitypowered.api.event.Subscribe +import com.velocitypowered.api.event.player.configuration.PlayerFinishConfigurationEvent +import com.velocitypowered.api.proxy.Player +import com.velocitypowered.api.proxy.ProxyServer +import com.velocitypowered.api.proxy.player.ResourcePackInfo +import de.bypixeltv.redivelocity.config.Config +import net.kyori.adventure.resource.ResourcePackCallback +import net.kyori.adventure.text.minimessage.MiniMessage +import java.util.concurrent.CompletableFuture + +class ResourcePackListeners @Inject constructor(private val config: Config, private val proxy: ProxyServer) { + + private val miniMessage = MiniMessage.miniMessage() + + private fun createPackRequest(player: Player, callback: ResourcePackCallback): ResourcePackInfo { + return proxy.createResourcePackBuilder(config.resourcepackUrl) + .setId(player.uniqueId) + .setPrompt(miniMessage.deserialize(config.resourcepackMessage)) + .build() + } + + @Subscribe + fun onConfigurationFinish(e: PlayerFinishConfigurationEvent) : EventTask { + println("Entered configuration finish event") + + // store a new countdown latch for this player + val userId = e.player.uniqueId + val future = CompletableFuture() + + // assemble the resource pack and + val pack = createPackRequest( + e.player, + callback = ResourcePackCallback.onTerminal( + { _, _ -> + // decrease the latch as it was successful + future.complete(true) + + // inform about successful load + println("Successfully finished resource pack download of player $userId.") + }, + { _, _ -> + // remove the latch as the operation failed + future.complete(false) + + // inform about failure + println("Failed resource pack download of player $userId.") + }, + ), + ) + + // send resource pack request + e.player.sendResourcePacks(pack) + println("Sending resource pack download request of player $userId.") + + // await the processing + return EventTask.resumeWhenComplete(future) + } +} \ No newline at end of file