/
dCuboid.java
445 lines (331 loc) · 12.5 KB
/
dCuboid.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package net.aufdemrand.denizen.objects;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.aufdemrand.denizen.objects.notable.Notable;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.notable.Note;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Location;
import org.bukkit.World;
public class dCuboid implements dObject, Notable {
/////////////////////
// STATIC METHODS
/////////////////
//////////////////
// OBJECT FETCHER
////////////////
/**
* Gets a Location Object from a string form of id,x,y,z,world
* or a dScript argument (location:)x,y,z,world. If including an Id,
* this location will persist and can be recalled at any time.
*
* @param string the string or dScript argument String
* @return a Location, or null if incorrectly formatted
*
*/
@ObjectFetcher("cu")
public static dCuboid valueOf(String string) {
if (string == null) return null;
////////
// Match location formats
// Split values
String[] positions = string.replace("cu@", "").split("\\|");
dB.echoDebug(Arrays.asList(positions).toString());
dLocation pos_1;
dLocation pos_2;
// Quality control
if (dLocation.matches(positions[0]) && dLocation.matches(positions[1])) {
pos_1 = dLocation.valueOf(positions[0]);
pos_2 = dLocation.valueOf(positions[1]);
// Must not be null
if (pos_1 == null || pos_2 == null) {
dB.echoError("valueOf in dCuboid returning null: '" + string + "'.");
return null;
} else if (pos_1.getWorld() != pos_2.getWorld()) {
dB.echoError("Worlds must match on cuboid construction.");
return null;
}
else
return new dCuboid(pos_1, pos_2);
}
////////
// Match @object format for Notable dCuboids
Matcher m;
final Pattern item_by_saved = Pattern.compile("(cu@)(.+)");
m = item_by_saved.matcher(string);
if (m.matches() && NotableManager.isType(m.group(2), dCuboid.class))
return (dCuboid) NotableManager.getSavedObject(m.group(2));
dB.echoError("valueOf dCuboid returning null: " + string);
return null;
}
public static boolean matches(String string) {
// regex patterns used for matching
final Pattern location_by_saved = Pattern.compile("(cu@)(.+)");
final Pattern location =
Pattern.compile("((-?\\d+,){3})\\w+\\|((-?\\d+,){3})\\w+",
Pattern.CASE_INSENSITIVE);
Matcher m;
// Check for named cuboid: cu@notable_cuboid
m = location_by_saved.matcher(string);
if (m.matches() && NotableManager.isType(m.group(2), dCuboid.class)) return true;
// Check for standard cuboid format: cu@x,y,z,world|x,y,z,world
m = location.matcher(string.replace("cu@", ""));
return m.matches();
}
///////////////////
// Constructors/Instance Methods
//////////////////
dLocation loc_1;
dLocation loc_2;
int x_distance;
int y_distance;
int z_distance;
// Only put dMaterials in filter.
ArrayList<dObject> filter = new ArrayList<dObject>();
private dCuboid(Location point_1, Location point_2) {
World world = point_1.getWorld();
int x_high = (point_1.getBlockX() >= point_2.getBlockX()
? point_1.getBlockX() : point_2.getBlockX());
int x_low = (point_1.getBlockX() <= point_2.getBlockX()
? point_1.getBlockX() : point_2.getBlockX());
x_distance = x_high - x_low;
int y_high = (point_1.getBlockY() >= point_2.getBlockY()
? point_1.getBlockY() : point_2.getBlockY());
int y_low = (point_1.getBlockY() <= point_2.getBlockY()
? point_1.getBlockY() : point_2.getBlockY());
y_distance = y_high - y_low;
int z_high = (point_1.getBlockZ() >= point_2.getBlockZ()
? point_1.getBlockZ() : point_2.getBlockZ());
int z_low = (point_1.getBlockZ() <= point_2.getBlockZ()
? point_1.getBlockZ() : point_2.getBlockZ());
z_distance = z_high - z_low;
loc_1 = new dLocation(world, x_low, y_low, z_low);
loc_2 = new dLocation(world, x_high, y_high, z_high);
}
public boolean isInsideCuboid(Location location) {
if (location.getWorld() != loc_1.getWorld()) return false;
if (!Utilities.isBetween(loc_1.getX(), loc_2.getX(), location.getX()))
return false;
if (!Utilities.isBetween(loc_1.getY(), loc_2.getY(), location.getY()))
return false;
return Utilities.isBetween(loc_1.getZ(), loc_2.getZ(), location.getZ());
}
public dCuboid addBlocksToFilter(List<dMaterial> addl) {
filter.addAll(addl);
return this;
}
public dCuboid removeBlocksFromFilter(List<dMaterial> addl) {
filter.removeAll(addl);
return this;
}
public dCuboid removeFilter() {
filter.clear();
return this;
}
public dCuboid setAsFilter(List<dMaterial> list) {
filter.clear();
filter.addAll(list);
return this;
}
public dList getOutline() {
// +-----+
// | |
// | |
// 1-----+
// + +
//
//
// + +
// +-----2
// | |
// | |
// +-----+
dList list = new dList("");
for (int y = loc_1.getBlockY(); y <= loc_1.getBlockY() + y_distance; y++) {
list.add(new dLocation(loc_1.getWorld(),
loc_1.getBlockX(),
y,
loc_1.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_2.getBlockX(),
y,
loc_2.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_1.getBlockX(),
y,
loc_2.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_2.getBlockX(),
y,
loc_1.getBlockZ()).identify());
}
for (int x = loc_1.getBlockX(); x <= loc_1.getBlockX() + x_distance; x++) {
list.add(new dLocation(loc_1.getWorld(),
x,
loc_1.getBlockY(),
loc_1.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
x,
loc_1.getBlockY(),
loc_2.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
x,
loc_2.getBlockY(),
loc_2.getBlockZ()).identify());
list.add(new dLocation(loc_1.getWorld(),
x,
loc_2.getBlockY(),
loc_1.getBlockZ()).identify());
}
for (int z = loc_1.getBlockZ(); z <= loc_1.getBlockZ() + z_distance; z++) {
list.add(new dLocation(loc_1.getWorld(),
loc_1.getBlockX(),
loc_1.getBlockY(),
z).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_2.getBlockX(),
loc_2.getBlockY(),
z).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_1.getBlockX(),
loc_2.getBlockY(),
z).identify());
list.add(new dLocation(loc_1.getWorld(),
loc_2.getBlockX(),
loc_1.getBlockY(),
z).identify());
}
return list;
}
public dList getBlocks() {
dLocation loc;
dList list = new dList("");
for (int x = 0; x != x_distance + 1; x++) {
for (int y = 0; y != y_distance + 1; y++) {
for (int z = 0; z != z_distance + 1; z++) {
loc = new dLocation(loc_1.clone()
.add((double) x, (double) y, (double) z));
if (!filter.isEmpty()) {
// Check filter
for (dObject material : filter)
if (loc.getBlock().getType().name().equalsIgnoreCase(((dMaterial) material)
.getMaterial().name()))
list.add(loc.identify());
} else
list.add(loc.identify());
}
}
}
return list;
}
///////////////////
// Notable
///////////////////
@Override
public boolean isUnique() {
return NotableManager.isSaved(this);
}
@Override
@Note("cuboid")
public String getSaveObject() {
return loc_1.getBlockX() + "," + loc_1.getBlockY()
+ "," + loc_1.getBlockZ() + "," + loc_1.getWorld().getName()
+ "|"
+ loc_2.getBlockX() + "," + loc_2.getBlockY()
+ "," + loc_2.getBlockZ() + "," + loc_2.getWorld().getName();
}
@Override
public void makeUnique(String id) {
NotableManager.saveAs(this, id);
}
@Override
public void forget() {
NotableManager.remove(this);
}
/////////////////////
// dObject
////////////////////
String prefix = "Cuboid";
@Override
public String getObjectType() {
return "cuboid";
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public dCuboid setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return (isUnique()
? "<G>" + prefix + "='<A>" + NotableManager.getSavedId(this)
+ "(<Y>" + identify()+ "<A>)<G>' "
: "<G>" + prefix + "='<Y>" + identify() + "<G>' ");
}
@Override
public String identify() {
if (isUnique())
return "cu@" + NotableManager.getSavedId(this);
else return "cu@" + loc_1.getBlockX() + "," + loc_1.getBlockY()
+ "," + loc_1.getBlockZ() + "," + loc_1.getWorld().getName()
+ "|"
+ loc_2.getBlockX() + "," + loc_2.getBlockY()
+ "," + loc_2.getBlockZ() + "," + loc_2.getWorld().getName();
}
@Override
public String toString() {
return identify();
}
/////////////////////
// dObject Attributes
/////////////////////
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;
// <--[tag]
// @attribute <cu@cuboid.get_blocks>
// @returns dList(dLocation)
// @description Returns each block location within the dCuboid.
// -->
if (attribute.startsWith("get_blocks"))
return new dList(getBlocks())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <cu@cuboid.get_outline>
// @returns dList(dLocation)
// @description Returns each block location on the outline of the dCuboid.
// -->
if (attribute.startsWith("get_outline"))
return new dList(getOutline())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <cu@cuboid.filter>
// @returns dList(dLocation)
// @description Returns the block locations from the dCuboid's filter.
// -->
if (attribute.startsWith("filter"))
return new dList(filter)
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <cu@cuboid.is_within[<location>]>
// @returns Element(Boolean)
// @description Returns true if the dCuboid is within the location.
// Otherwise, returns false.
// -->
if (attribute.startsWith("is_within")) {
dLocation loc = dLocation.valueOf(attribute.getContext(1));
return new Element(isInsideCuboid(loc))
.getAttribute(attribute.fulfill(1));
}
return new Element(identify()).getAttribute(attribute.fulfill(0));
}
}