Skip to content

Menu DSL Advanced

Gabriel Souza edited this page Jul 10, 2020 · 6 revisions

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

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))
}

result

Menu events

Currently, you only learn the slot's events, but the Menu has there on Events that you can listen.

  • We can use onOpen to 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
) : MenuPlayerInventory
menu("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 for onOpen you can't do anything about, onOpen means that the menu already opens to the Player. Differently, preOpen you 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
) : MenuPlayerCancellable
menu("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.

  • onUpdate works the same way that works on slot, is triggered when the menu updates with the configurable updateDelay.
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
) : MenuPlayerInventory
menu("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
) : MenuPlayer
menu("test", 3, false) {
    slot(2, 5, item(Material.DIAMOND))

    onClose {
        player.inventory.addItem(item(Material.EMERALD, amount = 5))
    }
}

Data

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 as onClick) 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

Clone this wiki locally