From dff3c88021d48895e7319e84c8dfe10c4f2372bf Mon Sep 17 00:00:00 2001 From: Boy Date: Mon, 29 Jul 2024 16:07:40 +0200 Subject: [PATCH] fix: tag-packets being incomplete --- gradle.properties | 4 +- gradle/libs.versions.toml | 4 +- .../staminaclimb/StaminaClimbPlugin.kt | 21 ++++- .../staminaclimb/StaminaCommands.kt | 17 ++-- .../staminaclimb/modules/StaminaModule.kt | 11 ++- .../modules/StaminaPaperModule.kt | 13 +-- .../com/mineinabyss/staminaclimb/nms/Tags.kt | 88 ++++++++----------- .../staminaclimb/stamina/StaminaBar.kt | 4 - src/main/resources/paper-plugin.yml | 6 +- 9 files changed, 85 insertions(+), 83 deletions(-) diff --git a/gradle.properties b/gradle.properties index 40232d0..693aadd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group=com.mineinabyss -version=0.22 -idofrontVersion=0.24.0-dev.8 +version=0.23 +idofrontVersion=0.24.15 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 937cd01..12bc3fd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,5 @@ [versions] -geary = "0.30.1-dev.10" -bonehurtingjuice = "1.10.1" +geary = "0.30.10-dev.1" [libraries] geary-papermc = { module = "com.mineinabyss:geary-papermc", version.ref = "geary" } -bonehurtingjuice = { module = "com.mineinabyss:bonehurtingjuice", version.ref = "bonehurtingjuice" } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaClimbPlugin.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaClimbPlugin.kt index f646ab4..8a82798 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaClimbPlugin.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaClimbPlugin.kt @@ -3,22 +3,34 @@ package com.mineinabyss.staminaclimb import com.mineinabyss.geary.autoscan.autoscan import com.mineinabyss.geary.modules.geary import com.mineinabyss.idofront.di.DI +import com.mineinabyss.idofront.nms.interceptClientbound import com.mineinabyss.idofront.plugin.listeners import com.mineinabyss.staminaclimb.climbing.ClimbBehaviour import com.mineinabyss.staminaclimb.climbing.ClimbBehaviour.stopClimbing import com.mineinabyss.staminaclimb.modules.StaminaClimbModule import com.mineinabyss.staminaclimb.modules.StaminaPaperModule +import com.mineinabyss.staminaclimb.modules.stamina +import com.mineinabyss.staminaclimb.nms.Tags +import com.mineinabyss.staminaclimb.nms.Tags.networkPayload +import com.mineinabyss.staminaclimb.nms.Tags.tags import com.mineinabyss.staminaclimb.stamina.StaminaBar import com.mineinabyss.staminaclimb.stamina.StaminaBar.registerBar import com.mineinabyss.staminaclimb.stamina.StaminaTask +import it.unimi.dsi.fastutil.ints.IntList +import net.minecraft.core.registries.Registries +import net.minecraft.network.protocol.Packet +import net.minecraft.network.protocol.common.ClientboundUpdateTagsPacket +import net.minecraft.resources.ResourceLocation +import net.minecraft.tags.BlockTags import org.bukkit.Bukkit +import org.bukkit.entity.Player import org.bukkit.plugin.java.JavaPlugin /** A reference to the StaminaClimb plugin */ class StaminaClimbPlugin : JavaPlugin() { override fun onLoad() { - DI.add(StaminaPaperModule(this)) + createClimbContext() geary { autoscan(classLoader, "com.mineinabyss.staminaclimb") { all() @@ -34,6 +46,13 @@ class StaminaClimbPlugin : JavaPlugin() { listeners(ClimbBehaviour, StaminaBar) StaminaCommands() + + Tags.interceptConfigPhaseTagPacket() + } + + fun createClimbContext() { + DI.remove() + DI.add(StaminaPaperModule(this)) } override fun onDisable() { diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaCommands.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaCommands.kt index 7b81884..8595294 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaCommands.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/StaminaCommands.kt @@ -6,10 +6,13 @@ import com.mineinabyss.idofront.messaging.info import com.mineinabyss.idofront.messaging.success import com.mineinabyss.staminaclimb.modules.stamina import com.mineinabyss.staminaclimb.nms.Tags +import net.minecraft.core.Registry +import net.minecraft.core.registries.BuiltInRegistries import net.minecraft.tags.BlockTags import org.bukkit.command.Command import org.bukkit.command.CommandSender import org.bukkit.command.TabCompleter +import kotlin.jvm.optionals.getOrNull class StaminaCommands : IdofrontCommandExecutor(), TabCompleter { override val commands = commands(stamina.plugin) { @@ -25,18 +28,18 @@ class StaminaCommands : IdofrontCommandExecutor(), TabCompleter { "staminaclimb" { "reload" { action { - stamina.configHolder.reload() + stamina.plugin.createClimbContext() sender.success("Config has been reloaded!") } } "tags" { action { - stamina.emptyClimbableMap.entries.find { it.key == BlockTags.FALL_DAMAGE_RESETTING.location }?.let { - sender.info("Fall damage resetting tag: ${it.value}") - } - stamina.normalClimbableMap.entries.find { it.key == BlockTags.CLIMBABLE.location }?.let { - sender.info("Climbable tag: ${it.value}") - } +// stamina.emptyClimbableMap.entries.find { it.key == BlockTags.CLIMBABLE.location }?.let { +// sender.info("Empty climbable tag: ${it.value.joinToString { BuiltInRegistries.BLOCK.getHolder(it).getOrNull()?.registeredName.toString() }}") +// } +// stamina.normalClimbableMap.entries.find { it.key == BlockTags.CLIMBABLE.location }?.let { +// sender.info("Normal Climbable Tag: ${it.value.joinToString { BuiltInRegistries.BLOCK.getHolder(it).getOrNull()?.registeredName.toString() }}") +// } } } } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaModule.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaModule.kt index c65d429..5775346 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaModule.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaModule.kt @@ -1,18 +1,17 @@ package com.mineinabyss.staminaclimb.modules -import com.mineinabyss.idofront.config.IdofrontConfig import com.mineinabyss.idofront.di.DI import com.mineinabyss.staminaclimb.StaminaClimbPlugin import com.mineinabyss.staminaclimb.config.StaminaConfig -import it.unimi.dsi.fastutil.ints.IntArrayList -import net.minecraft.resources.ResourceLocation +import net.minecraft.core.Registry +import net.minecraft.resources.ResourceKey +import net.minecraft.tags.TagNetworkSerialization.NetworkPayload val stamina: StaminaClimbModule by DI.observe() interface StaminaClimbModule { val plugin: StaminaClimbPlugin - val configHolder: IdofrontConfig val config: StaminaConfig - val emptyClimbableMap: Map - val normalClimbableMap: Map + val disabledClimbingTags: MutableMap?>, NetworkPayload> + val initialTags: MutableMap?>, NetworkPayload> } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaPaperModule.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaPaperModule.kt index 6145527..80ac82a 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaPaperModule.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/modules/StaminaPaperModule.kt @@ -5,13 +5,16 @@ import com.mineinabyss.idofront.config.config import com.mineinabyss.staminaclimb.StaminaClimbPlugin import com.mineinabyss.staminaclimb.config.StaminaConfig import com.mineinabyss.staminaclimb.nms.Tags +import it.unimi.dsi.fastutil.ints.IntArrayList +import net.minecraft.core.Registry +import net.minecraft.resources.ResourceKey +import net.minecraft.resources.ResourceLocation +import net.minecraft.tags.TagNetworkSerialization.NetworkPayload class StaminaPaperModule( override val plugin: StaminaClimbPlugin ) : StaminaClimbModule { - override val configHolder: IdofrontConfig = - config("config", plugin.dataFolder.toPath(), StaminaConfig()) - override val config: StaminaConfig by configHolder - override val emptyClimbableMap = Tags.createEmptyClimbableMap() - override val normalClimbableMap = Tags.createNormalClimbableMap() + override val config by config("config", plugin.dataPath, StaminaConfig()) + override val disabledClimbingTags: MutableMap?>, NetworkPayload> = mutableMapOf() + override val initialTags: MutableMap?>, NetworkPayload> = mutableMapOf() } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/nms/Tags.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/nms/Tags.kt index e3442da..ae0cec9 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/nms/Tags.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/nms/Tags.kt @@ -1,10 +1,10 @@ package com.mineinabyss.staminaclimb.nms +import com.mineinabyss.idofront.nms.interceptClientbound import com.mineinabyss.staminaclimb.modules.stamina -import it.unimi.dsi.fastutil.ints.IntArrayList import it.unimi.dsi.fastutil.ints.IntList -import net.minecraft.core.registries.BuiltInRegistries import net.minecraft.core.registries.Registries +import net.minecraft.network.protocol.Packet import net.minecraft.network.protocol.common.ClientboundUpdateTagsPacket import net.minecraft.resources.ResourceLocation import net.minecraft.tags.BlockTags @@ -13,67 +13,55 @@ import org.bukkit.craftbukkit.entity.CraftPlayer import org.bukkit.entity.Player object Tags { - fun createPayload(map: Map): NetworkPayload = - NetworkPayload::class.java.declaredConstructors.first() - .also { it.isAccessible = true } - .newInstance(map) as NetworkPayload val disabledPlayers = mutableSetOf() + /** + * Intercepts ClientboundUpdateTagsPacket sent to players during Configuration Phase & caches it. + * + * It then caches the initial tag-set and generates a copy without climbable & fall_damage_resetting entries + */ + fun interceptConfigPhaseTagPacket() { + stamina.plugin.interceptClientbound { packet: Packet<*>, player: Player? -> + if (packet !is ClientboundUpdateTagsPacket || player?.isOnline == true) return@interceptClientbound packet + if (stamina.initialTags.isNotEmpty()) return@interceptClientbound packet + + stamina.initialTags.putAll(packet.tags) + packet.tags.entries.map { registryEntry -> + if (registryEntry.key == Registries.BLOCK) { + val tags = registryEntry.value.tags().map { tag -> + tag.key to when (tag.key) { + BlockTags.CLIMBABLE.location, BlockTags.FALL_DAMAGE_RESETTING.location -> IntList.of() + else -> tag.value + } + }.toMap() + registryEntry.setValue(tags.networkPayload()) + } + registryEntry + }.forEach { + stamina.disabledClimbingTags[it.key] = it.value + } + + return@interceptClientbound packet + } + } + fun enableClimb(player: Player) { if (player !in disabledPlayers) return disabledPlayers.remove(player) - (player as CraftPlayer).handle.connection.send(updateTagPacket(true)) + (player as CraftPlayer).handle.connection.send(ClientboundUpdateTagsPacket(stamina.initialTags)) } fun disableClimb(player: Player) { if (player in disabledPlayers) return disabledPlayers.add(player) - (player as CraftPlayer).handle.connection.send(updateTagPacket(false)) - } - - private fun updateTagPacket(enable: Boolean): ClientboundUpdateTagsPacket { - return ClientboundUpdateTagsPacket( - mapOf( - Registries.BLOCK to createPayload( - when (enable) { - true -> stamina.normalClimbableMap - false -> stamina.emptyClimbableMap - } - ) - ) - ) + (player as CraftPlayer).handle.connection.send(ClientboundUpdateTagsPacket(stamina.disabledClimbingTags)) } - fun emptyFallDamageResetTag(player: Player): Map { - return BuiltInRegistries.BLOCK.tags.map { pair -> - pair.first.location to IntArrayList(pair.second.size()).apply { - if (pair.first.location == BlockTags.FALL_DAMAGE_RESETTING.location) return@apply - if (player in disabledPlayers && pair.first.location == BlockTags.CLIMBABLE.location) return@apply - pair.second.forEach { add(BuiltInRegistries.BLOCK.getId(it.value())) } - } - }.toList().toMap() - } - - fun createNormalClimbableMap(): Map { - return BuiltInRegistries.BLOCK.tags.map { pair -> - pair.first.location to IntArrayList(pair.second.size()).apply { - if (pair.first.location == BlockTags.FALL_DAMAGE_RESETTING.location) return@apply - pair.second.forEach { add(BuiltInRegistries.BLOCK.getId(it.value())) } - } - }.toList().toMap() - } + private val tagsField = NetworkPayload::class.java.getDeclaredField("tags").also { it.isAccessible = true } + private val payloadConstructor = NetworkPayload::class.java.declaredConstructors.first().also { it.isAccessible = true } + fun NetworkPayload.tags() = (tagsField.get(this) as Map).toMutableMap() + fun Map.networkPayload() = payloadConstructor.newInstance(this) as NetworkPayload - fun createEmptyClimbableMap(): Map { - return BuiltInRegistries.BLOCK.tags.map { pair -> - pair.first.location to IntArrayList(pair.second.size()).apply { - // If the tag is CLIMBABLE, don't add any blocks to the list - when (pair.first.location) { - BlockTags.CLIMBABLE.location, BlockTags.FALL_DAMAGE_RESETTING.location -> return@apply - else -> pair.second.forEach { add(BuiltInRegistries.BLOCK.getId(it.value())) } - } - } - }.toList().toMap() - } } diff --git a/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt b/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt index f627d62..144e3c6 100644 --- a/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt +++ b/src/main/kotlin/com/mineinabyss/staminaclimb/stamina/StaminaBar.kt @@ -7,7 +7,6 @@ import com.mineinabyss.staminaclimb.* import com.mineinabyss.staminaclimb.climbing.ClimbBehaviour import com.mineinabyss.staminaclimb.modules.stamina import com.mineinabyss.staminaclimb.nms.Tags -import com.mineinabyss.staminaclimb.nms.Tags.createPayload import kotlinx.coroutines.delay import net.kyori.adventure.bossbar.BossBar import net.minecraft.core.registries.Registries @@ -81,9 +80,6 @@ object StaminaBar : Listener { @EventHandler fun PlayerJoinEvent.onPlayerJoin() { - val map = Tags.emptyFallDamageResetTag(player) - val packet = ClientboundUpdateTagsPacket(mapOf(Registries.BLOCK to createPayload(map))) - (player as CraftPlayer).handle.connection.send(packet) registerBar(player) if (player.isClimbing && player.uniqueId in barProgressTracker) { player.setStamina(barProgressTracker[player.uniqueId]!!) diff --git a/src/main/resources/paper-plugin.yml b/src/main/resources/paper-plugin.yml index d9b34c6..7643843 100644 --- a/src/main/resources/paper-plugin.yml +++ b/src/main/resources/paper-plugin.yml @@ -1,7 +1,7 @@ name: StaminaClimb version: '${plugin_version}' author: Offz -api-version: '1.20' +api-version: '1.21' main: com.mineinabyss.staminaclimb.StaminaClimbPlugin prefix: SnC description: A mod that lets you climb and get exhausted @@ -12,7 +12,3 @@ dependencies: required: true load: BEFORE join-classpath: true - BoneHurtingJuice: - required: false - load: BEFORE - join-classpath: true