/
FlagCommand.java
207 lines (177 loc) · 8.35 KB
/
FlagCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.flags.FlagManager.Flag;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.event.Listener;
/**
* Sets a Player 'Flag'. Flags can hold information to check against
* with the FLAGGED requirement.
*
* @author Jeremy Schroeder
*/
public class FlagCommand extends AbstractCommand implements Listener {
public enum Action { SET_VALUE, SET_BOOLEAN, INCREASE, DECREASE, MULTIPLY, DIVIDE, INSERT, REMOVE, SPLIT, DELETE }
public enum Type { GLOBAL, NPC, PLAYER }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Set some defaults with information from the scriptEntry
String name = null;
String value = null;
Duration duration = new Duration(-1d);
Action action = Action.SET_VALUE;
Type type = Type.PLAYER;
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesDuration(arg))
duration = aH.getDurationFrom(arg);
else if (aH.matchesArg("GLOBAL, NPC, DENIZEN, GLOBAL", arg))
type = Type.valueOf(arg.toUpperCase().replace("DENIZEN", "NPC"));
// Determine flagAction and set the flagName/flagValue
else if (arg.split(":", 3).length > 1) {
String[] flagArgs = arg.split(":", 3);
name = flagArgs[0].toUpperCase();
if (flagArgs.length == 2) {
if (flagArgs[1].equals("+")
|| flagArgs[1].equals("++")) {
action = Action.INCREASE;
value = "1";
} else if (flagArgs[1].equals("-")
|| flagArgs[1].equals("--")) {
// Using equals instead of startsWith because
// people need to be able to set values like "-2"
action = Action.DECREASE;
value = "1";
} else if (flagArgs[1].startsWith("!")) {
action = Action.DELETE;
} else if (flagArgs[1].startsWith("<-")) {
action = Action.REMOVE;
} else {
action = Action.SET_VALUE;
value = arg.split(":")[1];
}
} else if (flagArgs.length == 3) {
if (flagArgs[1].startsWith("->")) action = Action.INSERT;
else if (flagArgs[1].startsWith("<-")) action = Action.REMOVE;
else if (flagArgs[1].startsWith("|")) action = Action.SPLIT;
else if (flagArgs[1].startsWith("+")) action = Action.INCREASE;
else if (flagArgs[1].startsWith("-")) action = Action.DECREASE;
else if (flagArgs[1].startsWith("*")) action = Action.MULTIPLY;
else if (flagArgs[1].startsWith("/")) action = Action.DIVIDE;
else action = Action.SET_VALUE;
if (action == Action.SET_VALUE)
value = flagArgs[1] + ":" + flagArgs[2];
else value = flagArgs[2];
}
} else {
name = arg.toUpperCase();
action = Action.SET_BOOLEAN;
}
}
String player = null;
if (type == Type.PLAYER) {
if (player == null && scriptEntry.getPlayer() != null)
player = scriptEntry.getPlayer().getName();
}
// Check required arguments
if (name == null)
throw new InvalidArgumentsException("Must specify a FLAG name.");
if (type == Type.NPC && scriptEntry.getNPC() == null)
throw new InvalidArgumentsException("Specified NPC-type flag with no valid NPC reference.");
if (type == Type.PLAYER && player == null)
throw new InvalidArgumentsException("Specified PLAYER-type flag with no valid Player reference.");
// Save objects to ScriptEntry for use with execute()
scriptEntry.addObject("name", name);
scriptEntry.addObject("value", value);
scriptEntry.addObject("duration", duration);
scriptEntry.addObject("action", action);
scriptEntry.addObject("type", type);
scriptEntry.addObject("player", player);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
String name = (String) scriptEntry.getObject("name");
String value = (String) scriptEntry.getObject("value");
Duration duration = (Duration) scriptEntry.getObject("duration");
Action action = (Action) scriptEntry.getObject("action");
Type type = (Type) scriptEntry.getObject("type");
int index = -1;
// Set working index, if specified.
// Usage example: - FLAG FLAGNAME[3]:VALUE specifies an index of 3 should be set with VALUE.
if (name.contains("[")) {
try {
index = Integer.valueOf(name.split("\\[")[1].replace("]", ""));
} catch (Exception e) { index = -1; }
name = name.split("\\[")[0];
}
Flag flag = null;
String player = (String) scriptEntry.getObject("player");
// Send information to debugger
dB.report(getName(),
aH.debugObj("Name", name)
+ (index > 0 ? aH.debugObj("Index", String.valueOf(index)) : "")
+ aH.debugObj("Type", type.toString())
+ aH.debugUniqueObj("Action/Value", action.toString(), (value != null ? value : "null"))
+ (duration.getSeconds() > 0 ? duration.debug() : "")
+ (type == Type.NPC ? aH.debugObj("NPC", scriptEntry.getNPC().toString()) : "")
+ (type == Type.PLAYER ? aH.debugObj("Player", player) : ""));
// Returns existing flag (if existing), or a new flag if not
switch (type) {
case NPC:
flag = denizen.flagManager().getNPCFlag(scriptEntry.getNPC().getId(), name);
break;
case PLAYER:
flag = denizen.flagManager().getPlayerFlag(player, name);
break;
case GLOBAL:
flag = denizen.flagManager().getGlobalFlag(name);
break;
}
// Do flagAction
switch (action) {
case INCREASE: case DECREASE: case MULTIPLY: case DIVIDE:
double currentValue = flag.get(index).asDouble();
flag.set(Double.toString(math(currentValue, Double.valueOf(value), action)), index);
break;
case SET_BOOLEAN:
flag.set("true", index);
break;
case SET_VALUE:
flag.set(value, index);
break;
case INSERT:
flag.add(value);
break;
case REMOVE:
flag.remove(value, index);
break;
case SPLIT:
flag.split(value);
break;
case DELETE:
flag.clear();
}
// Set flag duration
if (duration.getSeconds() > 0)
flag.setExpiration(System.currentTimeMillis()
+ Double.valueOf(duration.getSeconds() * 1000).longValue());
}
private double math(double currentValue, double value, Action flagAction) {
switch (flagAction) {
case INCREASE:
return currentValue + value;
case DECREASE:
return currentValue - value;
case MULTIPLY:
return currentValue * value;
case DIVIDE:
return currentValue / value;
default:
break;
}
return 0;
}
}