Skip to content

Commit

Permalink
fix: anvil renaming storing original rename name for next rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed May 29, 2024
1 parent 3f226bd commit 717fdf2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.shynixn.mccoroutine.bukkit.ticks
import com.jeff_media.morepersistentdatatypes.DataType
import com.mineinabyss.emojy.emojy
import com.mineinabyss.emojy.escapeEmoteIDs
import com.mineinabyss.emojy.nms.v1_20_R4.EmojyNMSHandler.Companion.ORIGINAL_ITEM_RENAME_TEXT
import com.mineinabyss.emojy.nms.v1_20_R4.EmojyNMSHandler.Companion.transformEmotes
import com.mineinabyss.emojy.transformEmoteIDs
import com.mineinabyss.idofront.items.editItemMeta
Expand Down Expand Up @@ -73,9 +74,17 @@ class EmojyListener : Listener {

@EventHandler
fun PrepareAnvilEvent.onAnvil() {
if (result?.itemMeta?.hasDisplayName() != true) return
val displayName = (inventory.renameText?.miniMsg() ?: inventory.firstItem?.itemMeta?.displayName())?.transformEmoteIDs(null, false, true)
if (result?.itemMeta?.hasDisplayName() != true || inventory.renameText == null) {
result?.editItemMeta { persistentDataContainer.remove(ORIGINAL_ITEM_RENAME_TEXT) }
return
}
val displayName = inventory.renameText?.miniMsg()?.transformEmoteIDs(null, false, true) ?: run {
result?.editItemMeta { persistentDataContainer.remove(ORIGINAL_ITEM_RENAME_TEXT) }
return
}

result = result?.editItemMeta {
persistentDataContainer.set(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING, inventory.renameText!!)
displayName(displayName)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

package com.mineinabyss.emojy.nms.v1_20_R4

import com.jeff_media.morepersistentdatatypes.DataType
import com.mineinabyss.emojy.*
import com.mineinabyss.emojy.config.SPACE_PERMISSION
import com.mineinabyss.emojy.nms.IEmojyNMSHandler
import com.mineinabyss.idofront.items.editItemMeta
import com.mineinabyss.idofront.plugin.listeners
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
Expand All @@ -21,12 +23,15 @@ import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextReplacementConfig
import net.kyori.adventure.translation.GlobalTranslator
import net.minecraft.core.NonNullList
import net.minecraft.network.Connection
import net.minecraft.network.protocol.game.*
import net.minecraft.network.syncher.EntityDataSerializer
import net.minecraft.network.syncher.SynchedEntityData
import org.bukkit.NamespacedKey
import org.bukkit.craftbukkit.inventory.CraftItemStack
import org.bukkit.entity.Player
import org.bukkit.inventory.AnvilInventory
import java.util.*

class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {
Expand Down Expand Up @@ -68,6 +73,16 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {
)
} ?: it
})
is ClientboundContainerSetContentPacket -> {
(connection.player.bukkitEntity.openInventory.topInventory as? AnvilInventory)?.let {
ClientboundContainerSetContentPacket(packet.containerId, packet.stateId,
NonNullList.of(packet.items.first(), *packet.items.map {
CraftItemStack.asNMSCopy(CraftItemStack.asBukkitCopy(it.copy()).editItemMeta {
setDisplayName(persistentDataContainer.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING) ?: return@editItemMeta)
})
}.toTypedArray()), packet.carriedItem)
} ?: packet
}
else -> packet
}, promise
)
Expand All @@ -88,6 +103,7 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {

val ORIGINAL_SIGN_FRONT_LINES = NamespacedKey.fromString("emojy:original_front_lines")!!
val ORIGINAL_SIGN_BACK_LINES = NamespacedKey.fromString("emojy:original_back_lines")!!
val ORIGINAL_ITEM_RENAME_TEXT = NamespacedKey.fromString("emojy:original_item_rename")!!

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

0 comments on commit 717fdf2

Please sign in to comment.