/
EllipsoidTag.java
594 lines (539 loc) · 21.8 KB
/
EllipsoidTag.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
package com.denizenscript.denizen.objects;
import com.denizenscript.denizen.objects.notable.NotableManager;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.Note;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.List;
public class EllipsoidTag implements ObjectTag, Notable, Cloneable, AreaContainmentObject {
// <--[language]
// @name EllipsoidTag Objects
// @group Object System
// @description
// An EllipsoidTag represents an ellipsoidal region in the world.
//
// The word 'ellipsoid' means a less strict sphere.
// Basically: an "ellipsoid" is to a 3D "sphere" what an "ellipse" (or "oval") is to a 2D "circle".
//
// These use the object notation "ellipsoid@".
// The identity format for ellipsoids is <x>,<y>,<z>,<world>,<x-radius>,<y-radius>,<z-radius>
// For example, 'ellipsoid@1,2,3,space,7,7,7'.
//
// -->
public static List<EllipsoidTag> getNotableEllipsoidsContaining(Location location) {
List<EllipsoidTag> ellipsoids = new ArrayList<>();
for (EllipsoidTag ellipsoid : NotableManager.getAllType(EllipsoidTag.class)) {
if (ellipsoid.contains(location)) {
ellipsoids.add(ellipsoid);
}
}
return ellipsoids;
}
//////////////////
// OBJECT FETCHER
////////////////
@Deprecated
public static EllipsoidTag valueOf(String string) {
return valueOf(string, null);
}
/**
* Gets an Ellipsoid Object from a string form.
*
* @param string the string
*/
@Fetchable("ellipsoid")
public static EllipsoidTag valueOf(String string, TagContext context) {
if (string.startsWith("ellipsoid@")) {
string = string.substring(10);
}
Notable noted = NotableManager.getSavedObject(string);
if (noted instanceof EllipsoidTag) {
return (EllipsoidTag) noted;
}
List<String> split = CoreUtilities.split(string, ',');
if (split.size() != 7) {
return null;
}
WorldTag world = WorldTag.valueOf(split.get(3), false);
if (world == null) {
return null;
}
for (int i = 0; i < 7; i++) {
if (i != 3 && !ArgumentHelper.matchesDouble(split.get(i))) {
if (context == null || context.showErrors()) {
Debug.echoError("EllipsoidTag input is not a valid decimal number: " + split.get(i));
return null;
}
}
}
LocationTag location = new LocationTag(world.getWorld(), Double.parseDouble(split.get(0)), Double.parseDouble(split.get(1)), Double.parseDouble(split.get(2)));
LocationTag size = new LocationTag(null, Double.parseDouble(split.get(4)), Double.parseDouble(split.get(5)), Double.parseDouble(split.get(6)));
return new EllipsoidTag(location, size);
}
/**
* Determines whether a string is a valid ellipsoid.
*
* @param arg the string
* @return true if matched, otherwise false
*/
public static boolean matches(String arg) {
try {
return EllipsoidTag.valueOf(arg, CoreUtilities.noDebugContext) != null;
}
catch (Exception e) {
return false;
}
}
@Override
public EllipsoidTag clone() {
if (noteName != null) {
return this;
}
return new EllipsoidTag(loc.clone(), size.clone());
}
@Override
public ObjectTag duplicate() {
return clone();
}
///////////////
// Constructors
/////////////
public EllipsoidTag(LocationTag loc, LocationTag size) {
this.loc = loc;
this.size = size;
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
private LocationTag loc;
private LocationTag size;
public String noteName = null;
public ListTag getBlocks(Attribute attribute) {
return getBlocks(null, attribute);
}
public ListTag getBlocks(List<MaterialTag> materials, Attribute attribute) {
List<LocationTag> initial = new CuboidTag(new Location(loc.getWorld(),
loc.getX() - size.getX(), loc.getY() - size.getY(), loc.getZ() - size.getZ()),
new Location(loc.getWorld(),
loc.getX() + size.getX(), loc.getY() + size.getY(), loc.getZ() + size.getZ()))
.getBlocks_internal(materials, attribute);
ListTag list = new ListTag();
for (LocationTag loc : initial) {
if (contains(loc)) {
list.addObject(loc);
}
}
return list;
}
public List<LocationTag> getBlockLocationsUnfiltered() {
List<LocationTag> initial = new CuboidTag(new Location(loc.getWorld(),
loc.getX() - size.getX(), loc.getY() - size.getY(), loc.getZ() - size.getZ()),
new Location(loc.getWorld(),
loc.getX() + size.getX(), loc.getY() + size.getY(), loc.getZ() + size.getZ()))
.getBlockLocationsUnfiltered();
List<LocationTag> locations = new ArrayList<>();
for (LocationTag loc : initial) {
if (contains(loc)) {
locations.add(loc);
}
}
return locations;
}
public ListTag getShell() {
ListTag output = new ListTag();
double yScale = size.getY();
int maxY = (int) Math.floor(yScale);
output.addObject(new LocationTag(loc.getBlockX(), loc.getBlockY() - maxY, loc.getBlockZ(), loc.getWorldName()));
if (maxY != 0) {
output.addObject(new LocationTag(loc.getBlockX(), loc.getBlockY() + maxY, loc.getBlockZ(), loc.getWorldName()));
}
for (int y = -maxY; y <= maxY; y++) {
double yProgMin = Math.min(1.0, (Math.abs(y) + 1) / yScale);
double yProgMax = Math.abs(y) / yScale;
double minSubWidth = Math.sqrt(1.0 - yProgMin * yProgMin);
double maxSubWidth = Math.sqrt(1.0 - yProgMax * yProgMax);
double minX = size.getX() * minSubWidth - 1;
double minZ = size.getZ() * minSubWidth - 1;
double maxX = size.getX() * maxSubWidth;
double maxZ = size.getZ() * maxSubWidth;
for (int x = 0; x < maxX; x++) {
for (int z = 0; z < maxZ; z++) {
double scaleTestMin = (x * x) / (minX * minX) + (z * z) / (minZ * minZ);
double scaleTestMax = (x * x) / (maxX * maxX) + (z * z) / (maxZ * maxZ);
if (scaleTestMin >= 1.0 && scaleTestMax <= 1.0) {
output.addObject(new LocationTag(loc.getBlockX() + x, loc.getBlockY() + y, loc.getBlockZ() + z, loc.getWorldName()));
if (x != 0) {
output.addObject(new LocationTag(loc.getBlockX() - x, loc.getBlockY() + y, loc.getBlockZ() + z, loc.getWorldName()));
}
if (z != 0) {
output.addObject(new LocationTag(loc.getBlockX() + x, loc.getBlockY() + y, loc.getBlockZ() - z, loc.getWorldName()));
}
if (x != 0 && z != 0) {
output.addObject(new LocationTag(loc.getBlockX() - x, loc.getBlockY() + y, loc.getBlockZ() - z, loc.getWorldName()));
}
}
}
}
}
return output;
}
public boolean contains(Location test) {
double xbase = test.getX() - loc.getX();
double ybase = test.getY() - loc.getY();
double zbase = test.getZ() - loc.getZ();
return ((xbase * xbase) / (size.getX() * size.getX())
+ (ybase * ybase) / (size.getY() * size.getY())
+ (zbase * zbase) / (size.getZ() * size.getZ()) <= 1);
}
String prefix = "ellipsoid";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debuggable() {
if (isUnique()) {
return "ellipsoid@" + NotableManager.getSavedId(this) + "<GR> (" + identifyFull() + ")";
}
else {
return identifyFull();
}
}
@Override
public boolean isUnique() {
return NotableManager.isSaved(this);
}
@Override
@Note("Ellipsoids")
public Object getSaveObject() {
return identifyFull().substring(10);
}
@Override
public void makeUnique(String id) {
EllipsoidTag toNote = clone();
toNote.noteName = id;
NotableManager.saveAs(toNote, id);
}
@Override
public void forget() {
NotableManager.remove(this);
noteName = null;
}
@Override
public int hashCode() {
if (noteName != null) {
return noteName.hashCode();
}
return loc.hashCode() + size.hashCode();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof EllipsoidTag)) {
return false;
}
EllipsoidTag ellipsoid2 = (EllipsoidTag) other;
if ((noteName == null) != (ellipsoid2.noteName == null)) {
return false;
}
if (noteName != null && !noteName.equals(ellipsoid2.noteName)) {
return false;
}
if (!loc.getWorldName().equals(ellipsoid2.loc.getWorldName())) {
return false;
}
if (loc.distanceSquared(ellipsoid2.loc) >= 0.25) {
return false;
}
if (size.distanceSquared(ellipsoid2.size) >= 0.25) {
return false;
}
return true;
}
@Override
public String getObjectType() {
return "Ellipsoid";
}
@Override
public String identify() {
if (isUnique()) {
return "ellipsoid@" + NotableManager.getSavedId(this);
}
else {
return identifyFull();
}
}
@Override
public String identifySimple() {
return identify();
}
public String identifyFull() {
return "ellipsoid@" + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + loc.getWorldName()
+ "," + size.getX() + "," + size.getY() + "," + size.getZ();
}
@Override
public String toString() {
return identify();
}
@Override
public ObjectTag setPrefix(String prefix) {
if (prefix != null) {
this.prefix = prefix;
}
return this;
}
public static void registerTags() {
// <--[tag]
// @attribute <EllipsoidTag.blocks[<material>|...]>
// @returns ListTag(LocationTag)
// @description
// Returns each block location within the EllipsoidTag.
// Optionally, specify a list of materials to only return locations with that block type.
// -->
registerTag("blocks", (attribute, object) -> {
if (attribute.hasContext(1)) {
return new ListTag(object.getBlocks(attribute.contextAsType(1, ListTag.class).filter(MaterialTag.class, attribute.context), attribute));
}
else {
return new ListTag(object.getBlocks(attribute));
}
}, "get_blocks");
// <--[tag]
// @attribute <EllipsoidTag.shell>
// @returns ListTag(LocationTag)
// @description
// Returns a 3D outline (shell) of this ellipsoid, as a list of block locations.
// -->
registerTag("shell", (attribute, object) -> {
return object.getShell();
});
// <--[tag]
// @attribute <EllipsoidTag.location>
// @returns LocationTag
// @description
// Returns the location of the ellipsoid.
// -->
registerTag("location", (attribute, object) -> {
return object.loc;
});
// <--[tag]
// @attribute <EllipsoidTag.size>
// @returns LocationTag
// @description
// Returns the size of the ellipsoid.
// -->
registerTag("size", (attribute, object) -> {
return object.size;
});
// <--[tag]
// @attribute <EllipsoidTag.add[<location>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, shifted by the input location.
// -->
registerTag("add", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("ellipsoid.add[...] tag must have an input.");
return null;
}
return new EllipsoidTag(object.loc.clone().add(attribute.contextAsType(1, LocationTag.class)), object.size.clone());
});
// <--[tag]
// @attribute <EllipsoidTag.contains[<location>]>
// @returns ElementTag(Boolean)
// @description
// Returns a boolean indicating whether the specified location is inside this ellipsoid.
// -->
registerTag("contains", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("ellipsoid.contains[...] tag must have an input.");
return null;
}
return new ElementTag(object.contains(attribute.contextAsType(1, LocationTag.class)));
});
// <--[tag]
// @attribute <EllipsoidTag.include[<location>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, with the size value adapted to include the specified world location.
// -->
registerTag("include", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("ellipsoid.include[...] tag must have an input.");
return null;
}
LocationTag target = attribute.contextAsType(1, LocationTag.class);
if (object.contains(target)) {
return object;
}
LocationTag size = object.size.clone();
Vector relative = target.toVector().subtract(object.loc.toVector());
// Cuboid minimum expansion
size.setX(Math.max(size.getX(), Math.abs(relative.getX())));
size.setY(Math.max(size.getY(), Math.abs(relative.getY())));
size.setZ(Math.max(size.getZ(), Math.abs(relative.getZ())));
EllipsoidTag result = new EllipsoidTag(object.loc.clone(), new LocationTag(size));
if (result.contains(target)) {
return result;
}
double sizeLen = size.length();
// Ellipsoid additional expand
while (!result.contains(target)) {
// I gave up on figuring out the math for this, so here's an awful loop-hack
double projX = (relative.getX() * relative.getX()) / (size.getX() * size.getX());
double projY = (relative.getY() * relative.getY()) / (size.getY() * size.getY());
double projZ = (relative.getZ() * relative.getZ()) / (size.getZ() * size.getZ());
double scale = Math.max(projX + projY + projZ, sizeLen * 0.01);
if (projX >= projY && projX >= projZ) {
size.setX(size.getX() + scale);
}
else if (projY >= projX && projY >= projZ) {
size.setY(size.getY() + scale);
}
else if (projZ >= projX && projZ >= projY) {
size.setZ(size.getZ() + scale);
}
else {
size = size.add(scale, scale, scale);
}
result.size = size;
}
return result;
});
// <--[tag]
// @attribute <EllipsoidTag.with_location[<location>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, set to the specified location.
// -->
registerTag("with_location", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("ellipsoid.with_location[...] tag must have an input.");
return null;
}
return new EllipsoidTag(attribute.contextAsType(1, LocationTag.class), object.size.clone());
});
// <--[tag]
// @attribute <EllipsoidTag.with_size[<location>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, set to the specified size.
// -->
registerTag("with_size", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("ellipsoid.with_size[...] tag must have an input.");
return null;
}
return new EllipsoidTag(object.loc.clone(), attribute.contextAsType(1, LocationTag.class));
});
// <--[tag]
// @attribute <EllipsoidTag.with_world[<world>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, set to the specified world.
// -->
registerTag("with_world", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("ellipsoid.with_world[...] tag must have an input.");
return null;
}
LocationTag loc = object.loc.clone();
loc.setWorld(attribute.contextAsType(1, WorldTag.class).getWorld());
return new EllipsoidTag(loc, object.size.clone());
});
// <--[tag]
// @attribute <EllipsoidTag.players>
// @returns ListTag(PlayerTag)
// @description
// Gets a list of all players currently within the EllipsoidTag.
// -->
registerTag("players", (attribute, object) -> {
ArrayList<PlayerTag> players = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
if (object.contains(player.getLocation())) {
players.add(PlayerTag.mirrorBukkitPlayer(player));
}
}
return new ListTag(players);
});
// <--[tag]
// @attribute <EllipsoidTag.npcs>
// @returns ListTag(NPCTag)
// @description
// Gets a list of all NPCs currently within the EllipsoidTag.
// -->
if (Depends.citizens != null) {
registerTag("npcs", (attribute, object) -> {
ArrayList<NPCTag> npcs = new ArrayList<>();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
NPCTag dnpc = new NPCTag(npc);
if (object.contains(dnpc.getLocation())) {
npcs.add(dnpc);
}
}
return new ListTag(npcs);
});
}
// <--[tag]
// @attribute <EllipsoidTag.entities[(<entity>|...)]>
// @returns ListTag(EntityTag)
// @description
// Gets a list of all entities currently within the EllipsoidTag, with an optional search parameter for the entity type.
// -->
registerTag("entities", (attribute, object) -> {
ArrayList<EntityTag> entities = new ArrayList<>();
ListTag types = new ListTag();
if (attribute.hasContext(1)) {
types = attribute.contextAsType(1, ListTag.class);
}
for (Entity ent : new WorldTag(object.loc.getWorld()).getEntitiesForTag()) {
EntityTag current = new EntityTag(ent);
if (object.contains(ent.getLocation())) {
if (!types.isEmpty()) {
for (String type : types) {
if (current.identifySimpleType().equalsIgnoreCase(type)) {
entities.add(current);
break;
}
}
}
else {
entities.add(new EntityTag(ent));
}
}
}
return new ListTag(entities);
});
}
public static ObjectTagProcessor<EllipsoidTag> tagProcessor = new ObjectTagProcessor<>();
public static void registerTag(String name, TagRunnable.ObjectInterface<EllipsoidTag> runnable, String... variants) {
tagProcessor.registerTag(name, runnable, variants);
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
@Override
public String getNoteName() {
return noteName;
}
@Override
public boolean doesContainLocation(Location loc) {
return contains(loc);
}
}