Add Some Fixes and Improvements

This commit is contained in:
2024-12-22 21:57:54 +01:00
parent 1ea6bd61f8
commit aaa808f90f
14 changed files with 242 additions and 58 deletions
@@ -0,0 +1,40 @@
/*
* 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.tntleague.inventory
import de.steamwar.kotlin.inventory.SWInventoryHolder
import de.steamwar.tntleague.config.TNTLeagueConfig
import de.steamwar.tntleague.inventory.DealerInventory.Companion.buyItem
import de.steamwar.tntleague.inventory.DealerInventory.Companion.itemsByCategory
import de.steamwar.tntleague.message
import net.kyori.adventure.text.Component
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)))
init {
itemsByCategory[category]!!.forEachIndexed { index, item ->
this[index] = item.first to { buyItem(player, item, it) }
}
}
}
@@ -19,6 +19,7 @@
package de.steamwar.tntleague.inventory
import de.steamwar.inventory.SWItem
import de.steamwar.kotlin.inventory.SWInventoryHolder
import de.steamwar.tntleague.config.TNTLeagueConfig
import de.steamwar.tntleague.game.TNTLeagueGame
@@ -31,6 +32,7 @@ import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.Sound
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
@@ -40,26 +42,19 @@ import kotlin.math.ceil
class DealerInventory(val player: Player): SWInventoryHolder() {
init {
items.forEachIndexed { index, item ->
this[index] = item.first to {
val price = item.second.price * if (it.isShiftClick) 5 else 1
val amount = item.second.amount * if (it.isShiftClick) 5 else 1
this[10] = SWItem(Material.REDSTONE_BLOCK, message.parse("DEALER_REDSTONE", player)).itemStack to openCategory(TNTLeagueConfig.ItemCategory.REDSTONE)
this[12] = SWItem(Material.END_STONE, message.parse("DEALER_BLOCKS", player)).itemStack to openCategory(TNTLeagueConfig.ItemCategory.BLOCKS)
this[14] = SWItem(Material.DIAMOND_PICKAXE, message.parse("DEALER_TOOLS", player)).itemStack to openCategory(TNTLeagueConfig.ItemCategory.TOOLS)
this[16] = SWItem(Material.BREWING_STAND, message.parse("DEALER_ANGLES", player)).itemStack to openCategory(TNTLeagueConfig.ItemCategory.ANGLES)
val team = TNTLeagueGame.getTeam(player) ?: return@to
if (team.coins < price) {
message.send("NOT_ENOUGH_COINS", player)
player.playSound(player, Sound.ENTITY_VILLAGER_HURT, 1f, 1f)
return@to
}
team.coins -= price
player.inventory.addItem(ItemStack.of(item.first.type, amount))
}
pinnedItems.forEach { item ->
this[item.key] = item.value.first to { buyItem(player, item.value, it) }
}
}
override fun createInventory(): Inventory = plugin.server.createInventory(this, ceil(TNTLeagueConfig.config.prices.size / 9f).toInt() * 9, Component.text(message.parse("DEALER", player)))
private fun openCategory(cat: TNTLeagueConfig.ItemCategory): (e: InventoryClickEvent) -> Unit = { player.openInventory(CategoryInventory(player, cat).inventory) }
override fun createInventory(): Inventory = plugin.server.createInventory(this, 6 * 9, Component.text(message.parse("DEALER", player)))
companion object {
private val priceKey = NamespacedKey(plugin, "price")
@@ -75,7 +70,23 @@ class DealerInventory(val player: Player): SWInventoryHolder() {
}
}
val items by lazy {
fun buyItem(player: Player, item: Pair<ItemStack, TNTLeagueConfig.Price>, e: InventoryClickEvent) {
val price = item.second.price * if (e.isShiftClick) 5 else 1
val amount = item.second.amount * if (e.isShiftClick) 5 else 1
val team = TNTLeagueGame.getTeam(player) ?: return
if (team.coins < price) {
message.send("NOT_ENOUGH_COINS", player)
player.playSound(player, Sound.ENTITY_VILLAGER_HURT, 1f, 1f)
return
}
team.coins -= price
player.inventory.addItem(ItemStack.of(item.first.type, amount).also { item.second.extras.forEach { extra -> it.itemMeta = extra.func(it.itemMeta) } })
}
private val items by lazy {
val prices = TNTLeagueConfig.config.prices
prices.map { (material, price) ->
@@ -96,5 +107,8 @@ class DealerInventory(val player: Player): SWInventoryHolder() {
} to price
}
}
val itemsByCategory = items.groupBy { it.second.category }
val pinnedItems = items.filter { it.second.pinned != -1 }.associateBy { it.second.pinned }
}
}