From bc6d02b363f43ef07ab422f3082473b119bd1a5e Mon Sep 17 00:00:00 2001 From: Hamza Ali Date: Fri, 13 Mar 2020 13:45:20 +0700 Subject: [PATCH] Ability to set commands for money, pretty large numbers in the shop. --- .../pw.hamzantal.shopreborn/ClickListeners.kt | 43 ++++++++++++++++--- .../pw.hamzantal.shopreborn/Configuration.kt | 23 +++++++--- .../pw.hamzantal.shopreborn/Messages.kt | 5 ++- .../kotlin/pw.hamzantal.shopreborn/Util.kt | 16 +++++-- src/main/resources/messages.yml | 5 ++- 5 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt b/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt index 7a83da4..2620d7e 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt @@ -53,7 +53,8 @@ fun mainClick(e: InventoryClickEvent) { val block = main.blocks.firstOrNull { it.slot == e.slot } ?: return block.commands.forEach { - p.performCommand(it) + if (it.contains("%PLAYER%")) Bukkit.getConsoleSender().execute(it.replace("%PLAYER%", p.name)) + else p.performCommand(it) } } @@ -86,10 +87,40 @@ fun shopClick(e: InventoryClickEvent, shop: ShopConfig) { //Buy / Sell Item val block = shop.blocks.firstOrNull { it.item == e.currentItem } ?: return if (block is ShopConfig.Command) { - block.commands.forEach { - if (it.contains("%PLAYER%")) - Bukkit.getConsoleSender().execute(it.replace("%PLAYER%", p.name)) - else p.chat(it) + val item = block.item + val cost = block.buy + if (cost < 0) { + block.commands.forEach { + if (it.contains("%PLAYER%")) + Bukkit.getConsoleSender().execute(it.replace("%PLAYER%", p.name)) + else p.chat(it) + } + return + } + + val name = if (item.itemMeta.hasDisplayName()) item.itemMeta.displayName else item.type.prettyName() + val op = Bukkit.getOfflinePlayer(p.uniqueId) + val balance = econ.getBalance(op) + + if (cost > balance) { + p.msg(GlobalConfig.messages.buyNotEnough) + return + } + + val r = econ.withdrawPlayer(op, cost) + if (r.transactionSuccess()) { + block.commands.forEach { + if (it.contains("%PLAYER%")) + Bukkit.getConsoleSender().execute(it.replace("%PLAYER%", p.name)) + else p.chat(it) + } + p.msg( + GlobalConfig.messages.commandSuccess + .replace("%NAME%", name) + .replace("%COST%", cost.withCurrency()) + ) + } else { + p.msg(GlobalConfig.messages.purchaseError.replace("%MSG%", r.errorMessage)) } } @@ -269,7 +300,7 @@ fun buy(e: InventoryClickEvent, p: Player, shop: ShopConfig, block: ShopConfig.I fun sell(e: InventoryClickEvent, p: Player, shop: ShopConfig, block: ShopConfig.Item) { val item = e.currentItem.clone() - if (block.sell == -1.0) { + if (block.sell == -1.0) { p.msg(GlobalConfig.messages.sellUnavailable) return } diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt b/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt index a1c6c6f..9f64ed4 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/Configuration.kt @@ -71,9 +71,9 @@ class MainConfig(file: File) : ConfigFile(file) { } class ShopConfig(val name: String, file: File) : ConfigFile(file) { - open class Block(val item: ItemStack) - class Item(item: ItemStack, val raw: ItemStack, val buy: Double, val sell: Double) : Block(item) - class Command(item: ItemStack, val asPlayer: Boolean, val commands: List) : Block(item) + open class Block(open val item: ItemStack, open val buy: Double) + data class Item(override val item: ItemStack, val raw: ItemStack, override val buy: Double, val sell: Double) : Block(item, buy) + data class Command(override val item: ItemStack, override val buy: Double, val asPlayer: Boolean, val commands: List) : Block(item, buy) init { run { @@ -108,16 +108,25 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) { val buy = it.getDouble("buyPrice", -1.0) val sell = it.getDouble("sellPrice", -1.0) + val additional = + when { + buy > 0 && sell > 0 -> GlobalConfig.messages.buyAndSell + buy > 0 -> GlobalConfig.messages.buyOnly + sell > 0 -> GlobalConfig.messages.sellOnly + else -> "" + } + val item = it.getConfigurationSection("item").asItem(buy, sell) when (type) { "command" -> Command( - it.getConfigurationSection("item").asItem(), + it.getConfigurationSection("item").asItem(buy), + buy, it.getBoolean("asplayer"), it.getStringList("commands") ) "item" -> Item( - item.addLore(GlobalConfig.messages.itemLore), + item.addLore(additional), it.getConfigurationSection("item").asItem(), buy, sell @@ -133,10 +142,10 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) { val inventory: Inventory init { - inventory = makeInventory(size, title, menuRow, blocks, 1) + inventory = makeInventory(size, title, menuRow, blocks, 1, pages) inventories += inventory for (i in 2..pages) { - inventories += makeInventory(size, title, menuRow, blocks, i) + inventories += makeInventory(size, title, menuRow, blocks, i, pages) } } } diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/Messages.kt b/src/main/kotlin/pw.hamzantal.shopreborn/Messages.kt index 98ec10b..6791308 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/Messages.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/Messages.kt @@ -8,7 +8,9 @@ class Messages(file: File, pl: ShopReborn) : ConfigFile(file) { val buyLore by string("buyLore") val sellLore by string("sellLore") - val itemLore by string("itemLore") + val buyOnly by string("buyOnly") + val sellOnly by string("sellOnly") + val buyAndSell by string("buyAndSell") val set1 by string("set1") val sub10 by string("sub10") @@ -37,6 +39,7 @@ class Messages(file: File, pl: ShopReborn) : ConfigFile(file) { val buyInvFull by string("buy.invFull") val buyUnavailable by string("buy.itemNotPurchasable") val buySuccess by string("buy.success") + val commandSuccess by string("buy.successCommand") val purchaseError by string("purchaseError") init { diff --git a/src/main/kotlin/pw.hamzantal.shopreborn/Util.kt b/src/main/kotlin/pw.hamzantal.shopreborn/Util.kt index c58b734..245176a 100644 --- a/src/main/kotlin/pw.hamzantal.shopreborn/Util.kt +++ b/src/main/kotlin/pw.hamzantal.shopreborn/Util.kt @@ -29,16 +29,24 @@ fun ItemStack.addLore(s: String): ItemStack { } } -fun makeInventory(size: Int, title: String, menuRow: Int = -1, items: List, page: Int): Inventory { +fun makeInventory( + size: Int, + title: String, + menuRow: Int = -1, + items: List, + page: Int, + total: Int +): Inventory { return Bukkit.createInventory(null, size, title.c).apply { if (menuRow > 0) { val buttons = GlobalConfig.main.buttons for (i in menuRow until menuRow + 9) { setItem(i, buttons.pane) } - setItem(menuRow, buttons.previous) + println("page=$page | total=$total") + if (page != 1) setItem(menuRow, buttons.previous) setItem(menuRow + 4, buttons.menu) - setItem(menuRow + 8, buttons.forward) + if (page != total) setItem(menuRow + 8, buttons.forward) } val dropping = (page - 1) * (size - kotlin.math.max(0, menuRow)) @@ -57,7 +65,7 @@ fun String.mixPlaceholder(amount: Int = 0, price: Double = 0.0): String = ) .c -fun Double.withCurrency() = String.format("%s%.2f", GlobalConfig.messages.currency, this) +fun Double.withCurrency() = String.format("%s%,.2f", GlobalConfig.messages.currency, this) fun ConfigurationSection.asItem(buy: Double = -1.0, sell: Double = -1.0): ItemStack { val material = Material.valueOf(getString("material").toUpperCase()) diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index b397f99..41f1a8d 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -2,7 +2,9 @@ currency: "$" buyLore: "&6Buy price: &c%PRICE%" sellLore: "&6Sell price: &c%PRICE%" -itemLore: "&6Left click to buy, Right click to sell" +buyOnly: "&6Left click to buy" +sellOnly: "&6Right click to sell" +buyAndSell: "&6Left click to buy, Right click to sell" set1: "&c&lSet to 1" sub10: "&c&lRemove 10" @@ -38,6 +40,7 @@ buy: itemNotPurchasable: "&cYou are not allowed to purchase this item." invFull: "&c&lYour inventory is full." success: "&a&lSuccessfully purchased %AMOUNT% %NAME% for %COST%" + successCommand: "&a&lSuccessfully purchased a %NAME% for %COST%" #(feel free to reword) purchaseError: "&cAn error has occurred while completing the transaction: %MSG%"