Files
SteamWar/TNTLeague/src/de/steamwar/tntleague/inventory/DealerInventory.kt
T
2024-12-03 17:17:29 +01:00

101 lines
4.2 KiB
Kotlin

/*
* 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.game.TNTLeagueGame
import de.steamwar.tntleague.message
import de.steamwar.tntleague.plugin
import net.kyori.adventure.sound.Sound
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
import java.util.*
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
val team = TNTLeagueGame.getTeam(player) ?: return@to
if (team.coins < price) {
message.send("NOT_ENOUGH_COINS", player)
player.playSound(Sound.sound(org.bukkit.Sound.ENTITY_VILLAGER_HURT.key, net.kyori.adventure.sound.Sound.Source.MASTER, 1f, 1f))
return@to
}
team.coins -= price
player.inventory.addItem(ItemStack.of(item.first.type, amount))
}
}
}
override fun createInventory(): Inventory = plugin.server.createInventory(this, ceil(TNTLeagueConfig.config.prices.size / 9f).toInt() * 9, Component.text(message.parse("DEALER", player)))
companion object {
private val priceKey = NamespacedKey(plugin, "price")
private val amountKey = NamespacedKey(plugin, "amount")
private val coinKey = NamespacedKey(plugin, "coin")
val coins = ItemStack(Material.RAW_GOLD).apply {
itemMeta = itemMeta.apply {
displayName(Component.text("Coins").color(NamedTextColor.GOLD))
persistentDataContainer.apply {
set(coinKey, PersistentDataType.BOOLEAN, true)
}
}
}
val items by lazy {
val prices = TNTLeagueConfig.config.prices
prices.map { (material, price) ->
ItemStack(material).apply {
itemMeta = itemMeta.apply {
displayName(Component.text(material.name.lowercase().replace("_", " ")
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() })
.color(NamedTextColor.GRAY).appendSpace().append(Component.text(price.amount).color(NamedTextColor.YELLOW)))
amount = price.amount
lore(listOf(Component.text(price.price).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD).appendSpace().append(Component.text("Coins").color(NamedTextColor.YELLOW))))
persistentDataContainer.apply {
set(priceKey, PersistentDataType.INTEGER, price.price)
set(amountKey, PersistentDataType.INTEGER, price.amount)
}
}
} to price
}
}
}
}