-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomManager.java
More file actions
260 lines (216 loc) · 10.7 KB
/
Copy pathCustomManager.java
File metadata and controls
260 lines (216 loc) · 10.7 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
package net.blueva.menu.managers.bedrock;
import dev.dejvokep.boostedyaml.YamlDocument;
import dev.dejvokep.boostedyaml.block.implementation.Section;
import net.blueva.menu.managers.ConditionManager;
import net.blueva.menu.utils.MessagesUtil;
import org.bukkit.entity.Player;
import org.geysermc.cumulus.form.CustomForm;
import org.geysermc.cumulus.response.CustomFormResponse;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
import java.util.*;
public class CustomManager {
public static void openMenu(Player player, YamlDocument menuConfig) {
FloodgatePlayer playerB = FloodgateApi.getInstance().getPlayer(player.getUniqueId());
if (playerB == null) {
return;
}
CustomForm.Builder formBuilder = CustomForm.builder()
.title(MessagesUtil.format(player, menuConfig.getString("menuName", "")));
// Storage to preserve component order and their actions
List<ComponentData> componentsOrder = new ArrayList<>();
// Process components
Section componentsConfig = menuConfig.getSection("components");
if (componentsConfig != null) {
for (Object componentKeyObj : componentsConfig.getKeys()) {
String componentKey = componentKeyObj.toString();
Section component = componentsConfig.getSection(componentKey);
if (component != null) {
String type = component.getString("type");
if (type != null) {
addComponent(formBuilder, component, type, player, componentKey, componentsOrder);
}
}
}
}
formBuilder.closedOrInvalidResultHandler(() ->
ActionManager.executeActions(player, menuConfig.getStringList("close_actions")));
CustomForm form = formBuilder.validResultHandler(response -> {
handleResponse(player, response, componentsOrder);
}).build();
playerB.sendForm(form);
}
private static void addComponent(CustomForm.Builder formBuilder, Section component,
String type, Player player, String componentKey,
List<ComponentData> componentsOrder) {
// Check display conditions before adding the component
boolean shouldDisplay = true;
if (component.contains("display_conditions")) {
Object conditionsObj = component.get("display_conditions");
if (conditionsObj instanceof Map) {
// Map format with all/any/none
@SuppressWarnings("unchecked")
Map<String, Object> conditionsMap = (Map<String, Object>) conditionsObj;
shouldDisplay = ConditionManager.evaluateConditionsMap(player, conditionsMap);
} else if (conditionsObj instanceof List) {
// Simple list format
@SuppressWarnings("unchecked")
List<String> conditionsList = (List<String>) conditionsObj;
shouldDisplay = ConditionManager.evaluateConditions(player, conditionsList);
}
}
// Only add the component if conditions pass
if (!shouldDisplay) {
return;
}
String text = MessagesUtil.format(player, component.getString("text", ""));
switch (type.toUpperCase()) {
case "DROPDOWN":
List<String> dropdownOptions = component.getStringList("options");
if (!dropdownOptions.isEmpty()) {
List<String> formattedOptions = MessagesUtil.format(player, dropdownOptions);
int defaultOption = component.getInt("default", 0);
formBuilder.dropdown(text, formattedOptions, defaultOption);
componentsOrder.add(new ComponentData(componentKey, "DROPDOWN", component.getStringList("actions"), dropdownOptions));
}
break;
case "INPUT":
String placeholder = MessagesUtil.format(player, component.getString("placeholder", ""));
String defaultText = MessagesUtil.format(player, component.getString("default", ""));
formBuilder.input(text, placeholder, defaultText);
componentsOrder.add(new ComponentData(componentKey, "INPUT", component.getStringList("actions"), null));
break;
case "TOGGLE":
boolean defaultValue = component.getBoolean("default", false);
formBuilder.toggle(text, defaultValue);
componentsOrder.add(new ComponentData(componentKey, "TOGGLE", component.getStringList("actions"), null));
break;
case "SLIDER":
float min = component.getDouble("min", 0.0).floatValue();
float max = component.getDouble("max", 10.0).floatValue();
float step = component.getDouble("step", 1.0).floatValue();
float defaultSlider = component.getDouble("default", (double)min).floatValue();
formBuilder.slider(text, min, max, step, defaultSlider);
componentsOrder.add(new ComponentData(componentKey, "SLIDER", component.getStringList("actions"), null));
break;
case "STEPSLIDER":
List<String> steps = component.getStringList("steps");
if (!steps.isEmpty()) {
List<String> formattedSteps = MessagesUtil.format(player, steps);
int defaultStep = component.getInt("default", 0);
formBuilder.stepSlider(text, formattedSteps, defaultStep);
componentsOrder.add(new ComponentData(componentKey, "STEPSLIDER", component.getStringList("actions"), steps));
}
break;
case "LABEL":
formBuilder.label(text);
componentsOrder.add(new ComponentData(componentKey, "LABEL", new ArrayList<>(), null));
break;
default:
break;
}
}
private static void handleResponse(Player player, CustomFormResponse response, List<ComponentData> componentsOrder) {
response.reset();
// Map to store all component values
Map<String, String> componentValues = new HashMap<>();
// First pass: collect all values
for (ComponentData componentData : componentsOrder) {
if (!response.hasNext()) {
break;
}
String value = null;
switch (componentData.getType()) {
case "DROPDOWN":
int dropdownIndex = response.asDropdown();
value = String.valueOf(dropdownIndex);
// Also store the selected option text if available
if (componentData.getOptions() != null && dropdownIndex < componentData.getOptions().size()) {
componentValues.put(componentData.getKey() + "_text", componentData.getOptions().get(dropdownIndex));
}
break;
case "INPUT":
value = response.asInput();
if (value == null) value = "";
break;
case "TOGGLE":
boolean toggleValue = response.asToggle();
value = String.valueOf(toggleValue);
break;
case "SLIDER":
float sliderValue = response.asSlider();
// If the value is an integer, use int formatting to avoid ".0"
value = (sliderValue == (int) sliderValue)
? String.valueOf((int) sliderValue)
: String.valueOf(sliderValue);
break;
case "STEPSLIDER":
int stepSliderIndex = response.asStepSlider();
value = String.valueOf(stepSliderIndex);
// Also store the selected step text if available
if (componentData.getOptions() != null && stepSliderIndex < componentData.getOptions().size()) {
componentValues.put(componentData.getKey() + "_text", componentData.getOptions().get(stepSliderIndex));
}
break;
case "LABEL":
response.skip(); // Labels have no value
continue;
default:
response.skip();
continue;
}
if (value != null) {
componentValues.put(componentData.getKey(), value);
}
}
// Second pass: run actions with all available values
for (ComponentData componentData : componentsOrder) {
List<String> actions = componentData.getActions();
if (actions != null && !actions.isEmpty()) {
executeActionsWithValues(player, actions, componentValues);
}
}
}
private static void executeActionsWithValues(Player player, List<String> actions, Map<String, String> componentValues) {
if (actions == null || actions.isEmpty()) {
return;
}
// Process each action by replacing ONLY component placeholders
// Let ActionManager process the rest (formatting, standard placeholders, etc.)
List<String> processedActions = new ArrayList<>();
for (String action : actions) {
String processedAction = action;
// Replace only {component_key} placeholders with their values
for (Map.Entry<String, String> entry : componentValues.entrySet()) {
processedAction = processedAction.replace("{" + entry.getKey() + "}", entry.getValue());
}
processedActions.add(processedAction);
}
ActionManager.executeActions(player, processedActions);
}
// Inner class for storing component data
private static class ComponentData {
private final String key;
private final String type;
private final List<String> actions;
private final List<String> options; // For dropdown and stepslider
public ComponentData(String key, String type, List<String> actions, List<String> options) {
this.key = key;
this.type = type;
this.actions = actions;
this.options = options;
}
public String getKey() {
return key;
}
public String getType() {
return type;
}
public List<String> getActions() {
return actions;
}
public List<String> getOptions() {
return options;
}
}
}