/
PolygonTag.java
742 lines (674 loc) · 25 KB
/
PolygonTag.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
package com.denizenscript.denizen.objects;
import com.denizenscript.denizen.utilities.Settings;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.SavableMapFlagTracker;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.Note;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import org.bukkit.Location;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class PolygonTag implements ObjectTag, Cloneable, Notable, Adjustable, AreaContainmentObject, FlaggableObject {
// <--[ObjectType]
// @name PolygonTag
// @prefix polygon
// @base ElementTag
// @implements FlaggableObject, AreaObject
// @format
// The identity format for polygons is <world>,<y-min>,<y-max>,<x1>,<z1>,... (the x,z pair repeats for as many points as the polygon has).
//
// @description
// A PolygonTag represents a polygonal region in the world.
//
// The word 'polygon' means an arbitrary 2D shape.
// PolygonTags, in addition to a 2D polygon, contain a minimum and maximum Y coordinate, to allow them to function in 3D.
//
// PolygonTags are NOT polyhedra.
//
// A PolygonTag with 4 points at right angles would cover an area also possible to be defined by a CuboidTag, however all other shapes a PolygonTag can form are unique.
//
// Compared to CuboidTags, PolygonTags are generally slower to process and more complex to work with, but offer the benefit of supporting more intricate shapes.
//
// Note that forming invalid polygons (duplicate corners, impossible shapes, etc) will not necessarily give any error message, and may cause weird results.
//
// This object type can be noted.
//
// This object type is flaggable when it is noted.
// Flags on this object type will be stored in the notables.yml file.
//
// -->
public WorldTag world;
public double yMin = 0, yMax = 0;
public List<Corner> corners = new ArrayList<>();
public Corner boxMin = new Corner(), boxMax = new Corner();
public String noteName = null;
public AbstractFlagTracker flagTracker = null;
public static class Corner {
public double x, z;
public Corner() {
}
public Corner(double x, double z) {
this.x = x;
this.z = z;
}
}
public PolygonTag(WorldTag world) {
this.world = world;
}
public PolygonTag(WorldTag world, double yMin, double yMax, List<Corner> corners) {
this.world = world;
this.yMin = yMin;
this.yMax = yMax;
for (Corner corner : corners) {
this.corners.add(new Corner(corner.x, corner.z));
}
recalculateBox();
}
public void recalculateBox() {
if (corners.size() == 0) {
return;
}
Corner firstCorner = corners.get(0);
boxMin.x = firstCorner.x;
boxMin.z = firstCorner.z;
boxMax.x = firstCorner.x;
boxMax.z = firstCorner.z;
for (Corner corner : corners) {
recalculateToFit(corner);
}
}
public void recalculateToFit(Corner corner) {
boxMin.x = Math.min(boxMin.x, corner.x);
boxMin.z = Math.min(boxMin.z, corner.z);
boxMax.x = Math.max(boxMax.x, corner.x);
boxMax.z = Math.max(boxMax.z, corner.z);
}
@Override
public PolygonTag clone() {
return new PolygonTag(world, yMin, yMax, corners);
}
@Fetchable("polygon")
public static PolygonTag valueOf(String string, TagContext context) {
if (string.startsWith("polygon@")) {
string = string.substring("polygon@".length());
}
if (string.contains("@")) {
return null;
}
Notable saved = NoteManager.getSavedObject(string);
if (saved instanceof PolygonTag) {
return (PolygonTag) saved;
}
List<String> parts = CoreUtilities.split(string, ',');
if (parts.size() < 3) {
return null;
}
WorldTag world = new WorldTag(parts.get(0));
for (int i = 1; i < parts.size(); i++) {
if (!ArgumentHelper.matchesDouble(parts.get(i))) {
return null;
}
}
PolygonTag toReturn = new PolygonTag(world);
toReturn.yMin = Double.parseDouble(parts.get(1));
toReturn.yMax = Double.parseDouble(parts.get(2));
for (int i = 3; i < parts.size(); i += 2) {
Corner corner = new Corner(Double.parseDouble(parts.get(i)), Double.parseDouble(parts.get(i + 1)));
toReturn.corners.add(corner);
}
toReturn.recalculateBox();
return toReturn;
}
public static boolean matches(String string) {
if (valueOf(string, CoreUtilities.noDebugContext) != null) {
return true;
}
return false;
}
@Override
public ObjectTag duplicate() {
if (noteName != null) {
return this;
}
return clone();
}
@Override
public int hashCode() {
if (noteName != null) {
return noteName.hashCode();
}
return (int) (boxMin.x + boxMin.z * 7 + boxMax.x * 29 + boxMax.z * 61);
}
@Override
public boolean equals(Object other) {
if (!(other instanceof PolygonTag)) {
return false;
}
PolygonTag poly2 = (PolygonTag) other;
if (poly2.corners.size() != corners.size()) {
return false;
}
if ((noteName == null) != (poly2.noteName == null)) {
return false;
}
if (noteName != null && !noteName.equals(poly2.noteName)) {
return false;
}
if (!world.getName().equals(poly2.world.getName())) {
return false;
}
if (yMin != poly2.yMin || yMax != poly2.yMax) {
return false;
}
for (int i = 0; i < corners.size(); i++) {
Corner corner1 = corners.get(i);
Corner corner2 = poly2.corners.get(i);
if (corner1.x != corner2.x || corner1.z != corner2.z) {
return false;
}
}
return true;
}
@Override
public String getNoteName() {
return noteName;
}
public static List<PolygonTag> getNotedPolygonsContaining(Location location) {
List<PolygonTag> polygons = new ArrayList<>();
for (PolygonTag polygon : NoteManager.getAllType(PolygonTag.class)) {
if (polygon.doesContainLocation(location)) {
polygons.add(polygon);
}
}
return polygons;
}
@Override
public boolean doesContainLocation(Location loc) {
if (loc.getWorld() == null) {
return false;
}
if (!loc.getWorld().getName().equals(world.getName())) {
return false;
}
double x = loc.getX();
double z = loc.getZ();
if (x < boxMin.x || x > boxMax.x || z < boxMin.z || z > boxMax.z) {
return false;
}
double y = loc.getY();
if (y < yMin || y > yMax) {
return false;
}
boolean isInside = false;
for (int i = 0; i < corners.size(); i++) {
Corner start = corners.get(i);
Corner end = (i + 1 == corners.size() ? corners.get(0) : corners.get(i + 1));
if (((start.z > z) != (end.z > z)) && (x < (end.x - start.x) * (z - start.z) / (end.z - start.z) + start.x)) {
isInside = !isInside;
}
}
return isInside;
}
public List<LocationTag> generateFlatBlockShell(double y) {
int max = Settings.blockTagsMaxBlocks();
ArrayList<LocationTag> toOutput = new ArrayList<>();
for (int x = (int) Math.floor(boxMin.x); x < boxMax.x; x++) {
for (int z = (int) Math.floor(boxMin.z); z < boxMax.z; z++) {
LocationTag possible = new LocationTag(x + 0.5, y, z + 0.5, world.getName());
if (doesContainLocation(possible)) {
toOutput.add(possible);
}
max--;
if (max <= 0) {
return toOutput;
}
}
}
return toOutput;
}
@Override
public ListTag getShell() {
int max = Settings.blockTagsMaxBlocks();
ListTag addTo = new ListTag();
List<LocationTag> flatShell = generateFlatBlockShell(yMin);
for (LocationTag loc : flatShell) {
addTo.addObject(loc.clone());
}
max -= flatShell.size();
if (max <= 0) {
return addTo;
}
for (LocationTag loc : flatShell) {
LocationTag newLoc = loc.clone();
newLoc.setY(yMax);
addTo.addObject(newLoc);
}
max -= flatShell.size();
if (max <= 0) {
return addTo;
}
int per = (int) (yMax - yMin);
if (per == 0) {
return addTo;
}
for (int i = 0; i < corners.size(); i++) {
Corner start = corners.get(i);
Corner end = (i + 1 == corners.size() ? corners.get(0) : corners.get(i + 1));
double xMove = (end.x - start.x);
double zMove = (end.z - start.z);
double len = Math.sqrt(xMove * xMove + zMove * zMove);
if (len < 0.1) {
continue;
}
xMove /= len;
zMove /= len;
double xSpot = start.x;
double zSpot = start.z;
max -= (int) (len + 1);
if (max <= 0) {
return addTo;
}
for (int j = 0; j < len; j++) {
for (double y = yMin + 1; y < yMax; y++) {
addTo.addObject(new LocationTag(xSpot, y, zSpot, world.getName()));
}
max -= per;
if (max <= 0) {
return addTo;
}
xSpot += xMove;
zSpot += zMove;
}
}
return addTo;
}
@Override
public ListTag getBlocks(Predicate<Location> test) {
int max = Settings.blockTagsMaxBlocks();
ListTag addTo = new ListTag();
List<LocationTag> flatShell = generateFlatBlockShell(yMin);
for (double y = yMin; y < yMax; y++) {
for (LocationTag loc : flatShell) {
LocationTag newLoc = loc.clone();
newLoc.setY(y);
if (test == null || test.test(newLoc)) {
addTo.addObject(newLoc);
}
}
max -= flatShell.size();
if (max <= 0) {
return addTo;
}
}
return addTo;
}
public void addOutline2D(double y, ListTag addTo) {
int max = Settings.blockTagsMaxBlocks();
for (int i = 0; i < corners.size(); i++) {
Corner start = corners.get(i);
Corner end = (i + 1 == corners.size() ? corners.get(0) : corners.get(i + 1));
double xMove = (end.x - start.x);
double zMove = (end.z - start.z);
double len = Math.sqrt(xMove * xMove + zMove * zMove);
if (len < 0.1) {
continue;
}
xMove /= len;
zMove /= len;
double xSpot = start.x;
double zSpot = start.z;
max -= (int) (len + 1);
if (max <= 0) {
return;
}
for (int j = 0; j < len; j++) {
addTo.addObject(new LocationTag(xSpot, y, zSpot, world.getName()));
xSpot += xMove;
zSpot += zMove;
}
}
}
public ListTag getOutline() {
int max = Settings.blockTagsMaxBlocks();
ListTag output = new ListTag();
addOutline2D(yMin, output);
if (max <= output.size()) {
return output;
}
if (yMin != yMax) {
addOutline2D(yMax, output);
}
max -= output.size();
if (max <= 0) {
return output;
}
int per = (int) (yMax - yMin);
if (per == 0) {
return output;
}
for (Corner corner : corners) {
for (double y = yMin + 1; y < yMax; y++) {
output.addObject(new LocationTag(corner.x, y, corner.z, world.getName()));
}
max -= per;
if (max <= 0) {
return output;
}
}
return output;
}
@Override
public boolean isUnique() {
return noteName != null;
}
@Override
@Note("Polygons")
public Object getSaveObject() {
YamlConfiguration section = new YamlConfiguration();
section.set("object", identifyFull());
section.set("flags", flagTracker.toString());
return section;
}
@Override
public void makeUnique(String id) {
PolygonTag toNote = clone();
toNote.noteName = id;
toNote.flagTracker = new SavableMapFlagTracker();
NoteManager.saveAs(toNote, id);
}
@Override
public void forget() {
NoteManager.remove(this);
noteName = null;
flagTracker = null;
}
String prefix = "Polygon";
@Override
public String getObjectType() {
return "polygon";
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public PolygonTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debuggable() {
if (isUnique()) {
return "<LG>polygon@<Y>" + noteName + " <GR>(" + identifyFull() + ")";
}
else {
return identifyFull().replace(",", "<LG>, <Y>");
}
}
@Override
public String identify() {
if (isUnique()) {
return "polygon@" + noteName;
}
else {
return identifyFull();
}
}
@Override
public String identifySimple() {
return identify();
}
public String identifyFull() {
StringBuilder sb = new StringBuilder();
sb.append("polygon@").append(world.getName()).append(",").append(yMin).append(",").append(yMax).append(",");
for (Corner corner : corners) {
sb.append(corner.x).append(",").append(corner.z).append(",");
}
if (corners.size() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
@Override
public String toString() {
return identify();
}
@Override
public AbstractFlagTracker getFlagTracker() {
return flagTracker;
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
if (noteName != null) {
this.flagTracker = tracker;
}
}
@Override
public String getReasonNotFlaggable() {
if (noteName == null) {
return "the area is not noted - only noted areas can hold flags";
}
return "unknown reason - something went wrong";
}
@Override
public CuboidTag getCuboidBoundary() {
LocationTag min = new LocationTag(Math.floor(boxMin.x), Math.floor(yMin), Math.floor(boxMin.z), world.getName());
LocationTag max = new LocationTag(Math.ceil(boxMax.x), Math.ceil(yMax), Math.ceil(boxMax.z), world.getName());
return new CuboidTag(min, max);
}
@Override
public WorldTag getWorld() {
return world;
}
@Override
public PolygonTag withWorld(WorldTag world) {
PolygonTag toReturn = clone();
toReturn.world = world;
return toReturn;
}
public static void registerTags() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
AreaContainmentObject.registerTags(PolygonTag.class, tagProcessor);
// <--[tag]
// @attribute <PolygonTag.max_y>
// @returns ElementTag(Decimal)
// @description
// Returns the maximum Y level for this polygon.
// -->
tagProcessor.registerTag(ElementTag.class, "max_y", (attribute, polygon) -> {
return new ElementTag(polygon.yMax);
});
// <--[tag]
// @attribute <PolygonTag.min_y>
// @returns ElementTag(Decimal)
// @description
// Returns the minimum Y level for this polygon.
// -->
tagProcessor.registerTag(ElementTag.class, "min_y", (attribute, polygon) -> {
return new ElementTag(polygon.yMin);
});
// <--[tag]
// @attribute <PolygonTag.note_name>
// @returns ElementTag
// @description
// Gets the name of a noted PolygonTag. If the polygon isn't noted, this is null.
// -->
tagProcessor.registerTag(ElementTag.class, "note_name", (attribute, polygon) -> {
String noteName = NoteManager.getSavedId(polygon);
if (noteName == null) {
return null;
}
return new ElementTag(noteName);
});
// <--[tag]
// @attribute <PolygonTag.corners>
// @returns ListTag(LocationTag)
// @description
// Returns a list of the polygon's corners, as locations with Y coordinate set to the y-min value.
// -->
tagProcessor.registerTag(ListTag.class, "corners", (attribute, polygon) -> {
ListTag list = new ListTag();
for (Corner corner : polygon.corners) {
list.addObject(new LocationTag(corner.x, polygon.yMin, corner.z, polygon.world.getName()));
}
return list;
});
// <--[tag]
// @attribute <PolygonTag.shift[<vector>]>
// @returns PolygonTag
// @description
// Returns a copy of the polygon, with all coordinates shifted by the given location-vector.
// -->
tagProcessor.registerTag(PolygonTag.class, "shift", (attribute, polygon) -> {
if (!attribute.hasParam()) {
attribute.echoError("PolygonTag.shift[...] tag must have an input.");
return null;
}
LocationTag shift = attribute.paramAsType(LocationTag.class);
PolygonTag toReturn = polygon.clone();
toReturn.yMin += shift.getY();
toReturn.yMax += shift.getY();
for (Corner corner : toReturn.corners) {
corner.x += shift.getX();
corner.z += shift.getZ();
}
toReturn.boxMin.x += shift.getX();
toReturn.boxMin.z += shift.getZ();
toReturn.boxMax.x += shift.getX();
toReturn.boxMax.z += shift.getZ();
return toReturn;
});
// <--[tag]
// @attribute <PolygonTag.with_corner[<location>]>
// @returns PolygonTag
// @mechanism PolygonTag.add_corner
// @description
// Returns a copy of the polygon, with the specified corner added to the end of the corner list.
// -->
tagProcessor.registerTag(PolygonTag.class, "with_corner", (attribute, polygon) -> {
if (!attribute.hasParam()) {
attribute.echoError("PolygonTag.with_corner[...] tag must have an input.");
return null;
}
LocationTag corner = attribute.paramAsType(LocationTag.class);
PolygonTag toReturn = polygon.clone();
Corner added = new Corner(corner.getX(), corner.getZ());
toReturn.corners.add(added);
toReturn.recalculateToFit(added);
return toReturn;
});
// <--[tag]
// @attribute <PolygonTag.with_y_min[<#.#>]>
// @returns PolygonTag
// @description
// Returns a copy of the polygon, with the specified minimum-Y value.
// -->
tagProcessor.registerTag(PolygonTag.class, "with_y_min", (attribute, polygon) -> {
if (!attribute.hasParam()) {
attribute.echoError("PolygonTag.with_y_min[...] tag must have an input.");
return null;
}
PolygonTag toReturn = polygon.clone();
toReturn.yMin = attribute.getDoubleParam();
return toReturn;
});
// <--[tag]
// @attribute <PolygonTag.with_y_max[<#.#>]>
// @returns PolygonTag
// @description
// Returns a copy of the polygon, with the specified maximum-Y value.
// -->
tagProcessor.registerTag(PolygonTag.class, "with_y_max", (attribute, polygon) -> {
if (!attribute.hasParam()) {
attribute.echoError("PolygonTag.with_y_max[...] tag must have an input.");
return null;
}
PolygonTag toReturn = polygon.clone();
toReturn.yMax = attribute.getDoubleParam();
return toReturn;
});
// <--[tag]
// @attribute <PolygonTag.include_y[<#.#>]>
// @returns PolygonTag
// @description
// Returns a copy of the polygon, with the specified Y value included as part of the Y range (expanding the Y-min or Y-max as needed).
// -->
tagProcessor.registerTag(PolygonTag.class, "include_y", (attribute, polygon) -> {
if (!attribute.hasParam()) {
attribute.echoError("PolygonTag.include_y[...] tag must have an input.");
return null;
}
PolygonTag toReturn = polygon.clone();
double y = attribute.getDoubleParam();
toReturn.yMin = Math.min(y, toReturn.yMin);
toReturn.yMax = Math.max(y, toReturn.yMax);
return toReturn;
});
// <--[tag]
// @attribute <PolygonTag.outline_2d[<#.#>]>
// @returns ListTag(LocationTag)
// @description
// Returns a list of locations along the 2D outline of this polygon, at the specified Y level (roughly a block-width of separation between each).
// -->
tagProcessor.registerTag(ListTag.class, "outline_2d", (attribute, polygon) -> {
if (!attribute.hasParam()) {
attribute.echoError("PolygonTag.outline_2d[...] tag must have an input.");
return null;
}
double y = attribute.getDoubleParam();
ListTag output = new ListTag();
polygon.addOutline2D(y, output);
return output;
});
// <--[tag]
// @attribute <PolygonTag.outline>
// @returns ListTag(LocationTag)
// @description
// Returns a list of locations along the 3D outline of this polygon (roughly a block-width of separation between each).
// -->
tagProcessor.registerTag(ListTag.class, "outline", (attribute, polygon) -> {
return polygon.getOutline();
});
}
public static ObjectTagProcessor<PolygonTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
public void applyProperty(Mechanism mechanism) {
if (NoteManager.isExactSavedObject(this)) {
mechanism.echoError("Cannot apply properties to noted objects.");
return;
}
adjust(mechanism);
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object PolygonTag
// @name add_corner
// @input LocationTag
// @description
// Adds a corner to the end of the polygon's corner list.
// @tags
// <PolygonTag.with_corner[<location>]>
// -->
if (mechanism.matches("add_corner") && mechanism.requireObject(LocationTag.class)) {
LocationTag loc = mechanism.valueAsType(LocationTag.class);
Corner newCorner = new Corner(loc.getX(), loc.getZ());
corners.add(newCorner);
recalculateToFit(newCorner);
}
}
}