Skip to content

Commit

Permalink
refactor: move player into Custom encoder/decoder initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 10, 2023
1 parent 98f5b8a commit d332f6b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class EmojyListener : Listener {
// Replace with result not original message to avoid borking other chat formatting
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
fun AsyncChatDecorateEvent.onPlayerChat() {
result(result().replaceEmoteIds(player()))
//result(result().replaceEmoteIds(player()))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ class EmojyNMSHandler : IEmojyNMSHandler {
Bukkit.getOnlinePlayers().forEach(::inject)
}

private fun Channel.inject() {
private fun Channel.inject(player: Player? = null) {
if (this !in encoder.keys && (this.pipeline().get("encoder") as ChannelHandler) !is CustomPacketEncoder)
encoder[this] = this.pipeline().replace("encoder", "encoder", CustomPacketEncoder())
encoder[this] = this.pipeline().replace("encoder", "encoder", CustomPacketEncoder(player))

if (this !in decoder.keys && (this.pipeline().get("decoder") as ChannelHandler) !is CustomPacketDecoder)
decoder[this] = this.pipeline().replace("decoder", "decoder", CustomPacketDecoder())
decoder[this] = this.pipeline().replace("decoder", "decoder", CustomPacketDecoder(player))

}

Expand All @@ -133,13 +133,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

override fun inject(player: Player) {
val channel = (player as? CraftPlayer)?.handle?.connection?.connection?.channel ?: return
channel.eventLoop().submit { channel.inject() }
channel.pipeline().forEach {
when (val handler = it.value) {
is CustomPacketEncoder -> handler.player = player
is CustomPacketDecoder -> handler.player = player
}
}
channel.eventLoop().submit { channel.inject(player) }
}

override fun uninject(player: Player) {
Expand Down Expand Up @@ -207,9 +201,8 @@ class EmojyNMSHandler : IEmojyNMSHandler {

}

private class CustomPacketEncoder : MessageToByteEncoder<Packet<*>>() {
private class CustomPacketEncoder(val player: Player?) : MessageToByteEncoder<Packet<*>>() {
private val protocolDirection = PacketFlow.CLIENTBOUND
var player: Player? = null

override fun encode(ctx: ChannelHandlerContext, msg: Packet<*>, out: ByteBuf) {
val enumProt = ctx.channel()?.attr(Connection.ATTRIBUTE_PROTOCOL)?.get() ?: throw RuntimeException("ConnectionProtocol unknown: $out")
Expand All @@ -231,8 +224,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}
}

private class CustomPacketDecoder : ByteToMessageDecoder() {
var player: Player? = null
private class CustomPacketDecoder(val player: Player?) : ByteToMessageDecoder() {

override fun decode(ctx: ChannelHandlerContext, buffer: ByteBuf, out: MutableList<Any>) {
val buffferCopy = buffer.copy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ class EmojyNMSHandler : IEmojyNMSHandler {
Bukkit.getOnlinePlayers().forEach(::inject)
}

private fun Channel.inject() {
private fun Channel.inject(player: Player? = null) {
if (this !in encoder.keys && (this.pipeline().get("encoder") as ChannelHandler) !is CustomPacketEncoder)
encoder[this] = this.pipeline().replace("encoder", "encoder", CustomPacketEncoder())
encoder[this] = this.pipeline().replace("encoder", "encoder", CustomPacketEncoder(player))

if (this !in decoder.keys && (this.pipeline().get("decoder") as ChannelHandler) !is CustomPacketDecoder)
decoder[this] = this.pipeline().replace("decoder", "decoder", CustomPacketDecoder())
decoder[this] = this.pipeline().replace("decoder", "decoder", CustomPacketDecoder(player))

}

Expand All @@ -131,13 +131,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

override fun inject(player: Player) {
val channel = (player as? CraftPlayer)?.handle?.connection?.connection?.channel ?: return
channel.eventLoop().submit { channel.inject() }
channel.pipeline().forEach {
when (val handler = it.value) {
is CustomPacketEncoder -> handler.setPlayer(player)
is CustomPacketDecoder -> handler.setPlayer(player)
}
}
channel.eventLoop().submit { channel.inject(player) }
}

override fun uninject(player: Player) {
Expand Down Expand Up @@ -201,13 +195,8 @@ class EmojyNMSHandler : IEmojyNMSHandler {

}

private class CustomPacketEncoder : MessageToByteEncoder<Packet<*>>() {
private class CustomPacketEncoder(val player: Player? = null) : MessageToByteEncoder<Packet<*>>() {
private val protocolDirection = PacketFlow.CLIENTBOUND
private var player: Player? = null

fun setPlayer(player: Player) {
this.player = player
}

override fun encode(ctx: ChannelHandlerContext, msg: Packet<*>, out: ByteBuf) {
val enumProt = ctx.channel()?.attr(Connection.ATTRIBUTE_CLIENTBOUND_PROTOCOL)?.get() ?: throw RuntimeException("ConnectionProtocol unknown: $out")
Expand All @@ -229,12 +218,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
}
}

private class CustomPacketDecoder : ByteToMessageDecoder() {
private var player: Player? = null

fun setPlayer(player: Player) {
this.player = player
}
private class CustomPacketDecoder(val player: Player? = null) : ByteToMessageDecoder() {

override fun decode(ctx: ChannelHandlerContext, buffer: ByteBuf, out: MutableList<Any>) {
val bufferCopy = buffer.copy()
Expand Down

0 comments on commit d332f6b

Please sign in to comment.