Add ComponentMessage, for TranslatableComponents in Paper >1.20

This commit is contained in:
2024-11-14 22:08:06 +01:00
parent a78974b903
commit d115975403
5 changed files with 90 additions and 0 deletions
+2
View File
@@ -32,4 +32,6 @@ tasks.shadowJar {
dependencies {
compileOnly(libs.paperapi21)
compileOnly(libs.nms21)
compileOnly(project(":SpigotCore"))
}
@@ -19,6 +19,7 @@
package de.steamwar.kotlin
import de.steamwar.kotlin.message.LanguageListener
import org.bukkit.plugin.java.JavaPlugin
class KotlinCore : JavaPlugin() {
@@ -32,6 +33,7 @@ class KotlinCore : JavaPlugin() {
}
override fun onEnable() {
server.pluginManager.registerEvents(LanguageListener, this)
}
override fun onDisable() {
@@ -0,0 +1,36 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.kotlin.message
import net.kyori.adventure.key.Key
import net.kyori.adventure.translation.GlobalTranslator
import net.kyori.adventure.translation.Translator
import java.text.MessageFormat
import java.util.*
class ComponentMessage(private val resourceBundleName: String, private val classLoader: ClassLoader) : Translator {
init {
GlobalTranslator.translator().addSource(this)
}
override fun name(): Key = Key.key("steamwar", resourceBundleName)
override fun translate(key: String, locale: Locale): MessageFormat = MessageFormat(ResourceBundle.getBundle(resourceBundleName, locale, classLoader).getString(key), locale)
}
@@ -0,0 +1,49 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.kotlin.message
import de.steamwar.kotlin.KotlinCore
import de.steamwar.sql.SteamwarUser
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerLocaleChangeEvent
object LanguageListener: Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
fun onPlayerJoin(event: PlayerJoinEvent) {
val user = SteamwarUser.get(event.player.uniqueId)
val player = event.player as CraftPlayer
player.handle.`adventure$locale` = user.locale
}
@EventHandler
fun onPlayerConfig(event: PlayerLocaleChangeEvent) {
Bukkit.getScheduler().runTask(KotlinCore.plugin, Runnable {
val user = SteamwarUser.get(event.player.uniqueId)
val player = event.player as CraftPlayer
player.handle.`adventure$locale` = user.locale
})
}
}