-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DetermineCommand.java
156 lines (121 loc) · 4.42 KB
/
DetermineCommand.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
package net.aufdemrand.denizencore.scripts.commands.core;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class DetermineCommand extends AbstractCommand {
//
// Static helpers
//
// Default 'DETERMINE_NONE' value.
public static String DETERMINE_NONE = "none";
// Map for keeping track of cache
// Key: ID, Value: outcome
private static Map<Long, dList> cache = new ConcurrentHashMap<Long, dList>(8, 0.9f, 1);
// Start at 0
public static long uniqueId = 0;
/**
* Increment the counter and return it, thus returning
* a unique id. Determinations are very short lived.
*
* @return long ID
*/
public static long getNewId() {
// Just in case? Start over if already max_value.
if (uniqueId == Long.MAX_VALUE) {
uniqueId = 0;
}
// Increment the counter
return uniqueId++;
}
/**
* Checks the cache for existence of an outcome.
*
* @param id the outcome id to check
* @return if the cache has the outcome
*/
public static boolean hasOutcome(long id) {
return cache.containsKey(id) && !cache.get(id).isEmpty();
}
/**
* Gets the outcome, and removes it from the cache.
*
* @param id the outcome id to check
* @return the outcome
*/
public static dList getOutcome(long id) {
dList outcome = cache.get(id);
cache.remove(id);
return outcome;
}
/**
* Gets the current value of the outcome.
* Note: The value of the outcome may change.
*
* @param id the outcome id to check
* @return the current value of the outcome
*/
public static String readOutcome(long id) {
return cache.get(id).isEmpty() ? DETERMINE_NONE : cache.get(id).get(0);
}
//
// Command Singleton
//
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
//
// Parse the arguments
//
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matches("passive", "passively")) {
scriptEntry.addObject("passively", new Element(true));
}
else if (!scriptEntry.hasObject("outcome")) {
scriptEntry.addObject("outcome", arg.hasPrefix() ? new Element(arg.raw_value) : arg.object);
}
else {
arg.reportUnhandled();
}
}
//
// Set defaults
//
scriptEntry.defaultObject("passively", new Element(false));
scriptEntry.defaultObject("outcome", new Element(DETERMINE_NONE));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dObject outcomeObj = scriptEntry.getdObject("outcome");
// Report!
dB.report(scriptEntry, getName(), outcomeObj.debug()
+ scriptEntry.getElement("passively").debug());
// Fetch the ScriptEntry elements
Boolean passively = scriptEntry.getElement("passively").asBoolean();
Long uniqueId = (Long) scriptEntry.getObject("reqid");
// Useful for debug
if (uniqueId == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot use determine in this queue!");
return;
}
// Store the outcome in the cache
dList strs = cache.get(uniqueId);
if (strs == null) {
strs = new dList();
cache.put(uniqueId, strs);
}
strs.addObject(outcomeObj);
if (!passively) {
// Stop the queue by clearing the remainder of it.
scriptEntry.getResidingQueue().clear();
}
}
}