Skip to content

Commit

Permalink
refactor: edit codec instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed May 28, 2024
1 parent 7b1c993 commit 0e6cc52
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 347 deletions.
4 changes: 2 additions & 2 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fun Component.transform(player: Player?, insert: Boolean, unescape: Boolean = tr
val legacy = LegacyComponentSerializer.legacySection()
private val spaceRegex: Regex = "(?<!\\\\):space_(-?\\d+):".toRegex()
private val escapedSpaceRegex: Regex = "\\\\(:space_(-?\\d+):)".toRegex()
private val colorableRegex: Regex = "\\|(c|colorable)".toRegex()
private val bitmapIndexRegex: Regex = "\\|([0-9]+)".toRegex()
val colorableRegex: Regex = "\\|(c|colorable)".toRegex()
val bitmapIndexRegex: Regex = "\\|([0-9]+)".toRegex()
//TODO Tags like rainbow and gradient, which split the text into multiple children, will break replacement below
// Above is due to Adventure-issue, nothing on our end for once. https://github.com/KyoriPowered/adventure/issues/872
// Find out why this is called 3 times
Expand Down
14 changes: 1 addition & 13 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,10 @@ class EmojyPlugin : JavaPlugin() {

EmojyGenerator.generateResourcePack()

listeners(EmojyListener())

server.onlinePlayers.forEach {
emojy.handler.inject(it)
}

EmojyCommands()

}

override fun onDisable() {
server.onlinePlayers.forEach {
emojy.handler.uninject(it)
}
}

fun createEmojyContext() {
DI.remove<EmojyConfig>()
DI.add(config<EmojyConfig>("config", dataPath, EmojyConfig(), onLoad = {
Expand All @@ -70,7 +58,7 @@ class EmojyPlugin : JavaPlugin() {
config<Map<String, String>>(it, dataPath / "languages", mapOf()).getOrLoad())
}.toSet()
override val logger by plugin.observeLogger()
override val handler: IEmojyNMSHandler = EmojyNMSHandlers.setup()
override val handler: IEmojyNMSHandler = EmojyNMSHandlers.setup(this@EmojyPlugin)
})

GlobalTranslator.translator().sources().filter { it.name() == EmojyTranslator.key }.forEach(GlobalTranslator.translator()::removeSource)
Expand Down
53 changes: 7 additions & 46 deletions core/src/main/kotlin/com/mineinabyss/emojy/nms/EmojyNMSHandlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,29 @@ package com.mineinabyss.emojy.nms

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.mineinabyss.emojy.EmojyPlugin
import com.mineinabyss.emojy.escapeEmoteIDs
import com.mineinabyss.emojy.transform
import com.mineinabyss.emojy.transformEmoteIDs
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
import org.bukkit.Bukkit
import org.bukkit.entity.Player

object EmojyNMSHandlers {

private val SUPPORTED_VERSION = arrayOf(/*"v1_19_R1", "v1_19_R2", "v1_19_R3", "v1_20_R1", "v1_20_R2", "v1_20_R3", */"v1_20_R4")
private val SUPPORTED_VERSION = arrayOf("v1_20_R4")

fun setup(): IEmojyNMSHandler {
fun setup(emojy: EmojyPlugin): IEmojyNMSHandler {
SUPPORTED_VERSION.forEach { version ->
runCatching {
//Class.forName("org.bukkit.craftbukkit.entity.CraftPlayer")
return Class.forName("com.mineinabyss.emojy.nms.${version}.EmojyNMSHandler").getConstructor()
.newInstance() as IEmojyNMSHandler
}
return Class.forName("com.mineinabyss.emojy.nms.${version}.EmojyNMSHandler").getConstructor(EmojyPlugin::class.java)
.newInstance(emojy) as IEmojyNMSHandler
}.onFailure { it.printStackTrace() }
}

throw IllegalStateException("Unsupported server version")
}

private val gson = GsonComponentSerializer.gson()
private val plain = PlainTextComponentSerializer.plainText()
//TODO toPlainText fixes the anvil issue with tags being escaped
// It does break all other formatting everywhere else though by removing tags
// serialize() doesnt but keeps the escaped tag from gson
// anvil goes like this, inputItem -> readUtf -> renameField -> writeNbt from raw string, which is why it breaks
fun JsonObject.formatString(player: Player? = null) : String {
return if (this.has("args") || this.has("text") || this.has("extra") || this.has("translate")) {
gson.serialize(gson.deserializeFromTree(this)/*.toPlainText()*/.serialize().miniMsg().transform(player, true, true))
} else this.toString()
}

fun writeTransformer(player: Player?, insert: Boolean, unescape: Boolean) = { string: String ->
runCatching {
val jsonObject = JsonParser.parseString(string).takeIf { it.isJsonObject }?.asJsonObject ?: return@runCatching string
if (jsonObject.has("args") || jsonObject.has("text") || jsonObject.has("extra") || jsonObject.has("translate")) {
val formatted = gson.deserializeFromTree(jsonObject).transformEmoteIDs(player, insert, unescape)
gson.serialize(formatted)
} else string
}.getOrNull() ?: string
}

fun readTransformer(player: Player?) = { string: String ->
runCatching {
val jsonObject = JsonParser.parseString(string).takeIf { it.isJsonObject }?.asJsonObject ?: return@runCatching string
if (jsonObject.has("args") || jsonObject.has("text") || jsonObject.has("extra") || jsonObject.has("translate")) {
val formatted = gson.deserializeFromTree(jsonObject).escapeEmoteIDs(player)
gson.serialize(formatted)
} else string
}.getOrNull() ?: string
}

fun transformer(player: Player? = null) = { string: String ->
runCatching {
val element = JsonParser.parseString(string)
if (element.isJsonObject) element.asJsonObject.formatString(player)
else string
}.getOrNull() ?: string
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.mineinabyss.emojy.nms

import org.bukkit.entity.Player
import java.util.Locale

interface IEmojyNMSHandler {

fun inject(player: Player)
val locals: MutableSet<Locale>

fun uninject(player: Player)
fun addLocaleCodec(locale: Locale)

val supported get() = false
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ dependencyResolutionManagement {
}
}

include("core", /*"v1_19_R1", "v1_19_R2", "v1_19_R3", "v1_20_R1", "v1_20_R2", */"v1_20_R3", "v1_20_R4")
include("core", "v1_20_R4")
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.mineinabyss.emojy
package com.mineinabyss.emojy.nms.v1_20_R4

import com.mineinabyss.emojy.emojy
import com.mineinabyss.emojy.escapeEmoteIDs
import com.mineinabyss.emojy.nms.EmojyNMSHandlers
import com.mineinabyss.emojy.transform
import com.mineinabyss.idofront.items.editItemMeta
import com.mineinabyss.idofront.messaging.logError
import com.mineinabyss.idofront.messaging.logVal
Expand All @@ -22,14 +25,14 @@ import org.bukkit.inventory.AnvilInventory
class EmojyListener : Listener {

@EventHandler
fun PlayerJoinEvent.injectPlayer() {
emojy.handler.inject(player)
fun PlayerJoinEvent.onJoin() {
emojy.handler.addLocaleCodec(player.locale())
}

// Replace with result not original message to avoid borking other chat formatting
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
fun AsyncChatDecorateEvent.onPlayerChat() {
result(result().transform(player(), true, true))
result(result().escapeEmoteIDs(player()))
}

@EventHandler
Expand Down
Loading

0 comments on commit 0e6cc52

Please sign in to comment.