-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
InventoryMenu.java
358 lines (326 loc) · 14.6 KB
/
InventoryMenu.java
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
package net.citizensnpcs.api.gui;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.WeakHashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Queues;
public class InventoryMenu implements Listener {
private PageContext page;
private final Queue<PageContext> stack = Queues.newArrayDeque();
private Collection<InventoryView> views = Lists.newArrayList();
public InventoryMenu(InventoryMenuInfo info) {
transition(info);
}
private InventoryMenuSlot createSlot(int[] dim, MenuSlot slotInfo) {
int pos = posToIndex(dim, slotInfo.value());
InventoryMenuSlot slot = page.ctx.getSlot(pos);
slot.initialise(slotInfo);
return slot;
}
private InventoryMenuTransition createTransition(int[] dim, MenuTransition transitionInfo) {
int pos = posToIndex(dim, transitionInfo.pos());
InventoryMenuSlot slot = page.ctx.getSlot(pos);
InventoryMenuTransition transition = new InventoryMenuTransition(this, slot, transitionInfo.value());
return transition;
}
@EventHandler(ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
if (!event.getInventory().equals(page.ctx.getInventory()))
return;
switch (event.getAction()) {
case COLLECT_TO_CURSOR:
event.setCancelled(true);
case NOTHING:
case UNKNOWN:
case DROP_ONE_CURSOR:
case DROP_ALL_CURSOR:
return;
default:
break;
}
InventoryMenuSlot slot = page.ctx.getSlot(event.getSlot());
page.page.onClick(slot, event);
slot.onClick(event);
for (InventoryMenuTransition transition : page.transitions) {
Class<? extends InventoryMenuPage> next = transition.accept(slot);
if (next != null) {
transition(next);
break;
}
}
}
@EventHandler(ignoreCancelled = true)
public void onInventoryClose(InventoryCloseEvent event) {
if (!event.getInventory().equals(page.ctx.getInventory()))
return;
page.page.onClose(event.getPlayer());
page = stack.poll();
transitionViewersToInventory(page.ctx.getInventory());
}
private int posToIndex(int[] dim, int[] pos) {
return pos[0] * dim[1] + pos[1];
}
public void present(Player player) {
InventoryView view = player.openInventory(page.ctx.getInventory());
views.add(view);
}
public void transition(Class<? extends InventoryMenuPage> clazz) {
if (!CACHED_INFOS.containsKey(clazz)) {
cacheInfo(clazz);
}
transition(CACHED_INFOS.get(clazz));
}
private void transition(InventoryMenuInfo info) {
if (page != null) {
stack.add(page);
}
page = new PageContext();
int[] dim = info.menuAnnotation.dimensions();
int size = dim[0] * dim[1];
Inventory inventory;
if (info.menuAnnotation.type() == InventoryType.CHEST || info.menuAnnotation.type() == null) {
inventory = Bukkit.createInventory(null, size, info.menuAnnotation.title());
} else {
inventory = Bukkit.createInventory(null, info.menuAnnotation.type(), info.menuAnnotation.title());
}
List<InventoryMenuTransition> transitions = Lists.newArrayList();
InventoryMenuSlot[] slots = new InventoryMenuSlot[inventory.getSize()];
page.patterns = new InventoryMenuPattern[info.patterns.length];
try {
page.page = info.constructor.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
page.ctx = new MenuContext(this, slots, inventory);
for (int i = 0; i < info.slots.length; i++) {
Bindable<MenuSlot> slotInfo = info.slots[i];
InventoryMenuSlot slot = createSlot(dim, slotInfo.data);
slotInfo.bind(page.page, slot);
}
for (int i = 0; i < info.transitions.length; i++) {
Bindable<MenuTransition> transitionInfo = info.transitions[i];
InventoryMenuTransition transition = createTransition(dim, transitionInfo.data);
transitionInfo.bind(page.page, transition);
transitions.add(transition);
}
for (int i = 0; i < info.patterns.length; i++) {
Bindable<MenuPatternInfo> patternInfo = info.patterns[i];
Collection<InventoryMenuSlot> patternSlots = Lists.newArrayList();
Collection<InventoryMenuTransition> patternTransitions = Lists.newArrayList();
for (MenuSlot slot : patternInfo.data.slots) {
patternSlots.add(createSlot(dim, slot));
}
for (MenuTransition transition : patternInfo.data.transitions) {
InventoryMenuTransition concreteTransition = createTransition(dim, transition);
patternTransitions.add(concreteTransition);
transitions.add(concreteTransition);
}
InventoryMenuPattern pat = new InventoryMenuPattern(patternInfo.data.info, patternSlots,
patternTransitions);
patternInfo.bind(page.page, pat);
page.patterns[i] = pat;
}
page.transitions = transitions.toArray(new InventoryMenuTransition[transitions.size()]);
transitionViewersToInventory(inventory);
}
private void transitionViewersToInventory(Inventory inventory) {
Collection<InventoryView> old = views;
views = Lists.newArrayListWithExpectedSize(old.size());
for (InventoryView view : old) {
view.close();
if (!view.getPlayer().isValid() || inventory == null)
continue;
views.add(view.getPlayer().openInventory(inventory));
}
}
private static class Bindable<T> {
MethodHandle bind;
T data;
public Bindable(MethodHandle bind, T data) {
this.bind = bind;
this.data = data;
}
public void bind(Object instance, Object value) {
if (bind == null)
return;
try {
bind.invoke(instance, value);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
private static class InventoryMenuInfo {
Constructor<? extends InventoryMenuPage> constructor;
Menu menuAnnotation;
Bindable<MenuPatternInfo>[] patterns;
Bindable<MenuSlot>[] slots;
Bindable<MenuTransition>[] transitions;
public InventoryMenuInfo(Class<?> clazz) {
patterns = getPatternBindables(clazz);
slots = getBindables(clazz, MenuSlot.class, InventoryMenuSlot.class);
transitions = getBindables(clazz, MenuTransition.class, InventoryMenuTransition.class);
}
@SuppressWarnings({ "unchecked" })
private <T extends Annotation> Bindable<T>[] getBindables(Class<?> clazz, Class<T> annotationType,
Class<?> concreteType) {
List<Bindable<T>> bindables = Lists.newArrayList();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
if (field.getAnnotationsByType(MenuPattern.class).length != 0)
continue;
T[] annotations = field.getAnnotationsByType(annotationType);
MethodHandle bind = null;
if (field.getType() == concreteType) {
try {
bind = LOOKUP.unreflectSetter(field);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
for (T t : annotations) {
bindables.add(new Bindable<T>(bind, t));
}
}
List<AccessibleObject> reflect = Lists.newArrayList();
reflect.addAll(Arrays.asList(clazz.getDeclaredConstructors()));
reflect.addAll(Arrays.asList(clazz.getDeclaredMethods()));
for (AccessibleObject object : reflect) {
object.setAccessible(true);
if (object.getAnnotationsByType(MenuPattern.class).length != 0)
continue;
for (T t : object.getAnnotationsByType(annotationType)) {
bindables.add(new Bindable<T>(null, t));
}
}
for (T t : clazz.getAnnotationsByType(annotationType)) {
bindables.add(new Bindable<T>(null, t));
}
return bindables.toArray(new Bindable[bindables.size()]);
}
private Bindable<MenuPatternInfo> getPatternBindable(MethodHandle bind, AccessibleObject object) {
MenuPattern[] annotation = object.getAnnotationsByType(MenuPattern.class);
MenuPattern pattern = annotation[0];
Collection<MenuSlot> slots = Lists.newArrayList();
for (MenuSlot slot : object.getAnnotationsByType(MenuSlot.class)) {
if (pattern.value().contains(Character.toString(slot.pat()))) {
slots.add(slot);
}
}
Collection<MenuTransition> transitions = Lists.newArrayList();
for (MenuTransition transition : object.getAnnotationsByType(MenuTransition.class)) {
if (pattern.value().contains(Character.toString(transition.pat()))) {
transitions.add(transition);
}
}
return new Bindable<MenuPatternInfo>(bind, new MenuPatternInfo(pattern, slots, transitions));
}
private Bindable<MenuPatternInfo> getPatternBindable(MethodHandle bind, Class<?> object) {
MenuPattern[] annotation = object.getAnnotationsByType(MenuPattern.class);
if (annotation.length != 1)
return null;
MenuPattern pattern = annotation[0];
Collection<MenuSlot> slots = Lists.newArrayList();
for (MenuSlot slot : object.getAnnotationsByType(MenuSlot.class)) {
if (pattern.value().contains(Character.toString(slot.pat()))) {
slots.add(slot);
}
}
Collection<MenuTransition> transitions = Lists.newArrayList();
for (MenuTransition transition : object.getAnnotationsByType(MenuTransition.class)) {
if (pattern.value().contains(Character.toString(transition.pat()))) {
transitions.add(transition);
}
}
return new Bindable<MenuPatternInfo>(bind, new MenuPatternInfo(pattern, slots, transitions));
}
@SuppressWarnings({ "unchecked" })
private Bindable<MenuPatternInfo>[] getPatternBindables(Class<?> clazz) {
Collection<Bindable<MenuPatternInfo>> bindables = Lists.newArrayList();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
MethodHandle bind = null;
if (field.getType() == InventoryMenuPattern.class) {
try {
bind = LOOKUP.unreflectSetter(field);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
bindables.add(getPatternBindable(bind, field));
}
List<AccessibleObject> reflect = Lists.newArrayList();
reflect.addAll(Arrays.asList(clazz.getDeclaredConstructors()));
reflect.addAll(Arrays.asList(clazz.getDeclaredMethods()));
for (AccessibleObject object : reflect) {
object.setAccessible(true);
if (object.getAnnotationsByType(MenuPattern.class).length != 0)
continue;
bindables.add(getPatternBindable(null, object));
}
bindables.add(getPatternBindable(null, clazz));
return Collections2.filter(bindables, Predicates.notNull()).toArray(new Bindable[bindables.size()]);
}
private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
}
private static class MenuPatternInfo {
MenuPattern info;
Collection<MenuSlot> slots = Lists.newArrayList();
Collection<MenuTransition> transitions = Lists.newArrayList();
public MenuPatternInfo(MenuPattern info, Collection<MenuSlot> slots, Collection<MenuTransition> transitions) {
this.info = info;
this.slots = slots;
this.transitions = transitions;
}
}
private static class PageContext {
private MenuContext ctx;
private InventoryMenuPage page;
private InventoryMenuPattern[] patterns;
private InventoryMenuTransition[] transitions;
}
private static void cacheInfo(Class<? extends InventoryMenuPage> clazz) {
InventoryMenuInfo info = new InventoryMenuInfo(clazz);
info.menuAnnotation = clazz.getAnnotation(Menu.class);
if (info.menuAnnotation == null) {
throw new IllegalArgumentException("Missing menu annotation");
}
try {
Constructor<? extends InventoryMenuPage> found = clazz.getDeclaredConstructor();
found.setAccessible(true);
info.constructor = found;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
CACHED_INFOS.put(clazz, info);
}
public static InventoryMenu create(Class<? extends InventoryMenuPage> clazz) {
if (!CACHED_INFOS.containsKey(clazz)) {
cacheInfo(clazz);
}
InventoryMenuInfo info = CACHED_INFOS.get(clazz);
return new InventoryMenu(info);
}
private static Map<Class<? extends InventoryMenuPage>, InventoryMenuInfo> CACHED_INFOS = new WeakHashMap<Class<? extends InventoryMenuPage>, InventoryMenuInfo>();
}