Skip to content

Commit

Permalink
fix: anvil renaming not working when color-codes are in the renameText
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 19, 2023
1 parent 380d16d commit c8b89d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
15 changes: 15 additions & 0 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyListener.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.mineinabyss.emojy

import com.mineinabyss.emojy.nms.EmojyNMSHandlers
import com.mineinabyss.idofront.items.editItemMeta
import com.mineinabyss.idofront.messaging.logError
import com.mineinabyss.idofront.messaging.logVal
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import io.papermc.paper.event.player.AsyncChatDecorateEvent
import io.papermc.paper.event.player.AsyncChatEvent
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.inventory.AnvilInventory

@Suppress("UnstableApiUsage")
class EmojyListener : Listener {
Expand All @@ -24,5 +30,14 @@ class EmojyListener : Listener {
fun AsyncChatDecorateEvent.onPlayerChat() {
result(result().transform(player(), true, true))
}

@EventHandler
fun PrepareAnvilEvent.onAnvil() {
val inventory = inventory as? AnvilInventory ?: return
val displayName = (inventory.renameText?.miniMsg() ?: inventory.firstItem?.itemMeta?.displayName())?.transform(inventory.viewers.first() as? Player, false)
result = result?.editItemMeta {
displayName(displayName)
}
}
}

14 changes: 10 additions & 4 deletions core/src/main/kotlin/com/mineinabyss/emojy/nms/EmojyNMSHandlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import com.mineinabyss.idofront.textcomponents.toPlainText
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import org.bukkit.Bukkit
import org.bukkit.entity.Player

Expand Down Expand Up @@ -34,16 +35,21 @@ object EmojyNMSHandlers {
}

private val gson = GsonComponentSerializer.gson()
fun JsonObject.formatString() : String {
private val plain = PlainTextComponentSerializer.plainText()
//TODO toPlainText fixes the anvil issue with tags being escaped
// It does break all other formatting everywhere else though by removing tags
// serialize() doesnt but keeps the escaped tag from gson
// anvil goes like this, inputItem -> readUtf -> renameField -> writeNbt from raw string, which is why it breaks
fun JsonObject.formatString(player: Player? = null) : String {
return if (this.has("args") || this.has("text") || this.has("extra") || this.has("translate")) {
gson.serialize(gson.deserialize(this.toString()).toPlainText().miniMsg().transform(null, true))
gson.serialize(gson.deserialize(this.toString())/*.toPlainText()*/.serialize().miniMsg().transform(player, true))
} else this.toString()
}

fun transformer() = { string: String ->
fun transformer(player: Player? = null) = { string: String ->
runCatching {
val element = JsonParser.parseString(string)
if (element.isJsonObject) element.asJsonObject.formatString()
if (element.isJsonObject) element.asJsonObject.formatString(player)
else string
}.getOrNull() ?: string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

override fun readNbt(): CompoundTag? {
return super.readNbt()?.apply {
transform(this, EmojyNMSHandlers.transformer())
transform(this, EmojyNMSHandlers.transformer(player))
}
}

Expand Down Expand Up @@ -232,7 +232,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {
val packetID = customDataSerializer.readVarInt()
val attribute = ctx.channel().attr(Connection.ATTRIBUTE_SERVERBOUND_PROTOCOL)
val packet = attribute.get().createPacket(packetID, customDataSerializer) ?: throw IOException("Bad packet id $packetID")

//ObfHelper.INSTANCE.deobfClassName(packet.javaClass.name).logVal("Packet: ")
out += when {
customDataSerializer.readableBytes() > 0 -> throw IOException("Packet $packetID ($packet) was larger than I expected, found ${customDataSerializer.readableBytes()} bytes extra whilst reading packet $packetID")
packet is ServerboundChatPacket -> {
Expand Down

0 comments on commit c8b89d0

Please sign in to comment.