Added onOpen to menu
This commit is contained in:
@@ -68,6 +68,14 @@ public interface MenuBuilder {
|
||||
*/
|
||||
MenuBuilder onClose(@NotNull CloseHandler action);
|
||||
|
||||
/**
|
||||
* Set the menu open handler.
|
||||
*
|
||||
* @param action The handler.
|
||||
* @return The builder.
|
||||
*/
|
||||
MenuBuilder onOpen(@NotNull OpenHandler action);
|
||||
|
||||
/**
|
||||
* Set the action to run on render.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.willfp.eco.core.gui.menu;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Interface to run on menu open.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface OpenHandler {
|
||||
/**
|
||||
* Performs this operation on the given arguments.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param menu The menu.
|
||||
*/
|
||||
void handle(@NotNull Player player,
|
||||
@NotNull Menu menu);
|
||||
}
|
||||
@@ -109,6 +109,10 @@ fun slot(
|
||||
fun MenuBuilder.onClose(action: (InventoryCloseEvent, Menu) -> Unit): MenuBuilder =
|
||||
this.onClose { a, b -> action(a, b) }
|
||||
|
||||
/** @see MenuBuilder.onOpen */
|
||||
fun MenuBuilder.onOpen(action: (Player, Menu) -> Unit): MenuBuilder =
|
||||
this.onOpen { a, b -> action(a, b) }
|
||||
|
||||
/** @see MenuBuilder.modify */
|
||||
fun MenuBuilder.modify(modifier: (MenuBuilder) -> Unit): MenuBuilder =
|
||||
this.modfiy { modifier(it) }
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.willfp.eco.internal.gui.menu
|
||||
|
||||
import com.willfp.eco.core.gui.menu.CloseHandler
|
||||
import com.willfp.eco.core.gui.menu.Menu
|
||||
import com.willfp.eco.core.gui.menu.OpenHandler
|
||||
import com.willfp.eco.core.gui.slot.Slot
|
||||
import com.willfp.eco.internal.gui.slot.EcoSlot
|
||||
import com.willfp.eco.util.NamespacedKeyUtils
|
||||
@@ -19,7 +20,8 @@ class EcoMenu(
|
||||
val slots: List<MutableList<EcoSlot>>,
|
||||
private val title: String,
|
||||
private val onClose: CloseHandler,
|
||||
private val onRender: (Player, Menu) -> Unit
|
||||
private val onRender: (Player, Menu) -> Unit,
|
||||
private val onOpen: OpenHandler
|
||||
) : Menu {
|
||||
override fun getSlot(row: Int, column: Int): Slot {
|
||||
if (row < 1 || row > this.rows) {
|
||||
@@ -49,6 +51,7 @@ class EcoMenu(
|
||||
|
||||
player.openInventory(inventory)
|
||||
MenuHandler.registerInventory(inventory, this, player)
|
||||
onOpen.handle(player, this)
|
||||
inventory.asRenderedInventory()?.generateCaptive()
|
||||
return inventory
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.gui.menu
|
||||
import com.willfp.eco.core.gui.menu.CloseHandler
|
||||
import com.willfp.eco.core.gui.menu.Menu
|
||||
import com.willfp.eco.core.gui.menu.MenuBuilder
|
||||
import com.willfp.eco.core.gui.menu.OpenHandler
|
||||
import com.willfp.eco.core.gui.slot.FillerMask
|
||||
import com.willfp.eco.core.gui.slot.FillerSlot
|
||||
import com.willfp.eco.core.gui.slot.Slot
|
||||
@@ -21,6 +22,7 @@ class EcoMenuBuilder(private val rows: Int ) : MenuBuilder {
|
||||
private var maskSlots: List<MutableList<Slot?>>
|
||||
private val slots: List<MutableList<Slot?>> = ListUtils.create2DList(rows, 9)
|
||||
private var onClose = CloseHandler { _, _ -> }
|
||||
private var onOpen = OpenHandler { _, _ -> }
|
||||
private var onRender: (Player, Menu) -> Unit = { _, _ -> }
|
||||
|
||||
override fun setTitle(title: String): MenuBuilder {
|
||||
@@ -54,6 +56,11 @@ class EcoMenuBuilder(private val rows: Int ) : MenuBuilder {
|
||||
return this
|
||||
}
|
||||
|
||||
override fun onOpen(action: OpenHandler): MenuBuilder {
|
||||
onOpen = action
|
||||
return this
|
||||
}
|
||||
|
||||
override fun onRender(action: BiConsumer<Player, Menu>): MenuBuilder {
|
||||
onRender = { a, b -> action.accept(a, b) }
|
||||
return this
|
||||
@@ -83,7 +90,7 @@ class EcoMenuBuilder(private val rows: Int ) : MenuBuilder {
|
||||
finalSlots.add(tempRow)
|
||||
}
|
||||
|
||||
return EcoMenu(rows, finalSlots, title, onClose, onRender)
|
||||
return EcoMenu(rows, finalSlots, title, onClose, onRender, onOpen)
|
||||
}
|
||||
|
||||
init {
|
||||
|
||||
Reference in New Issue
Block a user