Fixes, changes and Refactors

This commit is contained in:
2024-11-17 12:04:07 +01:00
parent 78853c70f8
commit 4bb1bc0cbd
13 changed files with 85 additions and 51 deletions
@@ -0,0 +1,60 @@
/*
* 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.inventory
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.InventoryHolder
import org.bukkit.inventory.ItemStack
abstract class SWInventoryHolder: InventoryHolder {
val _inventory: Inventory by lazy { createInventory() }
private val callbacks = mutableMapOf<Int, (event: InventoryClickEvent) -> Unit>()
override fun getInventory(): Inventory = _inventory
abstract fun createInventory(): Inventory
open fun handleInventoryClick(event: InventoryClickEvent) {
callbacks[event.slot]?.invoke(event)
}
fun addItem(item: ItemStack, slot: Int, callback: (event: InventoryClickEvent) -> Unit) {
_inventory.setItem(slot, item)
addCallback(slot, callback)
}
fun addCallback(slot: Int, callback: (event: InventoryClickEvent) -> Unit) {
callbacks[slot] = callback
}
open fun handleClose(event: InventoryCloseEvent) { }
operator fun set(slot: Int, item: Pair<ItemStack, (event: InventoryClickEvent) -> Unit>) {
addItem(item.first, slot, item.second)
}
operator fun set(slot: Int, item: ItemStack) {
addItem(item, slot) { }
}
}
@@ -19,31 +19,41 @@
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.craftbukkit.v1_21_R1.entity.CraftPlayer
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) {
val user = SteamwarUser.get(event.player.uniqueId)
val player = event.player as CraftPlayer
player.handle.`adventure$locale` = user.locale
event.player.adventureLocale = SteamwarUser.get(event.player.uniqueId).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
event.player.adventureLocale = SteamwarUser.get(event.player.uniqueId).locale
})
}
}
@@ -0,0 +1,92 @@
/*
* 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())
@@ -0,0 +1,65 @@
/*
* 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.util
import org.bukkit.Location
import org.bukkit.block.Block
class Area(loc1: Location, loc2: Location) {
val min: Location
val max: Location
init {
require(loc1.world == loc2.world) { "Locations must be in the same world" }
this.min = loc1 min loc2
this.max = loc1 max loc2
}
operator fun contains(loc: Location): Boolean {
return loc.world == min.world && loc.x >= min.x && loc.x <= max.x && loc.y >= min.y && loc.y <= max.y && loc.z >= min.z && loc.z <= max.z
}
val blocks: Sequence<Block>
inline get() = sequence {
for (x in locations) {
yield(x.block)
}
}
val locations: Sequence<Location>
inline get() = sequence {
for (x in min.blockX..max.blockX) {
for (y in min.blockY..max.blockY) {
for (z in min.blockZ..max.blockZ) {
yield(Location(min.world, x.toDouble(), y.toDouble(), z.toDouble()))
}
}
}
}
}
infix fun Location.max(other: Location): Location {
return Location(world, x.coerceAtLeast(other.x), y.coerceAtLeast(other.y), z.coerceAtLeast(other.z))
}
infix fun Location.min(other: Location): Location {
return Location(world, x.coerceAtMost(other.x), y.coerceAtMost(other.y), z.coerceAtMost(other.z))
}