-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathMenuHolder.java
More file actions
319 lines (246 loc) · 8.13 KB
/
Copy pathMenuHolder.java
File metadata and controls
319 lines (246 loc) · 8.13 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
package com.extendedclip.deluxemenus.menu;
import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.utils.StringUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
public class MenuHolder implements InventoryHolder {
private final Player viewer;
private Player placeholderPlayer;
private String menuName;
private Set<MenuItem> activeItems;
private BukkitTask updateTask = null;
private Inventory inventory;
private boolean updating;
private Map<String, String> typedArgs;
public MenuHolder(Player viewer) {
this.viewer = viewer;
}
public MenuHolder(Player viewer, String menuName,
Set<MenuItem> activeItems, Inventory inventory) {
this.viewer = viewer;
this.menuName = menuName;
this.activeItems = activeItems;
this.inventory = inventory;
}
public String getViewerName() {
return viewer.getName();
}
public BukkitTask getUpdateTask() {
return updateTask;
}
public Player getViewer() {
return viewer;
}
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public Set<MenuItem> getActiveItems() {
return activeItems;
}
public void setActiveItems(Set<MenuItem> items) {
this.activeItems = items;
}
public MenuHolder getHolder() {
return this;
}
public MenuItem getItem(int slot) {
for (MenuItem item : activeItems) {
if (item.options().slot() == slot) {
return item;
}
}
return null;
}
public Menu getMenu() {
return Menu.getMenu(menuName);
}
public String setPlaceholders(String string, int slot) {
// Set argument placeholders first
if (this.typedArgs != null && !this.typedArgs.isEmpty()) {
for (Entry<String, String> entry : typedArgs.entrySet()) {
string = string.replace("{" + entry.getKey() + "}", entry.getValue());
}
}
string = string.replace("{slot}", String.valueOf(slot));
// Then set actual PAPI placeholders
if (placeholderPlayer != null) {
return PlaceholderAPI.setPlaceholders((OfflinePlayer) placeholderPlayer, string);
} else {
return this.getViewer() == null ? string
: PlaceholderAPI.setPlaceholders((OfflinePlayer) this.getViewer(), string);
}
}
public String setArguments(String string) {
if (this.typedArgs == null || this.typedArgs.isEmpty()) {
return string;
}
for (Entry<String, String> entry : this.typedArgs.entrySet()) {
string = string.replace("{" + entry.getKey() + "}", entry.getValue());
}
return string;
}
public void refreshMenu() {
Menu menu = getMenu();
if (menu == null || menu.getMenuItems() == null || menu.getMenuItems().size() <= 0) {
return;
}
setUpdating(true);
stopPlaceholderUpdate();
Bukkit.getScheduler().runTaskAsynchronously(DeluxeMenus.getInstance(), () -> {
final Set<MenuItem> active = new HashSet<>();
for (int i = 0; i < getInventory().getSize(); i++) {
TreeMap<Integer, MenuItem> e = menu.getMenuItems().get(i);
if (e == null) {
getInventory().setItem(i, null);
continue;
}
boolean m = false;
for (MenuItem item : e.values()) {
if (item.options().viewRequirements().isPresent()) {
if (item.options().viewRequirements().get().evaluate(this, item.options().slot())) {
m = true;
active.add(item);
break;
}
} else {
m = true;
active.add(item);
break;
}
}
if (!m) {
getInventory().setItem(i, null);
}
}
if (active.isEmpty()) {
Menu.closeMenu(getViewer(), true);
}
Bukkit.getScheduler().runTask(DeluxeMenus.getInstance(), () -> {
boolean update = false;
for (MenuItem item : active) {
ItemStack iStack = item.getItemStack(this);
int slot = item.options().slot();
if (slot >= menu.getSize()) {
continue;
}
if (item.options().updatePlaceholders()) {
update = true;
}
getInventory().setItem(item.options().slot(), iStack);
}
setActiveItems(active);
if (update) {
startUpdatePlaceholdersTask();
}
setUpdating(false);
});
});
}
public void stopPlaceholderUpdate() {
if (updateTask != null) {
try {
updateTask.cancel();
} catch (Exception ignored) {
}
updateTask = null;
}
}
public void startUpdatePlaceholdersTask() {
if (updateTask != null) {
stopPlaceholderUpdate();
}
updateTask = new BukkitRunnable() {
@Override
public void run() {
if (updating) {
return;
}
Set<MenuItem> items = getActiveItems();
if (items == null) {
return;
}
for (MenuItem item : items) {
if (item.options().updatePlaceholders()) {
ItemStack i = inventory.getItem(item.options().slot());
if (i == null) {
continue;
}
int amt = i.getAmount();
if (item.options().dynamicAmount().isPresent()) {
try {
amt = Integer.parseInt(setPlaceholders(item.options().dynamicAmount().get(), item.options().slot()));
if (amt <= 0) {
amt = 1;
}
} catch (Exception exception) {
DeluxeMenus.printStacktrace(
"Something went wrong while updating item in slot " + item.options().slot() +
". Invalid dynamic amount: " + setPlaceholders(item.options().dynamicAmount().get(), item.options().slot()),
exception
);
}
}
ItemMeta meta = i.getItemMeta();
if (item.options().displayNameHasPlaceholders() && item.options().displayName().isPresent()) {
meta.setDisplayName(StringUtils.color(setPlaceholders(item.options().displayName().get(), item.options().slot())));
}
if (item.options().loreHasPlaceholders()) {
List<String> updated = new ArrayList<>();
for (String line : item.options().lore()) {
updated.add(StringUtils
.color(setPlaceholders(line, item.options().slot())));
}
meta.setLore(updated);
}
i.setItemMeta(meta);
i.setAmount(amt);
}
}
}
}.runTaskTimerAsynchronously(DeluxeMenus.getInstance(), 20L,
20L * Menu.getMenu(menuName).getUpdateInterval());
}
public boolean isUpdating() {
return updating;
}
public void setUpdating(boolean updating) {
this.updating = updating;
}
@Override
public Inventory getInventory() {
return this.inventory;
}
public void setInventory(Inventory i) {
this.inventory = i;
}
public Map<String, String> getTypedArgs() {
return typedArgs;
}
public void setTypedArgs(Map<String, String> typedArgs) {
this.typedArgs = typedArgs;
}
public void setPlaceholderPlayer(Player placeholderPlayer) {
this.placeholderPlayer = placeholderPlayer;
}
public Player getPlaceholderPlayer() {
return placeholderPlayer;
}
}