/
AdjustCommand.java
97 lines (70 loc) · 3.37 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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
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.asElement());
}
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());
scriptEntry.addObject("mechanism_value", new Element(""));
}
}
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!");
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element mechanism = scriptEntry.getElement("mechanism");
Element value = scriptEntry.getElement("mechanism_value");
dB.report(scriptEntry, getName(),
scriptEntry.getElement("object").debug()
+ mechanism.debug()
+ value.debug());
dList objects = dList.valueOf(scriptEntry.getElement("object").asString());
dList result = new dList();
for (String object: objects) {
Class object_class = ObjectFetcher.getObjectClass(object.split("@")[0]);
if (object_class == null) {
dB.echoError("Unfetchable object found '" + object + "'!");
return;
}
dObject fetched;
// Check to make sure this is a valid constructor by checking the 'matches' static method
if (!ObjectFetcher.checkMatch(object_class, object))
throw new CommandExecutionException('\'' + object + "' is returning null.");
// Get the object with the 'valueOf' static method
fetched = ObjectFetcher.getObjectFrom(object_class, object);
// Make sure this object is Adjustable
if (!(fetched instanceof Adjustable)) {
dB.echoError("'" + object + "' is not adjustable.");
return;
}
// Do the adjustment!
((Adjustable) fetched).adjust(new Mechanism(mechanism, value));
// Add it to the entry for later access
if (objects.size() == 1)
scriptEntry.addObject("result", fetched);
result.add(fetched.identify());
// :)
}
scriptEntry.addObject("result_list", result);
}
}