-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DataActionHelper.java
102 lines (96 loc) · 3.9 KB
/
DataActionHelper.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
package com.denizenscript.denizencore.utilities.data;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import java.util.List;
public class DataActionHelper {
public static DataAction parse(ActionableDataProvider provider, Argument arg, TagContext context) {
if ((arg.object instanceof ElementTag && ((ElementTag) arg.object).isRawInput) || !arg.hasPrefix()) {
return parse(provider, arg.getRawValue(), context);
}
DataAction action = new DataAction();
action.provider = provider;
parseKey(action, arg.getPrefix().getValue());
action.type = DataActionType.SET;
action.inputValue = arg.object;
return action;
}
public static void parseKey(DataAction action, String key) {
action.key = key;
int bracketIndex = action.key.indexOf('[');
if (bracketIndex >= 0) {
String index = action.key.substring(bracketIndex + 1, action.key.lastIndexOf(']'));
if (ArgumentHelper.matchesInteger(index)) {
action.key = action.key.substring(0, bracketIndex);
action.index = Integer.parseInt(index);
}
}
}
public static DataAction parse(ActionableDataProvider provider, String actionArgument, TagContext context) {
DataAction toReturn = new DataAction();
toReturn.provider = provider;
List<String> split = CoreUtilities.split(actionArgument, ':', 3);
parseKey(toReturn, split.get(0));
if (split.size() == 1) {
toReturn.type = DataActionType.AUTO_SET;
return toReturn;
}
String action = split.get(1);
if (split.size() == 2) {
switch (action) {
case "++":
toReturn.type = DataActionType.INCREMENT;
break;
case "--":
toReturn.type = DataActionType.DECREMENT;
break;
case "!":
toReturn.type = DataActionType.CLEAR;
break;
case "<-":
toReturn.type = DataActionType.REMOVE;
break;
default:
toReturn.type = DataActionType.SET;
toReturn.inputValue = ObjectFetcher.pickObjectFor(action, context);
break;
}
return toReturn;
}
toReturn.inputValue = ObjectFetcher.pickObjectFor(split.get(2), context);
switch (action) {
case "->":
toReturn.type = DataActionType.INSERT;
break;
case "<-":
toReturn.type = DataActionType.REMOVE;
break;
case "|":
toReturn.type = DataActionType.SPLIT;
break;
case "!|":
toReturn.type = DataActionType.SPLIT_NEW;
break;
case "+":
toReturn.type = DataActionType.ADD;
break;
case "-":
toReturn.type = DataActionType.SUBTRACT;
break;
case "*":
toReturn.type = DataActionType.MULTIPLY;
break;
case "/":
toReturn.type = DataActionType.DIVIDE;
break;
default:
toReturn.type = DataActionType.SET;
toReturn.inputValue = ObjectFetcher.pickObjectFor(split.get(1) + ":" + split.get(2), context);
break;
}
return toReturn;
}
}