Support for pages and config file value change from data to damage

- Automatically calculates the number of pages by seeing how many empty
slots are available
- Forwards and backwards buttons are now functional
- The configuration file changes from setting the value of data to set
things such as stained glass colors to damage to follow deluxe shop's
setup
master
ALI Hamza 2020-03-11 13:53:04 +07:00
parent fe26e15ac1
commit 34008d5d6b
No known key found for this signature in database
GPG Key ID: BCA8A46C87798C4C
6 changed files with 46 additions and 19 deletions

@ -4,7 +4,7 @@ plugins {
} }
group 'pw.hamzantal' group 'pw.hamzantal'
version '1.0-SNAPSHOT' version '1.0'
repositories { repositories {
mavenCentral() mavenCentral()

@ -2,4 +2,6 @@
./gradlew shadowJar ./gradlew shadowJar
mv build/libs/ShopReborn-1.0-SNAPSHOT.jar ~/Desktop/CodeRelated/server/plugins/ mv build/libs/ShopReborn-1.0.jar ~/Desktop/CodeRelated/server/plugins/
say up

@ -9,9 +9,9 @@ fun baseListener(e: InventoryClickEvent) {
mainClick(e) mainClick(e)
return return
} }
if (Configurations.shops.any { it.inventory == e.inventory }) {
shopClick(e) val shop = Configurations.shops.firstOrNull { it.inventories.contains(e.inventory)} ?: return
} shopClick(e, shop)
} }
fun mainClick(e: InventoryClickEvent) { fun mainClick(e: InventoryClickEvent) {
@ -27,21 +27,32 @@ fun mainClick(e: InventoryClickEvent) {
} }
} }
fun shopClick(e: InventoryClickEvent) { fun shopClick(e: InventoryClickEvent, shop: ShopConfig) {
e.isCancelled = true e.isCancelled = true
if (e.rawSlot > e.inventory.size) return if (e.rawSlot > e.inventory.size) return
val main = Configurations.main val main = Configurations.main
val p = e.whoClicked as Player val p = e.whoClicked as Player
val shop = Configurations.shops.firstOrNull { it.inventory == e.inventory }?: return
//Check Menu Buttons //Check Menu Buttons
if (e.currentItem == main.buttons.menu) { if (e.currentItem == main.buttons.menu) {
p.openInventory(main.inventory) p.openInventory(main.inventory)
return 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 //Buy / Sell Item
val block = shop.blocks.firstOrNull { it.item == e.currentItem } ?: return val block = shop.blocks.firstOrNull { it.item == e.currentItem } ?: return

@ -9,7 +9,8 @@ import org.bukkit.configuration.ConfigurationSection
import org.bukkit.inventory.Inventory import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import java.io.File import java.io.File
import java.lang.IllegalArgumentException import kotlin.math.ceil
import kotlin.math.max
object Configurations { object Configurations {
var dataFolder = File("") var dataFolder = File("")
@ -94,7 +95,7 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) {
} }
val title by string("name", "&a$name") val title by string("name", "&a$name")
val size by int("size", 9) val size by int("size", 18)
val menuRow by int("buttons.menuRow", -1) val menuRow by int("buttons.menuRow", -1)
val blocksRaw by section("items") val blocksRaw by section("items")
@ -123,9 +124,22 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) {
} }
?: listOf() ?: listOf()
val inventory: Inventory = Bukkit.createInventory(null, size, title.c).apply { val pages = ceil(blocks.size.toDouble() / (size - 9).toDouble()).toInt()
//Set Single Row Menu val inventories = mutableListOf<Inventory>()
if (menuRow != -1) {
val inventory: Inventory
init {
inventory = makeInventory(size, title, menuRow, blocks, 1)
inventories += inventory
for (i in 2..pages) {
inventories += makeInventory(size, title, menuRow, blocks, i)
}
}
}
fun makeInventory(size: Int, title: String, menuRow: Int = -1, items: List<ShopConfig.Block>, page: Int): Inventory {
return Bukkit.createInventory(null, size, title.c).apply {
if (menuRow > 0) {
val buttons = Configurations.main.buttons val buttons = Configurations.main.buttons
for (i in menuRow until menuRow + 9) { for (i in menuRow until menuRow + 9) {
setItem(i, buttons.pane) setItem(i, buttons.pane)
@ -135,15 +149,17 @@ class ShopConfig(val name: String, file: File) : ConfigFile(file) {
setItem(menuRow + 8, buttons.forward) setItem(menuRow + 8, buttons.forward)
} }
blocks.forEach { val dropping = (page - 1) * (size - max(0, menuRow))
setItem(firstEmpty(), it.item) items.drop(dropping).forEach {
if (firstEmpty() != -1) setItem(firstEmpty(), it.item)
else return@apply
} }
} }
} }
fun ConfigurationSection.asItem(buy: Int = -1, sell: Int = -1): ItemStack { fun ConfigurationSection.asItem(buy: Int = -1, sell: Int = -1): ItemStack {
val material = Material.valueOf(getString("material").toUpperCase()) val material = Material.valueOf(getString("material").toUpperCase())
val item = ItemStack(material, getInt("quantity", 1), getInt("data", 0).toShort()) val item = ItemStack(material, getInt("quantity", 1), getInt("damage", 0).toShort())
item.itemMeta = item.itemMeta.apply { item.itemMeta = item.itemMeta.apply {
displayName = getString("name", "").c displayName = getString("name", "").c

@ -12,7 +12,6 @@ class ShopReborn : JavaPlugin() {
override fun onEnable() { override fun onEnable() {
saveDefaultConfig() saveDefaultConfig()
Configurations.dataFolder = dataFolder Configurations.dataFolder = dataFolder
Configurations.main = Configurations.main =
MainConfig(dataFolder["config.yml"]) MainConfig(dataFolder["config.yml"])
@ -22,7 +21,6 @@ class ShopReborn : JavaPlugin() {
command("shop") { sender, args -> command("shop") { sender, args ->
if (sender !is Player) return@command if (sender !is Player) return@command
info("a")
if (args.isEmpty()) { if (args.isEmpty()) {
sender.openInventory(Configurations.main.inventory) sender.openInventory(Configurations.main.inventory)
return@command return@command

@ -1,7 +1,7 @@
buttons: buttons:
pane: pane:
material: STAINED_GLASS_PANE material: STAINED_GLASS_PANE
data: 7 # Gray damage: 7 # Gray
previous: previous:
material: FEATHER material: FEATHER
name: "&ePrevious Page" name: "&ePrevious Page"