/
AdjustCommand.java
176 lines (153 loc) · 6.77 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
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
package net.aufdemrand.denizencore.scripts.commands.core;
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;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.util.HashMap;
import java.util.function.Consumer;
public class AdjustCommand extends AbstractCommand {
// <--[command]
// @Name Adjust
// @Syntax adjust [<dObject>/def:<name>|...] [<mechanism>](:<value>)
// @Required 2
// @Short Adjusts a dObjects mechanism.
// @Group core
// @Video /denizen/vids/Properties%20and%20Mechanisms
//
// @Description
// Many dObjects contains options and properties that need to be adjusted. Denizen employs a mechanism
// interface to deal with those adjustments. To easily accomplish this, use this command with a valid object
// mechanism, and sometimes accompanying value.
//
// Specify "def:<name>" as an input to adjust a definition and automatically save the result back to the definition.
//
// To adjust an item in an inventory, use <@link command inventory>, as '- inventory adjust slot:<#> <mechanism>:<value>'.
//
// @Tags
// <entry[saveName].result> returns the adjusted object.
// <entry[saveName].result_list> returns a dList of adjusted objects.
//
// @Usage
// Use to set a custom display name on an entity.
// - adjust e@1000 'custom_name:ANGRY!'
//
// @Usage
// Use to set the skin of every online player.
// - adjust <server.list_online_players> skin:mcmonkey4eva
//
// @Usage
// Use to modify an item held in a definition.
// - adjust def:stick "display_name:Fancy stick"
//
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("object")) {
if (arg.object instanceof dList) {
scriptEntry.addObject("object", arg.object);
}
else if (arg.object instanceof Element) {
// Special parse to avoid prefixing issues
scriptEntry.addObject("object", dList.valueOf(arg.raw_value));
}
else {
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 static HashMap<String, Consumer<Mechanism>> specialAdjustables = new HashMap<>();
static {
specialAdjustables.put("system", UtilTags::adjustSystem);
}
public dObject adjust(dObject object, Element mechanismName, Element value, ScriptEntry entry) {
Mechanism mechanism = new Mechanism(mechanismName, value, entry.entryData.getTagContext());
return adjust(object, mechanism, entry);
}
public dObject adjust(dObject object, Mechanism mechanism, ScriptEntry entry) {
String objectString = object.toString();
String lowerObjectString = CoreUtilities.toLowerCase(objectString);
Consumer<Mechanism> specialAdjustable = specialAdjustables.get(lowerObjectString);
if (specialAdjustable != null) {
specialAdjustable.accept(mechanism);
return object;
}
if (lowerObjectString.startsWith("def:")) {
String defName = lowerObjectString.substring("def:".length());
dObject def = entry.getResidingQueue().getDefinitionObject(defName);
if (def == null) {
dB.echoError("Invalid definition name '" + defName + "', cannot adjust");
return object;
}
def = adjust(def, mechanism, entry);
entry.getResidingQueue().addDefinition(defName, def);
return def;
}
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;
}
}
if (object instanceof dList) {
dList subList = (dList) object;
dList result = new dList();
for (dObject listObject : subList.objectForms) {
listObject = adjust(listObject, mechanism, entry);
result.addObject(listObject);
}
return result;
}
// Make sure this object is Adjustable
if (!(object instanceof Adjustable)) {
dB.echoError("'" + objectString + "' is not an adjustable object type.");
return object;
}
((Adjustable) object).safeAdjust(mechanism);
return object;
}
@Override
public void execute(ScriptEntry scriptEntry) {
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);
}
}