package pw.hamzantal.shopreborn import hazae41.minecraft.kutils.bukkit.ConfigFile import hazae41.minecraft.kutils.bukkit.keys import hazae41.minecraft.kutils.get import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.configuration.ConfigurationSection import org.bukkit.inventory.Inventory import org.bukkit.inventory.ItemStack import java.io.File import kotlin.math.ceil import kotlin.math.max object Configurations { var dataFolder = File("") lateinit var main: MainConfig val shops = mutableListOf() } class Buttons(val pane: ItemStack, val previous: ItemStack, val menu: ItemStack, val forward: ItemStack) class MainConfig(file: File) : ConfigFile(file) { class Block(val item: ItemStack, val slot: Int, val commands: List) val shops by section("shops") val size by int("shopGUI.size") val title by string("shopGUI.title") val blocksRaw by section("shopGUI.blocks") val pane by section("buttons.pane") val previous by section("buttons.previous") val forward by section("buttons.forward") val menu by section("buttons.menu") val buttons = Buttons( pane!!.asItem(), previous!!.asItem(), menu!!.asItem(), forward!!.asItem() ) val blocks = blocksRaw?.keys ?.map { blocksRaw!!.getConfigurationSection(it) } ?.map { Block( it.getConfigurationSection("item").asItem(), it.getInt("slot"), it.getStringList("commands") ) } ?: listOf() val inventory: Inventory = Bukkit.createInventory(null, size, title.c).apply { blocks.forEach { setItem(it.slot, it.item) } } fun addShops() { Configurations.shops.addAll(shops!!.keys.map { ShopConfig( it, Configurations.dataFolder[shops!!.getString(it)] ) }) } } class ShopConfig(val name: String, file: File) : ConfigFile(file) { open class Block(val item: ItemStack) class Item(item: ItemStack, val buy: Int, val sell: Int) : Block(item) class Command(item: ItemStack, val asPlayer: Boolean, val commands: List) : Block(item) init { run { if (file.exists()) return@run file.createNewFile() set("name", "&a$name") set("size", 18) set("buttons.menuRow", 9) set("items.1.type", "item") set("items.1.item.material", "STONE") set("items.1.buyPrice", 5) set("items.2.type", "item") set("items.2.item.material", "GRASS") set("items.2.buyPrice", 6) set("items.2.sellPrice", 6) set("items.3.type", "item") set("items.3.item.material", "IRON_ORE") set("items.3.buyPrice", 10) } } val title by string("name", "&a$name") val size by int("size", 18) val menuRow by int("buttons.menuRow", -1) val blocksRaw by section("items") val blocks = blocksRaw?.keys ?.map { blocksRaw!!.getConfigurationSection(it) } ?.map { val type = it.getString("type") val buy = it.getInt("buyPrice", -1) val sell = it.getInt("sellPrice", -1) val item = it.getConfigurationSection("item").asItem(buy, sell) when (type) { "command" -> Command( item, it.getBoolean("asplayer"), it.getStringList("commands") ) "item" -> Item( item, buy, sell ) else -> throw IllegalArgumentException("Item type ($type) was not recognised") } } ?: listOf() val pages = ceil(blocks.size.toDouble() / (size - 9).toDouble()).toInt() val inventories = mutableListOf() val inventory: Inventory init { inventory = makeInventory(size, title, menuRow, blocks, 1) inventories += inventory for (i in 2..pages) { inventories += makeInventory(size, title, menuRow, blocks, i) } } } fun makeInventory(size: Int, title: String, menuRow: Int = -1, items: List, page: Int): Inventory { return Bukkit.createInventory(null, size, title.c).apply { if (menuRow > 0) { val buttons = Configurations.main.buttons for (i in menuRow until menuRow + 9) { setItem(i, buttons.pane) } setItem(menuRow, buttons.previous) setItem(menuRow + 4, buttons.menu) setItem(menuRow + 8, buttons.forward) } val dropping = (page - 1) * (size - max(0, menuRow)) items.drop(dropping).forEach { if (firstEmpty() != -1) setItem(firstEmpty(), it.item) else return@apply } } } fun ConfigurationSection.asItem(buy: Int = -1, sell: Int = -1): ItemStack { val material = Material.valueOf(getString("material").toUpperCase()) val item = ItemStack(material, getInt("quantity", 1), getInt("damage", 0).toShort()) item.itemMeta = item.itemMeta.apply { displayName = getString("name", "").c val configLore = getStringList("lore").map { it.c }.toMutableList() if (buy > 0) configLore += "&fPurchase for $$buy".c if (sell > 0) configLore += "&fSell for $$sell".c lore = configLore } return item }