-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
AdjustCommand.java
102 lines (83 loc) · 3.72 KB
/
AdjustCommand.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 net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.tags.core.ServerTags;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.tags.core.UtilTags;
public class AdjustCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("object")) {
scriptEntry.addObject("object", arg.asType(dList.class));
}
else if (!scriptEntry.hasObject("mechanism")) {
if (arg.hasPrefix()) {
scriptEntry.addObject("mechanism", new Element(arg.getPrefix().getValue()));
scriptEntry.addObject("mechanism_value", arg.asElement());
}
else {
scriptEntry.addObject("mechanism", arg.asElement());
}
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("object")) {
throw new InvalidArgumentsException("You must specify an object!");
}
if (!scriptEntry.hasObject("mechanism")) {
throw new InvalidArgumentsException("You must specify a mechanism!");
}
}
public dObject adjust(dObject object, Element mechanism, Element value, ScriptEntry entry) {
String objectString = object.toString();
if (objectString.equalsIgnoreCase("server")) {
ServerTags.adjustServer(new Mechanism(mechanism, value));
return object;
}
else if (objectString.equalsIgnoreCase("system")) {
UtilTags.adjustSystem(new Mechanism(mechanism, value));
return object;
}
if (object instanceof Element) {
object = ObjectFetcher.pickObjectFor(objectString, entry.entryData.getTagContext());
if (object instanceof Element) {
dB.echoError("Unable to determine what object to adjust (missing object notation?), for: " + objectString);
return object;
}
}
// Make sure this object is Adjustable
if (!(object instanceof Adjustable)) {
dB.echoError("'" + objectString + "' is not an adjustable object type.");
return object;
}
((Adjustable) object).safeAdjust(new Mechanism(mechanism, value, entry.entryData.getTagContext()));
return object;
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element mechanism = scriptEntry.getElement("mechanism");
Element value = scriptEntry.getElement("mechanism_value");
dList objects = scriptEntry.getdObject("object");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
objects.debug()
+ mechanism.debug()
+ (value == null ? "" : value.debug()));
}
dList result = new dList();
for (dObject object : objects.objectForms) {
object = adjust(object, mechanism, value, scriptEntry);
if (objects.size() == 1) {
scriptEntry.addObject("result", object);
}
result.addObject(object);
}
scriptEntry.addObject("result_list", result);
}
}