Added support for menu, previous, and next button in config file.

- Removed support for specific slot within stores
- Added functionality for menu button
- Pages are still in development
- Added support for lore for buy/sell prices
master
ALI Hamza 2020-03-11 11:38:37 +07:00
parent 8019ec4ba1
commit fe26e15ac1
No known key found for this signature in database
GPG Key ID: BCA8A46C87798C4C
4 changed files with 172 additions and 15 deletions

@ -1,5 +1,6 @@
package pw.hamzantal.shopreborn
import hazae41.minecraft.kutils.bukkit.msg
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryClickEvent
@ -8,6 +9,9 @@ fun baseListener(e: InventoryClickEvent) {
mainClick(e)
return
}
if (Configurations.shops.any { it.inventory == e.inventory }) {
shopClick(e)
}
}
fun mainClick(e: InventoryClickEvent) {
@ -21,4 +25,32 @@ fun mainClick(e: InventoryClickEvent) {
block.commands.forEach {
p.performCommand(it)
}
}
}
fun shopClick(e: InventoryClickEvent) {
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
}
//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(",")}")
}
}
}

@ -2,26 +2,43 @@ package pw.hamzantal.shopreborn
import hazae41.minecraft.kutils.bukkit.ConfigFile
import hazae41.minecraft.kutils.bukkit.keys
import hazae41.minecraft.kutils.get
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import java.io.File
import java.lang.IllegalArgumentException
object Configurations {
var dataFolder = File("")
lateinit var main: MainConfig
val shops = mutableListOf<ConfigFile>()
val shops = mutableListOf<ShopConfig>()
}
class Buttons(val pane: ItemStack, val previous: ItemStack, val menu: ItemStack, val forward: ItemStack)
class MainConfig(file: File) : ConfigFile(file) {
data class Block(val item: ItemStack, val slot: Int, val commands: List<String>)
class Block(val item: ItemStack, val slot: Int, val commands: List<String>)
var shops by section("shops")
val shops by section("shops")
var size by int("shopGUI.size")
var title by string("shopGUI.title")
var blocksRaw by section("shopGUI.blocks")
val size by int("shopGUI.size")
val title by string("shopGUI.title")
val blocksRaw by section("shopGUI.blocks")
val pane by section("buttons.pane")
val previous by section("buttons.previous")
val forward by section("buttons.forward")
val menu by section("buttons.menu")
val buttons = Buttons(
pane!!.asItem(),
previous!!.asItem(),
menu!!.asItem(),
forward!!.asItem()
)
val blocks = blocksRaw?.keys
?.map { blocksRaw!!.getConfigurationSection(it) }
@ -36,19 +53,105 @@ class MainConfig(file: File) : ConfigFile(file) {
val inventory: Inventory = Bukkit.createInventory(null, size, title.c).apply {
blocks.forEach {
println(it)
setItem(it.slot, it.item)
}
}
fun addShops() {
Configurations.shops.addAll(shops!!.keys.map {
ShopConfig(
it,
Configurations.dataFolder[shops!!.getString(it)]
)
})
}
}
class ShopConfig(val name: String, file: File) : ConfigFile(file) {
open class Block(val item: ItemStack)
class Item(item: ItemStack, val buy: Int, val sell: Int) : Block(item)
class Command(item: ItemStack, val asPlayer: Boolean, val commands: List<String>) : Block(item)
init {
run {
if (file.exists()) return@run
file.createNewFile()
set("name", "&a$name")
set("size", 18)
set("buttons.menuRow", 9)
set("items.1.type", "item")
set("items.1.item.material", "STONE")
set("items.1.buyPrice", 5)
set("items.2.type", "item")
set("items.2.item.material", "GRASS")
set("items.2.buyPrice", 6)
set("items.2.sellPrice", 6)
set("items.3.type", "item")
set("items.3.item.material", "IRON_ORE")
set("items.3.buyPrice", 10)
}
}
val title by string("name", "&a$name")
val size by int("size", 9)
val menuRow by int("buttons.menuRow", -1)
val blocksRaw by section("items")
val blocks = blocksRaw?.keys
?.map { blocksRaw!!.getConfigurationSection(it) }
?.map<ConfigurationSection, Block> {
val type = it.getString("type")
val buy = it.getInt("buyPrice", -1)
val sell = it.getInt("sellPrice", -1)
val item = it.getConfigurationSection("item").asItem(buy, sell)
when (type) {
"command" -> Command(
item,
it.getBoolean("asplayer"),
it.getStringList("commands")
)
"item" -> Item(
item,
buy,
sell
)
else -> throw IllegalArgumentException("Item type ($type) was not recognised")
}
}
?: listOf()
val inventory: Inventory = Bukkit.createInventory(null, size, title.c).apply {
//Set Single Row Menu
if (menuRow != -1) {
val buttons = Configurations.main.buttons
for (i in menuRow until menuRow + 9) {
setItem(i, buttons.pane)
}
setItem(menuRow, buttons.previous)
setItem(menuRow + 4, buttons.menu)
setItem(menuRow + 8, buttons.forward)
}
blocks.forEach {
setItem(firstEmpty(), it.item)
}
}
}
fun ConfigurationSection.asItem(): ItemStack {
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())
item.itemMeta = item.itemMeta.apply {
displayName = getString("name", "").c
lore = getStringList("lore").map { it.c }
val configLore = getStringList("lore").map { it.c }.toMutableList()
if (buy > 0) configLore += "&fPurchase for $$buy".c
if (sell > 0) configLore += "&fSell for $$sell".c
lore = configLore
}
return item
}

@ -1,10 +1,10 @@
package pw.hamzantal.shopreborn
import hazae41.minecraft.kutils.bukkit.command
import hazae41.minecraft.kutils.bukkit.info
import hazae41.minecraft.kutils.bukkit.listen
import hazae41.minecraft.kutils.get
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.plugin.java.JavaPlugin
class ShopReborn : JavaPlugin() {
@ -12,15 +12,24 @@ class ShopReborn : JavaPlugin() {
override fun onEnable() {
saveDefaultConfig()
Configurations.dataFolder = dataFolder
Configurations.main =
MainConfig(dataFolder["config.yml"])
listen<InventoryClickEvent>(callback = ::baseListener)
Configurations.main.addShops()
listen(callback = ::baseListener)
command("shop") { sender, args ->
if (sender !is Player) return@command
if (args.isEmpty())
info("a")
if (args.isEmpty()) {
sender.openInventory(Configurations.main.inventory)
return@command
}
val shop = Configurations.shops.firstOrNull { it.name == args.component1() } ?: return@command
sender.openInventory(shop.inventory)
}
}
}

@ -1,7 +1,20 @@
buttons:
pane:
material: STAINED_GLASS_PANE
data: 7 # Gray
previous:
material: FEATHER
name: "&ePrevious Page"
forward:
material: FEATHER
name: "&eNext Page"
menu:
material: NETHER_STAR
name: "&eGo back"
shops:
blocks: "blocks.yml"
shopGUI:
size: 18
title: "&2Select a shop"