-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
dLocation.java
347 lines (313 loc) · 12.7 KB
/
dLocation.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
package net.aufdemrand.denizen.utilities.arguments;
import net.aufdemrand.denizen.interfaces.dScriptArgument;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class dLocation extends org.bukkit.Location implements dScriptArgument {
public static Map<String, dLocation> locations = new ConcurrentHashMap<String, dLocation>(8, 0.9f, 1);
/**
* Gets a saved location based on an Id.
*
* @param id the Id key of the location
* @return the Location associated
*/
public static dLocation getSavedLocation(String id) {
return locations.get(id.toLowerCase());
}
public static String isSavedLocation(org.bukkit.Location location) {
for (Map.Entry<String, dLocation> entry : locations.entrySet()) {
if (Utilities.checkLocation(entry.getValue(), location, 1)) {
return entry.getKey();
}
}
return null;
}
/**
* Checks if there is a saved location with this Id.
*
* @param id the Id to check
* @return true if it exists, false if not
*/
public static boolean isSavedLocation(String id) {
return locations.containsKey(id.toLowerCase());
}
/**
* Called on server startup or /denizen reload locations. Should probably not be called manually.
*/
public static void _recallLocations() {
List<String> loclist = DenizenAPI.getCurrentInstance().getSaves().getStringList("dScript.Locations");
locations.clear();
for (String location : loclist) {
dLocation loc = valueOf(location);
// TODO: Finish this
}
}
/**
* Called by Denizen internally on a server shutdown or /denizen save. Should probably
* not be called manually.
*/
public static void _saveLocations() {
List<String> loclist = new ArrayList<String>();
for (Map.Entry<String, dLocation> entry : locations.entrySet())
loclist.add(entry.getValue().asString());
DenizenAPI.getCurrentInstance().getSaves().set("dScript.Locations", loclist);
}
/**
* 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
*
*/
public static dLocation valueOf(String string) {
if (string == null) return null;
// Strip prefix (ie. location:...)
if (string.split(":").length > 1)
string = string.split(":", 2)[1];
// Split values
String[] split = string.split(",");
// If 5 values, contains an id
if (split.length == 5)
try {
return new dLocation(split[0], Bukkit.getWorld(split[4]),
Double.valueOf(split[1]),
Double.valueOf(split[2]),
Double.valueOf(split[3]));
} catch(Exception e) {
return null;
}
// If 4 values, standard id-less dScript location format
else if (split.length == 4) {
try {
return new dLocation(Bukkit.getWorld(split[3]),
Double.valueOf(split[0]),
Double.valueOf(split[1]),
Double.valueOf(split[2]));
} catch(Exception e) {
return null;
}
}
return null;
}
private String Id;
private String prefix = "Location";
/**
* Creates a new saved Location. dLocations should only be given an 'Id'
* if they should be saved for later. dLocations with an 'Id' are persisted
* and can be recalled at any time with {@link #getSavedLocation(String)}
*
* @param id saved Id of the Location
* @param location the Bukkit Location to reference
*/
public dLocation(String id, org.bukkit.Location location) {
super(location.getWorld(), location.getX(), location.getY(), location.getZ());
this.Id = id.toLowerCase();
locations.put(Id, this);
dB.echoDebug(locations.toString());
}
/**
* Turns a Bukkit Location into a Location, which has some helpful methods
* for working with dScript. If working with temporary locations, this is
* a much better method to use than {@link #dLocation(String, org.bukkit.Location)}.
*
* @param location the Bukkit Location to reference
*/
public dLocation(org.bukkit.Location location) {
super(location.getWorld(), location.getX(), location.getY(), location.getZ());
}
/**
* Creates a new saved Location. dLocations should only be given an 'Id'
* if they should be saved for later. dLocations with an 'Id' are persisted
* and can be recalled at any time with {@link #getSavedLocation(String)}
*
* @param id saved Id of the Location
* @param world the Bukkit World referenced
* @param x the x-coordinate of the location
* @param y the y-coordinate of the location
* @param z the z-coordinate of the location
*
*/
public dLocation(String id, World world, double x, double y, double z) {
super(world, x, y, z);
this.Id = id.toLowerCase();
locations.put(Id, this);
}
/**
* Turns a world and coordinates into a Location, which has some helpful methods
* for working with dScript. If working with temporary locations, this is
* a much better method to use than {@link #dLocation(String, org.bukkit.World, double, double, double)}.
*
* @param world the world in which the location resides
* @param x x-coordinate of the location
* @param y y-coordinate of the location
* @param z z-coordinate of the location
*
*/
public dLocation(World world, double x, double y, double z) {
super(world, x, y, z);
}
public dLocation(World world, double x, double y, double z, float yaw, float pitch) {
super(world, x, y, z, yaw, pitch);
}
@Override
public String getDefaultPrefix() {
return prefix;
}
@Override
public String debug() {
return (Id != null ? "<G>" + prefix + "='<A>" + Id + "(<Y>" + getX() + "," + getY()
+ "," + getZ() + "," + getWorld().getName() + "<A>)<G>' "
: "<G>" + prefix + "='<Y>" + getX() + "," + getY()
+ "," + getZ() + "," + getWorld().getName() + "<G>' ");
}
@Override
public String as_dScriptArg() {
return getX() + "," + getY()
+ "," + getZ() + "," + getWorld().getName();
}
public String dScriptArgValue() {
return getDefaultPrefix().toLowerCase() + ":" + as_dScriptArg();
}
public String asString() {
if (Id == null) return null;
return Id + "," + getX() + "," + getY()
+ "," + getZ() + "," + getWorld().getName();
}
@Override
public dScriptArgument setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;
if (attribute.startsWith("biome.formatted"))
return new Element(getBlock().getBiome().name().toLowerCase().replace('_', ' '))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("biome.humidity"))
return new Element(String.valueOf(getBlock().getHumidity()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("biome.temperature"))
return new Element(String.valueOf(getBlock().getTemperature()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("biome"))
return new Element(String.valueOf(getBlock().getBiome().name()))
.getAttribute(attribute.fulfill(1));
// else if (type.equals("BLOCK"))
// {
// if (subType.equals("BELOW"))
// {
// fromLocation = new Location(fromLocation.add(0, -1, 0));
// }
//
// else if (subType.equals("MATERIAL") || specifier.equals("MATERIAL"))
// {
// event.setReplaced(fromLocation.getBlock().getType().toString());
// }
// }
//
// else if (type.equals("DIRECTION"))
// {
// if (fromLocation != null && toLocation != null)
// {
// event.setReplaced(Utilities.getCardinal(Utilities.getYaw
// (toLocation.toVector().subtract
// (fromLocation.toVector()).normalize())));
// }
// }
//
// else if (type.equals("DISTANCE"))
// {
// if (fromLocation != null && toLocation != null)
// {
// if (subType.equals("ASINT"))
// {
// event.setReplaced(String.valueOf((int)fromLocation.distance(toLocation)));
// }
// else if (subType.equals("VERTICAL"))
// {
// if (fromLocation.getWorld().getName() == toLocation.getWorld().getName()
// || specifier.equals("MULTIWORLD"))
// {
// // Only calculate distance between locations on different worlds
// // if the MULTIWORLD specifier is used
// event.setReplaced(String.valueOf(Math.abs(
// fromLocation.getY() - toLocation.getY())));
// }
// }
// else if (subType.equals("HORIZONTAL"))
// {
// if (fromLocation.getWorld().getName() == toLocation.getWorld().getName()
// || specifier.equals("MULTIWORLD"))
// {
// // Only calculate distance between locations on different worlds
// // if the MULTIWORLD specifier is used
// event.setReplaced(String.valueOf(Math.sqrt(
// Math.pow(fromLocation.getX() - toLocation.getX(), 2) +
// Math.pow(fromLocation.getZ() - toLocation.getZ(), 2))));
// }
// }
// else
// event.setReplaced(String.valueOf(fromLocation.distance(toLocation)));
// }
// }
//
// else if (type.equals("FORMATTED"))
// event.setReplaced("X '" + fromLocation.getX()
// + "', Y '" + fromLocation.getY()
// + "', Z '" + fromLocation.getZ()
// + "', in world '" + fromLocation.getWorld().getName() + "'");
//
// else if (type.equals("IS_LIQUID"))
// {
// event.setReplaced(String.valueOf(fromLocation.getBlock().isLiquid()));
// }
//
// else if (type.equals("LIGHT"))
// {
// if (subType.equals("BLOCKS"))
// event.setReplaced(String.valueOf((int) fromLocation.getBlock().getLightFromBlocks()));
// else if (subType.equals("SKY"))
// event.setReplaced(String.valueOf((int) fromLocation.getBlock().getLightFromSky()));
// else
// event.setReplaced(String.valueOf((int) fromLocation.getBlock().getLightLevel()));
// }
//
// else if (type.equals("POWER"))
// {
// event.setReplaced(String.valueOf((int) fromLocation.getBlock().getBlockPower()));
// }
//
// else if (type.equals("TIME"))
// {
// if (subType.equals("PERIOD"))
// if (fromLocation.getWorld().getTime() < 13500 ||
// fromLocation.getWorld().getTime() > 23000)
// event.setReplaced("day");
// else if (fromLocation.getWorld().getTime() > 13500)
// event.setReplaced("night");
// }
//
// else if (type.equals("WORLD"))
// event.setReplaced(fromLocation.getWorld().getName());
//
// else if (type.equals("X"))
// event.setReplaced(String.valueOf(fromLocation.getX()));
//
// else if (type.equals("Y"))
// event.setReplaced(String.valueOf(fromLocation.getY()));
//
// else if (type.equals("Z"))
// event.setReplaced(String.valueOf(fromLocation.getZ()));
return dScriptArgValue();
}
}