summaryrefslogtreecommitdiffstats
path: root/nms-patches/WorldServer.patch
blob: 394e4bab73952562c2591ae25c51168789aaaed4 (plain)
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
--- a/net/minecraft/server/WorldServer.java
+++ b/net/minecraft/server/WorldServer.java
@@ -16,14 +16,27 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+import java.util.*;
+import java.util.logging.Level;
+
+import org.bukkit.WeatherType;
+import org.bukkit.block.BlockState;
+import org.bukkit.craftbukkit.util.LongHash;
+import org.bukkit.craftbukkit.util.HashTreeSet;
+
+import org.bukkit.event.block.BlockFormEvent;
+import org.bukkit.event.weather.LightningStrikeEvent;
+// CraftBukkit end
+
 public class WorldServer extends World implements IAsyncTaskHandler {
 
     private static final Logger a = LogManager.getLogger();
     private final MinecraftServer server;
     public EntityTracker tracker;
     private final PlayerChunkMap manager;
-    private final Set<NextTickListEntry> L = Sets.newHashSet();
-    private final TreeSet<NextTickListEntry> M = new TreeSet();
+    // private final Set<NextTickListEntry> L = Sets.newHashSet(); // PAIL: Rename nextTickListHash
+    private final HashTreeSet<NextTickListEntry> M = new HashTreeSet<NextTickListEntry>(); // CraftBukkit - HashTreeSet // PAIL: Rename nextTickList
     private final Map<UUID, Entity> entitiesByUUID = Maps.newHashMap();
     public ChunkProviderServer chunkProviderServer;
     public boolean savingDisabled;
@@ -37,14 +50,22 @@
     private static final List<StructurePieceTreasure> U = Lists.newArrayList(new StructurePieceTreasure[] { new StructurePieceTreasure(Items.STICK, 0, 1, 3, 10), new StructurePieceTreasure(Item.getItemOf(Blocks.PLANKS), 0, 1, 3, 10), new StructurePieceTreasure(Item.getItemOf(Blocks.LOG), 0, 1, 3, 10), new StructurePieceTreasure(Items.STONE_AXE, 0, 1, 1, 3), new StructurePieceTreasure(Items.WOODEN_AXE, 0, 1, 1, 5), new StructurePieceTreasure(Items.STONE_PICKAXE, 0, 1, 1, 3), new StructurePieceTreasure(Items.WOODEN_PICKAXE, 0, 1, 1, 5), new StructurePieceTreasure(Items.APPLE, 0, 2, 3, 5), new StructurePieceTreasure(Items.BREAD, 0, 2, 3, 3), new StructurePieceTreasure(Item.getItemOf(Blocks.LOG2), 0, 1, 3, 10)});
     private List<NextTickListEntry> V = Lists.newArrayList();
 
-    public WorldServer(MinecraftServer minecraftserver, IDataManager idatamanager, WorldData worlddata, int i, MethodProfiler methodprofiler) {
-        super(idatamanager, worlddata, WorldProvider.byDimension(i), methodprofiler, false);
+    // CraftBukkit start
+    public final int dimension;
+
+    // Add env and gen to constructor
+    public WorldServer(MinecraftServer minecraftserver, IDataManager idatamanager, WorldData worlddata, int i, MethodProfiler methodprofiler, org.bukkit.World.Environment env, org.bukkit.generator.ChunkGenerator gen) {
+        super(idatamanager, worlddata, WorldProvider.byDimension(env.getId()), methodprofiler, false, gen, env);
+        this.dimension = i;
+        this.pvpMode = minecraftserver.getPVP();
+        worlddata.world = this;
+        // CraftBukkit end
         this.server = minecraftserver;
         this.tracker = new EntityTracker(this);
         this.manager = new PlayerChunkMap(this);
         this.worldProvider.a(this);
         this.chunkProvider = this.k();
-        this.Q = new PortalTravelAgent(this);
+        this.Q = new org.bukkit.craftbukkit.CraftTravelAgent(this); // CraftBukkit
         this.B();
         this.C();
         this.getWorldBorder().a(minecraftserver.aI());
@@ -63,6 +84,7 @@
             this.villages.a((World) this);
         }
 
+        if (getServer().getScoreboardManager() == null) { // CraftBukkit
         this.scoreboard = new ScoreboardServer(this.server);
         PersistentScoreboard persistentscoreboard = (PersistentScoreboard) this.worldMaps.get(PersistentScoreboard.class, "scoreboard");
 
@@ -73,6 +95,11 @@
 
         persistentscoreboard.a(this.scoreboard);
         ((ScoreboardServer) this.scoreboard).a(persistentscoreboard);
+        // CraftBukkit start
+        } else {
+            this.scoreboard = getServer().getScoreboardManager().getMainScoreboard().getHandle();
+        }
+        // CraftBukkit end
         this.getWorldBorder().setCenter(this.worldData.C(), this.worldData.D());
         this.getWorldBorder().setDamageAmount(this.worldData.I());
         this.getWorldBorder().setDamageBuffer(this.worldData.H());
@@ -84,9 +111,98 @@
             this.getWorldBorder().setSize(this.worldData.E());
         }
 
+        // CraftBukkit start
+        if (generator != null) {
+            getWorld().getPopulators().addAll(generator.getDefaultPopulators(getWorld()));
+        }
+        // CraftBukkit end
+
         return this;
     }
 
