From fe26e15ac15a0457a4046621063dc7a0a96d06fd Mon Sep 17 00:00:00 2001 From: Hamza Ali Date: Wed, 11 Mar 2020 11:38:37 +0700 Subject: [PATCH] Added support for menu, previous, and next button in config file. - Removed support for specific slot within stores - Added functionality for menu button - Pages are still in development - Added support for lore for buy/sell prices --- .../pw.hamzantal.shopreborn/ClickListeners.kt | 34 ++++- .../pw.hamzantal.shopreborn/Configuration.kt | 121 ++++++++++++++++-- .../pw.hamzantal.shopreborn/ShopReborn.kt | 17 ++- src/main/resources/config.yml | 15 ++- 4 files changed, 172 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt b/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt index 5fb3864..83916c7 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt @@ -1,5 +1,6 @@ package pw.hamzantal.shopreborn +import hazae41.minecraft.kutils.bukkit.msg import org.bukkit.entity.Player import org.bukkit.event.inventory.InventoryClickEvent @@ -8,6 +9,9 @@ fun baseListener(e: InventoryClickEvent) { mainClick(e) return } + if (Configurations.shops.any { it.inventory == e.inventory }) { + shopClick(e) + } } fun mainClick(e: InventoryClickEvent) { @@ -21,4 +25,32 @@ fun mainClick(e: InventoryClickEvent) { block.commands.forEach { p.performCommand(it) } -} \ No newline at end of file +} + +fun shopClick(e: InventoryClickEvent) { + e.isCancelled = true + if (e.rawSlot > e.inventory.size) return + + val main = Configurations.main + val p = e.whoClicked as Player + + val shop = Configurations.shops.firstOrNull { it.inventory == e.inventory }?: return + + //Check Menu Buttons + if (e.currentItem == main.buttons.menu) { + p.openInventory(main.inventory) + return + } + + //Buy / Sell Item + val block = shop.blocks.firstOrNull { it.item == e.currentItem } ?: return + + when(block) { + is ShopConfig.Item -> { + p.msg("Buying ${block.item.type.name} for ${block.buy}") + } + is ShopConfig.Command -> { + p.msg("Buying ${block.item.type.name} producing ${block.commands.joinToString(",")}") + } + } +} diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt b/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt index 114f167..e297328 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt @@ -2,26 +2,43 @@ 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 java.lang.IllegalArgumentException object Configurations { + var dataFolder = File("") lateinit var main: MainConfig - val shops = mutableListOf() + + val shops = mutableListOf() } +class Buttons(val pane: ItemStack, val previous: ItemStack, val menu: ItemStack, val forward: ItemStack) + class MainConfig(file: File) : ConfigFile(file) { - data class Block(val item: ItemStack, val slot: Int, val commands: List) + class Block(val item: ItemStack, val slot: Int, val commands: List) - var shops by section("shops") + val shops by section("shops") - var size by int("shopGUI.size") - var title by string("shopGUI.title") - var blocksRaw by section("shopGUI.blocks") + 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) } @@ -36,19 +53,105 @@ class MainConfig(file: File) : ConfigFile(file) { val inventory: Inventory = Bukkit.createInventory(null, size, title.c).apply { blocks.forEach { - println(it) 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", 9) + 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 inventory: Inventory = Bukkit.createInventory(null, size, title.c).apply { + //Set Single Row Menu + if (menuRow != -1) { + 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) + } + + blocks.forEach { + setItem(firstEmpty(), it.item) + } + } } -fun ConfigurationSection.asItem(): ItemStack { +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("data", 0).toShort()) item.itemMeta = item.itemMeta.apply { displayName = getString("name", "").c - lore = getStringList("lore").map { it.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 } \ No newline at end of file diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/ShopReborn.kt b/src/main/kotlin/pw.hamzantal.shopreborn/ShopReborn.kt index 3c909c4..01732a1 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/ShopReborn.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/ShopReborn.kt @@ -1,10 +1,10 @@ package pw.hamzantal.shopreborn import hazae41.minecraft.kutils.bukkit.command +import hazae41.minecraft.kutils.bukkit.info import hazae41.minecraft.kutils.bukkit.listen import hazae41.minecraft.kutils.get import org.bukkit.entity.Player -import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.plugin.java.JavaPlugin class ShopReborn : JavaPlugin() { @@ -12,15 +12,24 @@ class ShopReborn : JavaPlugin() { override fun onEnable() { saveDefaultConfig() + + Configurations.dataFolder = dataFolder Configurations.main = MainConfig(dataFolder["config.yml"]) - listen(callback = ::baseListener) + Configurations.main.addShops() + + listen(callback = ::baseListener) command("shop") { sender, args -> if (sender !is Player) return@command - - if (args.isEmpty()) + info("a") + if (args.isEmpty()) { sender.openInventory(Configurations.main.inventory) + return@command + } + + val shop = Configurations.shops.firstOrNull { it.name == args.component1() } ?: return@command + sender.openInventory(shop.inventory) } } } \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5e65eae..06e5fff 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,20 @@ +buttons: + pane: + material: STAINED_GLASS_PANE + data: 7 # Gray + previous: + material: FEATHER + name: "&ePrevious Page" + forward: + material: FEATHER + name: "&eNext Page" + menu: + material: NETHER_STAR + name: "&eGo back" + shops: blocks: "blocks.yml" - shopGUI: size: 18 title: "&2Select a shop"