Skip to content

Commit

Permalink
fix: use TextComponent#content instead of Component#serialize during …
Browse files Browse the repository at this point in the history
…regex checks
  • Loading branch information
Boy0000 committed Jul 11, 2024
1 parent 1d5b89f commit 4485728
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
19 changes: 16 additions & 3 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.TextReplacementConfig
import net.kyori.adventure.translation.GlobalTranslator
import org.bukkit.NamespacedKey
Expand All @@ -23,9 +24,21 @@ val ORIGINAL_ITEM_RENAME_TEXT = NamespacedKey.fromString("emojy:original_item_re

fun spaceComponent(space: Int) = Component.textOfChildren(Component.text(Space.of(space)).font(emojyConfig.spaceFont))

private fun Component.asFlatTextContent(): String {
return buildString {
append((this@asFlatTextContent as? TextComponent)?.content())
append(this@asFlatTextContent.children().joinToString("") { it.asFlatTextContent() })
append(this@asFlatTextContent.children().joinToString("") { it.asFlatTextContent() })
(this@asFlatTextContent.hoverEvent()?.value() as? Component)?.let {
append((it as? TextComponent)?.content())
append(it.children().joinToString("") { it.asFlatTextContent() })
}
}
}

fun Component.transformEmotes(locale: Locale? = null, insert: Boolean = false): Component {
var component = GlobalTranslator.render(this, locale ?: Locale.US)
val serialized = component.serialize()
val serialized = component.asFlatTextContent()

for (emote in emojy.emotes) emote.baseRegex.findAll(serialized).forEach { match ->

Expand Down Expand Up @@ -71,7 +84,7 @@ fun Component.transformEmotes(locale: Locale? = null, insert: Boolean = false):

fun Component.escapeEmoteIDs(player: Player?): Component {
var component = this
val serialized = component.serialize()
val serialized = component.asFlatTextContent()

// Replace all unicodes found in default font with a random one
// This is to prevent use of unicodes from the font the chat is in
Expand Down Expand Up @@ -123,7 +136,7 @@ fun Component.escapeEmoteIDs(player: Player?): Component {

fun Component.unescapeEmoteIds(): Component {
var component = this
val serialized = this.serialize()
val serialized = component.asFlatTextContent()

for (emote in emojy.emotes) emote.escapedRegex.findAll(serialized).forEach { match ->
component = component.replaceText(
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=com.mineinabyss
version=0.9
idofrontVersion=0.24.9
idofrontVersion=0.24.12
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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.minecraft.core.NonNullList
import net.minecraft.network.Connection
import net.minecraft.network.chat.ChatType
import net.minecraft.network.chat.Component
Expand All @@ -29,6 +30,7 @@ import net.minecraft.world.item.ItemStack
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 @@ -67,21 +69,21 @@ class EmojyNMSHandler(emojy: EmojyPlugin) : IEmojyNMSHandler {
} ?: it
})
is ClientboundContainerSetSlotPacket -> ClientboundContainerSetSlotPacket(packet.containerId, packet.stateId, packet.slot, packet.item.transformItemNameLore())
//is ClientboundContainerSetContentPacket -> ClientboundContainerSetContentPacket(
// packet.containerId, packet.stateId, NonNullList.of(packet.items.first(),
// *packet.items.map {
// val inv = connection.player.bukkitEntity.openInventory.topInventory
// val bukkit = CraftItemStack.asBukkitCopy(it)

// // If item is firstItem in AnvilInventory we want to set it to have the plain-text displayname
// if (inv is AnvilInventory && inv.firstItem == bukkit)
// bukkit.itemMeta?.persistentDataContainer?.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING)?.let { og ->
// CraftItemStack.asNMSCopy(bukkit.editItemMeta {
// setDisplayName(og)
// })
// } ?: it.transformItemNameLore()
// else it.transformItemNameLore()
// }.toTypedArray()), packet.carriedItem)
is ClientboundContainerSetContentPacket -> ClientboundContainerSetContentPacket(
packet.containerId, packet.stateId, NonNullList.of(packet.items.first(),
*packet.items.map {
val inv = connection.player.bukkitEntity.openInventory.topInventory
val bukkit = CraftItemStack.asBukkitCopy(it)

// If item is firstItem in AnvilInventory we want to set it to have the plain-text displayname
if (inv is AnvilInventory && inv.firstItem == bukkit)
bukkit.itemMeta?.persistentDataContainer?.get(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING)?.let { og ->
CraftItemStack.asNMSCopy(bukkit.editItemMeta {
setDisplayName(og)
})
} ?: 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 }
Expand Down

0 comments on commit 4485728

Please sign in to comment.