9
0
mirror of https://github.com/Auxilor/EcoMenus.git synced 2025-12-19 15:09:20 +00:00

Added pagination

This commit is contained in:
Auxilor
2023-06-03 19:48:16 +01:00
parent aa70210cab
commit 039d2221b7
5 changed files with 99 additions and 73 deletions

View File

@@ -24,11 +24,12 @@ class ConfigurableSlot(
) : PositionedComponent { ) : PositionedComponent {
override val row: Int = config.getInt("location.row") override val row: Int = config.getInt("location.row")
override val column: Int = config.getInt("location.column") override val column: Int = config.getInt("location.column")
val page: Int = config.getInt("location.page")
override val layer = runCatching { enumValueOf<MenuLayer>(config.getString("layer")) } override val layer = runCatching { enumValueOf<MenuLayer>(config.getString("layer")) }
.getOrElse { MenuLayer.MIDDLE } .getOrElse { MenuLayer.MIDDLE }
val context = baseContext.with("slot at row ${row}, column $column") val context = baseContext.with("slot at row ${row}, column $column, page $page")
private val conditions = Conditions.compile( private val conditions = Conditions.compile(
config.getSubsections("conditions"), config.getSubsections("conditions"),

View File

@@ -3,6 +3,7 @@ package com.willfp.ecomenus.components
import com.willfp.eco.core.gui.GUIComponent import com.willfp.eco.core.gui.GUIComponent
import com.willfp.eco.core.gui.menu.MenuBuilder import com.willfp.eco.core.gui.menu.MenuBuilder
import com.willfp.eco.core.gui.menu.MenuLayer import com.willfp.eco.core.gui.menu.MenuLayer
import com.willfp.eco.core.gui.page.PageBuilder
interface PositionedComponent : GUIComponent { interface PositionedComponent : GUIComponent {
val row: Int val row: Int
@@ -23,11 +24,13 @@ interface PositionedComponent : GUIComponent {
override fun getColumns() = columnSize override fun getColumns() = columnSize
} }
fun MenuBuilder.addComponent( // Little hack to prevent two identical methods
@Suppress("UNCHECKED_CAST")
fun <T : PageBuilder> T.addComponent(
component: PositionedComponent component: PositionedComponent
) = if (component.isEnabled) addComponent( ): T = if (component.isEnabled) addComponent(
component.layer, component.layer,
component.row, component.row,
component.column, component.column,
component component
) else this ) as T else this

View File

@@ -2,6 +2,7 @@ package com.willfp.ecomenus.components.impl
import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.gui.menu.Menu import com.willfp.eco.core.gui.menu.Menu
import com.willfp.eco.core.gui.menu.MenuLayer
import com.willfp.eco.core.gui.page.PageChanger import com.willfp.eco.core.gui.page.PageChanger
import com.willfp.eco.core.items.Items import com.willfp.eco.core.items.Items
import com.willfp.ecomenus.components.PositionedComponent import com.willfp.ecomenus.components.PositionedComponent
@@ -18,6 +19,7 @@ class PositionedPageChanger(
override val row: Int = config.getInt("location.row") override val row: Int = config.getInt("location.row")
override val column: Int = config.getInt("location.column") override val column: Int = config.getInt("location.column")
override val layer = MenuLayer.TOP
override val isEnabled = config.getBool("enabled") override val isEnabled = config.getBool("enabled")

View File

@@ -37,15 +37,26 @@ fun Menu.close(player: Player) =
this.previousMenus[player].popOrNull()?.open(player) ?: player.closeInventory() this.previousMenus[player].popOrNull()?.open(player) ?: player.closeInventory()
fun buildMenu(plugin: EcoPlugin, menu: EcoMenu, config: Config): Menu { fun buildMenu(plugin: EcoPlugin, menu: EcoMenu, config: Config): Menu {
val mask = FillerMask( val pageConfigs = config.getSubsections("pages")
MaskItems.fromItemNames(config.getStrings("mask.materials")),
*config.getStrings("mask.pattern").toTypedArray()
)
return menu(mask.rows) { val slots = mutableListOf<ConfigurableSlot>()
for (slotConfig in config.getSubsections("slots")) {
val slot = ConfigurableSlot(
plugin,
ViolationContext(plugin, "menu ${menu.id}"),
slotConfig
)
slots += slot
}
return menu(config.getInt("rows")) {
title = config.getFormattedString("title") title = config.getFormattedString("title")
setMask(mask) allowChangingHeldItem()
maxPages(pageConfigs.size)
addComponent( addComponent(
PositionedPageChanger( PositionedPageChanger(
@@ -61,19 +72,26 @@ fun buildMenu(plugin: EcoPlugin, menu: EcoMenu, config: Config): Menu {
) )
) )
for (page in config.getSubsections("pages")) { for (page in pageConfigs) {
addPage(page.getInt("page")) { val mask = FillerMask(
for (slotConfig in config.getSubsections("slots")) { MaskItems.fromItemNames(page.getStrings("mask.items")),
val slot = ConfigurableSlot( *page.getStrings("mask.pattern").toTypedArray()
plugin, )
ViolationContext(plugin, "menu ${menu.id}"),
slotConfig val pageNumber = page.getInt("page")
)
addPage(pageNumber) {
setMask(mask)
for (slot in slots) {
if (pageNumber != slot.page) {
continue
}
try { try {
addComponent(slot) this.addComponent(slot)
} catch (e: Exception) { } catch (e: Exception) {
slot.context.log(ConfigViolation("location", "The location of the slot is invalid!")) slot.context.log(ConfigViolation("location", "Invalid location!"))
} }
} }
} }

View File

@@ -10,30 +10,8 @@ title: "Example GUI"
# (Optional) The command to open the GUI, if not set, there will be no command. # (Optional) The command to open the GUI, if not set, there will be no command.
command: examplemenu command: examplemenu
mask: # The size of the GUI, between 1 and 6
# The way the mask works is by having a list of materials rows: 6
# And then a pattern to use those materials.
# The size of the GUI is determined by the size of the mask.
# The pattern is the rows in the GUI
# Each line must be 9 long, and the amount of rows should be the amount of rows in the GUI
# A zero represents nothing
# A 1 represents the first material
# A 2 represents the second material
# And so on, you can add up to 9.
materials:
- gray_stained_glass_pane
- black_stained_glass_pane
pattern:
- "211101112"
- "211111112"
- "210000012"
- "210010012"
- "211111112"
- "211101112"
# Read https://plugins.auxilor.io/effects/configuring-a-condition # Read https://plugins.auxilor.io/effects/configuring-a-condition
# The conditions required to open the GUI # The conditions required to open the GUI
@@ -62,38 +40,62 @@ backwards-arrow:
pages: pages:
- page: 1 - page: 1
slots: mask:
- item: barrier name:"&cClose" # The way the mask works is by having a list of materials
lore: [ ] # And then a pattern to use those materials.
location:
row: 6
column: 5
# (Optional) You can specify the layer of the slot.
# The layer can be any of: lower, middle, upper, or top (defaults to middle)
# This is useful if you want to have a slot on top of another slot, and
# have the upper slot show if some conditions are met.
layer: middle
# Read https://plugins.auxilor.io/effects/configuring-a-condition # The pattern is the rows in the GUI
# The conditions required to click the item # Each line must be 9 long, and the amount of rows should be the amount of rows in the GUI
conditions: [ ] # A zero represents nothing
# A 1 represents the first material
# A 2 represents the second material
# And so on, you can add up to 9.
# If the item should be shown if the conditions are not met items:
show-if-not-met: true - gray_stained_glass_pane
- black_stained_glass_pane
pattern:
- "211101112"
- "211111112"
- "210000012"
- "210010012"
- "211111112"
- "211101112"
left-click: slots:
- type: effects - item: barrier name:"&cClose"
effects: lore: [ ]
- id: send_message location:
args: row: 6
message: "&cYou clicked the close button!" column: 5
page: 1
- type: command # (Optional) You can specify the layer of the slot.
commands: [ ] # The layer can be any of: lower, middle, upper, or top (defaults to middle)
# This is useful if you want to have a slot on top of another slot, and
# have the upper slot show if some conditions are met.
layer: middle
- type: menu # Read https://plugins.auxilor.io/effects/configuring-a-condition
menu: other_example_menu # The conditions required to click the item
conditions: [ ]
- item: console_command # If the item should be shown if the conditions are not met
commands: show-if-not-met: true
- "eco give %player% 100"
left-click:
- type: effects
effects:
- id: send_message
args:
message: "&cYou clicked the close button!"
- type: command
commands: [ ]
- type: menu
menu: other_example_menu
- item: console_command
commands:
- "eco give %player% 100"