forked from SteamWar/SteamWar
Merge pull request 'TNTLeague/fixes' (#7) from TNTLeague/fixes into main
Reviewed-on: SteamWar/SteamWar#7 Reviewed-by: Lixfel <lixfel@noreply.localhost>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2025 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
|
||||
|
||||
import de.steamwar.inventory.SWInventory
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.event.inventory.InventoryClickEvent
|
||||
import org.bukkit.inventory.ItemStack
|
||||
|
||||
abstract class KotlinInventory(val player: Player) {
|
||||
|
||||
private val inv: SWInventory by lazy { createInventory() }
|
||||
|
||||
abstract fun createInventory(): SWInventory
|
||||
|
||||
fun open() = inv.open()
|
||||
|
||||
operator fun set(slot: Int, item: Pair<ItemStack, (event: InventoryClickEvent) -> Unit>) {
|
||||
inv.setItemEvent(slot, item.first, item.second)
|
||||
}
|
||||
|
||||
operator fun set(slot: Int, item: ItemStack) {
|
||||
inv.setItemEvent(slot, item) { }
|
||||
}
|
||||
}
|
||||
@@ -1,60 +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.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,9 +19,7 @@
|
||||
|
||||
package de.steamwar.tntleague.events
|
||||
|
||||
import de.steamwar.kotlin.inventory.SWInventoryHolder
|
||||
import de.steamwar.message.SubMessage
|
||||
import de.steamwar.tntleague.colorByTeam
|
||||
import de.steamwar.tntleague.config.TNTLeagueWorldConfig
|
||||
import de.steamwar.tntleague.game.TNTLeagueGame
|
||||
import de.steamwar.tntleague.game.TNTLeagueTeam
|
||||
@@ -34,9 +32,7 @@ import org.bukkit.event.EventPriority
|
||||
import org.bukkit.event.Listener
|
||||
import org.bukkit.event.entity.PlayerDeathEvent
|
||||
import org.bukkit.event.inventory.CraftItemEvent
|
||||
import org.bukkit.event.inventory.InventoryClickEvent
|
||||
import org.bukkit.event.player.*
|
||||
import java.util.logging.Level
|
||||
|
||||
object GlobalListener: Listener {
|
||||
|
||||
@@ -66,15 +62,6 @@ object GlobalListener: Listener {
|
||||
e.isCancelled = true
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
fun onPlayerClick(e: InventoryClickEvent) {
|
||||
val holder = e.inventory.getHolder(false)
|
||||
if (holder is SWInventoryHolder && e.clickedInventory == holder._inventory) {
|
||||
e.isCancelled = true
|
||||
holder.handleInventoryClick(e)
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
fun onPlayerMove(e: PlayerMoveEvent) {
|
||||
if (e.to.blockY < TNTLeagueWorldConfig.minHeight) {
|
||||
|
||||
@@ -44,7 +44,7 @@ object IngameListener: Listener {
|
||||
|
||||
if(e.rightClicked.type == EntityType.WANDERING_TRADER) {
|
||||
e.isCancelled = true
|
||||
e.player.openInventory(DealerInventory(e.player).getInventory())
|
||||
DealerInventory(e.player).open()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,11 +39,6 @@ object LobbyListener: Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
fun onPlayerQuit(e: PlayerQuitEvent) {
|
||||
TNTLeagueGame.playerLeave(e.player)
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
fun onPlayerDamage(e: EntityDamageEvent) {
|
||||
e.isCancelled = true
|
||||
|
||||
@@ -128,7 +128,7 @@ data class TNTLeagueTeam(val config: TNTLeagueWorldConfig.TeamConfig, private va
|
||||
|
||||
fun leave(player: Player) {
|
||||
members.remove(player)
|
||||
if (TNTLeagueGame.state != TNTLeagueGame.GameState.RUNNING) {
|
||||
if (TNTLeagueGame.state == TNTLeagueGame.GameState.LOBBY) {
|
||||
if (members.isEmpty()) {
|
||||
plugin.server.onlinePlayers.firstOrNull { it != player && TNTLeagueGame.getTeam(it) == null }?.run {
|
||||
members.add(this)
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
|
||||
package de.steamwar.tntleague.inventory
|
||||
|
||||
import de.steamwar.kotlin.inventory.SWInventoryHolder
|
||||
import de.steamwar.inventory.SWInventory
|
||||
import de.steamwar.kotlin.KotlinInventory
|
||||
import de.steamwar.tntleague.config.TNTLeagueConfig
|
||||
import de.steamwar.tntleague.inventory.DealerInventory.Companion.buyItem
|
||||
import de.steamwar.tntleague.inventory.DealerInventory.Companion.itemsByCategory
|
||||
@@ -29,8 +30,8 @@ import org.bukkit.Bukkit
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.inventory.Inventory
|
||||
|
||||
class CategoryInventory(val player: Player, category: TNTLeagueConfig.ItemCategory): SWInventoryHolder() {
|
||||
override fun createInventory(): Inventory = Bukkit.createInventory(this, 9 * 6, Component.text(message.parse("DEALER", player)))
|
||||
class CategoryInventory(player: Player, category: TNTLeagueConfig.ItemCategory): KotlinInventory(player) {
|
||||
override fun createInventory() = SWInventory(player, 9 * 6, message.parse("DEALER", player))
|
||||
|
||||
init {
|
||||
itemsByCategory[category]!!.forEachIndexed { index, item ->
|
||||
|
||||
@@ -19,8 +19,9 @@
|
||||
|
||||
package de.steamwar.tntleague.inventory
|
||||
|
||||
import de.steamwar.inventory.SWInventory
|
||||
import de.steamwar.inventory.SWItem
|
||||
import de.steamwar.kotlin.inventory.SWInventoryHolder
|
||||
import de.steamwar.kotlin.KotlinInventory
|
||||
import de.steamwar.tntleague.config.TNTLeagueConfig
|
||||
import de.steamwar.tntleague.game.TNTLeagueGame
|
||||
import de.steamwar.tntleague.message
|
||||
@@ -38,7 +39,7 @@ import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.persistence.PersistentDataType
|
||||
import java.util.*
|
||||
|
||||
class DealerInventory(val player: Player): SWInventoryHolder() {
|
||||
class DealerInventory(player: Player): KotlinInventory(player) {
|
||||
|
||||
init {
|
||||
this[10] = SWItem(Material.REDSTONE_BLOCK, message.parse("DEALER_REDSTONE", player)).itemStack to openCategory(TNTLeagueConfig.ItemCategory.REDSTONE)
|
||||
@@ -51,9 +52,9 @@ class DealerInventory(val player: Player): SWInventoryHolder() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun openCategory(cat: TNTLeagueConfig.ItemCategory): (e: InventoryClickEvent) -> Unit = { player.openInventory(CategoryInventory(player, cat).inventory) }
|
||||
private fun openCategory(cat: TNTLeagueConfig.ItemCategory): (e: InventoryClickEvent) -> Unit = { CategoryInventory(player, cat).open() }
|
||||
|
||||
override fun createInventory(): Inventory = plugin.server.createInventory(this, 6 * 9, Component.text(message.parse("DEALER", player)))
|
||||
override fun createInventory() = SWInventory(player, 9 * 6, message.parse("DEALER", player))
|
||||
|
||||
companion object {
|
||||
private val priceKey = NamespacedKey(plugin, "price")
|
||||
|
||||
Reference in New Issue
Block a user