/
dWorld.java
801 lines (679 loc) · 25.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
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
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.Difficulty;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_7_R1.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, Adjustable {
/////////////////////
// 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@
// @group Object Fetcher System
// @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.
// -->
@Fetchable("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.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.loaded_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.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.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 dLocation
// @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.auto_save>
// @returns Element(Boolean)
// @description
// Returns whether the world automatically saves.
// -->
if (attribute.startsWith("auto_save"))
return new Element(getWorld().isAutoSave())
.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.keep_spawn>
// @returns Element(Boolean)
// @description
// Returns whether the world's spawn area should be kept loaded into memory.
// -->
if (attribute.startsWith("keep_spawn"))
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 Duration
// @description
// Returns the world's ticks per animal spawn value.
// -->
if (attribute.startsWith("ticks_per_animal_spawn"))
return new Duration(getWorld().getTicksPerAnimalSpawns() )
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.ticks_per_monster_spawn>
// @returns Duration
// @description
// Returns the world's ticks per monster spawn value.
// -->
if (attribute.startsWith("ticks_per_monster_spawn"))
return new Duration(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.full>
// @returns Duration
// @description
// Returns the in-game time of this world.
if (attribute.startsWith("time.full"))
return new Element(getWorld().getFullTime())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.time>
// @returns Element(Number)
// @description
// Returns the relative in-game time of this world.
// -->
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.thunder_duration>
// @returns Duration
// @description
// Returns the duration of thunder.
// -->
if (attribute.startsWith("thunder_duration"))
return new Duration((long) getWorld().getThunderDuration())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.thundering>
// @returns Element(Boolean)
// @description
// Returns whether it is currently thundering in this world.
if (attribute.startsWith("thundering"))
return new Element(getWorld().isThundering())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <w@world.weather_duration>
// @returns Duration
// @description
// Returns the duration of storms.
// -->
if (attribute.startsWith("weather_duration"))
return new Duration((long) getWorld().getWeatherDuration())
.getAttribute(attribute.fulfill(1));
return new Element(identify()).getAttribute(attribute);
}
@Override
public void adjust(Mechanism mechanism) {
Element value = mechanism.getValue();
// <--[mechanism]
// @object dWorld
// @name ambient_spawn_limit
// @input Element(Integer)
// @description
// Sets the limit for number of ambient mobs that can spawn in a chunk in this world.
// @tags
// <w@world.ambient_spawn_limit>
// -->
if (mechanism.matches("ambient_spawn_limit")
&& mechanism.requireInteger("Invalid spawn limit specified.")) {
getWorld().setAmbientSpawnLimit(value.asInt());
}
// <--[mechanism]
// @object dWorld
// @name animal_spawn_limit
// @input Element(Integer)
// @description
// Sets the limit for number of animals that can spawn in a chunk in this world.
// @tags
// <w@world.animal_spawn_limit>
// -->
if (mechanism.matches("animal_spawn_limit")
&& mechanism.requireInteger("Invalid spawn limit specified.")) {
getWorld().setAnimalSpawnLimit(value.asInt());
}
// <--[mechanism]
// @object dWorld
// @name auto_save
// @input Element(Integer)
// @description
// Sets the limit for number of animals that can spawn in a chunk in this world.
// @tags
// <w@world.animal_spawn_limit>
// -->
if (mechanism.matches("auto_save")
&& mechanism.requireBoolean("Invalid boolean. Must specify TRUE or FALSE.")) {
getWorld().setAutoSave(value.asBoolean());
}
// <--[mechanism]
// @object dWorld
// @name difficulty
// @input Element
// @description
// Sets the limit for number of animals that can spawn in a chunk in this world.
// @tags
// <w@world.difficulty>
// -->
if (mechanism.matches("difficulty") && mechanism.requireEnum(true, Difficulty.values())) {
String upper = value.asString().toUpperCase();
Difficulty diff = null;
if (upper.matches("(PEACEFUL|EASY|NORMAL|HARD)")) {
diff = Difficulty.valueOf(upper);
}
else {
diff = Difficulty.getByValue(value.asInt());
}
if (diff != null)
getWorld().setDifficulty(diff);
}
// <--[mechanism]
// @object dWorld
// @name full_time
// @input Element(Integer)
// @description
// Sets the in-game time on the server.
// @tags
// <w@world.time.full>
// -->
if (mechanism.matches("full_time") && mechanism.requireInteger()) {
getWorld().setFullTime(value.asInt());
}
// <--[mechanism]
// @object dWorld
// @name keep_spawn
// @input Element(Boolean)
// @description
// Sets whether the world's spawn area should be kept loaded into memory.
// @tags
// <w@world.time.full>
// -->
if (mechanism.matches("keep_spawn") && mechanism.requireBoolean()) {
getWorld().setKeepSpawnInMemory(value.asBoolean());
}
// <--[mechanism]
// @object dWorld
// @name monster_spawn_limit
// @input Element(Integer)
// @description
// Sets the limit for number of monsters that can spawn in a chunk in this world.
// @tags
// <w@world.monster_spawn_limit>
// -->
if (mechanism.matches("monster_spawn_limit") && mechanism.requireInteger()) {
getWorld().setMonsterSpawnLimit(value.asInt());
}
// <--[mechanism]
// @object dWorld
// @name allow_pvp
// @input Element(Boolean)
// @description
// Sets whether player versus player combat is allowed in this world.
// @tags
// <w@world.allows_pvp>
// -->
if (mechanism.matches("allow_pvp") && mechanism.requireBoolean()) {
getWorld().setPVP(value.asBoolean());
}
// <--[mechanism]
// @object dWorld
// @name spawn_location
// @input dLocation
// @description
// Sets the spawn location of this world. (This ignores the world value of the dLocation.)
// @tags
// <w@world.spawn_location>
// -->
if (mechanism.matches("spawn_location") && mechanism.requireObject(dLocation.class)) {
dLocation loc = value.asType(dLocation.class);
getWorld().setSpawnLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
// <--[mechanism]
// @object dWorld
// @name storming
// @input Element(Boolean)
// @description
// Sets whether there is a storm.
// @tags
// <w@world.has_storm>
// -->
if (mechanism.matches("storming") && mechanism.requireBoolean()) {
getWorld().setStorm(value.asBoolean());
}
// <--[mechanism]
// @object dWorld
// @name thunder_duration
// @input Duration
// @description
// Sets the duration of thunder.
// @tags
// <w@world.thunder_duration>
// -->
if (mechanism.matches("thunder_duration") && mechanism.requireObject(Duration.class)) {
getWorld().setThunderDuration(value.asType(Duration.class).getTicksAsInt());
}
// <--[mechanism]
// @object dWorld
// @name thundering
// @input Element(Boolean)
// @description
// Sets whether it is thundering.
// @tags
// <w@world.thundering>
// -->
if (mechanism.matches("thundering") && mechanism.requireBoolean()) {
getWorld().setThundering(value.asBoolean());
}
// <--[mechanism]
// @object dWorld
// @name ticks_per_animal_spawns
// @input Duration
// @description
// Sets the time between animal spawns.
// @tags
// <w@world.ticks_per_animal_spawns>
// -->
if (mechanism.matches("ticks_per_animal_spawns") && mechanism.requireObject(Duration.class)) {
getWorld().setTicksPerAnimalSpawns(value.asType(Duration.class).getTicksAsInt());
}
// <--[mechanism]
// @object dWorld
// @name ticks_per_monster_spawns
// @input Duration
// @description
// Sets the time between monster spawns.
// @tags
// <w@world.ticks_per_monster_spawns>
// -->
if (mechanism.matches("ticks_per_monster_spawns") && mechanism.requireObject(Duration.class)) {
getWorld().setTicksPerMonsterSpawns(value.asType(Duration.class).getTicksAsInt());
}
// <--[mechanism]
// @object dWorld
// @name time
// @input Element(Integer)
// @description
// Sets the relative in-game time on the server.
// @tags
// <w@world.time>
// -->
if (mechanism.matches("time") && mechanism.requireInteger()) {
getWorld().setTime(value.asInt());
}
// <--[mechanism]
// @object dWorld
// @name water_animal_spawn_limit
// @input Element(Integer)
// @description
// Sets the limit for number of water animals that can spawn in a chunk in this world.
// @tags
// <w@world.water_animal_spawn_limit>
// -->
if (mechanism.matches("water_animal_spawn_limit") && mechanism.requireInteger()) {
getWorld().setWaterAnimalSpawnLimit(value.asInt());
}
// <--[mechanism]
// @object dWorld
// @name weather_duration
// @input Duration
// @description
// Set the remaining time in ticks of the current conditions.
// @tags
// <w@world.weather_duration>
// -->
if (mechanism.matches("weather_duration") && mechanism.requireObject(Duration.class)) {
getWorld().setWeatherDuration(value.asType(Duration.class).getTicksAsInt());
}
if (!mechanism.fulfilled())
dB.echoError("Invalid mechanism specified: " + mechanism.getName());
}
}