Skip to content

Commit

Permalink
1.20.3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Dec 6, 2023
1 parent 49ec543 commit 0684985
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 4 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

val questAdderGroup = "kor.toxicity.questadder"
val questAdderVersion = "1.2.0"
val questAdderVersion = "1.2.1"

val adventureVersion = "4.14.0"
val platformVersion = "4.3.1"
Expand Down Expand Up @@ -68,6 +68,7 @@ dependencies {
implementation(project(path = ":nms:v1_19_R3", configuration = "reobf"))
implementation(project(path = ":nms:v1_20_R1", configuration = "reobf"))
implementation(project(path = ":nms:v1_20_R2", configuration = "reobf"))
implementation(project(path = ":nms:v1_20_R3", configuration = "reobf"))
implementation(project(path = ":plugin", configuration = "shadow"))
}

Expand Down
13 changes: 13 additions & 0 deletions nms/v1_20_R3/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id("io.papermc.paperweight.userdev") version("1.5.6")
}

dependencies {
paperweight.paperDevBundle("1.20.3-R0.1-SNAPSHOT")
}

tasks {
assemble {
dependsOn(reobfJar)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package kor.toxicity.questadder.nms.v1_20_R3

import io.netty.channel.ChannelDuplexHandler
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
import kor.toxicity.questadder.QuestAdderBukkit
import kor.toxicity.questadder.manager.SlateManager
import kor.toxicity.questadder.nms.NMSChannel
import net.kyori.adventure.text.Component
import net.minecraft.core.BlockPos
import net.minecraft.network.Connection
import net.minecraft.network.protocol.game.*
import net.minecraft.server.level.ServerPlayer
import net.minecraft.server.network.ServerCommonPacketListenerImpl
import net.minecraft.world.level.block.Blocks
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer
import org.bukkit.entity.Player
import java.util.*

class NMSChannelImpl: NMSChannel {
private val playerChannelMap = HashMap<UUID, PlayerChannel>()
private val blockData = Blocks.OAK_SIGN.defaultBlockState()
private val air = Blocks.AIR.defaultBlockState()
private val channelField = ServerCommonPacketListenerImpl::class.java.declaredFields.first {
it.type == Connection::class.java
}
override fun inject(player: Player) {
playerChannelMap[player.uniqueId] = PlayerChannel((player as CraftPlayer).handle)
}

override fun uninject(player: Player) {
playerChannelMap.remove(player.uniqueId)?.remove()
}

override fun openSign(player: Player, array: List<Component>, callback: (Array<String>) -> Unit) {
playerChannelMap[player.uniqueId]?.let {
val originalLocation = player.location.apply {
y = 255.0
}
val blockPos = BlockPos(originalLocation.blockX, 255, originalLocation.blockZ)

val connection = (player as CraftPlayer).handle.connection

connection.send(ClientboundBlockUpdatePacket(blockPos, blockData))
QuestAdderBukkit.platform.changeSign(player, originalLocation, array)
connection.send(ClientboundOpenSignEditorPacket(blockPos, true))
it.signCallback = { arr ->
connection.send(ClientboundBlockUpdatePacket(blockPos, air))
QuestAdderBukkit.task(player.location) {
callback(arr)
}
}
}
}
private fun getConnection(player: ServerPlayer): Connection {
channelField.isAccessible = true
val get = channelField.get(player.connection) as Connection
channelField.isAccessible = false
return get
}

private inner class PlayerChannel(val player: ServerPlayer): ChannelDuplexHandler() {
var signCallback: ((Array<String>) -> Unit)? = null

private val channel = getConnection(player)

init {
val pipeLine = channel.channel.pipeline()
pipeLine.toMap().forEach {
if (it.value is Connection) {
pipeLine.addBefore(it.key, "questadder", this)
}
}
}
fun remove() {
val channel = channel.channel
channel.eventLoop().submit {
channel.pipeline().remove("questadder")
}
}

override fun write(ctx: ChannelHandlerContext, msg: Any, promise: ChannelPromise) {
if (SlateManager.isSlated(player.uuid)) {
when (msg) {
is ClientboundSetEquipmentPacket -> return
is ClientboundEntityEventPacket -> return
}
}
super.write(ctx, msg, promise)
}

override fun channelRead(ctx: ChannelHandlerContext, msg: Any) {
if (msg is ServerboundSignUpdatePacket) {
signCallback?.let {
it(msg.lines)
signCallback = null
}
}
super.channelRead(ctx, msg)
}
}
}
Loading

0 comments on commit 0684985

Please sign in to comment.