Skip to content

Commit

Permalink
refactor: changes to EmojyTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jan 29, 2024
1 parent 97ae253 commit 33388d9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 37 deletions.
12 changes: 8 additions & 4 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.mineinabyss.idofront.messaging.success
import com.mineinabyss.idofront.textcomponents.miniMsg
import net.kyori.adventure.inventory.Book
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TranslatableComponent
import net.kyori.adventure.translation.GlobalTranslator
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
Expand All @@ -16,12 +17,15 @@ import org.bukkit.entity.Player
class EmojyCommands : IdofrontCommandExecutor(), TabCompleter {
override val commands = commands(emojy.plugin) {
"emojy" {
"lang" {
emojy.languages.map { it.locale }.joinToString { it.toString() }.broadcastVal()
sender.sendMessage(emojy.languages.any { it.locale == (sender as Player).locale() }.toString())
}
"test" {
action {
val lang = emojy.languages.first()
sender.sendMessage(GlobalTranslator.render(("<lang:mineinabyss.tutorial.welcome.1>" + " : <lang:mineinabyss.tutorial.welcome.2>").miniMsg(), lang.locale))
sender.sendMessage(GlobalTranslator.render(("<lang:mineinabyss.tutorial.welcome.1>" + "</lang>" + " : <lang:mineinabyss.tutorial.welcome.2>").miniMsg(), lang.locale))
//emojy.languages.map { it.locale to it.keys }.joinToString(", ").broadcastVal()
val lang = (sender as? Player)?.locale()?.takeIf { it in emojy.languages.map { it.locale } } ?: emojy.languages.last().locale
sender.sendMessage(GlobalTranslator.render(("<lang:mineinabyss.tutorial.welcome.1>" + " : <lang:mineinabyss.tutorial.welcome.2>").miniMsg(), lang))
sender.sendMessage(GlobalTranslator.render(("<lang:mineinabyss.tutorial.welcome.1>" + "</lang>" + " : <lang:mineinabyss.tutorial.welcome.2>").miniMsg(), lang))
}
}
"list" {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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.TranslatableComponent
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.kyori.adventure.translation.GlobalTranslator
import org.bukkit.entity.Player
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/com/mineinabyss/emojy/EmojyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class EmojyPlugin : JavaPlugin() {
}.toSet()
})

GlobalTranslator.translator().sources().filter { it.name() == Key.key("emojy", "localization") }.forEach(GlobalTranslator.translator()::removeSource)
GlobalTranslator.translator().sources().filter { it.name() == EmojyTranslator.key }.forEach(GlobalTranslator.translator()::removeSource)
GlobalTranslator.translator().addSource(EmojyTranslator())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package com.mineinabyss.emojy.translator

import com.mineinabyss.emojy.emojy
import com.mineinabyss.idofront.textcomponents.IdofrontTextComponents
import com.mineinabyss.idofront.textcomponents.miniMsg
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TranslatableComponent
import net.kyori.adventure.translation.Translator
import java.util.*


class EmojyTranslator : MiniMessageTranslator() {
override fun getMiniMessageString(key: String, locale: Locale) =
emojy.languages.find { it.locale == locale }?.keys?.get(key)
class EmojyTranslator : Translator {

override fun name() = Key.key("emojy", "localization")
companion object {
val key = Key.key("emojy", "localization")
}

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 resultingComponent = mmString.miniMsg(EmojyArgumentTag(component.args()).takeIf { component.args().isNotEmpty() } ?: IdofrontTextComponents.globalResolver)
return when {
component.children().isEmpty() -> resultingComponent
else -> resultingComponent.children(component.children())
}
}

override fun name() = key
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class EmojyNMSHandler : IEmojyNMSHandler {

override fun readNbt(): CompoundTag? {
return super.readNbt()?.apply {
transform(this, EmojyNMSHandlers.transformer())
transform(this, EmojyNMSHandlers.transformer(player))
}
}

Expand Down

0 comments on commit 33388d9

Please sign in to comment.