From e784e4e38bab70957e5e8a6280e8182f0c990e41 Mon Sep 17 00:00:00 2001 From: Boy Date: Thu, 17 Oct 2024 20:06:16 +0200 Subject: [PATCH] feat: drop protocollib for nms-packet handling --- build.gradle.kts | 3 +- gradle.properties | 2 +- .../deeperworld/DeeperWorldPlugin.kt | 4 -- .../movement/SectionTeleportPacketAdapter.kt | 67 +++++++++---------- .../movement/TransitionTeleportHandler.kt | 31 ++++----- src/main/resources/paper-plugin.yml | 2 +- 6 files changed, 49 insertions(+), 60 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 14d5d95..07283f1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,6 +5,7 @@ plugins { alias(idofrontLibs.plugins.mia.papermc) alias(idofrontLibs.plugins.mia.copyjar) alias(idofrontLibs.plugins.mia.testing) + alias(idofrontLibs.plugins.mia.nms) alias(idofrontLibs.plugins.mia.publication) alias(idofrontLibs.plugins.mia.autoversion) } @@ -32,9 +33,9 @@ dependencies { // Plugin APIs compileOnly(idofrontLibs.minecraft.plugin.fawe.core) compileOnly(idofrontLibs.minecraft.plugin.fawe.bukkit) { isTransitive = false } - compileOnly(idofrontLibs.minecraft.plugin.protocollib) compileOnly(libs.minecraft.plugin.blocklocker) // Shaded implementation(idofrontLibs.bundles.idofront.core) + implementation(idofrontLibs.idofront.nms) } diff --git a/gradle.properties b/gradle.properties index c70906a..cbad6df 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group=com.mineinabyss version=0.11 -idofrontVersion=0.25.6 +idofrontVersion=0.25.14 diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/DeeperWorldPlugin.kt b/src/main/kotlin/com/mineinabyss/deeperworld/DeeperWorldPlugin.kt index 8a178a2..c059fa6 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/DeeperWorldPlugin.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/DeeperWorldPlugin.kt @@ -1,7 +1,5 @@ package com.mineinabyss.deeperworld -import com.comphenix.protocol.ProtocolLibrary -import com.comphenix.protocol.ProtocolManager import com.github.shynixn.mccoroutine.bukkit.launch import com.mineinabyss.deeperworld.MinecraftConstants.FULL_DAY_TIME import com.mineinabyss.deeperworld.listeners.MovementListener @@ -23,8 +21,6 @@ import kotlinx.coroutines.delay import org.bukkit.Material import org.bukkit.plugin.java.JavaPlugin -val protocolManager: ProtocolManager = ProtocolLibrary.getProtocolManager() - class DeeperWorldPlugin : JavaPlugin() { override fun onEnable() { createDeeperWorldContext() diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/SectionTeleportPacketAdapter.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/SectionTeleportPacketAdapter.kt index 2aa9388..8fe4146 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/SectionTeleportPacketAdapter.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/SectionTeleportPacketAdapter.kt @@ -1,15 +1,16 @@ package com.mineinabyss.deeperworld.movement -import com.comphenix.protocol.PacketType -import com.comphenix.protocol.events.PacketAdapter -import com.comphenix.protocol.events.PacketContainer -import com.comphenix.protocol.events.PacketEvent import com.github.shynixn.mccoroutine.bukkit.launch import com.mineinabyss.deeperworld.datastructures.VehicleTree import com.mineinabyss.deeperworld.deeperWorld -import com.mineinabyss.deeperworld.protocolManager +import com.mineinabyss.idofront.nms.PacketListener +import com.mineinabyss.idofront.nms.aliases.toNMS import com.mineinabyss.idofront.time.ticks import kotlinx.coroutines.delay +import net.kyori.adventure.key.Key +import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket +import net.minecraft.network.protocol.game.ClientboundPlayerLookAtPacket +import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket import org.bukkit.entity.LivingEntity import org.bukkit.entity.Player import org.bukkit.util.Vector @@ -27,55 +28,51 @@ class SectionTeleportPacketAdapter( private val oldFallDistance: Float, private val oldVelocity: Vector, private val vehicleTree: VehicleTree? = null -) : PacketAdapter( - deeperWorld.plugin, - PacketType.Play.Client.POSITION, - PacketType.Play.Client.POSITION_LOOK ) { - override fun onPacketReceiving(event: PacketEvent) { - if (event.player != player) return - protocolManager.removePacketListener(this) + private val PACKET_KEY = Key.key("deeperworld", "section_teleport_handler_${player.name}") - deeperWorld.plugin.launch { - delay(1.ticks) + fun addPacketListener() { + PacketListener.interceptClientbound(deeperWorld.plugin, PACKET_KEY.value()) { packet, player: Player? -> + if (this.player.uniqueId != player?.uniqueId) return@interceptClientbound packet + if (packet !is ClientboundPlayerLookAtPacket && packet !is ClientboundMoveEntityPacket) return@interceptClientbound packet - oldLeashedEntities.toSet().forEach { - if (it == player) return@forEach + PacketListener.unregisterListener(PACKET_KEY) - it.teleport(player) - it.setLeashHolder(player) - } + deeperWorld.plugin.launch { + delay(1.ticks) - if (vehicleTree != null) { - vehicleTree.root.values().toSet().forEach { + oldLeashedEntities.asSequence().forEach { if (it == player) return@forEach it.teleport(player) + it.setLeashHolder(player) } - vehicleTree.root.applyAll { vehicleNode -> - vehicleNode.children.forEach { - vehicleNode.value.addPassenger(it.value) + if (vehicleTree != null) { + vehicleTree.root.values().asSequence().forEach { + if (it != player) it.teleport(player) } - } - vehicleTree.root.value.fallDistance = oldFallDistance - vehicleTree.root.value.velocity = oldVelocity + vehicleTree.root.applyAll { vehicleNode -> + vehicleNode.children.forEach { + vehicleNode.value.addPassenger(it.value) + } + } - delay(deeperWorld.config.remountPacketDelay) + vehicleTree.root.value.fallDistance = oldFallDistance + vehicleTree.root.value.velocity = oldVelocity - player.vehicle?.let { vehicle -> - val playerVehicleID = vehicle.entityId - val passengerIDs = vehicle.passengers.map { it.entityId }.toIntArray() + delay(deeperWorld.config.remountPacketDelay) // Resends a mount packet to clients to prevent potential visual glitches where the client thinks it's dismounted. - protocolManager.sendServerPacket(player, PacketContainer(PacketType.Play.Server.MOUNT).apply { - integers.write(0, playerVehicleID) - integerArrays.write(0, passengerIDs) - }) + player.vehicle?.toNMS()?.let { vehicle -> + player.toNMS().connection.send(ClientboundSetPassengersPacket(vehicle)) + } } } + + return@interceptClientbound packet } } } diff --git a/src/main/kotlin/com/mineinabyss/deeperworld/movement/TransitionTeleportHandler.kt b/src/main/kotlin/com/mineinabyss/deeperworld/movement/TransitionTeleportHandler.kt index 8420365..48b0b76 100644 --- a/src/main/kotlin/com/mineinabyss/deeperworld/movement/TransitionTeleportHandler.kt +++ b/src/main/kotlin/com/mineinabyss/deeperworld/movement/TransitionTeleportHandler.kt @@ -5,7 +5,6 @@ import com.mineinabyss.deeperworld.datastructures.VehicleTree import com.mineinabyss.deeperworld.deeperWorld import com.mineinabyss.deeperworld.extensions.getPassengersRecursive import com.mineinabyss.deeperworld.extensions.getRootVehicle -import com.mineinabyss.deeperworld.protocolManager import com.mineinabyss.idofront.time.ticks import kotlinx.coroutines.delay import org.bukkit.Location @@ -55,15 +54,13 @@ class TransitionTeleportHandler(val player: Player, val from: Location, val to: delay(1.ticks) player.teleportWithSpectatorsAsync(to) { - protocolManager.addPacketListener( - SectionTeleportPacketAdapter( - player, - oldLeashedEntities, - oldFallDistance, - oldVelocity, - vehicleTree - ) - ) + SectionTeleportPacketAdapter( + player, + oldLeashedEntities, + oldFallDistance, + oldVelocity, + vehicleTree + ).addPacketListener() } } @@ -76,14 +73,12 @@ class TransitionTeleportHandler(val player: Player, val from: Location, val to: player.velocity = oldVelocity if (oldLeashedEntities.isNotEmpty()) { - protocolManager.addPacketListener( - SectionTeleportPacketAdapter( - player, - oldLeashedEntities, - oldFallDistance, - oldVelocity - ) - ) + SectionTeleportPacketAdapter( + player, + oldLeashedEntities, + oldFallDistance, + oldVelocity + ).addPacketListener() } } } diff --git a/src/main/resources/paper-plugin.yml b/src/main/resources/paper-plugin.yml index b14071f..6f1fefc 100644 --- a/src/main/resources/paper-plugin.yml +++ b/src/main/resources/paper-plugin.yml @@ -5,7 +5,7 @@ authors: [Derongan, Offz, Boy000, Norazan, Scyu] api-version: '1.21' description: A plugin for letting you create a deeper world. Or at least fake it. -softdepend: [Multiverse-Core, BlockLocker, ProtocolLib, FastAsyncWorldEdit] +softdepend: [BlockLocker, FastAsyncWorldEdit] permissions: deeperworld.*: