Skip to content

Commit

Permalink
refactor: various optimization tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Oct 19, 2024
1 parent c44d595 commit 899d754
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 44 deletions.
6 changes: 3 additions & 3 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ val emojyConfig by DI.observe<EmojyConfig>()
val templates get() = DI.observe<EmojyTemplates>().getOrNull() ?: EmojyTemplates()
interface EmojyContext {
val plugin: EmojyPlugin
val emotes: Set<Emotes.Emote>
val gifs: Set<Gifs.Gif>
val languages: Set<EmojyLanguage>
val emotes: Array<Emotes.Emote>
val gifs: Array<Gifs.Gif>
val languages: Array<EmojyLanguage>
val handler: IEmojyNMSHandler
val logger: ComponentLogger
}
8 changes: 4 additions & 4 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import team.unnamed.creative.texture.Texture
object EmojyGenerator {
private val gifFolder = emojy.plugin.dataFolder.resolve("gifs").apply { mkdirs() }
private val emotesFolder = emojy.plugin.dataFolder.resolve("emotes").apply { mkdirs() }
private val spaceProvider = FontProvider.space(Space.entries.asSequence().filterNot(Space.NULL::equals).associate { it.unicode to it.toNumber() })

fun generateResourcePack() {
val resourcePack = ResourcePack.resourcePack()
emojy.plugin.dataFolder.resolve("assets").deleteRecursively()

val textureFiles = emotesFolder.walkTopDown().filter { it.isFile }.associateBy { it.name }
val fontSpaceProvider = FontProvider.space().advance("\uE101", -1).build()
emojy.emotes.mapNotNull { emote ->
emojy.emotes.forEach { emote ->
resourcePack.font(emote.font)?.takeIf { emote.isMultiBitmap }?.let { font ->
when {
// Add a -1 advance to the font for ease of use
Expand All @@ -34,7 +35,7 @@ object EmojyGenerator {
resourcePack.font(font.toBuilder().addProvider(fontSpaceProvider).build())
// If the font has already added an entry for the emote, skip it
font.providers().any { it is BitMapFontProvider && it.file() == emote.texture } ->
return@mapNotNull emojy.logger.w("Skipping ${emote.id}-font because it is a bitmap and already added").let { null }
return@forEach emojy.logger.w("Skipping ${emote.id}-font because it is a bitmap and already added").let { null }
}
}

Expand All @@ -49,8 +50,7 @@ object EmojyGenerator {
it.generateSplitGif(resourcePack)
it.font().addTo(resourcePack)
}
Font.font(emojyConfig.spaceFont, FontProvider.space(Space.entries.filterNot(Space.NULL::equals)
.associate { it.unicode to it.toNumber() })).addTo(resourcePack)
Font.font(emojyConfig.spaceFont, spaceProvider).addTo(resourcePack)

MinecraftResourcePackWriter.minecraft().writeToZipFile(emojy.plugin.dataFolder.resolve("pack.zip"), resourcePack)
}
Expand Down
45 changes: 22 additions & 23 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.emojy

import com.mineinabyss.emojy.config.Emotes
import com.mineinabyss.emojy.config.SPACE_PERMISSION
import com.mineinabyss.idofront.font.Space
import com.mineinabyss.idofront.textcomponents.miniMsg
Expand All @@ -19,7 +20,10 @@ val colorableRegex: Regex = "\\|(c|colorable)".toRegex()
val bitmapIndexRegex: Regex = "\\|([0-9]+)".toRegex()

private val defaultKey = Key.key("default")
private val defaultEmoteReplacementConfigs = emojy.emotes.filter { it.font == defaultKey }.flatMap { it.unicodes }
.map { TextReplacementConfig.builder().match(it).replacement(randomComponent).build() }
private val randomKey = Key.key("random")
private val randomComponent = Component.text("random").font(randomKey)

val ORIGINAL_SIGN_FRONT_LINES = NamespacedKey.fromString("emojy:original_front_lines")!!
val ORIGINAL_SIGN_BACK_LINES = NamespacedKey.fromString("emojy:original_back_lines")!!
Expand All @@ -29,15 +33,17 @@ fun spaceString(space: Int) = "<font:${emojyConfig.spaceFont.asMinimalString()}>
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() })
}
var flattened = ""
val flatText = (this@asFlatTextContent as? TextComponent) ?: return flattened
flattened += flatText.content()
flattened += flatText.children().joinToString("") { it.asFlatTextContent() }
(flatText.hoverEvent()?.value() as? Component)?.let { hover ->
val hoverText = hover as? TextComponent ?: return@let
flattened += hoverText.content()
flattened += hoverText.children().joinToString("") { it.asFlatTextContent() }
}

return flattened
}

