Skip to content

Commit

Permalink
refactor: recursively check emotes texture-folder & code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Aug 28, 2024
1 parent 7828da4 commit e01f36d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 25 additions & 20 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -35,7 +32,7 @@ class EmojyPlugin : JavaPlugin() {

EmojyGenerator.generateResourcePack()

EmojyBrigadierCommands.registerCommands()
EmojyCommands.registerCommands()
}

fun createEmojyContext() {
Expand Down
28 changes: 12 additions & 16 deletions core/src/main/kotlin/com/mineinabyss/emojy/config/EmojyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ data class EmojyConfig(
val ignoredGifIds: Set<String> = mutableSetOf(),
val ignoredFonts: Set<@Serializable(KeySerializer::class) Key> = mutableSetOf()
) {
val ignoredEmotes: Set<Emotes.Emote>
get() = emojy.emotes.filter { it.id in ignoredEmoteIds || it.font in ignoredFonts }.toSet()
val ignoredGifs: Set<Gifs.Gif>
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() }
}
}

Expand All @@ -75,31 +73,29 @@ data class Emotes(val emotes: Set<Emote> = 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 = "(?<!\\\\):$id(\\|(c|colorable|\\d+))*:".toRegex()
@Transient
val escapedRegex = "\\\\:$id(\\|(c|colorable|\\d+))*:".toRegex()
@Transient val isMultiBitmap = bitmapWidth > 1 || bitmapHeight > 1
@Transient val baseRegex = "(?<!\\\\):$id(\\|(c|colorable|\\d+))*:".toRegex()
@Transient val escapedRegex = "\\\\:$id(\\|(c|colorable|\\d+))*:".toRegex()

// Beginning of Private Use Area \uE000 -> uF8FF
// Option: (Character.toCodePoint('\uE000', '\uFF8F')/37 + getIndex())
@Transient
private val lastUsedUnicode: MutableMap<Key, Int> = mutableMapOf()
@Transient private val lastUsedUnicode: MutableMap<Key, Int> = 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<CharArray> instead
val unicodes: MutableList<String> 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
}
}
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.21
idofrontVersion=0.25.4

0 comments on commit e01f36d

Please sign in to comment.