Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 195ee9a

Browse files
committed
feat(menu): add Inventory#addButton(slot, consumer) & Inventory#setItem(slot, item, consumer)
1 parent 1e6459e commit 195ee9a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

common/src/main/java/com/undefined/api/menu/normal/UndefinedMenu.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,27 @@ abstract class UndefinedMenu(private val title: String, private val size: Int) {
6060
* @param itemStack The ItemStack to set in the slot.
6161
* @param movable Whether the item is movable or not. Defaults to false.
6262
*/
63-
fun Inventory.setItem(slot: Int, itemStack:ItemStack, movable: Boolean = false) {
63+
fun Inventory.setItem(slot: Int, itemStack: ItemStack, movable: Boolean = false) {
6464
if (slot > size) return Bukkit.getLogger().warning("Menu : $title item outside of menu. Slot : $slot")
6565
movables.remove(slot)
6666
this.setItem(slot, itemStack)
6767
if (!movable) movables.add(slot)
6868
}
6969

70+
/**
71+
* Sets an item in the inventory at the specified slot. If the slot is outside the
72+
* inventory size, a warning message is logged and the method returns without
73+
* performing any action.
74+
*
75+
* @param slot The slot number where the item should be set.
76+
* @param itemStack The ItemStack to set in the slot.
77+
* @param consumer A lambda expression that takes a [ClickData] object as an argument and performs actions based on the click event.
78+
*/
79+
fun Inventory.setItem(slot: Int, itemStack: ItemStack, consumer: ClickData.() -> Unit) {
80+
this.setItem(slot, itemStack)
81+
buttons.add(Button(slot, consumer))
82+
}
83+
7084
/**
7185
* Adds a button to the inventory.
7286
*
@@ -76,6 +90,16 @@ abstract class UndefinedMenu(private val title: String, private val size: Int) {
7690
buttons.add(button)
7791
}
7892

93+
/**
94+
* Adds a button to the inventory.
95+
*
96+
* @param slot The slot number where the button should be placed.
97+
* @param consumer A lambda expression that takes a [ClickData] object as an argument and performs actions based on the click event.
98+
*/
99+
fun Inventory.addButton(slot: Int, consumer: ClickData.() -> Unit) {
100+
buttons.add(Button(slot, consumer))
101+
}
102+
79103
fun Inventory.addButtons(slot: List<Int>, consumer: ClickData.() -> Unit) {
80104
slot.forEach { addButton(Button(it, consumer)) }
81105
}

0 commit comments

Comments
 (0)