Ability to set commands for money, pretty large numbers in the shop.

master
ALI Hamza 2020-03-13 13:45:20 +07:00
parent 3a5cd8cda1
commit bc6d02b363
No known key found for this signature in database
GPG Key ID: BCA8A46C87798C4C
5 changed files with 73 additions and 19 deletions

@ -53,7 +53,8 @@ fun mainClick(e: InventoryClickEvent) {
val block = main.blocks.firstOrNull { it.slot == e.slot } ?: return val block = main.blocks.firstOrNull { it.slot == e.slot } ?: return
block.commands.forEach { block.commands.forEach {
p.performCommand(it) if (it.contains("%PLAYER%")) Bukkit.getConsoleSender().execute(it.replace("%PLAYER%", p.name))
else p.performCommand(it)
} }
} }
@ -86,11 +87,41 @@ fun shopClick(e: InventoryClickEvent, shop: ShopConfig) {
//Buy / Sell Item //Buy / Sell Item
val block = shop.blocks.firstOrNull { it.item == e.currentItem } ?: return val block = shop.blocks.firstOrNull { it.item == e.currentItem } ?: return
if (block is ShopConfig.Command) { if (block is ShopConfig.Command) {
val item = block.item
val cost = block.buy
if (cost < 0) {
block.commands.forEach { block.commands.forEach {
if (it.contains("%PLAYER%")) if (it.contains("%PLAYER%"))
Bukkit.getConsoleSender().execute(it.replace("%PLAYER%", p.name)) Bukkit.getConsoleSender().execute(it.replace("%PLAYER%", p.name))
else p.chat(it) 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))
}
} }
if (block is ShopConfig.Item) { if (block is ShopConfig.Item) {

@ -71,9 +71,9 @@ class MainConfig(file: File) : ConfigFile(file) {
} }
class ShopConfig(val name: String, file: File) : ConfigFile(file) { class ShopConfig(val name: String, file: File) : ConfigFile(file) {
open class Block(val item: ItemStack) open class Block(open val item: ItemStack, open val buy: Double)
class Item(item: ItemStack, val raw: ItemStack, val buy: Double, val sell: Double) : Block(item) data class Item(override val item: ItemStack, val raw: ItemStack, override val buy: Double, val sell: Double) : Block(item, buy)
class Command(item: ItemStack, val asPlayer: Boolean, val commands: List<String>) : Block(item) data class Command(override val item: ItemStack, override val buy: Double, val asPlayer: Boolean, val commands: List<String>) : Block(item, buy)
init { init {
run { run {
@ -108,16 +108,25 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) {
val buy = it.getDouble("buyPrice", -1.0) val buy = it.getDouble("buyPrice", -1.0)
val sell = it.getDouble("sellPrice", -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) val item = it.getConfigurationSection("item").asItem(buy, sell)
when (type) { when (type) {
"command" -> Command( "command" -> Command(
it.getConfigurationSection("item").asItem(), it.getConfigurationSection("item").asItem(buy),
buy,
it.getBoolean("asplayer"), it.getBoolean("asplayer"),
it.getStringList("commands") it.getStringList("commands")
) )
"item" -> Item( "item" -> Item(
item.addLore(GlobalConfig.messages.itemLore), item.addLore(additional),
it.getConfigurationSection("item").asItem(), it.getConfigurationSection("item").asItem(),
buy, buy,
sell sell
@ -133,10 +142,10 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) {
val inventory: Inventory val inventory: Inventory
init { init {
inventory = makeInventory(size, title, menuRow, blocks, 1) inventory = makeInventory(size, title, menuRow, blocks, 1, pages)
inventories += inventory inventories += inventory
for (i in 2..pages) { for (i in 2..pages) {
inventories += makeInventory(size, title, menuRow, blocks, i) inventories += makeInventory(size, title, menuRow, blocks, i, pages)
} }
} }
} }

@ -8,7 +8,9 @@ class Messages(file: File, pl: ShopReborn) : ConfigFile(file) {
val buyLore by string("buyLore") val buyLore by string("buyLore")
val sellLore by string("sellLore") 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 set1 by string("set1")
val sub10 by string("sub10") val sub10 by string("sub10")
@ -37,6 +39,7 @@ class Messages(file: File, pl: ShopReborn) : ConfigFile(file) {
val buyInvFull by string("buy.invFull") val buyInvFull by string("buy.invFull")
val buyUnavailable by string("buy.itemNotPurchasable") val buyUnavailable by string("buy.itemNotPurchasable")
val buySuccess by string("buy.success") val buySuccess by string("buy.success")
val commandSuccess by string("buy.successCommand")
val purchaseError by string("purchaseError") val purchaseError by string("purchaseError")
init { init {

@ -29,16 +29,24 @@ fun ItemStack.addLore(s: String): ItemStack {
} }
} }
fun makeInventory(size: Int, title: String, menuRow: Int = -1, items: List<ShopConfig.Block>, page: Int): Inventory { fun makeInventory(
size: Int,
title: String,
menuRow: Int = -1,
items: List<ShopConfig.Block>,
page: Int,
total: Int
): Inventory {
return Bukkit.createInventory(null, size, title.c).apply { return Bukkit.createInventory(null, size, title.c).apply {
if (menuRow > 0) { if (menuRow > 0) {
val buttons = GlobalConfig.main.buttons val buttons = GlobalConfig.main.buttons
for (i in menuRow until menuRow + 9) { for (i in menuRow until menuRow + 9) {
setItem(i, buttons.pane) 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 + 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)) 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 .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 { fun ConfigurationSection.asItem(buy: Double = -1.0, sell: Double = -1.0): ItemStack {
val material = Material.valueOf(getString("material").toUpperCase()) val material = Material.valueOf(getString("material").toUpperCase())

@ -2,7 +2,9 @@ currency: "$"
buyLore: "&6Buy price: &c%PRICE%" buyLore: "&6Buy price: &c%PRICE%"
sellLore: "&6Sell 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" set1: "&c&lSet to 1"
sub10: "&c&lRemove 10" sub10: "&c&lRemove 10"
@ -38,6 +40,7 @@ buy:
itemNotPurchasable: "&cYou are not allowed to purchase this item." itemNotPurchasable: "&cYou are not allowed to purchase this item."
invFull: "&c&lYour inventory is full." invFull: "&c&lYour inventory is full."
success: "&a&lSuccessfully purchased %AMOUNT% %NAME% for %COST%" 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%" purchaseError: "&cAn error has occurred while completing the transaction: %MSG%"