-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
dEllipsoid.java
252 lines (206 loc) · 7.71 KB
/
dEllipsoid.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
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.notable.Notable;
import net.aufdemrand.denizencore.objects.notable.Note;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.objects.properties.PropertyParser;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Location;
import java.util.ArrayList;
import java.util.List;
public class dEllipsoid implements dObject, Notable {
public static List<dEllipsoid> getNotableEllipsoidsContaining(Location location) {
List<dEllipsoid> cuboids = new ArrayList<dEllipsoid>();
for (dEllipsoid ellipsoid : NotableManager.getAllType(dEllipsoid.class))
if (ellipsoid.contains(location))
cuboids.add(ellipsoid);
return cuboids;
}
//////////////////
// OBJECT FETCHER
////////////////
public static dEllipsoid valueOf(String string) {
return valueOf(string, null);
}
/**
* Gets an Ellipsoid Object from a string form.
*
* @param string the string
*/
@Fetchable("ellipsoid")
public static dEllipsoid valueOf(String string, TagContext context) {
if (string.startsWith("ellipsoid@"))
string = string.substring(10);
if (NotableManager.isType(string, dEllipsoid.class))
return (dEllipsoid) NotableManager.getSavedObject(string);
List<String> split = CoreUtilities.split(string, ',');
if (split.size() != 7)
return null;
dLocation location = new dLocation(dWorld.valueOf(split.get(3)).getWorld(),
aH.getDoubleFrom(split.get(0)), aH.getDoubleFrom(split.get(1)), aH.getDoubleFrom(split.get(2)));
dLocation size = new dLocation(null, aH.getDoubleFrom(split.get(4)),
aH.getDoubleFrom(split.get(5)), aH.getDoubleFrom(split.get(6)));
return new dEllipsoid(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) {
return arg.startsWith("ellipsoid@");
}
///////////////
// Constructors
/////////////
public dEllipsoid(dLocation loc, dLocation size) {
this.loc = loc;
this.size = size;
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
private dLocation loc;
private dLocation size;
public dList getBlocks() {
return getBlocks(null);
}
public dList getBlocks(List<dMaterial> materials) {
List<dLocation> initial = new dCuboid(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);
dList list = new dList();
for (dLocation loc : initial) {
if (contains(loc)) {
list.add(loc.identify());
}
}
return list;
}
public List<dLocation> getBlockLocations() {
List<dLocation> initial = new dCuboid(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);
List<dLocation> locations = new ArrayList<dLocation>();
for (dLocation 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 debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return false;
}
@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 dObject setPrefix(String prefix) {
if (prefix != null)
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
// <--[tag]
// @attribute <ellipsoid@ellipsoid.get_blocks[<material>|...]>
// @returns dList(dLocation)
// @description
// Returns each block location within the dEllipsoid.
// Optionally, specify a list of materials to only return locations
// with that block type.
// -->
if (attribute.startsWith("get_blocks")) {
if (attribute.hasContext(1))
return new dList(getBlocks(dList.valueOf(attribute.getContext(1)).filter(dMaterial.class)))
.getAttribute(attribute.fulfill(1));
else
return new dList(getBlocks())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ellipsoid@ellipsoid.location>
// @returns dLocation
// @description
// Returns the location of the ellipsoid.
// -->
if (attribute.startsWith("location")) {
return loc.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ellipsoid@ellipsoid.size>
// @returns dLocation
// @description
// Returns the size of the ellipsoid.
// -->
if (attribute.startsWith("size")) {
return size.getAttribute(attribute.fulfill(1));
}
// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
if (returned != null) return returned;
}
return new Element(identify()).getAttribute(attribute);
}
}