Skip to content

Commit

Permalink
feat: add setting to send pack after login / during login if cached
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jul 22, 2024
1 parent 53a4a3d commit 3911019
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/main/kotlin/com/mineinabyss/packy/PackyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 6 additions & 10 deletions src/main/kotlin/com/mineinabyss/packy/PackyServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,26 +12,19 @@ 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
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
Expand Down Expand Up @@ -69,12 +61,16 @@ object PackyServer {

private val cachedPackyData = mutableMapOf<UUID, PackyData>()
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<PackyData>() ?: 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
Expand Down
13 changes: 12 additions & 1 deletion src/main/kotlin/com/mineinabyss/packy/config/PackyConfig.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/com/mineinabyss/packy/listener/PlayerListener.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
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
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
Expand Down

0 comments on commit 3911019

Please sign in to comment.