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
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
}

@ -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<String>) : 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<String>) : 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)
}
}
}

@ -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 {

@ -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 {
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())

@ -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%"