-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
AdjustCommand.java
215 lines (203 loc) · 9.38 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
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
208
209
210
211
212
213
214
215
package com.denizenscript.denizencore.scripts.commands.core;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.tags.core.UtilTagBase;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
public class AdjustCommand extends AbstractCommand {
public AdjustCommand() {
setName("adjust");
setSyntax("adjust [<ObjectTag>/def:<name>|...] [<mechanism>](:<value>)");
setRequiredArguments(2, 2);
isProcedural = true;
allowedDynamicPrefixes = true;
}
// <--[command]
// @Name Adjust
// @Syntax adjust [<ObjectTag>/def:<name>|...] [<mechanism>](:<value>)
// @Required 2
// @Maximum 2
// @Short Adjusts an object's mechanism.
// @Synonyms Mechanism
// @Group core
// @Guide https://guide.denizenscript.com/guides/basics/mechanisms.html
//
// @Description
// Many object tag types 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.
//
// You can optionally adjust a MapTag of mechanisms to values.
//
// To adjust an item in an inventory, use <@link command inventory>, as '- inventory adjust slot:<#> <mechanism>:<value>'.
// Note that that is only for items, not actual inventories.
// To adjust an actual InventoryTag mechanism, you should still use the normal 'adjust' command, not 'inventory adjust'.
//
// @Tags
// <entry[saveName].result> returns the adjusted object.
// <entry[saveName].result_list> returns a ListTag of adjusted objects.
//
// @Usage
// Use to set a custom display name on an entity.
// - adjust <[some_entity]> custom_name:ANGRY!
//
// @Usage
// Use to set the skin of every online player.
// - adjust <server.online_players> skin:Notch
//
// @Usage
// Use to modify an item held in a definition.
// - adjust def:stick "display_name:Fancy stick"
//
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (String mech : PropertyParser.allMechanismsEver) {
addOne.accept(mech);
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("object")) {
if (arg.object instanceof ListTag) {
scriptEntry.addObject("object", arg.object);
}
else if (arg.object instanceof ElementTag) {
// Special parse to avoid prefixing issues
scriptEntry.addObject("object", ListTag.valueOf(arg.getRawValue(), scriptEntry.getContext()));
}
else {
scriptEntry.addObject("object", arg.asType(ListTag.class));
}
}
else if (!scriptEntry.hasObject("mechanism")
&& !scriptEntry.hasObject("mechanism_map")) {
if (arg.getRawValue().startsWith("map@")) {
scriptEntry.addObject("mechanism_map", arg.asType(MapTag.class));
}
else if (arg.hasPrefix()) {
scriptEntry.addObject("mechanism", new ElementTag(arg.getPrefix().getValue()));
scriptEntry.addObject("mechanism_value", arg.object);
}
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") && !scriptEntry.hasObject("mechanism_map")) {
throw new InvalidArgumentsException("You must specify a mechanism!");
}
}
public static HashMap<String, Consumer<Mechanism>> specialAdjustables = new HashMap<>();
static {
specialAdjustables.put("system", UtilTagBase::adjustSystem);
}
public ObjectTag adjust(ObjectTag object, String mechanismName, ObjectTag value, ScriptEntry entry) {
Mechanism mechanism = new Mechanism(mechanismName, value, entry.entryData.getTagContext());
return adjust(object, mechanism, entry);
}
public ObjectTag adjust(ObjectTag object, Mechanism mechanism, ScriptEntry entry) {
if (object == null) {
Debug.echoError("Cannot adjust null object.");
return null;
}
if (object instanceof ElementTag) {
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());
ObjectTag def = entry.getResidingQueue().getDefinitionObject(defName);
if (def == null) {
Debug.echoError("Invalid definition name '" + defName + "', cannot adjust");
return object;
}
def = adjust(def, mechanism, entry);
entry.getResidingQueue().addDefinition(defName, def);
return def;
}
object = ObjectFetcher.pickObjectFor(objectString, entry.context);
if (object instanceof ElementTag) {
FlaggableObject altObject = DenizenCore.getImplementation().simpleWordToFlaggable(objectString, entry);
if (altObject != null && !(altObject instanceof ElementTag)) {
return altObject;
}
Debug.echoError("Unable to determine what object to adjust (missing object notation?), for: " + objectString);
return object;
}
}
if (object instanceof ListTag) {
ListTag subList = (ListTag) object;
ListTag result = new ListTag();
for (ObjectTag listObject : subList.objectForms) {
listObject = adjust(listObject, mechanism, entry);
result.addObject(listObject);
}
return result;
}
if (!object.isUnique()) {
object = ObjectFetcher.pickObjectFor(object.identify(), mechanism.context); // Create duplicate of object, instead of adjusting original
}
if (!(object instanceof Adjustable)) {
Debug.echoError("'" + object + "' is not an adjustable object type.");
return object;
}
if (entry.getResidingQueue().procedural && object.isUnique()) {
Debug.echoError("Cannot adjust a unique object within a procedural queue.");
return null;
}
((Adjustable) object).safeAdjust(mechanism);
return object;
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag mechanism = scriptEntry.getElement("mechanism");
ObjectTag value = scriptEntry.getObjectTag("mechanism_value");
ListTag objects = scriptEntry.getObjectTag("object");
MapTag mechanismMap = scriptEntry.getObjectTag("mechanism_map");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), objects, value, mechanism, mechanismMap);
}
ListTag result = new ListTag();
for (ObjectTag object : objects.objectForms) {
if (mechanismMap != null) {
for (Map.Entry<StringHolder, ObjectTag> entry : mechanismMap.map.entrySet()) {
object = adjust(object, entry.getKey().str, entry.getValue(), scriptEntry);
}
}
else {
object = adjust(object, mechanism.asString(), value, scriptEntry);
}
if (objects.size() == 1) {
scriptEntry.addObject("result", object);
}
result.addObject(object);
}
scriptEntry.addObject("result_list", result);
}
}