forked from ServerOpenMC/PluginV2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemMenuBuilder.java
More file actions
342 lines (317 loc) · 16 KB
/
Copy pathItemMenuBuilder.java
File metadata and controls
342 lines (317 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package fr.openmc.api.menulib.utils;
import fr.openmc.api.menulib.Menu;
import fr.openmc.api.menulib.MenuLib;
import fr.openmc.api.menulib.PaginatedMenu;
import fr.openmc.core.bootstrap.integration.OMCLogger;
import fr.openmc.core.registry.items.CustomItem;
import fr.openmc.core.utils.bukkit.ItemBuilder;
import fr.openmc.core.utils.text.messages.MessageType;
import fr.openmc.core.utils.text.messages.MessagesManager;
import fr.openmc.core.utils.text.messages.Prefix;
import io.papermc.paper.datacomponent.DataComponentType;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import java.util.function.Consumer;
/**
* The {@code ItemBuilder} class is a utility for creating and customizing {@link ItemStack} objects
* in a menu context. It provides methods to set item properties, handle click events, and manage
* metadata, making it easier to create interactive items within a menu system.
*/
public class ItemMenuBuilder extends ItemBuilder {
@Getter
private final Menu itemMenu;
@Getter
private boolean previousButton;
@Getter
private boolean nextButton;
@Getter
private boolean backButton;
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link Material}.
* This constructor initializes the {@code ItemBuilder} to create items using the given menu and material,
* with no additional customizations for the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param material The {@link Material} of the item. It determines the base appearance and behavior
* of the item being created.
*/
public ItemMenuBuilder(Menu itemMenu, Material material) {
this(itemMenu, material, null, false);
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link CustomItem}.
* This constructor initializes the {@code ItemBuilder} to create items using the given menu and custom item,
* with no additional customizations for the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param customItem The {@link CustomItem} of the item. It determines the base appearance and behavior
* of the item being created.
*/
public ItemMenuBuilder(Menu itemMenu, CustomItem customItem) {
this(itemMenu, customItem.getBest(), null, false);
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link Material}.
* This constructor initializes the {@code ItemBuilder} to create items using the given menu and custom item,
* with no additional customizations for the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param material The {@link Material} of the item. It determines the base appearance and behavior
* of the item being created.
*/
public ItemMenuBuilder(Menu itemMenu, Material material, boolean isBackButton) {
this(itemMenu, material, null, isBackButton);
this.backButton = isBackButton;
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link CustomItem}.
* This constructor initializes the {@code ItemBuilder} to create items using the given menu and custom item,
* with no additional customizations for the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param customItem The {@link CustomItem} of the item. It determines the base appearance and behavior
* of the item being created.
*/
public ItemMenuBuilder(Menu itemMenu, CustomItem customItem, boolean isBackButton) {
this(itemMenu, customItem.getBest(), null, isBackButton);
this.backButton = isBackButton;
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link ItemStack}.
* This constructor initializes the {@code ItemBuilder} to create items with the given menu and item,
* while not applying any additional customizations to the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} that this item will be associated with. This parameter represents
* the context in which the item will exist, such as an inventory or menu framework.
* @param item The {@link ItemStack} defining the base item configuration. It includes the material,
* amount, and current metadata of the item.
*/
public ItemMenuBuilder(Menu itemMenu, ItemStack item) {
this(itemMenu, item, null);
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu} and {@link ItemStack}.
* This constructor initializes the {@code ItemBuilder} to create items with the given menu and item,
* while not applying any additional customizations to the {@link ItemMeta}.
*
* @param itemMenu The {@link Menu} that this item will be associated with. This parameter represents
* the context in which the item will exist, such as an inventory or menu framework.
* @param item The {@link ItemStack} defining the base item configuration. It includes the material,
* amount, and current metadata of the item.
*/
public ItemMenuBuilder(Menu itemMenu, ItemStack item, boolean isBackButton) {
this(itemMenu, item, null, isBackButton);
this.backButton = isBackButton;
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu}, {@link Material},
* and an {@link Consumer} for customizing the {@link ItemMeta}.
* This constructor initializes the {@code ItemBuilder} with a menu, a material to define the base item,
* and a consumer for applying additional metadata customization to the item.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param material The {@link Material} of the item. It determines the base appearance and behavior
* of the item being created.
* @param itemMeta A {@link Consumer} that customizes the {@link ItemMeta} of the item. It allows further
* modification of properties such as the display name, lore, enchantments, and more.
*/
public ItemMenuBuilder(Menu itemMenu, Material material, Consumer<ItemMeta> itemMeta) {
this(itemMenu, new ItemStack(material), itemMeta);
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu}, {@link Material},
* and an {@link Consumer} for customizing the {@link ItemMeta}.
* This constructor initializes the {@code ItemBuilder} with a menu, a material to define the base item,
* and a consumer for applying additional metadata customization to the item.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param material The {@link Material} of the item. It determines the base appearance and behavior
* of the item being created.
* @param itemMeta A {@link Consumer} that customizes the {@link ItemMeta} of the item. It allows further
* modification of properties such as the display name, lore, enchantments, and more.
*/
public ItemMenuBuilder(Menu itemMenu, Material material, Consumer<ItemMeta> itemMeta, boolean isBackButton) {
this(itemMenu, new ItemStack(material), itemMeta);
this.backButton = isBackButton;
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu}, {@link ItemStack},
* and an {@link Consumer} for customizing the {@link ItemMeta}.
* This constructor initializes the {@code ItemBuilder} with a menu, a specific item to define
* the base configuration, and a consumer for applying additional metadata customizations to the item.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param item The {@link ItemStack} defining the base item configuration. It includes the material,
* amount, and current metadata of the item.
* @param itemMeta A {@link Consumer} that customizes the {@link ItemMeta} of the item. It allows further
* modification of properties such as the display name, lore, enchantments, and more.
*/
public ItemMenuBuilder(Menu itemMenu, ItemStack item, Consumer<ItemMeta> itemMeta) {
super(item, itemMeta);
this.itemMenu = itemMenu;
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu}, {@link ItemStack},
* and an {@link Consumer} for customizing the {@link ItemMeta}.
* This constructor initializes the {@code ItemBuilder} with a menu, a specific item to define
* the base configuration, and a consumer for applying additional metadata customizations to the item.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param item The {@link ItemStack} defining the base item configuration. It includes the material,
* amount, and current metadata of the item.
* @param itemMeta A {@link Consumer} that customizes the {@link ItemMeta} of the item. It allows further
* modification of properties such as the display name, lore, enchantments, and more.
*/
public ItemMenuBuilder(Menu itemMenu, ItemStack item, Consumer<ItemMeta> itemMeta, boolean isBackButton) {
super(item, itemMeta);
this.itemMenu = itemMenu;
this.backButton = isBackButton;
}
/**
* Constructs an {@code ItemBuilder} with the specified {@link Menu}, {@link CustomItem},
* and an {@link Consumer} for customizing the {@link ItemMeta}.
* This constructor initializes the {@code ItemBuilder} with a menu, a specific item to define
* the base configuration, and a consumer for applying additional metadata customizations to the item.
*
* @param itemMenu The {@link Menu} this item will be associated with. It represents the context in which
* the item exists, such as a specific inventory or menu framework.
* @param customitem The {@link CustomItem} defining the base item configuration. It includes the material,
* amount, and current metadata of the item.
* @param itemMeta A {@link Consumer} that customizes the {@link ItemMeta} of the item. It allows further
* modification of properties such as the display name, lore, enchantments, and more.
*/
public ItemMenuBuilder(Menu itemMenu, CustomItem customitem, Consumer<ItemMeta> itemMeta) {
super(customitem.getBest());
this.itemMenu = itemMenu;
}
/**
* Sets the unique identifier for the item using the specified {@code itemId}.
* The identifier is stored in the item's {@link PersistentDataContainer} as a
* {@link String} in a lower-case format, allowing it to be associated with
* specific functionality in the menu system.
*
* @param itemId The unique identifier to associate with the item. This value is stored
* in a lower-case format within the item's {@link PersistentDataContainer}.
* @return The current instance of {@link fr.openmc.api.menulib.utils.ItemMenuBuilder}, allowing for method chaining
* when creating and customizing items.
*/
public ItemMenuBuilder setItemId(String itemId) {
super.setItemId(itemId);
return this;
}
/**
* Sets the click event handler for the item. This method associates the specified
* {@link Consumer} with the item to define a custom behavior when the item is clicked
* in the menu.
*
* @param e A {@link Consumer} of {@link InventoryClickEvent} that specifies the action
* to be performed when the item is clicked.
* @return The current instance of {@link ItemMenuBuilder}, allowing method chaining
* for further customization of the item.
*/
public ItemMenuBuilder setOnClick(Consumer<InventoryClickEvent> e) {
try {
MenuLib.setItemClickEvent(itemMenu, this, e);
} catch (Exception ex) {
OMCLogger.error("An error occurred while setting the click event: {}", ex.getMessage(), ex);
}
return this;
}
/**
* Sets the item to act as a close button. When the item is clicked, it closes
* the inventory of the menu owner.
*
* @return The current instance of {@link ItemMenuBuilder}, allowing method chaining
* for further customization of the item.
*/
public ItemMenuBuilder setCloseButton() {
try {
Consumer<InventoryClickEvent> clickEventConsumer = inventoryClickEvent -> itemMenu.getOwner().closeInventory();
setOnClick(clickEventConsumer);
return this;
} catch (Exception e) {
MessagesManager.sendMessage(itemMenu.getOwner(), Component.translatable("api.menulib.an_error_occurred"), Prefix.OPENMC, MessageType.ERROR, false);
OMCLogger.error("An error occurred while setting the close button: {}", e.getMessage(), e);
itemMenu.getOwner().closeInventory();
}
return this;
}
/**
* Sets the item to act as a button that navigates to the next page in a paginated menu.
* When the item is clicked, the page within the associated {@link PaginatedMenu} is incremented,
* provided the current page is not the last. The updated menu is then reopened for the user.
*
* @return The current instance of {@link ItemMenuBuilder}, enabling method chaining
* for additional configurations of the item.
*/
public ItemMenuBuilder setNextPageButton() {
try {
Consumer<InventoryClickEvent> clickEventConsumer = inventoryClickEvent -> {
if (itemMenu instanceof PaginatedMenu menu) {
menu.setPage(menu.isLastPage() ? menu.getPage() : menu.getPage() + 1);
menu.open();
}
};
setOnClick(clickEventConsumer);
this.nextButton = true;
return this;
} catch (Exception e) {
MessagesManager.sendMessage(itemMenu.getOwner(), Component.translatable("api.menulib.an_error_occurred"), Prefix.OPENMC, MessageType.ERROR, false);
OMCLogger.error("An error occurred while setting the next page button: {}", e.getMessage(), e);
itemMenu.getOwner().closeInventory();
}
return this;
}
/**
* Sets the item to act as a button that navigates to the previous page in a paginated menu.
* When the item is clicked, the page within the associated {@link PaginatedMenu} is decremented,
* provided the current page is not the first. The updated menu is then reopened for the user.
*
* @return The current instance of {@link ItemMenuBuilder}, enabling method chaining
* for additional configurations of the item.
*/
public ItemMenuBuilder setPreviousPageButton() {
try {
Consumer<InventoryClickEvent> clickEventConsumer = inventoryClickEvent -> {
if (itemMenu instanceof PaginatedMenu menu) {
menu.setPage(menu.getPage() == 0 ? 0 : menu.getPage() - 1);
menu.open();
}
};
setOnClick(clickEventConsumer);
this.previousButton = true;
return this;
} catch (Exception e) {
itemMenu.getOwner().closeInventory();
MessagesManager.sendMessage(itemMenu.getOwner(), Component.translatable("api.menulib.an_error_occurred"), Prefix.OPENMC, MessageType.ERROR, false);
OMCLogger.error("An error occurred while setting the previous page button: {}", e.getMessage(), e);
}
return this;
}
public ItemMenuBuilder setBackButton() {
this.backButton = true;
return this;
}
@SuppressWarnings("UnstableApiUsage")
public ItemMenuBuilder hide(DataComponentType... typesToHide) {
super.hide(typesToHide);
return this;
}
public ItemMenuBuilder hideTooltip(boolean hideTooltip) {
super.hideTooltip(hideTooltip);
return this;
}
}