@@ -18,7 +18,12 @@ import org.bukkit.inventory.ItemStack
1818 * @property size The size of the menu.
1919 * @property list The list of ItemStacks for the menu.
2020 */
21- abstract class UndefinedPageMenu (private val title : String , private val size : Int , private val list : List <ItemStack >): UndefinedMenu(title, size) {
21+ abstract class UndefinedPageMenu (private val title : String , private val size : Int , private var list : List <ItemStack >): UndefinedMenu(title, size) {
22+
23+ /* *
24+ * Represents a lambda that returns a list of `ItemStack`.
25+ */
26+ private val lambdaList: ((Unit ) -> List <ItemStack >)? = null
2227
2328 /* *
2429 * Represents a nullable property that holds a reference to a PageButton object.
@@ -83,6 +88,32 @@ abstract class UndefinedPageMenu(private val title: String, private val size: In
8388 */
8489 private var totalPages = 0
8590
91+ /* *
92+ * Constructs a `UndefinedPageMenu` object with the given `title`, `menuSize`, and `list`.
93+ *
94+ * @param title The title of the menu.
95+ * @param size The size of the menu.
96+ * @param list A lambda that returns a list of `ItemStack` objects.
97+ */
98+ constructor (title: String , size: Int , list: (Unit ) -> List <ItemStack >) : this (
99+ title,
100+ size,
101+ list.invoke(Unit )
102+ )
103+
104+ /* *
105+ * Constructs a `UndefinedPageMenu` object with the given `title`, `menuSize`, and `list`.
106+ *
107+ * @param title The title of the menu.
108+ * @param menuSize The size of the menu. Defaults to `MenuSize.LARGE`.
109+ * @param list A lambda that returns a list of `ItemStack` objects.
110+ */
111+ constructor (title: String , menuSize: MenuSize = MenuSize .LARGE , list: (Unit ) -> List <ItemStack >) : this (
112+ title,
113+ menuSize.size,
114+ list.invoke(Unit )
115+ )
116+
86117 /* *
87118 * Constructs a `UndefinedPageMenu` object with the given `title`, `menuSize`, and `list`.
88119 *
@@ -96,6 +127,18 @@ abstract class UndefinedPageMenu(private val title: String, private val size: In
96127 list
97128 )
98129
130+ /* *
131+ * If a lambda list is present, will update the main list accordingly to the return value of the lambda.
132+ *
133+ * It will also generate the inventory for the second time if shouldGenerateInventory is set to true.
134+ *
135+ * @param shouldGenerateInventory A boolean that if true, will run generateInventory. It defaults to true.
136+ * @see generateInventory
137+ */
138+ fun update (shouldGenerateInventory : Boolean = true) {
139+ lambdaList?.let { list = it.invoke(Unit ) }
140+ if (shouldGenerateInventory) generateInventory()
141+ }
99142
100143 /* *
101144 * Navigates to the next page in the menu.
@@ -136,14 +179,12 @@ abstract class UndefinedPageMenu(private val title: String, private val size: In
136179 * @param display The lambda function used to configure the inventory.
137180 * @return The created inventory.
138181 */
139- fun createPageInventory (display : Inventory .() -> Unit ): Inventory {
182+ fun createPageInventory (display : Inventory .() -> Unit ): Inventory {
140183 inventory = Bukkit .createInventory(null , size, title).apply (display)
141184 var index = 0
142185
143186 inventory!! .contents.forEach {
144- if (it != null ) {
145- itemsMap[index] = it
146- }
187+ if (it != null ) itemsMap[index] = it
147188 index++
148189 }
149190
0 commit comments