/
InputMenus.java
246 lines (213 loc) · 8.79 KB
/
InputMenus.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
package net.citizensnpcs.api.gui;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.Event.Result;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import com.google.common.collect.Lists;
import net.citizensnpcs.api.gui.InputMenus.Choice.Type;
import net.citizensnpcs.api.util.Messaging;
public class InputMenus {
public static class BooleanSlotHandler implements Consumer<CitizensInventoryClickEvent> {
private final Function<Boolean, String> transformer;
private boolean value;
public BooleanSlotHandler(Function<Boolean, String> transformer) {
this(transformer, false);
}
public BooleanSlotHandler(Function<Boolean, String> transformer, boolean initial) {
this.transformer = transformer;
this.value = initial;
}
@Override
public void accept(CitizensInventoryClickEvent event) {
value = !value;
event.setCurrentItemDescription(transformer.apply(value));
event.setResult(Result.DENY);
}
}
public static class Choice<T> {
private boolean active;
private String description;
private Material material;
private T value;
public ItemStack createDisplayItem() {
ItemStack item = new ItemStack(getDisplayMaterial(), 1);
ItemMeta meta = item.getItemMeta();
if (getDescription().contains("\n")) {
String[] parts = getDescription().split("\n", 2);
meta.setDisplayName(parts[0]);
meta.setLore(Arrays.asList(parts[1].split("\n")));
} else if (getValue() instanceof Enum) {
String name = ((Enum<?>) getValue()).name();
meta.setDisplayName(name.charAt(0) + name.substring(1).toLowerCase());
meta.setLore(Arrays.asList(getDescription().split("\n")));
}
meta.setDisplayName((isActive() ? ChatColor.GREEN : ChatColor.RED) + meta.getDisplayName());
item.setItemMeta(meta);
return item;
}
public String getDescription() {
return Messaging.parseComponents(description);
}
public Material getDisplayMaterial() {
return material;
}
public T getValue() {
return value;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public enum Type {
PICKER,
TOGGLE;
}
public static <T> Choice<T> of(T value, Material display, String description, boolean active) {
Choice<T> ret = new Choice<T>();
ret.active = active;
ret.material = display;
ret.value = value;
ret.description = description;
return ret;
}
}
@Menu(type = InventoryType.CHEST)
private static class ChoiceInputMenu<T> extends InventoryMenuPage {
private final Consumer<List<Choice<T>>> callback;
private final Choice<T>[] choices;
private final String title;
private final Type type;
public ChoiceInputMenu(String title, Choice.Type type, Consumer<List<Choice<T>>> callback,
Choice<T>[] choices) {
this.title = title;
this.callback = callback;
this.choices = choices;
this.type = type;
}
@Override
public Inventory createInventory(String title) {
if (choices.length <= 3) {
return Bukkit.createInventory(null, InventoryType.HOPPER, this.title);
} else {
return Bukkit.createInventory(null, Math.min(54, choices.length / 5 * 9 + 9), this.title);
}
}
@Override
public void initialise(final MenuContext ctx) {
for (int i = 0; i < choices.length; i++) {
final Choice<T> choice = choices[i];
final InventoryMenuSlot slot = ctx.getSlot(i * 2);
slot.setItemStack(choice.createDisplayItem());
slot.setClickHandler((evt) -> {
evt.setCancelled(true);
boolean newState = !choice.isActive();
switch (type) {
case TOGGLE:
choice.setActive(newState);
slot.setItemStack(choice.createDisplayItem());
break;
case PICKER:
for (int j = 0; j < choices.length; j++) {
choices[j].setActive(false);
}
choice.setActive(true);
ctx.getMenu().transitionBack();
break;
}
});
}
}
@Override
public void onClose(HumanEntity entity) {
List<Choice<T>> ret = Lists.newArrayListWithExpectedSize(choices.length);
for (Choice<T> choice : choices) {
if (choice.isActive()) {
ret.add(choice);
}
}
callback.accept(ret);
}
}
@Menu(type = InventoryType.ANVIL)
private static class StringInputMenu extends InventoryMenuPage {
private final Function<String, Boolean> callback;
private MenuContext ctx;
@MenuSlot(slot = { 0, 0 }, material = Material.PAPER, amount = 1)
private InventoryMenuSlot from;
private final Supplier<String> initialValue;
public StringInputMenu(Supplier<String> initialValue, Function<String, Boolean> callback) {
this.initialValue = initialValue;
this.callback = callback;
}
@ClickHandler(slot = { 0, 0 })
public void cancel(InventoryMenuSlot slot, CitizensInventoryClickEvent evt) {
evt.setCancelled(true);
ctx.getMenu().transitionBack();
}
@Override
public void initialise(MenuContext ctx) {
this.ctx = ctx;
ItemStack item = from.getCurrentItem();
ItemMeta meta = item.getItemMeta();
String name = initialValue.get();
meta.setDisplayName(name == null ? "Not set" : name);
item.setItemMeta(meta);
}
@ClickHandler(slot = { 0, 2 })
public void save(InventoryMenuSlot slot, CitizensInventoryClickEvent evt) {
evt.setCancelled(true);
String res = slot.getCurrentItem() != null && slot.getCurrentItem().getItemMeta() != null
? slot.getCurrentItem().getItemMeta().getDisplayName()
: null;
if (res != null && (res.isEmpty() || res.equalsIgnoreCase("Not set") || res.equalsIgnoreCase("null"))) {
res = null;
}
if (callback.apply(res)) {
ctx.getMenu().transitionBack();
}
}
}
public static BooleanSlotHandler clickToggle(Function<Boolean, String> transformer, boolean initialValue) {
return new BooleanSlotHandler(transformer, initialValue);
}
public static InventoryMenuPage filteredStringSetter(Supplier<String> initialValue,
Function<String, Boolean> callback) {
return new StringInputMenu(initialValue, callback);
}
@SuppressWarnings("unchecked")
public static <T> InventoryMenuPage picker(String title, Consumer<Choice<T>> callback,
InputMenus.Choice<T>... choices) {
return new ChoiceInputMenu<T>(title, Choice.Type.PICKER, (chosen) -> {
callback.accept(chosen.size() > 0 ? chosen.get(0) : null);
}, choices);
}
public static InventoryMenuPage stringSetter(Supplier<String> initialValue, Consumer<String> callback) {
return new StringInputMenu(initialValue, (s) -> {
callback.accept(s);
return true;
});
}
@SuppressWarnings("unchecked")
public static <T> InventoryMenuPage toggle(String title, Consumer<List<Choice<T>>> callback,
InputMenus.Choice<T>... choices) {
return new ChoiceInputMenu<T>(title, Choice.Type.TOGGLE, callback, choices);
}
public static BooleanSlotHandler toggler(Consumer<Boolean> consumer, boolean initialValue) {
return new BooleanSlotHandler((b) -> {
consumer.accept(b);
return b ? ChatColor.GREEN + "On" : ChatColor.RED + "Off";
}, initialValue);
}
}