summaryrefslogtreecommitdiffstats
path: root/nms-patches/WorldServer.patch
blob: bde040ab1886771f5f7e59d61486f6b41b2d2c32 (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
--- a/net/minecraft/server/WorldServer.java
+++ b/net/minecraft/server/WorldServer.java
@@ -21,6 +21,18 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+import java.util.logging.Level;
+
+import org.bukkit.WeatherType;
+import org.bukkit.block.BlockState;
+import org.bukkit.craftbukkit.util.HashTreeSet;
+
+import org.bukkit.event.block.BlockFormEvent;
+import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
+import org.bukkit.event.weather.LightningStrikeEvent;
+// CraftBukkit end
+
 public class WorldServer extends World implements IAsyncTaskHandler {
 
     private static final Logger a = LogManager.getLogger();
@@ -39,28 +51,36 @@
     ObjectLinkedOpenHashSet<BlockActionData> d;
     private boolean P;
 
-    public WorldServer(MinecraftServer minecraftserver, IDataManager idatamanager, PersistentCollection persistentcollection, WorldData worlddata, DimensionManager dimensionmanager, MethodProfiler methodprofiler) {
-        super(idatamanager, persistentcollection, worlddata, dimensionmanager.e(), methodprofiler, false);
-        Predicate predicate = (block) -> {
+    // CraftBukkit start
+    public final DimensionManager dimension;
+
+    // Add env and gen to constructor
+    public WorldServer(MinecraftServer minecraftserver, IDataManager idatamanager, PersistentCollection persistentcollection, WorldData worlddata, DimensionManager dimensionmanager, MethodProfiler methodprofiler, org.bukkit.World.Environment env, org.bukkit.generator.ChunkGenerator gen) {
+        super(idatamanager, persistentcollection, worlddata, DimensionManager.a(env.getId()).e(), methodprofiler, false, gen, env);
+        this.dimension = dimensionmanager;
+        this.pvpMode = minecraftserver.getPVP();
+        worlddata.world = this;
+        // CraftBukkit end
+        Predicate<Block> predicate = (block) -> { // CraftBukkit - decompile error
             return block == null || block.getBlockData().isAir();
         };
         IRegistry iregistry = IRegistry.BLOCK;
 
         IRegistry.BLOCK.getClass();
         Function function = iregistry::getKey;
-        IRegistry iregistry1 = IRegistry.BLOCK;
+        IRegistry<Block> iregistry1 = IRegistry.BLOCK; // CraftBukkit - decompile error
 
         IRegistry.BLOCK.getClass();
-        this.nextTickListBlock = new TickListServer(this, predicate, function, iregistry1::getOrDefault, this::b);
-        predicate = (fluidtype) -> {
+        this.nextTickListBlock = new TickListServer<>(this, predicate, function, iregistry1::getOrDefault, this::b); // CraftBukkit - decompile error
+        Predicate<FluidType> predicate2 = (fluidtype) -> { // CraftBukkit - decompile error
             return fluidtype == null || fluidtype == FluidTypes.a;
         };
         iregistry = IRegistry.FLUID;
         IRegistry.FLUID.getClass();
         function = iregistry::getKey;
-        iregistry1 = IRegistry.FLUID;
+        IRegistry<FluidType> iregistry2 = IRegistry.FLUID; // CraftBukkit - decompile error
         IRegistry.FLUID.getClass();
-        this.nextTickListFluid = new TickListServer(this, predicate, function, iregistry1::getOrDefault, this::a);
+        this.nextTickListFluid = new TickListServer<>(this, predicate2, function, iregistry2::getOrDefault, this::a); // CraftBukkit - decompile error
         this.siegeManager = new VillageSiege(this);
         this.d = new ObjectLinkedOpenHashSet();
         this.server = minecraftserver;
@@ -68,7 +88,7 @@
         this.manager = new PlayerChunkMap(this);
         this.worldProvider.a((World) this);
         this.chunkProvider = this.r();
-        this.portalTravelAgent = new PortalTravelAgent(this);
+        this.portalTravelAgent = new org.bukkit.craftbukkit.CraftTravelAgent(this); // CraftBukkit
         this.P();
         this.Q();
         this.getWorldBorder().a(minecraftserver.au());
@@ -86,6 +106,7 @@
             this.villages.a((World) this);
         }
 
+        if (getServer().getScoreboardManager() == null) { // CraftBukkit
         PersistentScoreboard persistentscoreboard = (PersistentScoreboard) this.a(DimensionManager.OVERWORLD, PersistentScoreboard::new, "scoreboard");
 
         if (persistentscoreboard == null) {
@@ -95,6 +116,7 @@
 
         persistentscoreboard.a((Scoreboard) this.server.getScoreboard());
         this.server.getScoreboard().a((Runnable) (new RunnableSaveScoreboard(persistentscoreboard)));
+        } // CraftBukkit
         this.getWorldBorder().setCenter(this.worldData.B(), this.worldData.C());
         this.getWorldBorder().setDamageAmount(this.worldData.H());
         this.getWorldBorder().setDamageBuffer(this.worldData.G());
@@ -106,9 +128,130 @@
             this.getWorldBorder().setSize(this.worldData.D());
         }
 
+        // 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 TileEntityJukeBox)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.SPAWNER) {
+            if (!(result instanceof TileEntityMobSpawner)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if ((type == Blocks.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);
+            }
+        } else if (type == Blocks.ENCHANTING_TABLE) {
+            if (!(result instanceof TileEntityEnchantTable)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.END_PORTAL) {
+            if (!(result instanceof TileEntityEnderPortal)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type instanceof BlockSkullAbstract) {
+            if (!(result instanceof TileEntitySkull)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.DAYLIGHT_DETECTOR) {
+            if (!(result instanceof TileEntityLightDetector)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.COMPARATOR) {
+            if (!(result instanceof TileEntityComparator)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        }else if (type instanceof BlockBannerAbstract) {
+            if (!(result instanceof TileEntityBanner)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.STRUCTURE_BLOCK) {
+            if (!(result instanceof TileEntityStructure)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.END_GATEWAY) {
+            if (!(result instanceof TileEntityEndGateway)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.COMMAND_BLOCK) {
+            if (!(result instanceof TileEntityCommand)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type == Blocks.STRUCTURE_BLOCK) {
+            if (!(result instanceof TileEntityStructure)) {
+                result = fixTileEntity(pos, type, result);
+            }
+        } else if (type instanceof BlockBed) {
+            if (!(result instanceof TileEntityBed)) {
+                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(), type, found});
+
+        if (type instanceof ITileEntity) {
+            TileEntity replacement = ((ITileEntity) type).a(this);
+            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;
+        }
+    }
+    // CraftBukkit end
+
     public void doTick(BooleanSupplier booleansupplier) {
         this.P = true;
         super.doTick(booleansupplier);
@@ -128,9 +271,12 @@
         }
 
         this.methodProfiler.a("spawner");
-        if (this.getGameRules().getBoolean("doMobSpawning") && this.worldData.getType() != WorldType.DEBUG_ALL_BLOCK_STATES) {
-            this.spawnerCreature.a(this, this.allowMonsters, this.allowAnimals, this.worldData.getTime() % 400L == 0L);
-            this.getChunkProviderServer().a(this, this.allowMonsters, this.allowAnimals);
+        // 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.spawnerCreature.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);
+            this.getChunkProviderServer().a(this, this.allowMonsters && (this.ticksPerMonsterSpawns != 0 && time % this.ticksPerMonsterSpawns == 0L), this.allowAnimals && (this.ticksPerAnimalSpawns != 0 && time % this.ticksPerAnimalSpawns == 0L));
+            // CraftBukkit end
         }
 
         this.methodProfiler.c("chunkSource");
@@ -160,6 +306,8 @@
         this.methodProfiler.e();
         this.an();
         this.P = false;
+
+        this.getWorld().processChunkGC(); // CraftBukkit
     }
 
     public boolean j_() {
@@ -191,7 +339,7 @@
 
                 if (entityhuman.isSpectator()) {
                     ++i;
-                } else if (entityhuman.isSleeping()) {
+                } else if (entityhuman.isSleeping() || entityhuman.fauxSleeping) {
                     ++j;
                 }
             }
@@ -223,25 +371,46 @@
     }
 
     private void b() {
-        this.worldData.setWeatherDuration(0);
+        // CraftBukkit start
         this.worldData.setStorm(false);
-        this.worldData.setThunderDuration(0);
+        // 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.J && !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 {
@@ -316,10 +485,10 @@
                             entityhorseskeleton.s(true);
                             entityhorseskeleton.setAgeRaw(0);
                             entityhorseskeleton.setPosition((double) blockposition.getX(), (double) blockposition.getY(), (double) blockposition.getZ());
-                            this.addEntity(entityhorseskeleton);
+                            this.addEntity(entityhorseskeleton, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.LIGHTNING); // CraftBukkit
                         }
 
-                        this.strikeLightning(new EntityLightning(this, (double) blockposition.getX() + 0.5D, (double) blockposition.getY(), (double) blockposition.getZ() + 0.5D, flag2));
+                        this.strikeLightning(new EntityLightning(this, (double) blockposition.getX() + 0.5D, (double) blockposition.getY(), (double) blockposition.getZ() + 0.5D, flag2), org.bukkit.event.weather.LightningStrikeEvent.Cause.WEATHER); // CraftBukkit
                     }
                 }
 
@@ -332,11 +501,11 @@
                     BiomeBase biomebase = this.getBiome(blockposition);
 
                     if (biomebase.a((IWorldReader) this, blockposition1)) {
-                        this.setTypeUpdate(blockposition1, Blocks.ICE.getBlockData());
+                        org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockFormEvent(this, blockposition1, Blocks.ICE.getBlockData(), null); // CraftBukkit
                     }
 
                     if (flag && biomebase.b(this, blockposition)) {
-                        this.setTypeUpdate(blockposition, Blocks.SNOW.getBlockData());
+                        org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockFormEvent(this, blockposition, Blocks.SNOW.getBlockData(), null); // CraftBukkit
                     }
 
                     if (flag && this.getBiome(blockposition1).c() == BiomeBase.Precipitation.RAIN) {
@@ -385,7 +554,7 @@
     protected BlockPosition a(BlockPosition blockposition) {
         BlockPosition blockposition1 = this.getHighestBlockYAt(HeightMap.Type.MOTION_BLOCKING, blockposition);
         AxisAlignedBB axisalignedbb = (new AxisAlignedBB(blockposition1, new BlockPosition(blockposition1.getX(), this.getHeight(), blockposition1.getZ()))).g(3.0D);
-        List list = this.a(EntityLiving.class, axisalignedbb, (entityliving) -> {
+        List list = this.a(EntityLiving.class, axisalignedbb, (Predicate<EntityLiving>) (entityliving) -> { // CraftBukkit - decompile error
             return entityliving != null && entityliving.isAlive() && this.e(entityliving.getChunkCoordinates());
         });
 
@@ -401,7 +570,7 @@
     }
 
     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++ >= 300) {
                 return;
             }
@@ -490,6 +659,7 @@
 
     }
 
+    /* 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();
@@ -501,6 +671,7 @@
 
         super.entityJoinedWorld(entity, flag);
     }
+    // CraftBukkit end */
 
     private boolean getSpawnNPCs() {
         return this.server.getSpawnNPCs();
@@ -513,7 +684,21 @@
     protected IChunkProvider r() {
         IChunkLoader ichunkloader = this.dataManager.createChunkLoader(this.worldProvider);
 
-        return new ChunkProviderServer(this, ichunkloader, this.worldProvider.getChunkGenerator(), this.server);
+        // 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());
+        }
+
+        return new ChunkProviderServer(this, ichunkloader, gen, this.server);
+        // CraftBukkit end
     }
 
     public boolean a(EntityHuman entityhuman, BlockPosition blockposition) {
@@ -572,6 +757,22 @@
             BlockPosition blockposition = worldchunkmanager.a(0, 0, 256, list, random);
             ChunkCoordIntPair chunkcoordintpair = blockposition == null ? new ChunkCoordIntPair(0, 0) : new ChunkCoordIntPair(blockposition);
 
+            // 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()));
+                        return;
+                    }
+                }
+            }
+            // CraftBukkit end
+
             if (blockposition == null) {
                 WorldServer.a.warn("Unable to find spawn biome");
             }
@@ -647,6 +848,7 @@
         ChunkProviderServer chunkproviderserver = this.getChunkProviderServer();
 
         if (chunkproviderserver.d()) {
+            org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit
             if (iprogressupdate != null) {
                 iprogressupdate.a(new ChatMessage("menu.savingLevel", new Object[0]));
             }
@@ -657,7 +859,8 @@
             }
 
             chunkproviderserver.a(flag);
-            ArrayList arraylist = Lists.newArrayList(chunkproviderserver.a());
+            // CraftBukkit - ArrayList -> Collection
+            java.util.Collection arraylist = chunkproviderserver.a();
             Iterator iterator = arraylist.iterator();
 
             while (iterator.hasNext()) {
@@ -691,6 +894,12 @@
             }
         }
 
+        // CraftBukkit start - Save secondary data for nether/end
+        if (this instanceof SecondaryWorldServer) {
+            ((SecondaryWorldServer) this).c(); // As above
+        }
+        // CraftBukkit end
+
         this.worldData.a(this.getWorldBorder().getSize());
         this.worldData.d(this.getWorldBorder().getCenterX());
         this.worldData.c(this.getWorldBorder().getCenterZ());
@@ -705,9 +914,13 @@
         this.h().a();
     }
 
-    public boolean addEntity(Entity entity) {
-        return this.j(entity) ? super.addEntity(entity) : false;
+    // CraftBukkit start
+    public boolean addEntity(Entity entity, SpawnReason spawnReason) { // Changed signature, added SpawnReason
+        // World.addEntity(Entity) will call this, and we still want to perform
+        // existing entity checking when it's called with a SpawnReason
+        return this.j(entity) ? super.addEntity(entity, spawnReason) : false;
     }
+    // CraftBukkit end
 
     public void a(Stream<Entity> stream) {
         stream.forEach((entity) -> {
@@ -721,7 +934,7 @@
 
     private boolean j(Entity entity) {
         if (entity.dead) {
-            WorldServer.a.warn("Tried to add entity {} but it was marked as removed already", EntityTypes.getName(entity.P()));
+            // WorldServer.a.warn("Tried to add entity {} but it was marked as removed already", EntityTypes.getName(entity.P())); // CraftBukkit
             return false;
         } else {
             UUID uuid = entity.getUniqueID();
@@ -733,7 +946,7 @@
                     this.g.remove(entity1);
                 } else {
                     if (!(entity instanceof EntityHuman)) {
-                        WorldServer.a.warn("Keeping entity {} that already exists with UUID {}", EntityTypes.getName(entity1.P()), uuid.toString());
+                        // WorldServer.a.warn("Keeping entity {} that already exists with UUID {}", EntityTypes.getName(entity1.P()), uuid.toString()); // CraftBukkit
                         return false;
                     }
 
@@ -785,9 +998,21 @@
 
     }
 
+    // CraftBukkit start
     public boolean strikeLightning(Entity entity) {
+        return this.strikeLightning(entity, LightningStrikeEvent.Cause.UNKNOWN);
+    }
+
+    public boolean strikeLightning(Entity entity, LightningStrikeEvent.Cause cause) {
+        LightningStrikeEvent lightning = new LightningStrikeEvent(this.getWorld(), (org.bukkit.entity.LightningStrike) entity.getBukkitEntity(), cause);
+        this.getServer().getPluginManager().callEvent(lightning);
+
+        if (lightning.isCancelled()) {
+            return false;
+        }
+        // CraftBukkit end
         if (super.strikeLightning(entity)) {
-            this.server.getPlayerList().sendPacketNearby((EntityHuman) null, entity.locX, entity.locY, entity.locZ, 512.0D, this.worldProvider.getDimensionManager(), new PacketPlayOutSpawnEntityWeather(entity));
+            this.server.getPlayerList().sendPacketNearby((EntityHuman) null, entity.locX, entity.locY, entity.locZ, 512.0D, dimension, new PacketPlayOutSpawnEntityWeather(entity)); // CraftBukkit - Use dimension
             return true;
         } else {
             return false;
@@ -803,6 +1028,14 @@
     }
 
     public Explosion createExplosion(@Nullable Entity entity, DamageSource damagesource, double d0, double d1, double d2, float f, boolean flag, boolean flag1) {
+        // CraftBukkit start
+        Explosion explosion = super.createExplosion(entity, damagesource, d0, d1, d2, f, flag, flag1);
+
+        if (explosion.wasCanceled) {
+            return explosion;
+        }
+
+        /* Remove
         Explosion explosion = new Explosion(this, entity, d0, d1, d2, f, flag, flag1);
 
         if (damagesource != null) {
@@ -811,6 +1044,8 @@
 
         explosion.a();
         explosion.a(false);
+        */
+        // CraftBukkit end - TODO: Check if explosions are still properly implemented
         if (!flag1) {
             explosion.clearBlocks();
         }
@@ -837,7 +1072,8 @@
             BlockActionData blockactiondata = (BlockActionData) this.d.removeFirst();
 
             if (this.a(blockactiondata)) {
-                this.server.getPlayerList().sendPacketNearby((EntityHuman) null, (double) blockactiondata.a().getX(), (double) blockactiondata.a().getY(), (double) blockactiondata.a().getZ(), 64.0D, this.worldProvider.getDimensionManager(), new PacketPlayOutBlockAction(blockactiondata.a(), blockactiondata.b(), blockactiondata.c(), blockactiondata.d()));
+                // CraftBukkit - this.worldProvider.dimension -> this.dimension
+                this.server.getPlayerList().sendPacketNearby((EntityHuman) null, (double) blockactiondata.a().getX(), (double) blockactiondata.a().getY(), (double) blockactiondata.a().getZ(), 64.0D, dimension, new PacketPlayOutBlockAction(blockactiondata.a(), blockactiondata.b(), blockactiondata.c(), blockactiondata.d()));
             }
         }
 
@@ -858,6 +1094,7 @@
         boolean flag = this.isRaining();
 
         super.w();
+        /* CraftBukkit start
         if (this.o != this.p) {
             this.server.getPlayerList().a((Packet) (new PacketPlayOutGameStateChange(7, this.p)), this.worldProvider.getDimensionManager());
         }
@@ -876,6 +1113,21 @@
             this.server.getPlayerList().sendAll(new PacketPlayOutGameStateChange(7, this.p));
             this.server.getPlayerList().sendAll(new PacketPlayOutGameStateChange(8, this.r));
         }
+        // */
+        if (flag != this.isRaining()) {
+            // 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
 
     }
 
@@ -909,13 +1161,20 @@
     }
 
     public <T extends ParticleParam> int a(T t0, double d0, double d1, double d2, int i, double d3, double d4, double d5, double d6) {
-        PacketPlayOutWorldParticles packetplayoutworldparticles = new PacketPlayOutWorldParticles(t0, false, (float) d0, (float) d1, (float) d2, (float) d3, (float) d4, (float) d5, (float) d6, i);
+        // CraftBukkit - visibility api support
+        return sendParticles(null, t0, d0, d1, d2, i, d3, d4, d5, d6, false);
+    }
+
+    public <T extends ParticleParam> int sendParticles(EntityPlayer sender, T t0, double d0, double d1, double d2, int i, double d3, double d4, double d5, double d6, boolean force) {
+        PacketPlayOutWorldParticles packetplayoutworldparticles = new PacketPlayOutWorldParticles(t0, force, (float) d0, (float) d1, (float) d2, (float) d3, (float) d4, (float) d5, (float) d6, i);
+        // CraftBukkit end
         int j = 0;
 
         for (int k = 0; k < this.players.size(); ++k) {
             EntityPlayer entityplayer = (EntityPlayer) this.players.get(k);
+            if (sender != null && !entityplayer.getBukkitEntity().canSee(sender.getBukkitEntity())) continue; // CraftBukkit
 
-            if (this.a(entityplayer, false, d0, d1, d2, packetplayoutworldparticles)) {
+            if (this.a(entityplayer, force, d0, d1, d2, packetplayoutworldparticles)) { // CraftBukkit
                 ++j;
             }
         }