Skip to content

Commit

Permalink
feat: BossBar support
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jun 2, 2024
1 parent e383e05 commit e7406ac
Showing 1 changed file with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import com.mineinabyss.idofront.items.editItemMeta
import com.mineinabyss.idofront.plugin.listeners
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import com.mojang.serialization.Codec
import io.netty.channel.Channel
import io.netty.channel.ChannelDuplexHandler
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
import io.papermc.paper.adventure.AdventureComponent
import io.papermc.paper.adventure.PaperAdventure
import io.papermc.paper.network.ChannelInitializeListenerHolder
import net.kyori.adventure.text.Component
import net.minecraft.core.NonNullList
import net.minecraft.network.Connection
import net.minecraft.network.chat.Component
import net.minecraft.network.protocol.game.*
import net.minecraft.network.syncher.EntityDataSerializer
import net.minecraft.network.syncher.SynchedEntityData
Expand All @@ -36,16 +35,6 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {
init {
emojy.listeners(EmojyListener())

val codecs = (PaperAdventure::class.java.getDeclaredField("LOCALIZED_CODECS").apply { isAccessible = true }
.get(null) as MutableMap<Locale, Codec<Component>>)

/*for (locale in Locale.getAvailableLocales()) {
codecs[locale] = AdventureCodecs.COMPONENT_CODEC.xmap(
{ component -> component }, // decode
{ component -> GlobalTranslator.render(component, locale) } // encode
)
}*/

val key = NamespacedKey.fromString("packet_listener", emojy)
ChannelInitializeListenerHolder.addListener(key!!) { channel: Channel ->
channel.pipeline().addBefore("packet_handler", key.toString(), object : ChannelDuplexHandler() {
Expand Down Expand Up @@ -86,6 +75,43 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {
} ?: it.transformItemNameLore()
else it.transformItemNameLore()
}.toTypedArray()), packet.carriedItem)
is ClientboundBossEventPacket -> {
// Access the private field 'operation'
val operationField = ClientboundBossEventPacket::class.java.getDeclaredField("operation").apply { isAccessible = true }
val operation = operationField.get(packet)

when (operation::class.java.simpleName) {
"AddOperation" -> {
val nameField = operation::class.java.getDeclaredField("name").apply { isAccessible = true }
// Get the component, serialize it and replace "\\<" as it might be escaped if not an AdventureBossbar
val name = PaperAdventure.asAdventure(nameField.get(operation) as Component).serialize().replace("\\<", "<").miniMsg()
nameField.set(operation, PaperAdventure.asVanilla(name.transformEmotes(connection.locale())))
}
"UpdateNameOperation" -> {
val accessorMethod = operation::class.java.methods.find { it.name == "name" }
accessorMethod?.isAccessible = true
if (accessorMethod != null) {
val name = PaperAdventure.asAdventure(accessorMethod.invoke(operation) as Component)
.serialize().replace("\\<", "<")
.miniMsg().transformEmotes(connection.locale())

val updateNameOperationClass = operation::class.java.enclosingClass.declaredClasses.find {
it.simpleName == "UpdateNameOperation"
} ?: throw IllegalStateException("UpdateNameOperation class not found")

val constructor = updateNameOperationClass.getDeclaredConstructor(Component::class.java).apply { isAccessible = true }
// Create a new instance of UpdateNameOperation with the modified name

val updatedOperation = constructor.newInstance(PaperAdventure.asVanilla(name))

// Set the updated operation in the packet
operationField.set(packet, updatedOperation)
}
}
}

packet
}
else -> packet
}, promise
)
Expand Down Expand Up @@ -120,19 +146,19 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {
return miniMsg().transformEmotes(locale, insert).serialize()
}

fun net.minecraft.network.chat.Component.transformEmotes(locale: Locale? = null, insert: Boolean = false): net.minecraft.network.chat.Component {
fun Component.transformEmotes(locale: Locale? = null, insert: Boolean = false): Component {
return PaperAdventure.asVanilla(PaperAdventure.asAdventure(this).transformEmotes(locale, insert))
}

fun String.escapeEmoteIDs(player: Player?): String {
return miniMsg().escapeEmoteIDs(player).serialize()
}

fun net.minecraft.network.chat.Component.escapeEmoteIDs(player: Player?): net.minecraft.network.chat.Component {
fun Component.escapeEmoteIDs(player: Player?): Component {
return PaperAdventure.asVanilla((PaperAdventure.asAdventure(this)).escapeEmoteIDs(player))
}

fun net.minecraft.network.chat.Component.unescapeEmoteIds(): net.minecraft.network.chat.Component {
fun Component.unescapeEmoteIds(): Component {
return PaperAdventure.asVanilla(PaperAdventure.asAdventure(this).unescapeEmoteIds())
}

Expand Down

0 comments on commit e7406ac

Please sign in to comment.