-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagedMenuBuilder.java
More file actions
250 lines (227 loc) · 7.81 KB
/
PagedMenuBuilder.java
File metadata and controls
250 lines (227 loc) · 7.81 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
package net.staticstudios.menus.menu;
import net.kyori.adventure.text.Component;
import net.staticstudios.menus.StaticMenus;
import net.staticstudios.menus.action.ViewerAction;
import net.staticstudios.menus.button.Button;
import net.staticstudios.menus.options.MenuOptions;
import net.staticstudios.menus.viewer.MenuViewer;
import java.util.*;
import java.util.function.Function;
public class PagedMenuBuilder implements Cloneable, MenuBuilder {
private final boolean mutable;
private final Map<Menu.Action, List<ViewerAction>> actions = new HashMap<>();
private final Map<Character, Button> buttonMappings = new HashMap<>();
private final List<Button> buttons = new ArrayList<>();
private final String template;
private String id;
private Component title;
private MenuOptions options = new MenuOptions();
private boolean shrinkToFit = false;
private char marker;
private Button fallback = Button.EMPTY;
protected PagedMenuBuilder(boolean mutable, String template) {
this.mutable = mutable;
this.template = template.replace("\n", "").replace("\r", "").replace("\t", "");
int size = this.template.length();
if (size < 9 || size > 54 || size % 9 != 0) {
throw new IllegalArgumentException("Size must be between 9 and 54 and divisible by 9");
}
}
/**
* Set the ID of the menu.
* This is used to track history and for other internal purposes.
*
* @param id The ID
* @return The builder
*/
public PagedMenuBuilder id(String id) {
PagedMenuBuilder builder = clone();
builder.id = id;
return builder;
}
/**
* Set the title of the menu.
*
* @param title The title
* @return The builder
*/
public PagedMenuBuilder title(Component title) {
PagedMenuBuilder builder = clone();
builder.title = title;
return builder;
}
/**
* Set the title of the menu.
*
* @param title The title
* @return The builder
*/
public PagedMenuBuilder title(String title) {
PagedMenuBuilder builder = clone();
builder.title = StaticMenus.getMiniMessage().deserialize("<black>" + title);
return builder;
}
/**
* Add an action to run when the menu is opened.
*
* @param action The action
* @return The builder
*/
public PagedMenuBuilder onOpen(ViewerAction action) {
PagedMenuBuilder builder = clone();
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.OPEN, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.OPEN, actions);
return builder;
}
/**
* Add an action to run when the menu is closed.
*
* @param action The action
* @return The builder
*/
public PagedMenuBuilder onClose(ViewerAction action) {
PagedMenuBuilder builder = clone();
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.CLOSE, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.CLOSE, actions);
return builder;
}
/**
* Add an action to run when the next page is opened.
*
* @param action The action
* @return The builder
*/
public PagedMenuBuilder onNextPage(ViewerAction action) {
PagedMenuBuilder builder = clone();
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.NEXT_PAGE, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.NEXT_PAGE, actions);
return builder;
}
/**
* Add an action to run when the previous page is opened.
*
* @param action The action
* @return The builder
*/
public PagedMenuBuilder onPreviousPage(ViewerAction action) {
PagedMenuBuilder builder = clone();
List<ViewerAction> actions = new ArrayList<>(builder.actions.getOrDefault(Menu.Action.PREVIOUS_PAGE, new ArrayList<>()));
actions.add(action);
builder.actions.put(Menu.Action.PREVIOUS_PAGE, actions);
return builder;
}
/**
* Set the options for the menu.
*
* @param optionsEditor A function to edit the options
* @return The builder
*/
public PagedMenuBuilder options(Function<MenuOptions, MenuOptions> optionsEditor) {
PagedMenuBuilder builder = clone();
builder.options = optionsEditor.apply(builder.options);
return builder;
}
/**
* Set the default placeholder button.
*
* @param defaultPlaceholder The default placeholder button
* @return The builder
*/
public PagedMenuBuilder defaultPlaceholder(Button defaultPlaceholder) {
PagedMenuBuilder builder = clone();
builder.options = builder.options.defaultPlaceholder(defaultPlaceholder);
return builder;
}
/**
* Add a button mapping.
*
* @param character The character to map
* @param button The button to map to
* @return The builder
*/
public PagedMenuBuilder button(char character, Button button) {
if (character == 'P' || character == 'N') {
throw new IllegalArgumentException("P and N are reserved characters");
}
PagedMenuBuilder builder = clone();
builder.buttonMappings.put(character, button);
return builder;
}
/**
* Set the buttons for the menu.
*
* @param buttons The buttons
* @return The builder
*/
public PagedMenuBuilder buttons(List<Button> buttons) {
PagedMenuBuilder builder = clone();
builder.buttons.addAll(buttons);
return builder;
}
/**
* Set the buttons for the menu.
*
* @param buttons The buttons
* @return The builder
*/
public PagedMenuBuilder buttons(Button... buttons) {
PagedMenuBuilder builder = clone();
builder.buttons.addAll(Arrays.asList(buttons));
return builder;
}
/**
* Set whether the menu should shrink to fit the buttons.
*
* @param shrinkToFit Whether the menu should shrink to fit the buttons
* @return The builder
*/
public PagedMenuBuilder shrinkToFit(boolean shrinkToFit) {
PagedMenuBuilder builder = clone();
builder.shrinkToFit = shrinkToFit;
return builder;
}
/**
* Sets the marker character
*
* @param marker The character to use as a marker
* @return The builder
*/
public PagedMenuBuilder marker(char marker) {
PagedMenuBuilder builder = clone();
builder.marker = marker;
return builder;
}
/**
* Sets the marker and the empty marker
*
* @param marker The character to use as a marker
* @param fallback The button to use as a fallback in place of a marker if there are no more buttons to show, but there are still placeholders
* @return The builder
*/
public PagedMenuBuilder marker(char marker, Button fallback) {
PagedMenuBuilder builder = clone();
builder.marker = marker;
builder.fallback = fallback;
return builder;
}
@Override
public PagedMenu build(MenuViewer viewer) {
if (id == null) throw new IllegalStateException("ID must be set");
if (title == null) throw new IllegalStateException("Title must be set");
if (marker == 0) throw new IllegalStateException("Placeholder must be set");
return new PagedMenu(id, viewer, title, actions, template, marker, fallback, shrinkToFit, buttons, buttonMappings, options);
}
public PagedMenuBuilder clone() {
if (mutable) {
return this;
}
try {
return (PagedMenuBuilder) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}