From 34008d5d6b9f05a2ea6e2db8aee6fa1aa0d9bb09 Mon Sep 17 00:00:00 2001 From: Hamza Ali Date: Wed, 11 Mar 2020 13:53:04 +0700 Subject: [PATCH] Support for pages and config file value change from data to damage - Automatically calculates the number of pages by seeing how many empty slots are available - Forwards and backwards buttons are now functional - The configuration file changes from setting the value of data to set things such as stained glass colors to damage to follow deluxe shop's setup --- build.gradle | 2 +- compile.sh | 4 ++- .../pw.hamzantal.shopreborn/ClickListeners.kt | 23 +++++++++---- .../pw.hamzantal.shopreborn/Configuration.kt | 32 ++++++++++++++----- .../pw.hamzantal.shopreborn/ShopReborn.kt | 2 -- src/main/resources/config.yml | 2 +- 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index e11b49a..6f2897d 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'pw.hamzantal' -version '1.0-SNAPSHOT' +version '1.0' repositories { mavenCentral() diff --git a/compile.sh b/compile.sh index fa081f9..5d1170b 100644 --- a/compile.sh +++ b/compile.sh @@ -2,4 +2,6 @@ ./gradlew shadowJar -mv build/libs/ShopReborn-1.0-SNAPSHOT.jar ~/Desktop/CodeRelated/server/plugins/ \ No newline at end of file +mv build/libs/ShopReborn-1.0.jar ~/Desktop/CodeRelated/server/plugins/ + +say up \ No newline at end of file diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt b/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt index 83916c7..f43eef0 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt @@ -9,9 +9,9 @@ fun baseListener(e: InventoryClickEvent) { mainClick(e) return } - if (Configurations.shops.any { it.inventory == e.inventory }) { - shopClick(e) - } + + val shop = Configurations.shops.firstOrNull { it.inventories.contains(e.inventory)} ?: return + shopClick(e, shop) } fun mainClick(e: InventoryClickEvent) { @@ -27,21 +27,32 @@ fun mainClick(e: InventoryClickEvent) { } } -fun shopClick(e: InventoryClickEvent) { +fun shopClick(e: InventoryClickEvent, shop: ShopConfig) { 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 } + if (e.currentItem == main.buttons.forward) { + val current = shop.inventories.indexOf(e.inventory) + if (current + 1 == shop.inventories.size) return + p.openInventory(shop.inventories[current + 1]) + return + } + + if(e.currentItem == main.buttons.previous) { + val current = shop.inventories.indexOf(e.inventory) + if (current == 0) return + p.openInventory(shop.inventories[current - 1]) + } + //Buy / Sell Item val block = shop.blocks.firstOrNull { it.item == e.currentItem } ?: return diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt b/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt index e297328..c34f373 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt @@ -9,7 +9,8 @@ import org.bukkit.configuration.ConfigurationSection import org.bukkit.inventory.Inventory import org.bukkit.inventory.ItemStack import java.io.File -import java.lang.IllegalArgumentException +import kotlin.math.ceil +import kotlin.math.max object Configurations { var dataFolder = File("") @@ -94,7 +95,7 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) { } val title by string("name", "&a$name") - val size by int("size", 9) + val size by int("size", 18) val menuRow by int("buttons.menuRow", -1) val blocksRaw by section("items") @@ -123,9 +124,22 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) { } ?: listOf() - val inventory: Inventory = Bukkit.createInventory(null, size, title.c).apply { - //Set Single Row Menu - if (menuRow != -1) { + 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) @@ -135,15 +149,17 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) { setItem(menuRow + 8, buttons.forward) } - blocks.forEach { - setItem(firstEmpty(), it.item) + 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("data", 0).toShort()) + val item = ItemStack(material, getInt("quantity", 1), getInt("damage", 0).toShort()) item.itemMeta = item.itemMeta.apply { displayName = getString("name", "").c diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/ShopReborn.kt b/src/main/kotlin/pw.hamzantal.shopreborn/ShopReborn.kt index 01732a1..f97b72f 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/ShopReborn.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/ShopReborn.kt @@ -12,7 +12,6 @@ class ShopReborn : JavaPlugin() { override fun onEnable() { saveDefaultConfig() - Configurations.dataFolder = dataFolder Configurations.main = MainConfig(dataFolder["config.yml"]) @@ -22,7 +21,6 @@ class ShopReborn : JavaPlugin() { command("shop") { sender, args -> if (sender !is Player) return@command - info("a") if (args.isEmpty()) { sender.openInventory(Configurations.main.inventory) return@command diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 06e5fff..8b4224c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ buttons: pane: material: STAINED_GLASS_PANE - data: 7 # Gray + damage: 7 # Gray previous: material: FEATHER name: "&ePrevious Page"