From e01f36df63da3bfb8f1e84526c807d48d30d0a5c Mon Sep 17 00:00:00 2001 From: Boy Date: Wed, 28 Aug 2024 13:15:18 +0200 Subject: [PATCH] refactor: recursively check emotes texture-folder & code cleanup --- ...yBrigadierCommands.kt => EmojyCommands.kt} | 2 +- .../com/mineinabyss/emojy/EmojyGenerator.kt | 45 ++++++++++--------- .../com/mineinabyss/emojy/EmojyPlugin.kt | 5 +-- .../mineinabyss/emojy/config/EmojyConfig.kt | 28 +++++------- gradle.properties | 2 +- 5 files changed, 40 insertions(+), 42 deletions(-) rename core/src/main/kotlin/com/mineinabyss/emojy/{EmojyBrigadierCommands.kt => EmojyCommands.kt} (98%) diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyBrigadierCommands.kt b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyCommands.kt similarity index 98% rename from core/src/main/kotlin/com/mineinabyss/emojy/EmojyBrigadierCommands.kt rename to core/src/main/kotlin/com/mineinabyss/emojy/EmojyCommands.kt index 818d325..a475e35 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyBrigadierCommands.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyCommands.kt @@ -6,7 +6,7 @@ import com.mineinabyss.idofront.textcomponents.miniMsg import net.kyori.adventure.text.Component import org.bukkit.entity.Player -object EmojyBrigadierCommands { +object EmojyCommands { fun registerCommands() { emojy.plugin.commands { diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyGenerator.kt b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyGenerator.kt index 6628ac1..a28823e 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyGenerator.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyGenerator.kt @@ -4,8 +4,7 @@ import com.aaaaahhhhhhh.bananapuncher714.gifconverter.GifConverter import com.mineinabyss.emojy.config.Gifs import com.mineinabyss.idofront.font.Space import com.mineinabyss.idofront.font.Space.Companion.toNumber -import com.mineinabyss.idofront.messaging.logError -import com.mineinabyss.idofront.messaging.logWarn +import com.mineinabyss.idofront.resourcepacks.ResourcePacks import net.kyori.adventure.key.Key import team.unnamed.creative.ResourcePack import team.unnamed.creative.base.Writable @@ -15,37 +14,43 @@ import team.unnamed.creative.font.FontProvider import team.unnamed.creative.font.SpaceFontProvider import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter import team.unnamed.creative.texture.Texture -import java.io.File object EmojyGenerator { private val gifFolder = emojy.plugin.dataFolder.resolve("gifs").apply { mkdirs() } private val emotesFolder = emojy.plugin.dataFolder.resolve("emotes").apply { mkdirs() } + fun generateResourcePack() { val resourcePack = ResourcePack.resourcePack() - File(emojy.plugin.dataFolder, "/assets").deleteRecursively() + emojy.plugin.dataFolder.resolve("assets").deleteRecursively() - emojy.emotes.forEach { emote -> - val font = resourcePack.font(emote.font) - if (emote.isMultiBitmap && font != null) when { - // Add a -1 advance to the font for ease of use - // Mainly needed for ESC menu and default font due to no other font being supported - font.providers().none { it is SpaceFontProvider } -> - resourcePack.font(font.toBuilder().addProvider(FontProvider.space().advance("\uE101", -1).build()).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@forEach emojy.logger.d("Skipping ${emote.id}-font because it is a bitmap and already added") + val textureFiles = emotesFolder.walkTopDown().filter { it.isFile }.associateBy { it.name } + val fontSpaceProvider = FontProvider.space().advance("\uE101", -1).build() + emojy.emotes.mapNotNull { emote -> + resourcePack.font(emote.font)?.takeIf { emote.isMultiBitmap }?.let { font -> + when { + // Add a -1 advance to the font for ease of use + // Mainly needed for ESC menu and default font due to no other font being supported + font.providers().none { it is SpaceFontProvider } -> + 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 } + } } - resourcePack.font(emote.appendFont(resourcePack)) - emotesFolder.listFiles()?.find { f -> f.nameWithoutExtension == emote.texture.value().substringAfterLast("/").removeSuffix(".png") }?.let { - resourcePack.texture(Texture.texture(emote.texture, Writable.file(it))) - } ?: emojy.logger.d("Could not find texture for ${emote.id}") + emote.appendFont(resourcePack).addTo(resourcePack) + val texture = textureFiles[emote.texture.value().substringAfterLast("/")] + if (texture == null && ResourcePacks.defaultVanillaResourcePack?.texture(emote.texture) == null) + emojy.logger.w("Could not find texture for ${emote.id}") + Texture.texture(emote.texture, texture?.let(Writable::file) ?: Writable.EMPTY).addTo(resourcePack) } + emojy.gifs.forEach { it.generateSplitGif(resourcePack) - resourcePack.font(it.font()) + it.font().addTo(resourcePack) } - resourcePack.font(Font.font(emojyConfig.spaceFont, FontProvider.space(Space.entries.filterNot(Space.NULL::equals).associate { it.unicode to it.toNumber() }))) + Font.font(emojyConfig.spaceFont, FontProvider.space(Space.entries.filterNot(Space.NULL::equals) + .associate { it.unicode to it.toNumber() })).addTo(resourcePack) MinecraftResourcePackWriter.minecraft().writeToZipFile(emojy.plugin.dataFolder.resolve("pack.zip"), resourcePack) } diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyPlugin.kt b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyPlugin.kt index ed7d726..b3c1347 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/EmojyPlugin.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/EmojyPlugin.kt @@ -13,9 +13,6 @@ import com.mineinabyss.idofront.di.DI import com.mineinabyss.idofront.messaging.ComponentLogger import com.mineinabyss.idofront.messaging.injectLogger import com.mineinabyss.idofront.messaging.observeLogger -import com.mineinabyss.idofront.plugin.dataPath -import com.mineinabyss.idofront.plugin.listeners -import net.kyori.adventure.key.Key import net.kyori.adventure.translation.GlobalTranslator import org.bukkit.plugin.java.JavaPlugin import java.util.* @@ -35,7 +32,7 @@ class EmojyPlugin : JavaPlugin() { EmojyGenerator.generateResourcePack() - EmojyBrigadierCommands.registerCommands() + EmojyCommands.registerCommands() } fun createEmojyContext() { diff --git a/core/src/main/kotlin/com/mineinabyss/emojy/config/EmojyConfig.kt b/core/src/main/kotlin/com/mineinabyss/emojy/config/EmojyConfig.kt index 38e46ee..89a30c6 100644 --- a/core/src/main/kotlin/com/mineinabyss/emojy/config/EmojyConfig.kt +++ b/core/src/main/kotlin/com/mineinabyss/emojy/config/EmojyConfig.kt @@ -49,10 +49,8 @@ data class EmojyConfig( val ignoredGifIds: Set = mutableSetOf(), val ignoredFonts: Set<@Serializable(KeySerializer::class) Key> = mutableSetOf() ) { - val ignoredEmotes: Set - get() = emojy.emotes.filter { it.id in ignoredEmoteIds || it.font in ignoredFonts }.toSet() - val ignoredGifs: Set - get() = emojy.gifs.filter { it.id in ignoredGifIds || it.font in ignoredFonts }.toSet() + val ignoredEmotes by lazy { emojy.emotes.filter { it.id in ignoredEmoteIds || it.font in ignoredFonts }.toSet() } + val ignoredGifs by lazy { emojy.gifs.filter { it.id in ignoredGifIds || it.font in ignoredFonts }.toSet() } } } @@ -75,31 +73,29 @@ data class Emotes(val emotes: Set = mutableSetOf()) { @EncodeDefault(NEVER) val bitmapWidth: Int = template?.bitmapWidth ?: 1, @EncodeDefault(NEVER) val bitmapHeight: Int = template?.bitmapHeight ?: 1, ) { - val isMultiBitmap: Boolean get() = bitmapWidth > 1 || bitmapHeight > 1 - @Transient - val baseRegex = "(? 1 || bitmapHeight > 1 + @Transient val baseRegex = "(? uF8FF // Option: (Character.toCodePoint('\uE000', '\uFF8F')/37 + getIndex()) - @Transient - private val lastUsedUnicode: MutableMap = mutableMapOf() + @Transient private val lastUsedUnicode: MutableMap = mutableMapOf() // We get this lazily so we dont need to use a function and check every time - // but also because EmojyContext needs to be registered, so a normal vbal does not work + // but also because EmojyContext needs to be registered, so a normal val does not work //TODO Rework to be List instead val unicodes: MutableList by lazy { mutableListOf("").apply { for (i in 0 until bitmapHeight) { for (j in 0 until bitmapWidth) { val lastUnicode = lastUsedUnicode[font] ?: 0 - val row = ((getOrNull(i) ?: "") + Character.toChars( + val index = getOrNull(i) + val row = (index ?: "") + Character.toChars( PRIVATE_USE_FIRST + lastUnicode + emojy.emotes .filter { it.font == font }.map { it }.indexOf(this@Emote) - ).firstOrNull().toString()) - if (getOrNull(i) == null) - add(i, row) else set(i, row) + ).firstOrNull().toString() + + if (index == null) add(i, row) else set(i, row) lastUsedUnicode[font] = lastUnicode + 1 } } diff --git a/gradle.properties b/gradle.properties index 73f4720..69f07d4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group=com.mineinabyss version=0.9 -idofrontVersion=0.24.21 +idofrontVersion=0.25.4