/
EllipsoidTag.java
384 lines (331 loc) · 12.7 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
package com.denizenscript.denizen.objects;
import com.denizenscript.denizen.objects.notable.NotableManager;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.*;
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 org.bukkit.Location;
import java.util.ArrayList;
import java.util.List;
public class EllipsoidTag implements ObjectTag, Notable {
// <--[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.debug) {
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 ObjectTag duplicate() {
try {
return (ObjectTag) clone();
}
catch (CloneNotSupportedException ex) {
Debug.echoError(ex);
return null;
}
}
///////////////
// Constructors
/////////////
public EllipsoidTag(LocationTag loc, LocationTag size) {
this.loc = loc;
this.size = size;
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
private LocationTag loc;
private LocationTag size;
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 List<LocationTag> getBlockLocations(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(null, attribute);
List<LocationTag> locations = new ArrayList<>();
for (LocationTag loc : initial) {
if (contains(loc)) {
locations.add(loc);
}
}
return locations;
}
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) {
NotableManager.saveAs(this, id);
}
@Override
public void forget() {
NotableManager.remove(this);
}
@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.getWorld().getName()
+ "," + 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.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.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());
});
}
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);
}
}