shop-reborn/src/main/kotlin/pw.hamzantal.shopreborn/ClickListeners.kt

68 lines
1.9 KiB
Kotlin

package pw.hamzantal.shopreborn
import hazae41.minecraft.kutils.bukkit.msg
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryClickEvent
fun baseListener(e: InventoryClickEvent) {
if (e.inventory == Configurations.main.inventory) {
mainClick(e)
return
}
val shop = Configurations.shops.firstOrNull { it.inventories.contains(e.inventory)} ?: return
shopClick(e, shop)
}
fun mainClick(e: InventoryClickEvent) {
e.isCancelled = true
if (e.rawSlot > e.inventory.size) return
val main = Configurations.main
val p = e.whoClicked as Player
val block = main.blocks.firstOrNull { it.slot == e.slot } ?: return
block.commands.forEach {
p.performCommand(it)
}
}
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
//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
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(",")}")
}
}
}