-
Notifications
You must be signed in to change notification settings - Fork 0
/
CraftWorld.java
1706 lines (1447 loc) · 67.2 KB
/
CraftWorld.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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Futures;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.Validate;
import org.bukkit.BlockChangeDelegate;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Difficulty;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.TreeType;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.craftbukkit.block.CraftBlockState;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.metadata.BlockMetadataStore;
import org.bukkit.craftbukkit.potion.CraftPotionUtil;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.entity.minecart.CommandMinecart;
import org.bukkit.entity.minecart.ExplosiveMinecart;
import org.bukkit.entity.minecart.HopperMinecart;
import org.bukkit.entity.minecart.PoweredMinecart;
import org.bukkit.entity.minecart.SpawnerMinecart;
import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.world.SpawnChangeEvent;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.messaging.StandardMessenger;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import org.bukkit.util.Consumer;
import org.bukkit.util.Vector;
import net.minecraft.block.BlockChorusFlower;
import net.minecraft.block.BlockRedstoneDiode;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityAreaEffectCloud;
import net.minecraft.entity.EntityHanging;
import net.minecraft.entity.EntityLeashKnot;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.boss.EntityDragon;
import net.minecraft.entity.boss.EntityWither;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.item.EntityArmorStand;
import net.minecraft.entity.item.EntityBoat;
import net.minecraft.entity.item.EntityEnderCrystal;
import net.minecraft.entity.item.EntityEnderEye;
import net.minecraft.entity.item.EntityEnderPearl;
import net.minecraft.entity.item.EntityExpBottle;
import net.minecraft.entity.item.EntityFallingBlock;
import net.minecraft.entity.item.EntityFireworkRocket;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityItemFrame;
import net.minecraft.entity.item.EntityMinecartChest;
import net.minecraft.entity.item.EntityMinecartCommandBlock;
import net.minecraft.entity.item.EntityMinecartEmpty;
import net.minecraft.entity.item.EntityMinecartFurnace;
import net.minecraft.entity.item.EntityMinecartHopper;
import net.minecraft.entity.item.EntityMinecartMobSpawner;
import net.minecraft.entity.item.EntityMinecartTNT;
import net.minecraft.entity.item.EntityPainting;
import net.minecraft.entity.item.EntityTNTPrimed;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.monster.EntityCaveSpider;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityElderGuardian;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.monster.EntityEndermite;
import net.minecraft.entity.monster.EntityEvoker;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.entity.monster.EntityGiantZombie;
import net.minecraft.entity.monster.EntityGuardian;
import net.minecraft.entity.monster.EntityHusk;
import net.minecraft.entity.monster.EntityIllusionIllager;
import net.minecraft.entity.monster.EntityIronGolem;
import net.minecraft.entity.monster.EntityMagmaCube;
import net.minecraft.entity.monster.EntityPigZombie;
import net.minecraft.entity.monster.EntityPolarBear;
import net.minecraft.entity.monster.EntityShulker;
import net.minecraft.entity.monster.EntitySilverfish;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntitySlime;
import net.minecraft.entity.monster.EntitySnowman;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityStray;
import net.minecraft.entity.monster.EntityVex;
import net.minecraft.entity.monster.EntityVindicator;
import net.minecraft.entity.monster.EntityWitch;
import net.minecraft.entity.monster.EntityWitherSkeleton;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.monster.EntityZombieVillager;
import net.minecraft.entity.passive.EntityBat;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntityDonkey;
import net.minecraft.entity.passive.EntityHorse;
import net.minecraft.entity.passive.EntityLlama;
import net.minecraft.entity.passive.EntityMooshroom;
import net.minecraft.entity.passive.EntityMule;
import net.minecraft.entity.passive.EntityOcelot;
import net.minecraft.entity.passive.EntityParrot;
import net.minecraft.entity.passive.EntityPig;
import net.minecraft.entity.passive.EntityRabbit;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.passive.EntitySkeletonHorse;
import net.minecraft.entity.passive.EntitySquid;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.passive.EntityZombieHorse;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.entity.projectile.EntityDragonFireball;
import net.minecraft.entity.projectile.EntityEgg;
import net.minecraft.entity.projectile.EntityEvokerFangs;
import net.minecraft.entity.projectile.EntityFireball;
import net.minecraft.entity.projectile.EntityLargeFireball;
import net.minecraft.entity.projectile.EntityLlamaSpit;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.entity.projectile.EntityShulkerBullet;
import net.minecraft.entity.projectile.EntitySmallFireball;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.entity.projectile.EntitySpectralArrow;
import net.minecraft.entity.projectile.EntityTippedArrow;
import net.minecraft.entity.projectile.EntityWitherSkull;
import net.minecraft.init.Blocks;
import net.minecraft.network.play.server.SPacketCustomSound;
import net.minecraft.network.play.server.SPacketEffect;
import net.minecraft.network.play.server.SPacketTimeUpdate;
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.GameRules;
import net.minecraft.world.MinecraftException;
import net.minecraft.world.WorldProviderEnd;
import net.minecraft.world.WorldProviderHell;
import net.minecraft.world.WorldProviderSurface;
import net.minecraft.world.WorldServer;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraft.world.gen.feature.WorldGenBigTree;
import net.minecraft.world.gen.feature.WorldGenBirchTree;
import net.minecraft.world.gen.feature.WorldGenCanopyTree;
import net.minecraft.world.gen.feature.WorldGenMegaJungle;
import net.minecraft.world.gen.feature.WorldGenMegaPineTree;
import net.minecraft.world.gen.feature.WorldGenSavannaTree;
import net.minecraft.world.gen.feature.WorldGenShrub;
import net.minecraft.world.gen.feature.WorldGenSwamp;
import net.minecraft.world.gen.feature.WorldGenTaiga1;
import net.minecraft.world.gen.feature.WorldGenTaiga2;
import net.minecraft.world.gen.feature.WorldGenTrees;
import net.minecraft.world.storage.SaveHandler;
import org.bukkit.World.Environment;
public class CraftWorld implements World {
public static final int CUSTOM_DIMENSION_OFFSET = 10;
private final WorldServer world;
private WorldBorder worldBorder;
private Environment environment;
private final CraftServer server = (CraftServer) Bukkit.getServer();
private final ChunkGenerator generator;
private final List<BlockPopulator> populators = new ArrayList<BlockPopulator>();
private final BlockMetadataStore blockMetadata = new BlockMetadataStore(this);
private int monsterSpawn = -1;
private int animalSpawn = -1;
private int waterAnimalSpawn = -1;
private int ambientSpawn = -1;
private int chunkLoadCount = 0;
private int chunkGCTickCount;
private static final Random rand = new Random();
public CraftWorld(WorldServer world, ChunkGenerator gen, Environment env) {
this.world = world;
this.generator = gen;
environment = env;
if (server.chunkGCPeriod > 0) {
chunkGCTickCount = rand.nextInt(server.chunkGCPeriod);
}
}
public Block getBlockAt(int x, int y, int z) {
return CraftBlock.at(world, new BlockPos(x, y, z));
}
public int getHighestBlockYAt(int x, int z) {
if (!isChunkLoaded(x >> 4, z >> 4)) {
loadChunk(x >> 4, z >> 4);
}
return world.getHighestBlockYAt(HeightMap.Type.LIGHT_BLOCKING, new BlockPos(x, 0, z)).getY();
}
public Location getSpawnLocation() {
BlockPos spawn = world.getSpawn();
return new Location(this, spawn.getX(), spawn.getY(), spawn.getZ());
}
@Override
public boolean setSpawnLocation(Location location) {
Preconditions.checkArgument(location != null, "location");
return equals(location.getWorld()) ? setSpawnLocation(location.getBlockX(), location.getBlockY(), location.getBlockZ()) : false;
}
public boolean setSpawnLocation(int x, int y, int z) {
try {
Location previousLocation = getSpawnLocation();
world.worldInfo.setSpawn(new BlockPos(x, y, z));
// Notify anyone who's listening.
SpawnChangeEvent event = new SpawnChangeEvent(this, previousLocation);
server.getPluginManager().callEvent(event);
return true;
} catch (Exception e) {
return false;
}
}
public Chunk getChunkAt(int x, int z) {
return this.world.getChunkProviderServer().getChunkAt(x, z).bukkitChunk;
}
public Chunk getChunkAt(Block block) {
return getChunkAt(block.getX() >> 4, block.getZ() >> 4);
}
public boolean isChunkLoaded(int x, int z) {
return world.getChunkProviderServer().isLoaded(x, z);
}
public Chunk[] getLoadedChunks() {
Object[] chunks = world.getChunkProviderServer().chunks.values().toArray();
org.bukkit.Chunk[] craftChunks = new CraftChunk[chunks.length];
for (int i = 0; i < chunks.length; i++) {
minecraft.world.chunk.Chunk chunk = (minecraft.world.chunk.Chunk) chunks[i];
craftChunks[i] = chunk.bukkitChunk;
}
return craftChunks;
}
public void loadChunk(int x, int z) {
loadChunk(x, z, true);
}
public boolean unloadChunk(Chunk chunk) {
return unloadChunk(chunk.getX(), chunk.getZ());
}
public boolean unloadChunk(int x, int z) {
return unloadChunk(x, z, true);
}
public boolean unloadChunk(int x, int z, boolean save) {
return unloadChunk(x, z, save, false);
}
public boolean unloadChunkRequest(int x, int z) {
return unloadChunkRequest(x, z, true);
}
public boolean unloadChunkRequest(int x, int z, boolean safe) {
if (safe && isChunkInUse(x, z)) {
return false;
}
minecraft.world.chunk.Chunk chunk = world.getChunkProviderServer().getLoadedChunkAt(x, z);
if (chunk != null) {
world.getChunkProviderServer().unload(chunk);
}
return true;
}
public boolean unloadChunk(int x, int z, boolean save, boolean safe) {
if (isChunkInUse(x, z)) {
return false;
}
return unloadChunk0(x, z, save);
}
private boolean unloadChunk0(int x, int z, boolean save) {
minecraft.world.chunk.Chunk chunk = world.getChunkProviderServer().getChunkIfLoaded(x, z);
if (chunk == null) {
return true;
}
// If chunk had previously been queued to save, must do save to avoid loss of that data
return world.getChunkProviderServer().unloadChunk(chunk, chunk.mustSave || save);
}
public boolean regenerateChunk(int x, int z) {
if (!unloadChunk0(x, z, false)) {
return false;
}
final long chunkKey = ChunkPos.asLong(x, z);
world.getChunkProviderServer().unloadQueue.remove(chunkKey);
minecraft.world.chunk.Chunk chunk = null;
chunk = Futures.getUnchecked(world.getChunkProviderServer().generateChunk(x, z));
PlayerChunkMapEntry playerChunk = world.getPlayerChunkMap().getEntry(x, z);
if (playerChunk != null) {
playerChunk.chunk = chunk;
}
if (chunk != null) {
world.getChunkProviderServer().chunks.put(chunkKey, chunk);
chunk.onLoad();
refreshChunk(x, z);
}
return chunk != null;
}
public boolean refreshChunk(int x, int z) {
if (!isChunkLoaded(x, z)) {
return false;
}
int px = x << 4;
int pz = z << 4;
// If there are more than 64 updates to a chunk at once, it will update all 'touched' sections within the chunk
// And will include biome data if all sections have been 'touched'
// This flags 65 blocks distributed across all the sections of the chunk, so that everything is sent, including biomes
int height = getMaxHeight() / 16;
for (int idx = 0; idx < 64; idx++) {
world.notifyBlockUpdate(new BlockPos(px + (idx / height), ((idx % height) * 16), pz), Blocks.AIR.getDefaultState(), Blocks.STONE.getDefaultState(), 3);
}
world.notifyBlockUpdate(new BlockPos(px + 15, (height * 16) - 1, pz + 15), Blocks.AIR.getDefaultState(), Blocks.STONE.getDefaultState(), 3);
return true;
}
public boolean isChunkInUse(int x, int z) {
return world.getPlayerChunkMap().isChunkInUse(x, z);
}
public boolean loadChunk(int x, int z, boolean generate) {
chunkLoadCount++;
if (generate) {
// Use the default variant of loadChunk when generate == true.
return world.getChunkProviderServer().getChunkAt(x, z) != null;
}
return world.getChunkProviderServer().getOrLoadChunkAt(x, z) != null;
}
public boolean isChunkLoaded(Chunk chunk) {
return isChunkLoaded(chunk.getX(), chunk.getZ());
}
public void loadChunk(Chunk chunk) {
loadChunk(chunk.getX(), chunk.getZ());
((CraftChunk) getChunkAt(chunk.getX(), chunk.getZ())).getHandle().bukkitChunk = chunk;
}
public WorldServer getHandle() {
return world;
}
public org.bukkit.entity.Item dropItem(Location loc, ItemStack item) {
Validate.notNull(item, "Cannot drop a Null item.");
EntityItem entity = new EntityItem(world, loc.getX(), loc.getY(), loc.getZ(), CraftItemStack.asNMSCopy(item));
entity.pickupDelay = 10;
world.addEntity(entity, SpawnReason.CUSTOM);
// TODO this is inconsistent with how Entity.getBukkitEntity() works.
// However, this entity is not at the moment backed by a server entity class so it may be left.
return new CraftItem(world.getServer(), entity);
}
private static void randomLocationWithinBlock(Location loc, double xs, double ys, double zs) {
double prevX = loc.getX();
double prevY = loc.getY();
double prevZ = loc.getZ();
loc.add(xs, ys, zs);
if (loc.getX() < Math.floor(prevX)) {
loc.setX(Math.floor(prevX));
}
if (loc.getX() >= Math.ceil(prevX)) {
loc.setX(Math.ceil(prevX - 0.01));
}
if (loc.getY() < Math.floor(prevY)) {
loc.setY(Math.floor(prevY));
}
if (loc.getY() >= Math.ceil(prevY)) {
loc.setY(Math.ceil(prevY - 0.01));
}
if (loc.getZ() < Math.floor(prevZ)) {
loc.setZ(Math.floor(prevZ));
}
if (loc.getZ() >= Math.ceil(prevZ)) {
loc.setZ(Math.ceil(prevZ - 0.01));
}
}
public org.bukkit.entity.Item dropItemNaturally(Location loc, ItemStack item) {
double xs = world.rand.nextFloat() * 0.7F - 0.35D;
double ys = world.rand.nextFloat() * 0.7F - 0.35D;
double zs = world.rand.nextFloat() * 0.7F - 0.35D;
loc = loc.clone();
// Makes sure the new item is created within the block the location points to.
// This prevents item spill in 1-block wide farms.
randomLocationWithinBlock(loc, xs, ys, zs);
return dropItem(loc, item);
}
public Arrow spawnArrow(Location loc, Vector velocity, float speed, float spread) {
return spawnArrow(loc, velocity, speed, spread, Arrow.class);
}
public <T extends Arrow> T spawnArrow(Location loc, Vector velocity, float speed, float spread, Class<T> clazz) {
Validate.notNull(loc, "Can not spawn arrow with a null location");
Validate.notNull(velocity, "Can not spawn arrow with a null velocity");
Validate.notNull(clazz, "Can not spawn an arrow with no class");
EntityArrow arrow;
if (TippedArrow.class.isAssignableFrom(clazz)) {
arrow = new EntityTippedArrow(world);
((EntityTippedArrow) arrow).setType(CraftPotionUtil.fromBukkit(new PotionData(PotionType.WATER, false, false)));
} else if (SpectralArrow.class.isAssignableFrom(clazz)) {
arrow = new EntitySpectralArrow(world);
} else if (Trident.class.isAssignableFrom(clazz)){
arrow = new EntityThrownTrident(world);
} else {
arrow = new EntityTippedArrow(world);
}
arrow.setLocationAndAngles(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
arrow.shoot(velocity.getX(), velocity.getY(), velocity.getZ(), speed, spread);
world.addEntity(arrow);
return (T) arrow.getBukkitEntity();
}
public Entity spawnEntity(Location loc, EntityType entityType) {
return spawn(loc, entityType.getEntityClass());
}
public LightningStrike strikeLightning(Location loc) {
EntityLightningBolt lightning = new EntityLightningBolt(world, loc.getX(), loc.getY(), loc.getZ(), false);
world.addWeatherEffect(lightning);
return new CraftLightningStrike(server, lightning);
}
public LightningStrike strikeLightningEffect(Location loc) {
EntityLightningBolt lightning = new EntityLightningBolt(world, loc.getX(), loc.getY(), loc.getZ(), true);
world.addWeatherEffect(lightning);
return new CraftLightningStrike(server, lightning);
}
public boolean generateTree(Location loc, TreeType type) {
BlockPos pos = new BlockPos(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
world.gen.feature.WorldGenerator gen;
switch (type) {
case BIG_TREE:
gen = new WorldGenBigTree(true);
break;
case BIRCH:
gen = new WorldGenBirchTree(true, false);
break;
case REDWOOD:
gen = new WorldGenTaiga2(true);
break;
case TALL_REDWOOD:
gen = new WorldGenTaiga1();
break;
case JUNGLE:
gen = new WorldGenMegaJungle(true, 10, 20, Blocks.JUNGLE_LOG.getDefaultState(), Blocks.JUNGLE_LEAVES.getDefaultState());
break;
case SMALL_JUNGLE:
gen = new WorldGenTrees(true, 4 + rand.nextInt(7), Blocks.JUNGLE_LOG.getDefaultState(), Blocks.JUNGLE_LEAVES.getDefaultState(), false);
break;
case COCOA_TREE:
gen = new WorldGenMegaJungle(true, 10, 20, Blocks.JUNGLE_LOG.getDefaultState(), Blocks.JUNGLE_LEAVES.getDefaultState());
break;
case JUNGLE_BUSH:
gen = new WorldGenShrub(Blocks.JUNGLE_LOG.getDefaultState(), Blocks.OAK_LEAVES.getDefaultState());
break;
case RED_MUSHROOM:
gen = new WorldGenHugeMushroomRed();
break;
case BROWN_MUSHROOM:
gen = new WorldGenHugeMushroomBrown();
break;
case SWAMP:
gen = new WorldGenSwamp();
break;
case ACACIA:
gen = new WorldGenSavannaTree(true);
break;
case DARK_OAK:
gen = new WorldGenCanopyTree(true);
break;
case MEGA_REDWOOD:
gen = new WorldGenMegaPineTree(false, rand.nextBoolean());
break;
case TALL_BIRCH:
gen = new WorldGenBirchTree(true, true);
break;
case CHORUS_PLANT:
((BlockChorusFlower) Blocks.CHORUS_FLOWER).a(world, pos, rand, 8);
return true;
case TREE:
default:
gen = new WorldGenTrees(true);
break;
}
return gen.generate(world, world.provider.createChunkGenerator(), rand, pos, new WorldGenFeatureEmptyConfiguration());
}
public boolean generateTree(Location loc, TreeType type, BlockChangeDelegate delegate) {
world.captureTreeGeneration = true;
world.captureBlockStates = true;
boolean grownTree = generateTree(loc, type);
world.captureBlockStates = false;
world.captureTreeGeneration = false;
if (grownTree) { // Copy block data to delegate
for (BlockState blockstate : world.capturedBlockStates) {
int x = blockstate.getX();
int y = blockstate.getY();
int z = blockstate.getZ();
BlockPos position = new BlockPos(x, y, z);
minecraft.block.state.IBlockState oldBlock = world.getBlockState(position);
int flag = ((CraftBlockState) blockstate).getFlag();
delegate.setBlockData(x, y, z, blockstate.getBlockData());
minecraft.block.state.IBlockState newBlock = world.getBlockState(position);
world.notifyAndUpdatePhysics(position, null, oldBlock, newBlock, newBlock, flag);
}
world.capturedBlockStates.clear();
return true;
} else {
world.capturedBlockStates.clear();
return false;
}
}
public TileEntity getTileEntityAt(final int x, final int y, final int z) {
return world.getTileEntity(new BlockPos(x, y, z));
}
public String getName() {
return world.worldInfo.getWorldName();
}
@Deprecated
public long getId() {
return world.worldInfo.getSeed();
}
public UUID getUID() {
return world.getDataManager().getUUID();
}
@Override
public String toString() {
return "CraftWorld{name=" + getName() + '}';
}
public long getTime() {
long time = getFullTime() % 24000;
if (time < 0) time += 24000;
return time;
}
public void setTime(long time) {
long margin = (time - getFullTime()) % 24000;
if (margin < 0) margin += 24000;
setFullTime(getFullTime() + margin);
}
public long getFullTime() {
return world.getWorldTime();
}
public void setFullTime(long time) {
world.setWorldTime(time);
// Forces the client to update to the new time immediately
for (Player p : getPlayers()) {
CraftPlayer cp = (CraftPlayer) p;
if (cp.getHandle().connection == null) continue;
cp.getHandle().connection.sendPacket(new SPacketTimeUpdate(cp.getHandle().world.getTotalWorldTime(), cp.getHandle().getPlayerTime(), cp.getHandle().world.getGameRules().getBoolean("doDaylightCycle")));
}
}
public boolean createExplosion(double x, double y, double z, float power) {
return createExplosion(x, y, z, power, false, true);
}
public boolean createExplosion(double x, double y, double z, float power, boolean setFire) {
return createExplosion(x, y, z, power, setFire, true);
}
public boolean createExplosion(double x, double y, double z, float power, boolean setFire, boolean breakBlocks) {
return !world.newExplosion(null, x, y, z, power, setFire, breakBlocks).wasCanceled;
}
public boolean createExplosion(Location loc, float power) {
return createExplosion(loc, power, false);
}
public boolean createExplosion(Location loc, float power, boolean setFire) {
return createExplosion(loc.getX(), loc.getY(), loc.getZ(), power, setFire);
}
public Environment getEnvironment() {
return environment;
}
public void setEnvironment(Environment env) {
if (environment != env) {
environment = env;
switch (env) {
case NORMAL:
world.provider = new WorldProviderSurface();
break;
case NETHER:
world.provider = new WorldProviderHell();
break;
case THE_END:
world.provider = new WorldProviderEnd();
break;
}
}
}
public Block getBlockAt(Location location) {
return getBlockAt(location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
public int getHighestBlockYAt(Location location) {
return getHighestBlockYAt(location.getBlockX(), location.getBlockZ());
}
public Chunk getChunkAt(Location location) {
return getChunkAt(location.getBlockX() >> 4, location.getBlockZ() >> 4);
}
public ChunkGenerator getGenerator() {
return generator;
}
public List<BlockPopulator> getPopulators() {
return populators;
}
public Block getHighestBlockAt(int x, int z) {
return getBlockAt(x, getHighestBlockYAt(x, z), z);
}
public Block getHighestBlockAt(Location location) {
return getHighestBlockAt(location.getBlockX(), location.getBlockZ());
}
public Biome getBiome(int x, int z) {
return CraftBlock.biomeBaseToBiome(this.world.getBiome(new BlockPos(x, 0, z)));
}
public void setBiome(int x, int z, Biome bio) {
Biome bb = CraftBlock.biomeToBiomeBase(bio);
if (this.world.isLoaded(new BlockPos(x, 0, z))) {
minecraft.world.chunk.Chunk chunk = this.world.getChunkFromBlockCoords(new BlockPos(x, 0, z));
if (chunk != null) {
Biome[] biomevals = chunk.getBiomeIndex();
biomevals[((z & 0xF) << 4) | (x & 0xF)] = bb;
chunk.markDirty(); // SPIGOT-2890
}
}
}
public double getTemperature(int x, int z) {
return this.world.getBiome(new BlockPos(x, 0, z)).getDefaultTemperature();
}
public double getHumidity(int x, int z) {
return this.world.getBiome(new BlockPos(x, 0, z)).getRainfall();
}
public List<Entity> getEntities() {
List<Entity> list = new ArrayList<Entity>();
for (Object o : world.loadedEntityList) {
if (o instanceof net.minecraft.entity.Entity) {
net.minecraft.entity.Entity mcEnt = (net.minecraft.entity.Entity) o;
Entity bukkitEntity = mcEnt.getBukkitEntity();
// Assuming that bukkitEntity isn't null
if (bukkitEntity != null) {
list.add(bukkitEntity);
}
}
}
return list;
}
public List<LivingEntity> getLivingEntities() {
List<LivingEntity> list = new ArrayList<LivingEntity>();
for (Object o : world.loadedEntityList) {
if (o instanceof net.minecraft.entity.Entity) {
net.minecraft.entity.Entity mcEnt = (net.minecraft.entity.Entity) o;
Entity bukkitEntity = mcEnt.getBukkitEntity();
// Assuming that bukkitEntity isn't null
if (bukkitEntity != null && bukkitEntity instanceof LivingEntity) {
list.add((LivingEntity) bukkitEntity);
}
}
}
return list;
}
@SuppressWarnings("unchecked")
@Deprecated
public <T extends Entity> Collection<T> getEntitiesByClass(Class<T>... classes) {
return (Collection<T>)getEntitiesByClasses(classes);
}
@SuppressWarnings("unchecked")
public <T extends Entity> Collection<T> getEntitiesByClass(Class<T> clazz) {
Collection<T> list = new ArrayList<T>();
for (Object entity: world.loadedEntityList) {
if (entity instanceof net.minecraft.entity.Entity) {
Entity bukkitEntity = ((net.minecraft.entity.Entity) entity).getBukkitEntity();
if (bukkitEntity == null) {
continue;
}
Class<?> bukkitClass = bukkitEntity.getClass();
if (clazz.isAssignableFrom(bukkitClass)) {
list.add((T) bukkitEntity);
}
}
}
return list;
}
public Collection<Entity> getEntitiesByClasses(Class<?>... classes) {
Collection<Entity> list = new ArrayList<Entity>();
for (Object entity: world.loadedEntityList) {
if (entity instanceof net.minecraft.entity.Entity) {
Entity bukkitEntity = ((net.minecraft.entity.Entity) entity).getBukkitEntity();
if (bukkitEntity == null) {
continue;
}
Class<?> bukkitClass = bukkitEntity.getClass();
for (Class<?> clazz : classes) {
if (clazz.isAssignableFrom(bukkitClass)) {
list.add(bukkitEntity);
break;
}
}
}
}
return list;
}
@Override
public Collection<Entity> getNearbyEntities(Location location, double x, double y, double z) {
if (location == null || !location.getWorld().equals(this)) {
return Collections.emptyList();
}
AxisAlignedBB bb = new AxisAlignedBB(location.getX() - x, location.getY() - y, location.getZ() - z, location.getX() + x, location.getY() + y, location.getZ() + z);
List<net.minecraft.entity.Entity> entityList = getHandle().getEntities((net.minecraft.entity.Entity) null, bb, null);
List<Entity> bukkitEntityList = new ArrayList<org.bukkit.entity.Entity>(entityList.size());
for (Object entity : entityList) {
bukkitEntityList.add(((net.minecraft.entity.Entity) entity).getBukkitEntity());
}
return bukkitEntityList;
}
public List<Player> getPlayers() {
List<Player> list = new ArrayList<Player>(world.playerEntities.size());
for (EntityPlayer human : world.playerEntities) {
HumanEntity bukkitEntity = human.getBukkitEntity();
if ((bukkitEntity != null) && (bukkitEntity instanceof Player)) {
list.add((Player) bukkitEntity);
}
}
return list;
}
public void save() {
this.server.checkSaveState();
try {
boolean oldSave = world.disableLevelSaving;
world.disableLevelSaving = false;
world.saveAllChunks(true, null);
world.disableLevelSaving = oldSave;
} catch (MinecraftException ex) {
ex.printStackTrace();
}
}
public boolean isAutoSave() {
return !world.disableLevelSaving;
}
public void setAutoSave(boolean value) {
world.disableLevelSaving = !value;
}
public void setDifficulty(Difficulty difficulty) {
this.getHandle().worldInfo.setDifficulty(EnumDifficulty.getDifficultyEnum(difficulty.getValue()));
}
public Difficulty getDifficulty() {
return Difficulty.getByValue(this.getHandle().getDifficulty().ordinal());
}
public BlockMetadataStore getBlockMetadata() {
return blockMetadata;
}
public boolean hasStorm() {
return world.worldInfo.isRaining();
}
public void setStorm(boolean hasStorm) {
world.worldInfo.setRaining(hasStorm);
setWeatherDuration(0); // Reset weather duration (legacy behaviour)
}
public int getWeatherDuration() {
return world.worldInfo.getRainTime();
}
public void setWeatherDuration(int duration) {
world.worldInfo.setRainTime(duration);
}
public boolean isThundering() {
return world.worldInfo.isThundering();
}
public void setThundering(boolean thundering) {
world.worldInfo.setThundering(thundering);
setThunderDuration(0); // Reset weather duration (legacy behaviour)
}
public int getThunderDuration() {
return world.worldInfo.getThunderTime();
}
public void setThunderDuration(int duration) {
world.worldInfo.setThunderTime(duration);
}
public long getSeed() {
return world.worldInfo.getSeed();
}
public boolean getPVP() {
return world.pvpMode;
}
public void setPVP(boolean pvp) {
world.pvpMode = pvp;
}
public void playEffect(Player player, Effect effect, int data) {
playEffect(player.getLocation(), effect, data, 0);
}
public void playEffect(Location location, Effect effect, int data) {
playEffect(location, effect, data, 64);
}
public <T> void playEffect(Location loc, Effect effect, T data) {
playEffect(loc, effect, data, 64);
}
public <T> void playEffect(Location loc, Effect effect, T data, int radius) {
if (data != null) {
Validate.isTrue(effect.getData() != null && effect.getData().isAssignableFrom(data.getClass()), "Wrong kind of data for this effect!");
} else {
Validate.isTrue(effect.getData() == null, "Wrong kind of data for this effect!");
}
int datavalue = data == null ? 0 : CraftEffect.getDataValue(effect, data);
playEffect(loc, effect, datavalue, radius);
}
public void playEffect(Location location, Effect effect, int data, int radius) {
Validate.notNull(location, "Location cannot be null");
Validate.notNull(effect, "Effect cannot be null");
Validate.notNull(location.getWorld(), "World cannot be null");
int packetData = effect.getId();
SPacketEffect packet = new SPacketEffect(packetData, new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), data, false);
int distance;
radius *= radius;
for (Player player : getPlayers()) {
if (((CraftPlayer) player).getHandle().connection == null) continue;
if (!location.getWorld().equals(player.getWorld())) continue;
distance = (int) player.getLocation().distanceSquared(location);
if (distance <= radius) {
((CraftPlayer) player).getHandle().connection.sendPacket(packet);
}
}
}
public <T extends Entity> T spawn(Location location, Class<T> clazz) throws IllegalArgumentException {
return spawn(location, clazz, null, SpawnReason.CUSTOM);
}
@Override
public <T extends Entity> T spawn(Location location, Class<T> clazz, Consumer<T> function) throws IllegalArgumentException {
return spawn(location, clazz, function, SpawnReason.CUSTOM);
}
@Override
public FallingBlock spawnFallingBlock(Location location, MaterialData data) throws IllegalArgumentException {
Validate.notNull(data, "MaterialData cannot be null");
return spawnFallingBlock(location, data.getItemType(), data.getData());
}
public FallingBlock spawnFallingBlock(Location location, org.bukkit.Material material, byte data) throws IllegalArgumentException {
Validate.notNull(location, "Location cannot be null");
Validate.notNull(material, "Material cannot be null");
Validate.isTrue(material.isBlock(), "Material must be a block");
EntityFallingBlock entity = new EntityFallingBlock(world, location.getX(), location.getY(), location.getZ(), CraftMagicNumbers.getBlock(material).getDefaultState());
entity.fallTime = 1;
world.addEntity(entity, SpawnReason.CUSTOM);
return (FallingBlock) entity.getBukkitEntity();
}
@Override