Remove Component Messages

This commit is contained in:
2024-11-24 21:35:13 +01:00
parent 9415a3f217
commit ec990cb52a
14 changed files with 115 additions and 349 deletions
@@ -19,7 +19,6 @@
package de.steamwar.kotlin
import de.steamwar.kotlin.message.LanguageListener
import org.bukkit.plugin.java.JavaPlugin
class KotlinCore : JavaPlugin() {
@@ -33,7 +32,7 @@ class KotlinCore : JavaPlugin() {
}
override fun onEnable() {
server.pluginManager.registerEvents(LanguageListener, this)
}
override fun onDisable() {
@@ -1,43 +0,0 @@
/*
* 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) =
ResourceBundle.getBundle(resourceBundleName, locale, classLoader).let {
if (it.containsKey(key)) {
MessageFormat(it.getString(key), locale)
} else {
null
}
}
}
@@ -1,59 +0,0 @@
/*
* 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 com.comphenix.tinyprotocol.Reflection
import de.steamwar.kotlin.KotlinCore
import de.steamwar.sql.SteamwarUser
import org.bukkit.Bukkit
import org.bukkit.entity.Player
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
import java.util.Locale
object LanguageListener: Listener {
private val getHandleMethod = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle")
private val localeField = Reflection.getField("{nms.server.level}.EntityPlayer", "adventure\$locale", Locale::class.java)
private val Player.handle
get() = getHandleMethod.invoke(this)
private var Player.adventureLocale
get() = localeField.get(handle) as Locale
set(value) {
localeField.set(handle, value)
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
fun onPlayerJoin(event: PlayerJoinEvent) {
event.player.adventureLocale = SteamwarUser.get(event.player.uniqueId).locale
}
@EventHandler
fun onPlayerConfig(event: PlayerLocaleChangeEvent) {
Bukkit.getScheduler().runTask(KotlinCore.plugin, Runnable {
event.player.adventureLocale = SteamwarUser.get(event.player.uniqueId).locale
})
}
}
@@ -1,92 +0,0 @@
/*
* 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.text.Component
import net.kyori.adventure.text.ComponentLike
import net.kyori.adventure.text.TranslatableComponent
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.Style
import net.kyori.adventure.text.format.TextDecoration
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.kyori.adventure.translation.GlobalTranslator
import org.bukkit.entity.Player
import java.util.Locale
object StyleConfig {
var prefix = Component.text("Steam").yellow()
.append(Component.text("War").darkGray())
.appendSpace()
set(value) {
field = value
chatPrefix = value
.append(Component.text("»").darkGray())
.appendSpace()
}
var chatPrefix = prefix
.append(Component.text("»").darkGray())
.appendSpace()
}
fun TranslatableComponent.basic(): Component = StyleConfig.chatPrefix.append(this.gray())
fun TranslatableComponent.error(): Component = StyleConfig.chatPrefix.append(this.red())
fun TranslatableComponent.success(): Component = StyleConfig.chatPrefix.append(this.green())
fun String.component(): Component = Component.text(this)
fun Component.bold(): Component = this.decorate(TextDecoration.BOLD)
fun String.bold(): Component = this.component().bold()
fun Component.yellow(): Component = this.color(NamedTextColor.YELLOW)
fun String.yellow(): Component = this.component().yellow()
fun Component.red(): Component = this.color(NamedTextColor.RED)
fun String.red(): Component = this.component().red()
fun Component.green(): Component = this.color(NamedTextColor.GREEN)
fun String.green(): Component = this.component().green()
fun Component.gray(): Component = this.color(NamedTextColor.GRAY)
fun String.gray(): Component = this.component().gray()
fun Component.darkGray(): Component = this.color(NamedTextColor.DARK_GRAY)
fun String.darkGray(): Component = this.component().darkGray()
fun Component.gold(): Component = this.color(NamedTextColor.GOLD)
fun translate(key: String, vararg args: ComponentLike): TranslatableComponent = Component.translatable(key, *args).decoration(TextDecoration.ITALIC, false)
fun Component.reset(): Component = this.style(Style.empty())
fun Component.translate(locale: Locale): Component = GlobalTranslator.render(this, locale)
fun Component.translate(p: Player): Component = this.translate(p.locale())
fun Component.toLegacy(): String = LegacyComponentSerializer.legacySection().serialize(this)
fun Component.space(): Component = append(Component.space())