+    // CraftBukkit start
+    @Override
+    public TileEntity getTileEntity(BlockPosition pos) {
+        TileEntity result = super.getTileEntity(pos);
+        Block type = getType(pos).getBlock();
+
+        if (type == Blocks.CHEST) {
+            if (!(result instanceof TileEntityChest)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.FURNACE) {
+            if (!(result instanceof TileEntityFurnace)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.DROPPER) {
+            if (!(result instanceof TileEntityDropper)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.DISPENSER) {
+            if (!(result instanceof TileEntityDispenser)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.JUKEBOX) {
+            if (!(result instanceof BlockJukeBox.TileEntityRecordPlayer)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.NOTEBLOCK) {
+            if (!(result instanceof TileEntityNote)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.MOB_SPAWNER) {
+            if (!(result instanceof TileEntityMobSpawner)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if ((type == Blocks.STANDING_SIGN) || (type == Blocks.WALL_SIGN)) {
+            if (!(result instanceof TileEntitySign)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.ENDER_CHEST) {
+            if (!(result instanceof TileEntityEnderChest)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.BREWING_STAND) {
+            if (!(result instanceof TileEntityBrewingStand)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.BEACON) {
+            if (!(result instanceof TileEntityBeacon)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.HOPPER) {
+            if (!(result instanceof TileEntityHopper)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        }
+
+        return result;
+    }
+
+    private TileEntity fixTileEntity(BlockPosition pos, Block type, TileEntity found) {
+        this.getServer().getLogger().log(Level.SEVERE, "Block at {0},{1},{2} is {3} but has {4}" + ". "
+                + "Bukkit will attempt to fix this, but there may be additional damage that we cannot recover.", new Object[]{pos.getX(), pos.getY(), pos.getZ(), org.bukkit.Material.getMaterial(Block.getId(type)).toString(), found});
+
+        if (type instanceof IContainer) {
+            TileEntity replacement = ((IContainer) type).a(this, type.toLegacyData(this.getType(pos)));
+            replacement.world = this;
+            this.setTileEntity(pos, replacement);
+            return replacement;
+        } else {
+            this.getServer().getLogger().severe("Don't know how to fix for this type... Can't do anything! :(");
+            return found;
+        }
+    }
+
+    private boolean canSpawn(int x, int z) {
+        if (this.generator != null) {
+            return this.generator.canSpawn(this.getWorld(), x, z);
+        } else {
+            return this.worldProvider.canSpawn(x, z);
+        }
+    }
+    // CraftBukkit end
+
     public void doTick() {
         super.doTick();
         if (this.getWorldData().isHardcore() && this.getDifficulty() != EnumDifficulty.HARD) {
@@ -104,9 +220,11 @@
             this.e();
         }
 
-        this.methodProfiler.a("mobSpawner");
-        if (this.getGameRules().getBoolean("doMobSpawning") && this.worldData.getType() != WorldType.DEBUG_ALL_BLOCK_STATES) {
-            this.R.a(this, this.allowMonsters, this.allowAnimals, this.worldData.getTime() % 400L == 0L);
+        // CraftBukkit start - Only call spawner if we have players online and the world allows for mobs or animals
+        long time = this.worldData.getTime();
+        if (this.getGameRules().getBoolean("doMobSpawning") && this.worldData.getType() != WorldType.DEBUG_ALL_BLOCK_STATES && (this.allowMonsters || this.allowAnimals) && (this instanceof WorldServer && this.players.size() > 0)) {
+            this.R.a(this, this.allowMonsters && (this.ticksPerMonsterSpawns != 0 && time % this.ticksPerMonsterSpawns == 0L), this.allowAnimals && (this.ticksPerAnimalSpawns != 0 && time % this.ticksPerAnimalSpawns == 0L), this.worldData.getTime() % 400L == 0L);
+            // CraftBukkit end
         }
 
         this.methodProfiler.c("chunkSource");
@@ -135,6 +253,8 @@
         this.Q.a(this.getTime());
         this.methodProfiler.b();
         this.ak();
+
+        this.getWorld().processChunkGC(); // CraftBukkit
     }
 
     public BiomeBase.BiomeMeta a(EnumCreatureType enumcreaturetype, BlockPosition blockposition) {
@@ -161,7 +281,7 @@
 
                 if (entityhuman.isSpectator()) {
                     ++i;
-                } else if (entityhuman.isSleeping()) {
+                } else if (entityhuman.isSleeping() || entityhuman.fauxSleeping) {
                     ++j;
                 }
             }
@@ -187,25 +307,46 @@
     }
 
     private void ag() {
-        this.worldData.setWeatherDuration(0);
         this.worldData.setStorm(false);
-        this.worldData.setThunderDuration(0);
+        // CraftBukkit start
+        // If we stop due to everyone sleeping we should reset the weather duration to some other random value.
+        // Not that everyone ever manages to get the whole server to sleep at the same time....
+        if (!this.worldData.hasStorm()) {
+            this.worldData.setWeatherDuration(0);
+        }
+        // CraftBukkit end
         this.worldData.setThundering(false);
+        // CraftBukkit start
+        // If we stop due to everyone sleeping we should reset the weather duration to some other random value.
+        // Not that everyone ever manages to get the whole server to sleep at the same time....
+        if (!this.worldData.isThundering()) {
+            this.worldData.setThunderDuration(0);
+        }
+        // CraftBukkit end
     }
 
     public boolean everyoneDeeplySleeping() {
         if (this.O && !this.isClientSide) {
             Iterator iterator = this.players.iterator();
 
+            // CraftBukkit - This allows us to assume that some people are in bed but not really, allowing time to pass in spite of AFKers
+            boolean foundActualSleepers = false;
+
             EntityHuman entityhuman;
 
             do {
                 if (!iterator.hasNext()) {
-                    return true;
+                    return foundActualSleepers;
                 }
 
                 entityhuman = (EntityHuman) iterator.next();
-            } while (!entityhuman.isSpectator() && entityhuman.isDeeplySleeping());
+
+                // CraftBukkit start
+                if (entityhuman.isDeeplySleeping()) {
+                    foundActualSleepers = true;
+                }
+            } while (!entityhuman.isSpectator() || entityhuman.isDeeplySleeping() || entityhuman.fauxSleeping);
+            // CraftBukkit end
 
             return false;
         } else {
@@ -228,13 +369,20 @@
             int i = 0;
             int j = 0;
 
-            for (Iterator iterator1 = this.chunkTickList.iterator(); iterator1.hasNext(); this.methodProfiler.b()) {
-                ChunkCoordIntPair chunkcoordintpair1 = (ChunkCoordIntPair) iterator1.next();
-                int k = chunkcoordintpair1.x * 16;
-                int l = chunkcoordintpair1.z * 16;
+            // CraftBukkit start
+            //for (Iterator iterator1 = this.chunkTickList.iterator(); iterator1.hasNext(); this.methodProfiler.b()) {
+            //    ChunkCoordIntPair chunkcoordintpair1 = (ChunkCoordIntPair) iterator1.next();
+            //    int k = chunkcoordintpair1.x * 16;
+            //    int l = chunkcoordintpair1.z * 16;
+            for (long chunkCoord : chunkTickList.popAll()) {
+                int chunkX = LongHash.msw(chunkCoord);
+                int chunkZ = LongHash.lsw(chunkCoord);
+                int k = chunkX * 16;
+                int l = chunkZ * 16;
 
                 this.methodProfiler.a("getChunk");
-                Chunk chunk = this.getChunkAt(chunkcoordintpair1.x, chunkcoordintpair1.z);
+                Chunk chunk = this.getChunkAt(chunkX, chunkZ);
+                // CraftBukkit end
 
                 this.a(k, l, chunk);
                 this.methodProfiler.c("tickChunk");
@@ -260,11 +408,29 @@
                     BlockPosition blockposition1 = blockposition.down();
 
                     if (this.w(blockposition1)) {
-                        this.setTypeUpdate(blockposition1, Blocks.ICE.getBlockData());
+                        // CraftBukkit start
+                        BlockState blockState = this.getWorld().getBlockAt(blockposition1.getX(), blockposition1.getY(), blockposition1.getZ()).getState();
+                        blockState.setTypeId(Block.getId(Blocks.ICE));
+
+                        BlockFormEvent iceBlockForm = new BlockFormEvent(blockState.getBlock(), blockState);
+                        this.getServer().getPluginManager().callEvent(iceBlockForm);
+                        if (!iceBlockForm.isCancelled()) {
+                            blockState.update(true);
+                        }
+                        // CraftBukkit end
                     }
 
                     if (this.S() && this.f(blockposition, true)) {
-                        this.setTypeUpdate(blockposition, Blocks.SNOW_LAYER.getBlockData());
+                        // CraftBukkit start
+                        BlockState blockState = this.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ()).getState();
+                        blockState.setTypeId(Block.getId(Blocks.SNOW_LAYER));
+
+                        BlockFormEvent snow = new BlockFormEvent(blockState.getBlock(), blockState);
+                        this.getServer().getPluginManager().callEvent(snow);
+                        if (!snow.isCancelled()) {
+                            blockState.update(true);
+                        }
+                        // CraftBukkit end
                     }
 
                     if (this.S() && this.getBiome(blockposition1).e()) {
@@ -359,8 +525,8 @@
                 nextticklistentry.a(j);
             }
 
-            if (!this.L.contains(nextticklistentry)) {
-                this.L.add(nextticklistentry);
+            // CraftBukkit - use M, PAIL: Rename nextTickList
+            if (!this.M.contains(nextticklistentry)) {
                 this.M.add(nextticklistentry);
             }
         }
@@ -375,15 +541,15 @@
             nextticklistentry.a((long) i + this.worldData.getTime());
         }
 
-        if (!this.L.contains(nextticklistentry)) {
-            this.L.add(nextticklistentry);
+        // CraftBukkit - use M, PAIL: Rename nextTickList
+        if (!this.M.contains(nextticklistentry)) {
             this.M.add(nextticklistentry);
         }
 
     }
 
     public void tickEntities() {
-        if (this.players.isEmpty()) {
+        if (false && this.players.isEmpty()) { // CraftBukkit - this prevents entity cleanup, other issues on servers with no players
             if (this.emptyTime++ >= 1200) {
                 return;
             }
@@ -404,11 +570,17 @@
         } else {
             int i = this.M.size();
 
-            if (i != this.L.size()) {
+            if (false) { // CraftBukkit
                 throw new IllegalStateException("TickNextTick list out of synch");
             } else {
                 if (i > 1000) {
-                    i = 1000;
+                    // CraftBukkit start - If the server has too much to process over time, try to alleviate that
+                    if (i > 20 * 1000) {
+                        i = i / 20;
+                    } else {
+                        i = 1000;
+                    }
+                    // CraftBukkit end
                 }
 
                 this.methodProfiler.a("cleaning");
@@ -421,8 +593,8 @@
                         break;
                     }
 
+                    // CraftBukkit - use M, PAIL: Rename nextTickList
                     this.M.remove(nextticklistentry);
-                    this.L.remove(nextticklistentry);
                     this.V.add(nextticklistentry);
                 }
 
@@ -489,7 +661,7 @@
 
                 if (blockposition.getX() >= structureboundingbox.a && blockposition.getX() < structureboundingbox.d && blockposition.getZ() >= structureboundingbox.c && blockposition.getZ() < structureboundingbox.f) {
                     if (flag) {
-                        this.L.remove(nextticklistentry);
+                        // CraftBukkit - use M
                         iterator.remove();
                     }
 
@@ -505,6 +677,7 @@
         return arraylist;
     }
 
+    /* CraftBukkit start - We prevent spawning in general, so this butchering is not needed
     public void entityJoinedWorld(Entity entity, boolean flag) {
         if (!this.getSpawnAnimals() && (entity instanceof EntityAnimal || entity instanceof EntityWaterAnimal)) {
             entity.die();
@@ -516,6 +689,7 @@
 
         super.entityJoinedWorld(entity, flag);
     }
+    // CraftBukkit end */
 
     private boolean getSpawnNPCs() {
         return this.server.getSpawnNPCs();
@@ -528,13 +702,43 @@
     protected IChunkProvider k() {
         IChunkLoader ichunkloader = this.dataManager.createChunkLoader(this.worldProvider);
 
-        this.chunkProviderServer = new ChunkProviderServer(this, ichunkloader, this.worldProvider.getChunkProvider());
+        // CraftBukkit start
+        org.bukkit.craftbukkit.generator.InternalChunkGenerator gen;
+
+        if (this.generator != null) {
+            gen = new org.bukkit.craftbukkit.generator.CustomChunkGenerator(this, this.getSeed(), this.generator);
+        } else if (this.worldProvider instanceof WorldProviderHell) {
+            gen = new org.bukkit.craftbukkit.generator.NetherChunkGenerator(this, this.getSeed());
+        } else if (this.worldProvider instanceof WorldProviderTheEnd) {
+            gen = new org.bukkit.craftbukkit.generator.SkyLandsChunkGenerator(this, this.getSeed());
+        } else {
+            gen = new org.bukkit.craftbukkit.generator.NormalChunkGenerator(this, this.getSeed());
+        }
+
+        this.chunkProviderServer = new ChunkProviderServer(this, ichunkloader, gen);
+        // CraftBukkit end
         return this.chunkProviderServer;
     }
 
     public List<TileEntity> getTileEntities(int i, int j, int k, int l, int i1, int j1) {
         ArrayList arraylist = Lists.newArrayList();
 
+        // CraftBukkit start - Get tile entities from chunks instead of world
+        for (int chunkX = (i >> 4); chunkX <= ((l - 1) >> 4); chunkX++) {
+            for (int chunkZ = (k >> 4); chunkZ <= ((j1 - 1) >> 4); chunkZ++) {
+                Chunk chunk = getChunkAt(chunkX, chunkZ);
+                if (chunk == null) {
+                    continue;
+                }
+                for (Object te : chunk.tileEntities.values()) {
+                    TileEntity tileentity = (TileEntity) te;
+                    if ((tileentity.position.getX() >= i) && (tileentity.position.getY() >= j) && (tileentity.position.getZ() >= k) && (tileentity.position.getX() < l) && (tileentity.position.getY() < i1) && (tileentity.position.getZ() < j1)) {
+                        arraylist.add(tileentity);
+                    }
+                }
+            }
+        }
+        /*
         for (int k1 = 0; k1 < this.h.size(); ++k1) {
             TileEntity tileentity = (TileEntity) this.h.get(k1);
             BlockPosition blockposition = tileentity.getPosition();
@@ -543,6 +747,8 @@
                 arraylist.add(tileentity);
             }
         }
+        */
+        // CraftBukkit end
 
         return arraylist;
     }
@@ -606,6 +812,23 @@
             int j = this.worldProvider.getSeaLevel();
             int k = 0;
 
+            // CraftBukkit start
+            if (this.generator != null) {
+                Random rand = new Random(this.getSeed());
+                org.bukkit.Location spawn = this.generator.getFixedSpawnLocation(((WorldServer) this).getWorld(), rand);
+
+                if (spawn != null) {
+                    if (spawn.getWorld() != ((WorldServer) this).getWorld()) {
+                        throw new IllegalStateException("Cannot set spawn point for " + this.worldData.getName() + " to be in another world (" + spawn.getWorld().getName() + ")");
+                    } else {
+                        this.worldData.setSpawn(new BlockPosition(spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ()));
+                        this.isLoading = false;
+                        return;
+                    }
+                }
+            }
+            // CraftBukkit end
+
             if (blockposition != null) {
                 i = blockposition.getX();
                 k = blockposition.getZ();
@@ -615,7 +838,7 @@
 
             int l = 0;
 
-            while (!this.worldProvider.canSpawn(i, k)) {
+            while (!this.canSpawn(i, k)) { // CraftBukkit - use our own canSpawn
                 i += random.nextInt(64) - random.nextInt(64);
                 k += random.nextInt(64) - random.nextInt(64);
                 ++l;
@@ -654,6 +877,7 @@
 
     public void save(boolean flag, IProgressUpdate iprogressupdate) throws ExceptionWorldConflict {
         if (this.chunkProvider.canSave()) {
+            org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit
             if (iprogressupdate != null) {
                 iprogressupdate.a("Saving level");
             }
@@ -664,7 +888,8 @@
             }
 
             this.chunkProvider.saveChunks(flag, iprogressupdate);
-            ArrayList arraylist = Lists.newArrayList(this.chunkProviderServer.a());
+            // CraftBukkit - ArrayList -> Collection
+            Collection arraylist = this.chunkProviderServer.a();
             Iterator iterator = arraylist.iterator();
 
             while (iterator.hasNext()) {
@@ -695,12 +920,29 @@
         this.worldData.k(this.getWorldBorder().getWarningTime());
         this.worldData.b(this.getWorldBorder().j());
         this.worldData.e(this.getWorldBorder().i());
+        // CraftBukkit start - save worldMaps once, rather than once per shared world
+        if (!(this instanceof SecondaryWorldServer)) {
+            this.worldMaps.a();
+        }
         this.dataManager.saveWorldData(this.worldData, this.server.getPlayerList().t());
-        this.worldMaps.a();
+        // CraftBukkit end
     }
 
     protected void a(Entity entity) {
         super.a(entity);
+        // CraftBukkit start
+        UUID uuid = entity.getUniqueID();
+
+        if (this.entitiesByUUID.containsKey(uuid)) {
+            Entity entity1 = (Entity) this.entitiesByUUID.get(uuid);
+
+            if (this.g.contains(entity1)) {
+                WorldServer.a.warn("Tried to add entity " + EntityTypes.b(entity1) + " with pending removal and duplicate UUID " + uuid.toString());
+            } else {
+                WorldServer.a.warn("Tried to add entity " + EntityTypes.b(entity1) + " that already exists with UUID " + uuid.toString());
+            }
+        }
+        // CraftBukkit end
         this.entitiesById.a(entity.getId(), entity);
         this.entitiesByUUID.put(entity.getUniqueID(), entity);
         Entity[] aentity = entity.aB();
@@ -728,8 +970,16 @@
     }
 
     public boolean strikeLightning(Entity entity) {
+        // CraftBukkit start
+        LightningStrikeEvent lightning = new LightningStrikeEvent(this.getWorld(), (org.bukkit.entity.LightningStrike) entity.getBukkitEntity());
+        this.getServer().getPluginManager().callEvent(lightning);
+
+        if (lightning.isCancelled()) {
+            return false;
+        }
         if (super.strikeLightning(entity)) {
-            this.server.getPlayerList().sendPacketNearby(entity.locX, entity.locY, entity.locZ, 512.0D, this.worldProvider.getDimension(), new PacketPlayOutSpawnEntityWeather(entity));
+            this.server.getPlayerList().sendPacketNearby(entity.locX, entity.locY, entity.locZ, 512.0D, dimension, new PacketPlayOutSpawnEntityWeather(entity));
+            // CraftBukkit end
             return true;
         } else {
             return false;
@@ -741,10 +991,20 @@
     }
 
     public Explosion createExplosion(Entity entity, double d0, double d1, double d2, float f, boolean flag, boolean flag1) {
+        // CraftBukkit start
+        Explosion explosion = super.createExplosion(entity, d0, d1, d2, f, flag, flag1);
+
+        if (explosion.wasCanceled) {
+            return explosion;
+        }
+
+        /* Remove
         Explosion explosion = new Explosion(this, entity, d0, d1, d2, f, flag, flag1);
 
         explosion.a();
         explosion.a(false);
+        */
+        // CraftBukkit end - TODO: Check if explosions are still properly implemented
         if (!flag1) {
             explosion.clearBlocks();
         }
@@ -790,7 +1050,8 @@
                 BlockActionData blockactiondata = (BlockActionData) iterator.next();
 
                 if (this.a(blockactiondata)) {
-                    this.server.getPlayerList().sendPacketNearby((double) blockactiondata.a().getX(), (double) blockactiondata.a().getY(), (double) blockactiondata.a().getZ(), 64.0D, this.worldProvider.getDimension(), new PacketPlayOutBlockAction(blockactiondata.a(), blockactiondata.d(), blockactiondata.b(), blockactiondata.c()));
+                    // CraftBukkit - this.worldProvider.dimension -> this.dimension
+                    this.server.getPlayerList().sendPacketNearby((double) blockactiondata.a().getX(), (double) blockactiondata.a().getY(), (double) blockactiondata.a().getZ(), 64.0D, dimension, new PacketPlayOutBlockAction(blockactiondata.a(), blockactiondata.d(), blockactiondata.b(), blockactiondata.c()));
                 }
             }
 
@@ -813,6 +1074,7 @@
         boolean flag = this.S();
 
         super.p();
+        /* CraftBukkit start
         if (this.o != this.p) {
             this.server.getPlayerList().a(new PacketPlayOutGameStateChange(7, this.p), this.worldProvider.getDimension());
         }
@@ -831,6 +1093,21 @@
             this.server.getPlayerList().sendAll(new PacketPlayOutGameStateChange(7, this.p));
             this.server.getPlayerList().sendAll(new PacketPlayOutGameStateChange(8, this.r));
         }
+        // */
+        if (flag != this.S()) {
+            // Only send weather packets to those affected
+            for (int i = 0; i < this.players.size(); ++i) {
+                if (((EntityPlayer) this.players.get(i)).world == this) {
+                    ((EntityPlayer) this.players.get(i)).setPlayerWeather((!flag ? WeatherType.DOWNFALL : WeatherType.CLEAR), false);
+                }
+            }
+        }
+        for (int i = 0; i < this.players.size(); ++i) {
+            if (((EntityPlayer) this.players.get(i)).world == this) {
+                ((EntityPlayer) this.players.get(i)).updateWeather(this.o, this.p, this.q, this.r);
+            }
+        }
+        // CraftBukkit end
 
     }
 
@@ -859,10 +1136,17 @@
     }
 
     public void a(EnumParticle enumparticle, boolean flag, double d0, double d1, double d2, int i, double d3, double d4, double d5, double d6, int... aint) {
+        // CraftBukkit - visibility api support
+        sendParticles(null, enumparticle, flag, d0, d1, d2, i, d3, d4, d5, d6, aint);
+    }
+
+    public void sendParticles(EntityPlayer sender, EnumParticle enumparticle, boolean flag, double d0, double d1, double d2, int i, double d3, double d4, double d5, double d6, int... aint) {
+        // CraftBukkit end
         PacketPlayOutWorldParticles packetplayoutworldparticles = new PacketPlayOutWorldParticles(enumparticle, flag, (float) d0, (float) d1, (float) d2, (float) d3, (float) d4, (float) d5, (float) d6, i, aint);
 
         for (int j = 0; j < this.players.size(); ++j) {
             EntityPlayer entityplayer = (EntityPlayer) this.players.get(j);
+            if (sender != null && !entityplayer.getBukkitEntity().canSee(sender.getBukkitEntity())) continue; // CraftBukkit
             BlockPosition blockposition = entityplayer.getChunkCoordinates();
             double d7 = blockposition.c(d0, d1, d2);