-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathActionButtonData.java
More file actions
200 lines (158 loc) · 6.52 KB
/
ActionButtonData.java
File metadata and controls
200 lines (158 loc) · 6.52 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
package xyz.imcodist.quickmenu.data;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.InputUtil;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.CustomModelDataComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import xyz.imcodist.quickmenu.QuickMenu;
import xyz.imcodist.quickmenu.data.command_actions.BaseActionData;
import xyz.imcodist.quickmenu.data.command_actions.CommandActionData;
import xyz.imcodist.quickmenu.data.command_actions.DelayActionData;
import xyz.imcodist.quickmenu.data.command_actions.KeybindActionData;
import xyz.imcodist.quickmenu.other.ActionButtonDelayHandler;
import xyz.imcodist.quickmenu.other.ModConfigModel;
import java.util.ArrayList;
import java.util.List;
public class ActionButtonData {
public String name;
public ArrayList<BaseActionData> actions = new ArrayList<>();
public ItemStack icon;
public ArrayList<Integer> keybind = new ArrayList<>();
public boolean keyPressed = false;
public ActionButtonDataJSON toJSON() {
ActionButtonDataJSON jsonData = new ActionButtonDataJSON();
jsonData.name = name;
jsonData.actions = new ArrayList<>();
jsonData.keybind = keybind;
actions.forEach((action) -> {
ArrayList<String> actionArray = new ArrayList<>();
actionArray.add(action.getJsonType());
actionArray.add(action.getJsonValue());
jsonData.actions.add(actionArray);
});
if (icon != null) {
if (icon.getRegistryEntry().getKey().isPresent()) {
jsonData.icon = icon.getRegistryEntry().getKey().get().getValue().toString();
}
String cmdString = "";
CustomModelDataComponent customModelDataComponent = icon.getOrDefault(DataComponentTypes.CUSTOM_MODEL_DATA, CustomModelDataComponent.DEFAULT);
if (!customModelDataComponent.strings().isEmpty()) cmdString = customModelDataComponent.strings().getFirst();
jsonData.customModelData = cmdString;
}
return jsonData;
}
public static ActionButtonData fromJSON(ActionButtonDataJSON json) {
ActionButtonData data = new ActionButtonData();
data.name = json.name;
data.actions = new ArrayList<>();
data.keybind = json.keybind;
json.actions.forEach((actionArray) -> {
BaseActionData actionData = getActionDataType(actionArray.get(0), actionArray.get(1));
data.actions.add(actionData);
});
if (json.icon != null) {
data.icon = new ItemStack(Registries.ITEM.get(Identifier.of(json.icon)));
CustomModelDataValues customModelDataValues = new CustomModelDataValues(json.customModelData);
data.icon.set(DataComponentTypes.CUSTOM_MODEL_DATA, customModelDataValues.getComponent());
}
return data;
}
private static BaseActionData getActionDataType(String type, String value) {
switch (type) {
case "base" -> {
return new BaseActionData();
}
case "cmd" -> {
CommandActionData commandActionData = new CommandActionData();
commandActionData.command = value;
return commandActionData;
}
case "key" -> {
KeybindActionData keybindActionData = new KeybindActionData();
keybindActionData.keybindTranslationKey = value;
return keybindActionData;
}
case "delay" -> {
var delayAction = new DelayActionData();
delayAction.ticks = Math.max(0, Long.parseLong(value));
return delayAction;
}
}
return null;
}
public InputUtil.Key getKey() {
if (keybind.size() < 4) return null;
return InputUtil.fromKeyCode(keybind.get(0), keybind.get(1));
}
public void run() {
run(false);
}
public void run(boolean isKeybind) {
// Show run message.
ModConfigModel.DisplayRunText displayRunText = QuickMenu.CONFIG.displayRunText();
if (displayRunText == ModConfigModel.DisplayRunText.ALWAYS || displayRunText == ModConfigModel.DisplayRunText.KEYBIND_ONLY && isKeybind) {
MinecraftClient client = MinecraftClient.getInstance();
if (client != null && client.player != null) {
client.player.sendMessage(Text.of("Ran action \"" + name + "\""), true);
}
}
// MinecraftClient.getInstance()
// Run the buttons action.
// actions.forEach(BaseActionData::run);
new ActionButtonDataContext(new ArrayList<>(actions), 0).run();
}
public static class ActionButtonDataContext {
private final List<BaseActionData> actions;
private long delay;
public ActionButtonDataContext(
List<BaseActionData> actions,
long delay
) {
this.actions = actions;
this.delay = delay;
}
public void run() {
for (var idx = 0; idx < actions.size(); idx++) {
var action = actions.get(idx);
var nextDelay = action.run();
if (nextDelay > 0) {
// TODO : Envelope the remaining actions in a timer.
var nextSet = new ActionButtonDataContext(actions.subList(idx + 1, actions.size()), nextDelay);
// TODO - Create a timer construct that listens
ActionButtonDelayHandler.INSTANCE.add(nextSet);
return;
}
}
}
public long getDelay() {
return delay;
}
public void decrementDelay()
{
delay = Math.max(0, delay - 1);
}
public boolean isReady()
{
return delay == 0;
}
}
public static class CustomModelDataValues {
public List<String> stringList;
public List<Float> floatList = List.of();
public CustomModelDataValues(String cmdStr) {
stringList = List.of(cmdStr);
try{
floatList = List.of(Float.parseFloat(cmdStr));
} catch (Exception ignored) {}
}
public CustomModelDataComponent getComponent() {
return new CustomModelDataComponent(
this.floatList, List.of(),
this.stringList, List.of()
);
}
}
}