From 391101965bef18a362453f922809bb4c1f7a623d Mon Sep 17 00:00:00 2001 From: Boy Date: Mon, 22 Jul 2024 17:11:40 +0200 Subject: [PATCH] feat: add setting to send pack after login / during login if cached --- .../kotlin/com/mineinabyss/packy/PackyPlugin.kt | 8 ++++++++ .../kotlin/com/mineinabyss/packy/PackyServer.kt | 16 ++++++---------- .../com/mineinabyss/packy/config/PackyConfig.kt | 13 ++++++++++++- .../mineinabyss/packy/listener/PlayerListener.kt | 11 +++++++++++ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt b/src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt index 727c549..9761a57 100644 --- a/src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt +++ b/src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt @@ -12,12 +12,20 @@ import com.mineinabyss.idofront.config.config import com.mineinabyss.idofront.di.DI import com.mineinabyss.idofront.messaging.observeLogger import com.mineinabyss.idofront.nms.PacketListener +import com.mineinabyss.idofront.nms.interceptClientbound +import com.mineinabyss.idofront.nms.interceptServerbound import com.mineinabyss.idofront.plugin.listeners import com.mineinabyss.packy.config.* import com.mineinabyss.packy.listener.PlayerListener import com.mineinabyss.packy.listener.TemplateLoadTriggers import kotlinx.coroutines.Job import kotlinx.serialization.modules.EmptySerializersModule +import net.minecraft.network.Connection +import net.minecraft.network.protocol.Packet +import net.minecraft.network.protocol.common.ClientboundPingPacket +import net.minecraft.network.protocol.ping.ServerboundPingRequestPacket +import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket +import net.minecraft.network.protocol.status.ServerboundStatusRequestPacket import org.bukkit.plugin.java.JavaPlugin import team.unnamed.creative.ResourcePack diff --git a/src/main/kotlin/com/mineinabyss/packy/PackyServer.kt b/src/main/kotlin/com/mineinabyss/packy/PackyServer.kt index d2a5256..f1c6fb4 100644 --- a/src/main/kotlin/com/mineinabyss/packy/PackyServer.kt +++ b/src/main/kotlin/com/mineinabyss/packy/PackyServer.kt @@ -4,7 +4,6 @@ import com.github.shynixn.mccoroutine.bukkit.launch import com.github.shynixn.mccoroutine.bukkit.ticks import com.mineinabyss.geary.papermc.datastore.decode import com.mineinabyss.idofront.events.call -import com.mineinabyss.idofront.messaging.broadcast import com.mineinabyss.idofront.nms.interceptClientbound import com.mineinabyss.idofront.nms.interceptServerbound import com.mineinabyss.idofront.nms.nbt.getOfflinePDC @@ -13,12 +12,7 @@ import com.mineinabyss.packy.components.PackyData import com.mineinabyss.packy.components.packyData import com.mineinabyss.packy.config.packy import com.mineinabyss.packy.helpers.TemplateIds -import io.netty.channel.Channel -import io.netty.channel.ChannelDuplexHandler -import io.netty.channel.ChannelHandlerContext -import io.netty.channel.ChannelPromise import io.papermc.paper.adventure.PaperAdventure -import io.papermc.paper.network.ChannelInitializeListenerHolder import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import net.kyori.adventure.resource.ResourcePackRequest @@ -26,13 +20,11 @@ import net.kyori.adventure.text.Component import net.kyori.adventure.text.format.NamedTextColor import net.minecraft.network.Connection import net.minecraft.network.protocol.Packet -import net.minecraft.network.protocol.common.ClientboundDisconnectPacket -import net.minecraft.network.protocol.common.ClientboundResourcePackPopPacket +import net.minecraft.network.protocol.common.ClientboundPingPacket import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket import net.minecraft.network.protocol.common.ServerboundResourcePackPacket import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket -import org.bukkit.NamespacedKey -import org.bukkit.craftbukkit.entity.CraftPlayer +import net.minecraft.network.protocol.ping.ServerboundPingRequestPacket import org.bukkit.entity.Player import org.bukkit.event.player.PlayerResourcePackStatusEvent import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter @@ -69,12 +61,16 @@ object PackyServer { private val cachedPackyData = mutableMapOf() fun registerConfigPacketHandler() { + if (!packy.config.dispatch.sendPreJoin) return packy.plugin.interceptClientbound { packet: Packet<*>, connection: Connection -> if (packet !is ClientboundFinishConfigurationPacket) return@interceptClientbound packet val player = connection.player?.bukkitEntity ?: return@interceptClientbound packet if (player.resourcePackStatus != null) return@interceptClientbound packet val packyData = player.getOfflinePDC()?.decode() ?: return@interceptClientbound packet + if (packy.config.dispatch.sendPreJoinOnCached && PackyGenerator.getCachedPack(packyData.enabledPackIds) == null) + return@interceptClientbound packet + cachedPackyData[player.uniqueId] = packyData packy.plugin.launch { val info = PackyGenerator.getOrCreateCachedPack(packyData.enabledPackIds).await().resourcePackInfo diff --git a/src/main/kotlin/com/mineinabyss/packy/config/PackyConfig.kt b/src/main/kotlin/com/mineinabyss/packy/config/PackyConfig.kt index d5de523..5aa2cbc 100644 --- a/src/main/kotlin/com/mineinabyss/packy/config/PackyConfig.kt +++ b/src/main/kotlin/com/mineinabyss/packy/config/PackyConfig.kt @@ -1,6 +1,5 @@ package com.mineinabyss.packy.config -import co.touchlab.kermit.Severity import com.charleskorn.kaml.YamlComment import com.mineinabyss.guiy.modifiers.Modifier import com.mineinabyss.guiy.modifiers.placement.absolute.at @@ -14,6 +13,7 @@ import kotlinx.serialization.EncodeDefault.Mode.ALWAYS import kotlinx.serialization.EncodeDefault.Mode.NEVER import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.Serializable +import net.kyori.adventure.resource.ResourcePackStatus import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.inventory.ItemStack @@ -26,6 +26,7 @@ data class PackyConfig( @EncodeDefault(ALWAYS) val mcmeta: PackyMcMeta = PackyMcMeta(), @EncodeDefault(ALWAYS) val icon: String = "pack.png", @EncodeDefault(ALWAYS) val server: PackyServer = PackyServer(), + @EncodeDefault(ALWAYS) val dispatch: PackyDispatch = PackyDispatch(), @EncodeDefault(ALWAYS) val prompt: String = "", @EncodeDefault(ALWAYS) val force: Boolean = false, @YamlComment("What ObfuscationType to use, valid options are FULL, SIMPLE & NONE") @@ -40,6 +41,16 @@ data class PackyConfig( @EncodeDefault(ALWAYS) val menu: PackyMenu = PackyMenu() ) { + @Serializable + data class PackyDispatch( + @YamlComment("Sends the pack before the player loads into the world") + val sendPreJoin: Boolean = true, + @YamlComment("If the pack has been generated and cached, allow pre-join dispatch") + val sendPreJoinOnCached: Boolean = true, + @YamlComment("The delay to wait before sending the pack") + val sendDelay: @Serializable(DurationSerializer::class) Duration = 0.seconds + ) + enum class ObfuscationType { FULL, SIMPLE, NONE } diff --git a/src/main/kotlin/com/mineinabyss/packy/listener/PlayerListener.kt b/src/main/kotlin/com/mineinabyss/packy/listener/PlayerListener.kt index 48dc6fd..30b9104 100644 --- a/src/main/kotlin/com/mineinabyss/packy/listener/PlayerListener.kt +++ b/src/main/kotlin/com/mineinabyss/packy/listener/PlayerListener.kt @@ -1,7 +1,10 @@ package com.mineinabyss.packy.listener +import com.github.shynixn.mccoroutine.bukkit.launch +import com.mineinabyss.packy.PackyServer import com.mineinabyss.packy.components.packyData import com.mineinabyss.packy.config.packy +import kotlinx.coroutines.delay import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.Listener @@ -9,6 +12,14 @@ import org.bukkit.event.player.PlayerJoinEvent class PlayerListener : Listener { + @EventHandler(priority = EventPriority.HIGHEST) + fun PlayerJoinEvent.sendPack() { + if (PackyServer.packServer != null && !player.hasResourcePack()) packy.plugin.launch { + delay(packy.config.dispatch.sendDelay) + PackyServer.sendPack(player) + } + } + @EventHandler(priority = EventPriority.NORMAL) fun PlayerJoinEvent.filterPackyData() { // Remove old or forced keys from enabledPackAddons