Skip to content

Commit

Permalink
fix: handling of books & formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jun 24, 2024
1 parent 8ed7022 commit e26eca0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
13 changes: 6 additions & 7 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.mineinabyss.idofront.textcomponents.serialize
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextReplacementConfig
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.kyori.adventure.translation.GlobalTranslator
import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
Expand Down Expand Up @@ -37,11 +36,11 @@ fun Component.transformEmotes(locale: Locale? = null, insert: Boolean = false):
TextReplacementConfig.builder()
.match(emote.baseRegex.pattern).once()
.replacement(
emote.formattedUnicode(
Component.textOfChildren(emote.formattedUnicode(
insert = insert,
colorable = colorable,
bitmapIndex = bitmapIndex
)
))
)
.build()
)
Expand Down Expand Up @@ -91,8 +90,8 @@ fun Component.escapeEmoteIDs(player: Player?): Component {

component = component.replaceText(
TextReplacementConfig.builder()
.matchLiteral(match.value).once()
.replacement("\\${match.value}".miniMsg())
.match("(?<!\\\\)${match.value}")
.replacement(Component.text("\\${match.value}"))
.build()
)
}
Expand All @@ -101,7 +100,7 @@ fun Component.escapeEmoteIDs(player: Player?): Component {
if (gif.checkPermission(player)) return@forEach
component = component.replaceText(
TextReplacementConfig.builder()
.matchLiteral(match.value).once()
.match("(?<!\\\\)${match.value}")
.replacement("\\${match.value}".miniMsg())
.build()
)
Expand All @@ -113,7 +112,7 @@ fun Component.escapeEmoteIDs(player: Player?): Component {

component = component.replaceText(
TextReplacementConfig.builder()
.matchLiteral(match.value).once()
.match("(?<!\\\\)${match.value}")
.replacement("\\:space_$space:".miniMsg())
.build()
)
Expand Down
14 changes: 9 additions & 5 deletions core/src/main/kotlin/com/mineinabyss/emojy/config/EmojyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
package com.mineinabyss.emojy.config

import co.touchlab.kermit.Severity
import com.mineinabyss.emojy.*
import com.mineinabyss.idofront.messaging.*
import com.mineinabyss.emojy.emojy
import com.mineinabyss.emojy.emojyConfig
import com.mineinabyss.emojy.spaceComponent
import com.mineinabyss.emojy.templates
import com.mineinabyss.idofront.serialization.KeySerializer
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import kotlinx.serialization.*
import kotlinx.serialization.EncodeDefault.Mode.NEVER
import kotlinx.serialization.Transient
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.event.HoverEvent
Expand Down Expand Up @@ -111,7 +112,7 @@ data class Emotes(val emotes: Set<Emote> = mutableSetOf()) {

private val permission get() = "emojy.emote.$id"
private val fontPermission get() = "emojy.font.$font"
private fun fontProvider() = FontProvider.bitMap(texture, height, ascent, unicodes.map { it.toString() })
private fun fontProvider() = FontProvider.bitMap(texture, height, ascent, unicodes)
fun appendFont(resourcePack: ResourcePack) =
(resourcePack.font(font)?.toBuilder() ?: Font.font().key(font)).addProvider(fontProvider()).build()

Expand Down Expand Up @@ -152,7 +153,10 @@ data class Emotes(val emotes: Set<Emote> = mutableSetOf()) {
("<red>Type <i>:</i>$id<i>:</i> or <i><u>Shift + Click</i> this to use this emote").miniMsg()
)
)
return if (appendSpace) Component.textOfChildren(bitmap, spaceComponent(2)) else bitmap

if (appendSpace) bitmap.append(spaceComponent(2))

return bitmap
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.mineinabyss.idofront.items.editItemMeta
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 io.papermc.paper.event.player.PlayerOpenSignEvent
import kotlinx.coroutines.delay
import net.minecraft.core.BlockPos
Expand All @@ -21,6 +20,7 @@ import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.block.SignChangeEvent
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.event.player.PlayerEditBookEvent

@Suppress("UnstableApiUsage")
class EmojyListener : Listener {
Expand All @@ -36,8 +36,16 @@ class EmojyListener : Listener {
val state = (block.state as Sign)
val type = DataType.asList(DataType.STRING)
val sideLines = lines().map { it.serialize() }.toList()
val frontLines = if (side == Side.FRONT) sideLines else state.persistentDataContainer.getOrDefault(ORIGINAL_SIGN_FRONT_LINES, type, mutableListOf("", "", "", ""))
val backLines = if (side == Side.BACK) sideLines else state.persistentDataContainer.getOrDefault(ORIGINAL_SIGN_BACK_LINES, type, mutableListOf("", "", "", ""))
val frontLines = if (side == Side.FRONT) sideLines else state.persistentDataContainer.getOrDefault(
ORIGINAL_SIGN_FRONT_LINES,
type,
mutableListOf("", "", "", "")
)
val backLines = if (side == Side.BACK) sideLines else state.persistentDataContainer.getOrDefault(
ORIGINAL_SIGN_BACK_LINES,
type,
mutableListOf("", "", "", "")
)

state.persistentDataContainer.set(ORIGINAL_SIGN_FRONT_LINES, type, frontLines)
state.persistentDataContainer.set(ORIGINAL_SIGN_BACK_LINES, type, backLines)
Expand All @@ -52,29 +60,41 @@ class EmojyListener : Listener {
fun PlayerOpenSignEvent.onSignEdit() {
if (cause == PlayerOpenSignEvent.Cause.PLACE) return

sign.persistentDataContainer.get(when (sign.getInteractableSideFor(player)) {
Side.FRONT -> ORIGINAL_SIGN_FRONT_LINES
Side.BACK -> ORIGINAL_SIGN_BACK_LINES
}, DataType.asList(DataType.STRING))?.forEachIndexed { index, s ->
sign.persistentDataContainer.get(
when (sign.getInteractableSideFor(player)) {
Side.FRONT -> ORIGINAL_SIGN_FRONT_LINES
Side.BACK -> ORIGINAL_SIGN_BACK_LINES
}, DataType.asList(DataType.STRING)
)?.forEachIndexed { index, s ->
sign.getSide(side).line(index, s.miniMsg())
}
sign.update(true)
isCancelled = true
emojy.plugin.launch {
delay(2.ticks)
(player as CraftPlayer).handle.level().getBlockEntity(BlockPos(sign.x, sign.y, sign.z), BlockEntityType.SIGN).ifPresent {
it.setAllowedPlayerEditor(player.uniqueId)
(player as CraftPlayer).handle.openTextEdit(it, side == Side.FRONT)
}
(player as CraftPlayer).handle.level()
.getBlockEntity(BlockPos(sign.x, sign.y, sign.z), BlockEntityType.SIGN).ifPresent {
it.setAllowedPlayerEditor(player.uniqueId)
(player as CraftPlayer).handle.openTextEdit(it, side == Side.FRONT)
}
}
}

@EventHandler
fun PrepareAnvilEvent.onAnvil() {
result = result?.editItemMeta {
if (inventory.renameText == null || result?.itemMeta?.hasDisplayName() != true) {
persistentDataContainer.remove(ORIGINAL_ITEM_RENAME_TEXT) }
else persistentDataContainer.set(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING, inventory.renameText!!)
persistentDataContainer.remove(ORIGINAL_ITEM_RENAME_TEXT)
} else persistentDataContainer.set(ORIGINAL_ITEM_RENAME_TEXT, DataType.STRING, inventory.renameText!!)
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun PlayerEditBookEvent.onBookEdit() {
if (isSigning) newBookMeta = newBookMeta.apply {
pages(pages().map {
it.escapeEmoteIDs(player).transformEmotes().unescapeEmoteIds()
})
}
}
}
Expand Down

0 comments on commit e26eca0

Please sign in to comment.