Skip to content

Commit

Permalink
feat: drop protocollib for nms-packet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Oct 17, 2024
1 parent 7e8ee86 commit e784e4e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 60 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=com.mineinabyss
version=0.11
idofrontVersion=0.25.6
idofrontVersion=0.25.14
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

}
Expand All @@ -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()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/paper-plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*:
Expand Down

0 comments on commit e784e4e

Please sign in to comment.