fun String.transformEmotes(insert: Boolean = false): String {
Expand Down Expand Up @@ -152,17 +158,10 @@ fun Component.escapeEmoteIDs(player: Player?): Component {

// 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
for (emote in emojy.emotes.filter { it.font == defaultKey && !it.checkPermission(player) }) emote.unicodes.forEach {
component = component.replaceText(
TextReplacementConfig.builder()
.matchLiteral(it)
.replacement(it.miniMsg().font(randomKey))
.build()
)
}
for (defaultConfig in defaultEmoteReplacementConfigs) component = component.replaceText(defaultConfig)

for (emote in emojy.emotes) emote.baseRegex.findAll(serialized).forEach { match ->
if (emote.checkPermission(player)) return@forEach
for (emote in emojy.emotes) for (match in emote.baseRegex.findAll(serialized)) {
if (emote.checkPermission(player)) continue

component = component.replaceText(
TextReplacementConfig.builder()
Expand All @@ -172,8 +171,8 @@ fun Component.escapeEmoteIDs(player: Player?): Component {
)
}

for (gif in emojy.gifs) gif.baseRegex.findAll(serialized).forEach { match ->
if (gif.checkPermission(player)) return@forEach
for (gif in emojy.gifs) for (match in gif.baseRegex.findAll(serialized)) {
if (gif.checkPermission(player)) continue
component = component.replaceText(
TextReplacementConfig.builder()
.match("(?<!\\\\)${match.value}")
Expand All @@ -182,9 +181,9 @@ fun Component.escapeEmoteIDs(player: Player?): Component {
)
}

spaceRegex.findAll(serialized).forEach { match ->
if (player?.hasPermission(SPACE_PERMISSION) != false) return@forEach
val space = match.groupValues[1].toIntOrNull() ?: return@forEach
for (match in spaceRegex.findAll(serialized)) {
if (player?.hasPermission(SPACE_PERMISSION) != false) continue
val space = match.groupValues[1].toIntOrNull() ?: continue

component = component.replaceText(
TextReplacementConfig.builder()
Expand Down
19 changes: 7 additions & 12 deletions core/src/main/kotlin/com/mineinabyss/emojy/config/EmojyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,13 @@ data class Gifs(val gifs: Set<Gif> = mutableSetOf()) {
@EncodeDefault(NEVER) val height: Int = 8,
@EncodeDefault(NEVER) val type: GifType = GifType.SHADER
) {
@Transient
val framePath = Key.key(_framePath.asString().removeSuffix("/") + "/")
@Transient
val font = Key.key(framePath.namespace(), id)
@Transient
val permission = "emojy.gif.$id"
@Transient
val baseRegex = "(?<!\\\\):$id:".toRegex()
@Transient
val escapedRegex = "\\\\:$id:".toRegex()

val gifFile get() = emojy.plugin.dataFolder.resolve("gifs/${id}.gif").apply { mkdirs() }
@Transient val framePath = Key.key(_framePath.asString().removeSuffix("/") + "/")
@Transient val font = Key.key(framePath.namespace(), id)
@Transient val permission = "emojy.gif.$id"
@Transient val baseRegex = "(?<!\\\\):$id:".toRegex()
@Transient val escapedRegex = "\\\\:$id:".toRegex()

@Transient val gifFile = emojy.plugin.dataFolder.resolve("gifs/${id}.gif").apply { mkdirs() }
private var aspectRatio by Delegates.notNull<Float>()

enum class GifType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EmojyTranslator : Translator {
override fun translate(key: String, locale: Locale) = null

override fun translate(component: TranslatableComponent, locale: Locale): Component? {
val mmString = emojy.languages.find { it.locale == locale }?.keys?.get(component.key()) ?: return null
val mmString = emojy.languages.firstOrNull { it.locale == locale }?.keys?.get(component.key()) ?: return null
val resultingComponent = mmString.miniMsg(EmojyArgumentTag(component.arguments()).takeIf { component.arguments().isNotEmpty() } ?: IdofrontTextComponents.globalResolver)
return when {
component.children().isEmpty() -> resultingComponent
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.25.4
idofrontVersion=0.25.16

0 comments on commit 899d754

Please sign in to comment.