/
dWorld.java
506 lines (411 loc) · 15.3 KB
/
dWorld.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
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_6_R3.CraftChunk;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class dWorld implements dObject {
/////////////////////
// STATIC METHODS
/////////////////
static Map<String, dWorld> worlds = new HashMap<String, dWorld>();
public static dWorld mirrorBukkitWorld(World world) {
if (world == null) return null;
if (worlds.containsKey(world.getName())) return worlds.get(world.getName());
else return new dWorld(world);
}
/////////////////////
// OBJECT FETCHER
/////////////////
// <--[language]
// @name w@
// @description
// w@ refers to the 'object identifier' of a dWorld. The 'w@' is notation for Denizen's Object
// Fetcher. The only valid constructor for a dWorld is the name of the world it should be
// associated with. For example, to reference the world named 'world1', use w@world1.
// World names are case insensitive.
// -->
@ObjectFetcher("w")
public static dWorld valueOf(String string) {
if (string == null) return null;
string = string.replace("w@", "");
////////
// Match world name
World returnable = null;
for (World world : Bukkit.getWorlds())
if (world.getName().equalsIgnoreCase(string))
returnable = world;
if (returnable != null) {
if (worlds.containsKey(returnable.getName()))
return worlds.get(returnable.getName());
else return new dWorld(returnable);
}
else dB.echoError("Invalid World! '" + string
+ "' could not be found.");
return null;
}
public static boolean matches(String arg) {
arg = arg.replace("w@", "");
World returnable = null;
for (World world : Bukkit.getWorlds())
if (world.getName().equalsIgnoreCase(arg))
returnable = world;
return returnable != null;
}
public World getWorld() {
return Bukkit.getWorld(world_name);
}
public String getName() {
return world_name;
}
public List<Entity> getEntities() {
return getWorld().getEntities();
}
private String prefix;
String world_name;
public dWorld(World world) {
this(null, world);
}
public dWorld(String prefix, World world) {
if (prefix == null) this.prefix = "World";
else this.prefix = prefix;
this.world_name = world.getName();
if (!worlds.containsKey(world.getName()))
worlds.put(world.getName(), this);
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String getObjectType() {
return "World";
}
@Override
public String identify() {
return "w@" + world_name;
}
@Override
public String toString() {
return identify();
}
@Override
public dObject setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;
/////////////////////
// DEBUG ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <w@world.prefix>
// @returns Element
// @description
// Returns the prefix of the world dObject.
// -->
if (attribute.startsWith("prefix"))
return new Element(prefix)
.getAttribute(attribute.fulfill(1));
/////////////////////
// ENTITY LIST ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <w@world.entities>
// @returns dList(dEntity)
// @description
// Returns a list of entities in this world.
// -->
if (attribute.startsWith("entities")) {
ArrayList<dEntity> entities = new ArrayList<dEntity>();
for (Entity entity : getWorld().getEntities()) {
entities.add(new dEntity(entity));
}
return new dList(entities)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <w@world.living_entities>
// @returns dList(dEntity)
// @description
// Returns a list of living entities in this world.
// -->
if (attribute.startsWith("living_entities")) {
ArrayList<dEntity> entities = new ArrayList<dEntity>();
for (Entity entity : getWorld().getLivingEntities()) {
entities.add(new dEntity(entity));
}
return new dList(entities)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <w@world.players>
// @returns dList(dPlayer)
// @description
// Returns a list of online players in this world.
// -->
if (attribute.startsWith("players")) {
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (Player player : getWorld().getPlayers()) {
if (!CitizensAPI.getNPCRegistry().isNPC(player))
players.add(new dPlayer(player));
}
return new dList(players)
.getAttribute(attribute.fulfill(1));
}
/////////////////////
// GEOGRAPHY ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <w@world.random_loaded_chunk>
// @returns dChunk
// @description
// returns a random loaded chunk.
// -->
if (attribute.startsWith("random_loaded_chunk")) {
int random = Utilities.getRandom().nextInt(getWorld().getLoadedChunks().length);
return new dChunk((CraftChunk) getWorld().getLoadedChunks()[random])
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <w@world.rloaded_chunks>
// @returns dList(dChunk)
// @description
// returns a list of all the currently loaded chunks.
// -->
if (attribute.startsWith("loaded_chunks")) {
dList chunks = new dList();
for (Chunk ent : this.getWorld().getLoadedChunks())
chunks.add(new dChunk((CraftChunk) ent).identify());
return chunks.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <w@world.sea_level>
// @returns Element(Number)
// @description
// returns the level of the sea.
// -->
if (attribute.startsWith("sea_level"))
return new Element(getWorld().getSeaLevel())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.spawn_location>
// @returns Element(Number)
// @description
// returns the spawn location of the world.
// -->
if (attribute.startsWith("spawn_location"))
return new dLocation(getWorld().getSpawnLocation())
.getAttribute(attribute.fulfill(1));
/////////////////////
// IDENTIFICATION ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <w@world.name>
// @returns Element
// @description
// returns the name of the world.
// -->
if (attribute.startsWith("name"))
return new Element(getWorld().getName())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.seed>
// @returns Element
// @description
// returns the world seed.
// -->
if (attribute.startsWith("seed"))
return new Element(getWorld().getSeed())
.getAttribute(attribute.fulfill(1));
/////////////////////
// SETTINGS ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <w@world.allows_animals>
// @returns Element(boolean)
// @description
// Returns whether animals can spawn in this world.
// -->
if (attribute.startsWith("allows_animals"))
return new Element(getWorld().getAllowAnimals())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.allows_monsters>
// @returns Element(boolean)
// @description
// Returns whether monsters can spawn in this world.
// -->
if (attribute.startsWith("allows_monsters"))
return new Element(getWorld().getAllowMonsters())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.allows_pvp>
// @returns Element(boolean)
// @description
// Returns whether player versus player combat is allowed in this world.
// -->
if (attribute.startsWith("allows_pvp"))
return new Element(getWorld().getPVP())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.ambient_spawn_limit>
// @returns Element(Number)
// @description
// Returns the number of ambient mobs that can spawn in a chunk in this world.
// -->
if (attribute.startsWith("ambient_spawn_limit"))
return new Element(getWorld().getAmbientSpawnLimit())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.animal_spawn_limit>
// @returns Element(Number)
// @description
// Returns the number of animals that can spawn in a chunk in this world.
// -->
if (attribute.startsWith("animal_spawn_limit"))
return new Element(getWorld().getAnimalSpawnLimit())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.can_generate_structures>
// @returns Element(boolean)
// @description
// Returns whether the world will generate structures.
// -->
if (attribute.startsWith("can_generate_structures"))
return new Element(getWorld().canGenerateStructures())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.difficulty>
// @returns Element
// @description
// returns the name of the difficulty level.
// -->
if (attribute.startsWith("difficulty"))
return new Element(getWorld().getDifficulty().name())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.max_height>
// @returns Element(Number)
// @description
// Returns the maximum height of this world.
// -->
if (attribute.startsWith("max_height"))
return new Element(getWorld().getMaxHeight())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.monster_spawn_limit>
// @returns Element(Number)
// @description
// Returns the number of monsters that can spawn in a chunk in this world.
// -->
if (attribute.startsWith("monster_spawn_limit"))
return new Element(getWorld().getMonsterSpawnLimit())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.ticks_per_animal_spawn>
// @returns Element(Number)
// @description
// Returns the world's ticks per animal spawn value.
// -->
if (attribute.startsWith("ticks_per_animal_spawn"))
return new Element(getWorld().getTicksPerAnimalSpawns() )
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.ticks_per_monster_spawn>
// @returns Element(Number)
// @description
// Returns the world's ticks per monster spawn value.
// -->
if (attribute.startsWith("ticks_per_monster_spawn"))
return new Element(getWorld().getTicksPerMonsterSpawns() )
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.water_animal_spawn_limit>
// @returns Element(Number)
// @description
// Returns the number of water animals that can spawn in a chunk in this world.
// -->
if (attribute.startsWith("water_animal_spawn_limit"))
return new Element(getWorld().getWaterAnimalSpawnLimit())
.getAttribute(attribute.fulfill(1));
/////////////////////
// TIME ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <w@world.time.period>
// @returns Element
// @description
// returns the time as 'day', 'night', 'dawn', or 'dusk'.
// -->
if (attribute.startsWith("time.period")) {
long time = getWorld().getTime();
String period;
if (time >= 23000) period = "dawn";
else if (time >= 13500) period = "night";
else if (time >= 12500) period = "dusk";
else period = "day";
return new Element(period).getAttribute(attribute.fulfill(2));
}
// <--[tag]
// @attribute <w@world.time>
// @returns Element(Number)
// @description
// returns the current time in ticks.
// -->
if (attribute.startsWith("time"))
return new Element(getWorld().getTime())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.moon_phase>
// @returns Element(Number)
// @description
// returns the current phase of the moon, as an integer from 1 to 8.
// -->
if (attribute.startsWith("moon_phase")
|| attribute.startsWith("moonphase"))
return new Element((int)((getWorld().getFullTime() / 24000) % 8) + 1)
.getAttribute(attribute.fulfill(1));
/////////////////////
// WEATHER ATTRIBUTES
/////////////////
// <--[tag]
// @attribute <w@world.has_storm>
// @returns Element(boolean)
// @description
// returns whether there is currently a storm in this world.
// -->
if (attribute.startsWith("has_storm"))
return new Element(getWorld().hasStorm())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.weather_duration>
// @returns Element
// @description
// Returns the duration of storms.
// -->
if (attribute.startsWith("weather_duration"))
return Duration.valueOf(getWorld().getWeatherDuration() + "t")
.getAttribute(attribute.fulfill(1));
return new Element(identify()).getAttribute(attribute);
}
}