/
SchematicCommand.java
396 lines (361 loc) · 16.4 KB
/
SchematicCommand.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.utilities.blocks.BlockData;
import net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet;
import net.aufdemrand.denizen.utilities.debugging.dB;
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.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.scripts.commands.Holdable;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.ReplaceableTagEvent;
import net.aufdemrand.denizencore.tags.TagManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
public class SchematicCommand extends AbstractCommand implements Holdable {
@Override
public void onEnable() {
TagManager.registerTagEvents(this);
schematics = new HashMap<String, CuboidBlockSet>();
}
private enum Type {CREATE, LOAD, UNLOAD, ROTATE, PASTE, SAVE}
public static Map<String, CuboidBlockSet> schematics;
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", new Element(arg.raw_value.toUpperCase()));
}
else if (!scriptEntry.hasObject("name")
&& arg.matchesPrefix("name")) {
scriptEntry.addObject("name", arg.asElement());
}
else if (!scriptEntry.hasObject("filename")
&& arg.matchesPrefix("filename")) {
scriptEntry.addObject("filename", arg.asElement());
}
else if (!scriptEntry.hasObject("angle")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("angle", arg.asElement());
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("location", arg.asType(dLocation.class));
}
else if (!scriptEntry.hasObject("cuboid")
&& arg.matchesArgumentType(dCuboid.class)) {
scriptEntry.addObject("cuboid", arg.asType(dCuboid.class));
}
else if (!scriptEntry.hasObject("delayed")
&& arg.matches("delayed")) {
scriptEntry.addObject("delayed", new Element("true"));
}
else if (!scriptEntry.hasObject("noair")
&& arg.matches("noair")) {
scriptEntry.addObject("noair", new Element("true"));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("type")) {
throw new InvalidArgumentsException("Missing type argument!");
}
if (!scriptEntry.hasObject("name")) {
throw new InvalidArgumentsException("Missing name argument!");
}
}
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
Element angle = scriptEntry.getElement("angle");
Element type = scriptEntry.getElement("type");
Element name = scriptEntry.getElement("name");
Element filename = scriptEntry.getElement("filename");
Element noair = scriptEntry.getElement("noair");
Element delayed = scriptEntry.getElement("delayed");
dLocation location = scriptEntry.getdObject("location");
dCuboid cuboid = scriptEntry.getdObject("cuboid");
dB.report(scriptEntry, getName(), type.debug()
+ name.debug()
+ (location != null ? location.debug() : "")
+ (filename != null ? filename.debug() : "")
+ (cuboid != null ? cuboid.debug() : "")
+ (angle != null ? angle.debug() : "")
+ (noair != null ? noair.debug(): "")
+ (delayed != null ? delayed.debug() : ""));
CuboidBlockSet set;
Type ttype = Type.valueOf(type.asString());
if (scriptEntry.shouldWaitFor() && ttype != Type.PASTE) {
dB.echoError("Tried to wait for a non-paste schematic command.");
scriptEntry.setFinished(true);
}
String fname = filename != null ? filename.asString(): name.asString();
switch (ttype) {
case CREATE:
if (schematics.containsKey(name.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is already loaded.");
return;
}
if (cuboid == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Missing cuboid argument!");
return;
}
if (location == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Missing origin location argument!");
return;
}
try {
// TODO: Make me waitable!
set = new CuboidBlockSet(cuboid, location);
schematics.put(name.asString().toUpperCase(), set);
}
catch (Exception ex) {
dB.echoError(scriptEntry.getResidingQueue(), "Error creating schematic object " + name.asString() + ".");
dB.echoError(scriptEntry.getResidingQueue(), ex);
return;
}
break;
case LOAD:
if (schematics.containsKey(name.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is already loaded.");
return;
}
try {
String directory = URLDecoder.decode(System.getProperty("user.dir"));
File f = new File(directory + "/plugins/Denizen/schematics/" + fname + ".schematic");
if (!f.exists()) {
dB.echoError("Schematic file " + fname + " does not exist. Are you sure it's in " + directory + "/plugins/Denizen/schematics/?");
return;
}
InputStream fs = new FileInputStream(f);
// TODO: Make me waitable!
set = CuboidBlockSet.fromMCEditStream(fs);
fs.close();
schematics.put(name.asString().toUpperCase(), set);
}
catch (Exception ex) {
dB.echoError(scriptEntry.getResidingQueue(), "Error loading schematic file " + name.asString() + ".");
dB.echoError(scriptEntry.getResidingQueue(), ex);
return;
}
break;
case UNLOAD:
if (!schematics.containsKey(name.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
schematics.remove(name.asString().toUpperCase());
break;
case ROTATE:
if (!schematics.containsKey(name.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
if (angle == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Missing angle argument!");
return;
}
dB.echoError(scriptEntry.getResidingQueue(), "Schematic rotation is TODO!");
// TODO: Make me waitable!
int ang = angle.asInt();
if (ang < 0) {
ang = 360 + ang;
}
while (ang > 0) {
ang -= 90;
schematics.get(name.asString().toUpperCase()).rotateOne();
}
break;
case PASTE:
if (!schematics.containsKey(name.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
if (location == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Missing location argument!");
return;
}
try {
if (delayed != null && delayed.asBoolean()) {
schematics.get(name.asString().toUpperCase()).setBlocksDelayed(location, new Runnable() {
@Override
public void run() {
scriptEntry.setFinished(true);
}
}, noair != null && noair.asBoolean());
}
else {
scriptEntry.setFinished(true);
schematics.get(name.asString().toUpperCase()).setBlocks(location, noair != null && noair.asBoolean());
}
}
catch (Exception ex) {
dB.echoError(scriptEntry.getResidingQueue(), "Exception pasting schematic file " + name.asString() + ".");
dB.echoError(scriptEntry.getResidingQueue(), ex);
return;
}
break;
case SAVE:
if (!schematics.containsKey(name.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
return;
}
try {
set = schematics.get(name.asString().toUpperCase());
String directory = URLDecoder.decode(System.getProperty("user.dir"));
File f = new File(directory + "/plugins/Denizen/schematics/" + fname + ".schematic");
f.getParentFile().mkdirs();
// TODO: Make me waitable!
FileOutputStream fs = new FileOutputStream(f);
set.saveMCEditFormatToStream(fs);
fs.flush();
fs.close();
}
catch (Exception ex) {
dB.echoError(scriptEntry.getResidingQueue(), "Error saving schematic file " + fname + ".");
dB.echoError(scriptEntry.getResidingQueue(), ex);
return;
}
break;
}
}
@TagManager.TagEvents
public void schematicTags(ReplaceableTagEvent event) {
if (!event.matches("schematic, schem")) {
return;
}
String id = event.hasNameContext() ? event.getNameContext().toUpperCase(): null;
Attribute attribute = event.getAttributes().fulfill(1);
// <--[tag]
// @attribute <schematic.list>
// @returns dList(Element)
// @description
// Returns a list of all loaded schematics.
// -->
if (attribute.startsWith("list")) {
event.setReplaced(new dList(schematics.keySet()).getAttribute(attribute.fulfill(1)));
}
if (id == null) {
return;
}
if (!schematics.containsKey(id)) {
// Meta below
if (attribute.startsWith("exists")) {
event.setReplaced(new Element(false)
.getAttribute(attribute.fulfill(1)));
return;
}
dB.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null, "Schematic file " + id + " is not loaded.");
return;
}
CuboidBlockSet set = schematics.get(id);
//
// Check attributes
//
// <--[tag]
// @attribute <schematic[<name>].exists>
// @returns Element(Boolean)
// @description
// Returns whether the schematic exists.
// -->
if (attribute.startsWith("exists")) {
event.setReplaced(new Element(true)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].height>
// @returns Element(Number)
// @description
// Returns the height (Y) of the schematic.
// -->
if (attribute.startsWith("height")) {
event.setReplaced(new Element(set.y_length)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].length>
// @returns Element(Number)
// @description
// Returns the length (Z) of the schematic.
// -->
if (attribute.startsWith("length")) {
event.setReplaced(new Element(set.z_height)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].width>
// @returns Element(Number)
// @description
// Returns the width (X) of the schematic.
// -->
if (attribute.startsWith("width")) {
event.setReplaced(new Element(set.x_width)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].block[<location>]>
// @returns dMaterial
// @description
// Returns the material for the block at the location in the schematic.
// -->
if (attribute.startsWith("block")) {
if (attribute.hasContext(1) && dLocation.matches(attribute.getContext(1))) {
dLocation location = dLocation.valueOf(attribute.getContext(1));
BlockData block = set.blockAt(location.getX(), location.getY(), location.getZ());
event.setReplaced(dMaterial.getMaterialFrom(block.material, block.data)
.getAttribute(attribute.fulfill(1)));
return;
}
}
// <--[tag]
// @attribute <schematic[<name>].origin>
// @returns dLocation
// @description
// Returns the origin location of the schematic.
// -->
if (attribute.startsWith("origin")) {
event.setReplaced(new dLocation(null, set.center_x, set.center_y, set.center_z)
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].blocks>
// @returns Element(Number)
// @description
// Returns the number of blocks in the schematic.
// -->
if (attribute.startsWith("blocks")) {
event.setReplaced(new Element(set.blocks.size())
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <schematic[<name>].cuboid[<origin location>]>
// @returns dCuboid
// @description
// Returns a cuboid of where the schematic would be if it was pasted at an origin.
// -->
if (attribute.startsWith("cuboid") && attribute.hasContext(1)) {
dLocation origin = dLocation.valueOf(attribute.getContext(1));
event.setReplaced(set.getCuboid(origin)
.getAttribute(attribute.fulfill(1)));
return;
}
}
}