-
Notifications
You must be signed in to change notification settings - Fork 18
Menu DSL Advanced
Menu DSL in one hand can be simple to use but in the other, super powerful, here you will learn what Menu DSL can do to help you.
Base slot works on providing you access to all slots that you don't declare.
menu("test", 3, true) {
baseSlot.apply {
onRender {
showingItem = item(Material.STAINED_GLASS_PANE)
}
}
slot(2, 5, item(Material.DIAMOND))
}Currently, you only learn the slot's events, but the Menu has there on Events that you can listen.
- We can use
onOpento listen when the menu Opens
typealias MenuPlayerOpenEvent = MenuPlayerOpen.() -> Unit
fun onOpen(open: MenuPlayerOpenEvent)
class MenuPlayerOpen(
override val menu: Menu<*>,
override val player: Player,
override val inventory: Inventory
) : MenuPlayerInventorymenu("test", 3, false) {
slot(2, 5, item(Material.DIAMOND))
onOpen {
log.info("The player ${player.name} open the reward menu")
}
}Here we log a message whe a player opens the Menu.
-
preOpen: When you listen foronOpenyou can't do anything about,onOpenmeans that the menu already opens to the Player. Differently,preOpenyou can cancel the open of a Menu.
typealias MenuPlayerPreOpenEvent = MenuPlayerPreOpen.() -> Unit
fun preOpen(preOpen: MenuPlayerPreOpenEvent)
class MenuPlayerPreOpen(
override val menu: Menu<*>,
override val player: Player,
override var canceled: Boolean = false
) : MenuPlayerCancellablemenu("test", 3, false) {
slot(2, 5, item(Material.DIAMOND))
onOpen {
log.info("The player ${player.name} open the reward menu")
}
preOpen {
if(!player.hasPermission("rank.that.was.reward")
canceled = true
}
}Here we only enable to open the Menu for the players that have the permission rank.that.was.reward.
-
onUpdateworks the same way that works on slot, is triggered when the menu updates with the configurableupdateDelay.
typealias MenuPlayerUpdateEvent = MenuPlayerUpdate.() -> Unit
fun onUpdate(update: MenuPlayerUpdateEvent)
class MenuPlayerUpdate(
override val menu: Menu<*>,
override val player: Player,
override val inventory: Inventory,
var title: String
) : MenuPlayerInventorymenu("test", 3, false) {
updateDelay = 20 // 1 second
slot(2, 5, item(Material.DIAMOND)) {
onClick {
Console.command("rank-reward ${player.name}")
close()
}
}
preOpen {
if(!player.hasPermission("rank.that.was.reward")
canceled = true
}
onUpdate {
getItem(2, 5)?.amount?.inc()
}
}Here we update very single second we increment one in the amount of the DIAMOND.
-
onClose: Called when the player closes the Menu
typealias MenuPlayerCloseEvent = MenuPlayerClose.() -> Unit
fun onClose(close: MenuPlayerCloseEvent)
class MenuPlayerClose(
override val menu: Menu<*>,
override val player: Player
) : MenuPlayermenu("test", 3, false) {
slot(2, 5, item(Material.DIAMOND))
onClose {
player.inventory.addItem(item(Material.EMERALD, amount = 5))
}
}One of the most powerful features of the Menu is the Data. The Menu holds data values in a Map/Dictionary that you can easily insert and access.
The Menu has four types of data cache.
-
data- Global data that is shared with all viewers/players. -
playerData- Holds only the Player data and its is cleared when the Player closes the Menu. -
slotData- Global slot data that is shared with all viewers/players and you can only access within a Slot. -
playerSlotData- Holds only the Player slot data, that you can only access within as slot (such asonClick) and its is cleared when the Player closes the Menu.
data and playerData can easily be access when it's not inside that menu, this means that you can insert some value before open the Menu to a Player, and use this data inside the Menu to different things.
WIP