-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathButton.java
More file actions
458 lines (360 loc) · 13.8 KB
/
Button.java
File metadata and controls
458 lines (360 loc) · 13.8 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package fr.maxlego08.menu.api.button;
import fr.maxlego08.menu.api.Inventory;
import fr.maxlego08.menu.api.MenuItemStack;
import fr.maxlego08.menu.api.MenuPlugin;
import fr.maxlego08.menu.api.engine.InventoryEngine;
import fr.maxlego08.menu.api.engine.Pagination;
import fr.maxlego08.menu.api.players.DataManager;
import fr.maxlego08.menu.api.requirement.Action;
import fr.maxlego08.menu.api.requirement.RefreshRequirement;
import fr.maxlego08.menu.api.requirement.Requirement;
import fr.maxlego08.menu.api.requirement.data.ActionPlayerData;
import fr.maxlego08.menu.api.sound.SoundOption;
import fr.maxlego08.menu.api.utils.OpenLink;
import fr.maxlego08.menu.api.utils.Placeholders;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
public abstract class Button extends PlaceholderButton {
private MenuPlugin plugin;
private String buttonName;
private MenuItemStack itemStack;
private boolean isPermanent = false;
private boolean closeInventory = false;
private boolean useCache = true;
private List<String> messages = new ArrayList<>();
private SoundOption soundOption;
private String playerHead;
private OpenLink openLink;
private boolean isUpdated = false;
private boolean isMasterButtonUpdated = false;
private boolean refreshOnClick = false;
private List<ActionPlayerData> datas = new ArrayList<>();
private boolean updateOnClick = false;
private boolean isOpenAsync = false;
private List<Requirement> clickRequirements = new ArrayList<>();
private Requirement viewRequirement;
private List<Action> actions = new ArrayList<>();
private List<ButtonOption> options = new ArrayList<>();
private RefreshRequirement refreshRequirement;
private int priority; // only use for convert DeluxeMenus config to zmenu object
private boolean isInPlayerInventory;
public String getName() {
return this.buttonName;
}
public MenuItemStack getItemStack() {
return this.itemStack;
}
/**
* @param itemStack the itemStack to set
*/
public Button setItemStack(MenuItemStack itemStack) {
this.itemStack = itemStack;
return this;
}
public ItemStack getCustomItemStack(Player player) {
if (this.itemStack == null) return null;
ItemStack itemStack = this.itemStack.build(player, this.useCache);
if (this.playerHead != null && itemStack.getItemMeta() instanceof SkullMeta) {
return this.plugin.getInventoryManager().postProcessSkullItemStack(itemStack, this, player);
}
return itemStack;
}
public int getSlot() {
return this.slots.getFirst();
}
/**
* @param slot the slot to set
*/
public Button setSlot(int slot) {
this.slots = new ArrayList<>();
this.slots.add(slot);
return this;
}
public boolean isClickable() {
return true;
}
public boolean isPermanent() {
return this.isPermanent;
}
/**
* @param isPermanent the isPermanent to set
*/
public Button setPermanent(boolean isPermanent) {
this.isPermanent = isPermanent;
return this;
}
public List<String> getMessages() {
return this.messages;
}
public Button setMessages(List<String> messages) {
this.messages = messages;
return this;
}
public int getRealSlot(int inventorySize, int page) {
int slot = getSlot();
return this.isPermanent() ? slot : slot - ((page - 1) * inventorySize);
}
public SoundOption getSound() {
return this.soundOption;
}
public boolean hasSpecialRender() {
return this.getSlots().size() > 1;
}
public String getPlayerHead() {
return this.playerHead;
}
public Button setPlayerHead(String playerHead) {
this.playerHead = playerHead;
return this;
}
public void onRender(Player player, InventoryEngine inventoryEngine) {
if (inventoryEngine.getPage() == this.getPage() || this.isPermanent()) {
int inventorySize = this.isPlayerInventory() ? 36 : inventoryEngine.getInventory().getSize();
int[] slots = this.getSlots().stream().map(slot -> {
if (!this.isPermanent) {
return slot - ((this.getPage() - 1) * inventorySize);
}
return slot;
}).mapToInt(Integer::intValue).toArray();
inventoryEngine.displayFinalButton(this, slots);
}
}
public void onLeftClick(Player player, InventoryClickEvent event, InventoryEngine inventory, int slot) {
}
public void onRightClick(Player player, InventoryClickEvent event, InventoryEngine inventory, int slot) {
}
public void onMiddleClick(Player player, InventoryClickEvent event, InventoryEngine inventory, int slot) {
}
public void onInventoryClose(Player player, InventoryEngine inventory) {
}
public void onClick(Player player, @Nullable InventoryClickEvent event, InventoryEngine inventory, int slot, Placeholders placeholders) {
if (this.closeInventory()) {
player.closeInventory();
}
if (!this.datas.isEmpty()) {
DataManager dataManager = this.plugin.getDataManager();
for (ActionPlayerData actionPlayerData : this.datas) {
actionPlayerData.execute(player, dataManager);
}
}
if (!this.messages.isEmpty()) {
if (this.openLink != null) {
this.openLink.send(player, this.messages);
} else {
this.messages.forEach(message -> plugin.getMetaUpdater().sendMessage(player, this.plugin.parse(player, placeholders.parse(message))));
}
}
if (this.soundOption != null) {
this.soundOption.play(player);
}
AtomicBoolean isSuccess = new AtomicBoolean(true);
this.clickRequirements.forEach(requirement -> {
if (event == null || requirement.getClickTypes().contains(event.getClick())) {
isSuccess.set(requirement.execute(player, this, inventory, placeholders));
}
});
this.actions.forEach(action -> action.preExecute(player, this, inventory, placeholders));
this.options.forEach(option -> option.onClick(this, player, event, inventory, slot, isSuccess.get()));
this.execute(this.plugin, event == null ? ClickType.LEFT : event.getClick(), placeholders, player);
}
public void onInventoryOpen(Player player, InventoryEngine inventory, Placeholders placeholders) {
}
public boolean closeInventory() {
return this.closeInventory;
}
public Button setButtonName(String buttonName) {
this.buttonName = buttonName;
return this;
}
public Button setCloseInventory(boolean closeInventory) {
this.closeInventory = closeInventory;
return this;
}
public Button setSoundOption(SoundOption soundOption) {
this.soundOption = soundOption;
return this;
}
public OpenLink getOpenLink() {
return this.openLink;
}
public void setOpenLink(OpenLink openLink) {
this.openLink = openLink;
}
public boolean isUpdated() {
return this.isUpdated;
}
public void setUpdated(boolean isUpdated) {
this.isUpdated = isUpdated;
}
public boolean isRefreshOnClick() {
return this.refreshOnClick;
}
public void setRefreshOnClick(boolean refreshOnClick) {
this.refreshOnClick = refreshOnClick;
}
public List<ActionPlayerData> getData() {
return this.datas;
}
public void setDatas(List<ActionPlayerData> datas) {
this.datas = datas;
}
public void setPlugin(MenuPlugin plugin) {
this.plugin = plugin;
}
public boolean updateOnClick() {
return this.updateOnClick;
}
public void setUpdateOnClick(boolean updateOnClick) {
this.updateOnClick = updateOnClick;
}
public List<String> buildLore(Player player) {
return this.itemStack.getLore();
}
public String buildDisplayName(Player player) {
return this.itemStack.getDisplayName();
}
public void onBackClick(Player player, InventoryClickEvent event, InventoryEngine inventory, List<Inventory> oldInventories, Inventory toInventory, int slot) {
}
public List<Requirement> getClickRequirements() {
return this.clickRequirements;
}
public void setClickRequirements(List<Requirement> clickRequirements) {
this.clickRequirements = clickRequirements;
}
public Requirement getViewRequirement() {
return this.viewRequirement;
}
public void setViewRequirement(Requirement viewRequirement) {
this.viewRequirement = viewRequirement;
}
public boolean hasPermission() {
return this.viewRequirement != null || super.hasPermission();
}
public boolean checkPermission(Player player, InventoryEngine inventory, Placeholders placeholders) {
return super.checkPermission(player, inventory, placeholders) && (this.viewRequirement == null || this.viewRequirement.execute(player, this, inventory, placeholders));
}
public List<Action> getActions() {
return this.actions;
}
public void setActions(List<Action> actions) {
this.actions = actions;
}
public void onDrag(InventoryDragEvent event, Player player, InventoryEngine inventoryDefault) {
}
public void onInventoryClick(InventoryClickEvent event, Player player, InventoryEngine inventoryDefault) {
}
public boolean isUseCache() {
return this.useCache;
}
public void setUseCache(boolean useCache) {
this.useCache = useCache;
}
public List<ButtonOption> getOptions() {
return this.options;
}
public void setOptions(List<ButtonOption> options) {
this.options = options;
}
public boolean hasCustomRender() {
return false;
}
public boolean isUpdatedMasterButton() {
return this.isMasterButtonUpdated;
}
public void setMasterButtonUpdated(boolean masterButtonUpdated) {
isMasterButtonUpdated = masterButtonUpdated;
}
public boolean isOpenAsync() {
return isOpenAsync;
}
public void setOpenAsync(boolean openAsync) {
isOpenAsync = openAsync;
}
public boolean hasRefreshRequirement() {
return this.refreshRequirement != null;
}
public RefreshRequirement getRefreshRequirement() {
return refreshRequirement;
}
public void setRefreshRequirement(RefreshRequirement refreshRequirement) {
this.refreshRequirement = refreshRequirement;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
/**
* Paginate a given list of elements and set them as elements of this button
* according to the page of the inventory.
*
* @param elements the elements to paginate
* @param inventory the inventory to get the page from
* @param consumer a consumer that will be used to set the elements, it will
* be called with the slot of the element and the element
* itself
* @param <T> the type of the elements
*/
protected <T> void paginate(List<T> elements, InventoryEngine inventory, BiConsumer<Integer, T> consumer) {
Pagination<T> pagination = new Pagination<>();
elements = pagination.paginate(elements, this.slots.size(), inventory.getPage());
for (int i = 0; i != Math.min(elements.size(), this.slots.size()); i++) {
int slot = slots.get(i);
T element = elements.get(i);
consumer.accept(slot, element);
}
}
/**
* Returns whether this button is displayed in the player's inventory.
*
* @return true if this button is displayed in the player's inventory, false otherwise
*/
public boolean isPlayerInventory() {
return this.isInPlayerInventory;
}
/**
* Sets whether this button is displayed in the player's inventory.
*
* @param inPlayerInventory true if this button is displayed in the player's inventory, false otherwise
*/
public void setPlayerInventory(boolean inPlayerInventory) {
isInPlayerInventory = inPlayerInventory;
}
/**
* Returns the button to display in the inventory. This method is called every
* time the inventory is updated.
* The default implementation of this method returns the button itself.
*
* @param inventoryEngine the inventory engine
* @param player the player to display the button for
* @return the button to display
*/
public Button getDisplayButton(InventoryEngine inventoryEngine, Player player) {
return this;
}
/**
* Builds the item stack for the given player, with the owner of the skull
* being the given offline player, and the given placeholders.
*
* @param player the player to build the item stack for
* @param owner the owner of the skull
* @param placeholders the placeholders to use
* @return the built item stack
*/
protected ItemStack buildAsOwner(Player player, OfflinePlayer owner, Placeholders placeholders) {
ItemStack itemStack = getItemStack().build(player, false, placeholders);
SkullMeta skullMeta = (SkullMeta) itemStack.getItemMeta();
skullMeta.setOwningPlayer(owner);
itemStack.setItemMeta(skullMeta);
return itemStack;
}
}