--- a/net/minecraft/server/World.java
+++ b/net/minecraft/server/World.java
@@ -13,6 +13,25 @@
 import java.util.UUID;
 import java.util.concurrent.Callable;
 
+// CraftBukkit start
+import com.google.common.collect.Maps;
+import java.util.Map;
+
+import org.bukkit.Bukkit;
+import org.bukkit.block.BlockState;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+import org.bukkit.craftbukkit.util.LongHashSet;
+import org.bukkit.generator.ChunkGenerator;
+import org.bukkit.craftbukkit.CraftServer;
+import org.bukkit.craftbukkit.CraftWorld;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.event.block.BlockCanBuildEvent;
+import org.bukkit.event.block.BlockPhysicsEvent;
+import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
+import org.bukkit.event.weather.WeatherChangeEvent;
+import org.bukkit.event.weather.ThunderChangeEvent;
+// CraftBukkit end
+
 public abstract class World implements IBlockAccess {
 
     private int a = 63;
@@ -36,27 +55,72 @@
     protected float r;
     private int J;
     public final Random random = new Random();
-    public final WorldProvider worldProvider;
+    public WorldProvider worldProvider; // CraftBukkit - remove final
     protected List<IWorldAccess> u = Lists.newArrayList();
     protected IChunkProvider chunkProvider;
     protected final IDataManager dataManager;
-    protected WorldData worldData;
+    public WorldData worldData; // CraftBukkit - public
     protected boolean isLoading;
-    protected PersistentCollection worldMaps;
+    public PersistentCollection worldMaps; // CraftBukkit - public
     protected PersistentVillage villages;
     public final MethodProfiler methodProfiler;
     private final Calendar K = Calendar.getInstance();
-    protected Scoreboard scoreboard = new Scoreboard();
+    public Scoreboard scoreboard = new Scoreboard(); // CraftBukkit - public
     public final boolean isClientSide;
-    protected Set<ChunkCoordIntPair> chunkTickList = Sets.newHashSet();
+    // CraftBukkit - longhashset
+    protected LongHashSet chunkTickList = new LongHashSet();
     private int L;
-    protected boolean allowMonsters;
-    protected boolean allowAnimals;
+    public boolean allowMonsters; // CraftBukkit - public
+    public boolean allowAnimals; // CraftBukkit - public
     private boolean M;
     private final WorldBorder N;
     int[] H;
 
-    protected World(IDataManager idatamanager, WorldData worlddata, WorldProvider worldprovider, MethodProfiler methodprofiler, boolean flag) {
+    // CraftBukkit start Added the following
+    private final CraftWorld world;
+    public boolean pvpMode;
+    public boolean keepSpawnInMemory = true;
+    public ChunkGenerator generator;
+
+    public boolean captureBlockStates = false;
+    public boolean captureTreeGeneration = false;
+    public ArrayList<BlockState> capturedBlockStates= new ArrayList<BlockState>(){
+        @Override
+        public boolean add( BlockState blockState ) {
+            Iterator<BlockState> blockStateIterator = this.iterator();
+            while( blockStateIterator.hasNext() ) {
+                BlockState blockState1 = blockStateIterator.next();
+                if ( blockState1.getLocation().equals( blockState.getLocation() ) ) {
+                    return false;
+                }
+            }
+
+            return super.add( blockState );
+        }
+    };
+    public long ticksPerAnimalSpawns;
+    public long ticksPerMonsterSpawns;
+    public boolean populating;
+    private int tickPosition;
+
+    public CraftWorld getWorld() {
+        return this.world;
+    }
+
+    public CraftServer getServer() {
+        return (CraftServer) Bukkit.getServer();
+    }
+
+    public Chunk getChunkIfLoaded(int x, int z) {
+        return ((ChunkProviderServer) this.chunkProvider).getChunkIfLoaded(x, z);
+    }
+
+    protected World(IDataManager idatamanager, WorldData worlddata, WorldProvider worldprovider, MethodProfiler methodprofiler, boolean flag, ChunkGenerator gen, org.bukkit.World.Environment env) {
+        this.generator = gen;
+        this.world = new CraftWorld((WorldServer) this, gen, env);
+        this.ticksPerAnimalSpawns = this.getServer().getTicksPerAnimalSpawns(); // CraftBukkit
+        this.ticksPerMonsterSpawns = this.getServer().getTicksPerMonsterSpawns(); // CraftBukkit
+        // CraftBukkit end
         this.L = this.random.nextInt(12000);
         this.allowMonsters = true;
         this.allowAnimals = true;
@@ -67,6 +131,35 @@
         this.worldProvider = worldprovider;
         this.isClientSide = flag;
         this.N = worldprovider.getWorldBorder();
+        // CraftBukkit start
+        // Moved from PlayerList
+        this.N.a(new IWorldBorderListener() {
+            public void a(WorldBorder worldborder, double d0) {
+                getServer().getHandle().sendAll(new PacketPlayOutWorldBorder(worldborder, PacketPlayOutWorldBorder.EnumWorldBorderAction.SET_SIZE), World.this);
+            }
+
+            public void a(WorldBorder worldborder, double d0, double d1, long i) {
+                getServer().getHandle().sendAll(new PacketPlayOutWorldBorder(worldborder, PacketPlayOutWorldBorder.EnumWorldBorderAction.LERP_SIZE), World.this);
+            }
+
+            public void a(WorldBorder worldborder, double d0, double d1) {
+                getServer().getHandle().sendAll(new PacketPlayOutWorldBorder(worldborder, PacketPlayOutWorldBorder.EnumWorldBorderAction.SET_CENTER), World.this);
+            }
+
+            public void a(WorldBorder worldborder, int i) {
+                getServer().getHandle().sendAll(new PacketPlayOutWorldBorder(worldborder, PacketPlayOutWorldBorder.EnumWorldBorderAction.SET_WARNING_TIME), World.this);
+            }
+
+            public void b(WorldBorder worldborder, int i) {
+                getServer().getHandle().sendAll(new PacketPlayOutWorldBorder(worldborder, PacketPlayOutWorldBorder.EnumWorldBorderAction.SET_WARNING_BLOCKS), World.this);
+            }
+
+            public void b(WorldBorder worldborder, double d0) {}
+
+            public void c(WorldBorder worldborder, double d0) {}
+        }); 
+        this.getServer().addWorld(this.world); 
+        // CraftBukkit end
     }
 
     public World b() {
@@ -193,6 +286,27 @@
     }
 
     public boolean setTypeAndData(BlockPosition blockposition, IBlockData iblockdata, int i) {
+        // CraftBukkit start - tree generation
+        if (this.captureTreeGeneration) {
+            BlockState blockstate = null;
+            Iterator<BlockState> it = capturedBlockStates.iterator();
+            while (it.hasNext()) {
+                BlockState previous = it.next();
+                if (previous.getX() == blockposition.getX() && previous.getY() == blockposition.getY() && previous.getZ() == blockposition.getZ()) {
+                    blockstate = previous;
+                    it.remove();
+                    break;
+                }
+            }
+            if (blockstate == null) {
+                blockstate = org.bukkit.craftbukkit.block.CraftBlockState.getBlockState(this, blockposition.getX(), blockposition.getY(), blockposition.getZ(), i);
+            }
+            blockstate.setTypeId(CraftMagicNumbers.getId(iblockdata.getBlock()));
+            blockstate.setRawData((byte) iblockdata.getBlock().toLegacyData(iblockdata));
+            this.capturedBlockStates.add(blockstate);
+            return true;
+        }
+        // CraftBukkit end
         if (!this.isValidLocation(blockposition)) {
             return false;
         } else if (!this.isClientSide && this.worldData.getType() == WorldType.DEBUG_ALL_BLOCK_STATES) {
@@ -200,9 +314,23 @@
         } else {
             Chunk chunk = this.getChunkAtWorldCoords(blockposition);
             Block block = iblockdata.getBlock();
+
+            // CraftBukkit start - capture blockstates
+            BlockState blockstate = null;
+            if (this.captureBlockStates) {
+                blockstate = org.bukkit.craftbukkit.block.CraftBlockState.getBlockState(this, blockposition.getX(), blockposition.getY(), blockposition.getZ(), i);
+                this.capturedBlockStates.add(blockstate);
+            }
+            // CraftBukkit end
+
             IBlockData iblockdata1 = chunk.a(blockposition, iblockdata);
 
             if (iblockdata1 == null) {
+                // CraftBukkit start - remove blockstate if failed
+                if (this.captureBlockStates) {
+                    this.capturedBlockStates.remove(blockstate);
+                }
+                // CraftBukkit end
                 return false;
             } else {
                 Block block1 = iblockdata1.getBlock();
@@ -213,6 +341,7 @@
                     this.methodProfiler.b();
                 }
 
+                /*
                 if ((i & 2) != 0 && (!this.isClientSide || (i & 4) == 0) && chunk.isReady()) {
                     this.notify(blockposition);
                 }
@@ -223,12 +352,35 @@
                         this.updateAdjacentComparators(blockposition, block);
                     }
                 }
+                */
+
+                // CraftBukkit start
+                if (!this.captureBlockStates) { // Don't notify clients or update physics while capturing blockstates
+                    // Modularize client and physic updates
+                    notifyAndUpdatePhysics(blockposition, chunk, block1, block, i);
+                }
+                // CraftBukkit end
 
                 return true;
             }
         }
     }
 
+    // CraftBukkit start - Split off from original setTypeAndData(int i, int j, int k, Block block, int l, int i1) method in order to directly send client and physic updates
+    public void notifyAndUpdatePhysics(BlockPosition blockposition, Chunk chunk, Block oldBlock, Block newBLock, int flag) {
+        if ((flag & 2) != 0 && (chunk == null || chunk.isReady())) {  // allow chunk to be null here as chunk.isReady() is false when we send our notification during block placement
+            this.notify(blockposition);
+        }
+
+        if (!this.isClientSide && (flag & 1) != 0) {
+            this.update(blockposition, oldBlock);
+            if (newBLock.isComplexRedstone()) {
+                this.updateAdjacentComparators(blockposition, newBLock);
+            }
+        }
+    }
+    // CraftBukkit end
+
     public boolean setAir(BlockPosition blockposition) {
         return this.setTypeAndData(blockposition, Blocks.AIR.getBlockData(), 3);
     }
@@ -262,6 +414,11 @@
 
     public void update(BlockPosition blockposition, Block block) {
         if (this.worldData.getType() != WorldType.DEBUG_ALL_BLOCK_STATES) {
+            // CraftBukkit start
+            if (populating) {
+                return;
+            }
+            // CraftBukkit end
             this.applyPhysics(blockposition, block);
         }
 
@@ -337,6 +494,17 @@
             IBlockData iblockdata = this.getType(blockposition);
 
             try {
+                // CraftBukkit start
+                CraftWorld world = ((WorldServer) this).getWorld();
+                if (world != null) {
+                    BlockPhysicsEvent event = new BlockPhysicsEvent(world.getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ()), CraftMagicNumbers.getId(block));
+                    this.getServer().getPluginManager().callEvent(event);
+
+                    if (event.isCancelled()) {
+                        return;
+                    }
+                }
+                // CraftBukkit end
                 iblockdata.getBlock().doPhysics(this, blockposition, iblockdata, block);
             } catch (Throwable throwable) {
                 CrashReport crashreport = CrashReport.a(throwable, "Exception while updating neighbours");
@@ -518,6 +686,17 @@
     }
 
     public IBlockData getType(BlockPosition blockposition) {
+        // CraftBukkit start - tree generation
+        if (captureTreeGeneration) {
+            Iterator<BlockState> it = capturedBlockStates.iterator();
+            while (it.hasNext()) {
+                BlockState previous = it.next();
+                if (previous.getX() == blockposition.getX() && previous.getY() == blockposition.getY() && previous.getZ() == blockposition.getZ()) {
+                    return CraftMagicNumbers.getBlock(previous.getTypeId()).fromLegacyData(previous.getRawData());
+                }
+            }
+        }
+        // CraftBukkit end
         if (!this.isValidLocation(blockposition)) {
             return Blocks.AIR.getBlockData();
         } else {
@@ -723,6 +902,13 @@
     }
 
     public boolean addEntity(Entity entity) {
+        // CraftBukkit start - Used for entities other than creatures
+        return addEntity(entity, SpawnReason.DEFAULT);
+    }
+
+    public boolean addEntity(Entity entity, SpawnReason spawnReason) { // Changed signature, added SpawnReason
+        if (entity == null) return false;
+        // CraftBukkit end
         int i = MathHelper.floor(entity.locX / 16.0D);
         int j = MathHelper.floor(entity.locZ / 16.0D);
         boolean flag = entity.attachedToPlayer;
@@ -731,7 +917,35 @@
             flag = true;
         }
 
+        // CraftBukkit start
+        org.bukkit.event.Cancellable event = null;
+        if (entity instanceof EntityLiving && !(entity instanceof EntityPlayer)) {
+            boolean isAnimal = entity instanceof EntityAnimal || entity instanceof EntityWaterAnimal || entity instanceof EntityGolem;
+            boolean isMonster = entity instanceof EntityMonster || entity instanceof EntityGhast || entity instanceof EntitySlime;
+
+            if (spawnReason != SpawnReason.CUSTOM) {
+                if (isAnimal && !allowAnimals || isMonster && !allowMonsters) {
+                    entity.dead = true;
+                    return false;
+                }
+            }
+
+            event = CraftEventFactory.callCreatureSpawnEvent((EntityLiving) entity, spawnReason);
+        } else if (entity instanceof EntityItem) {
+            event = CraftEventFactory.callItemSpawnEvent((EntityItem) entity);
+        } else if (entity.getBukkitEntity() instanceof org.bukkit.entity.Projectile) {
+            // Not all projectiles extend EntityProjectile, so check for Bukkit interface instead
+            event = CraftEventFactory.callProjectileLaunchEvent(entity);
+        }
+
+        if (event != null && (event.isCancelled() || entity.dead)) {
+            entity.dead = true;
+            return false;
+        }
+        // CraftBukkit end
+
         if (!flag && !this.isChunkLoaded(i, j, true)) {
+            entity.dead = true;
             return false;
         } else {
             if (entity instanceof EntityHuman) {
@@ -753,6 +967,7 @@
             ((IWorldAccess) this.u.get(i)).a(entity);
         }
 
+        entity.valid = true; // CraftBukkit
     }
 
     protected void b(Entity entity) {
@@ -760,6 +975,7 @@
             ((IWorldAccess) this.u.get(i)).b(entity);
         }
 
+        entity.valid = false; // CraftBukkit
     }
 
     public void kill(Entity entity) {
@@ -794,7 +1010,15 @@
             this.getChunkAt(i, j).b(entity);
         }
 
-        this.entityList.remove(entity);
+        // CraftBukkit start - Decrement loop variable field if we've already ticked this entity
+        int index = this.entityList.indexOf(entity);
+        if (index != -1) {
+            if (index <= this.tickPosition) {
+                this.tickPosition--;
+            }
+            this.entityList.remove(index);
+        }
+        // CraftBukkit end
         this.b(entity);
     }
 
@@ -978,6 +1202,11 @@
 
         for (i = 0; i < this.k.size(); ++i) {
             entity = (Entity) this.k.get(i);
+            // CraftBukkit start - Fixed an NPE
+            if (entity == null) {
+                continue;
+            }
+            // CraftBukkit end
 
             try {
                 ++entity.ticksLived;
@@ -1021,8 +1250,10 @@
         this.g.clear();
         this.methodProfiler.c("regular");
 
-        for (i = 0; i < this.entityList.size(); ++i) {
-            entity = (Entity) this.entityList.get(i);
+        // CraftBukkit start - Use field for loop variable
+        for (this.tickPosition = 0; this.tickPosition < this.entityList.size(); ++this.tickPosition) {
+            entity = (Entity) this.entityList.get(this.tickPosition);
+            // CraftBukkit end
             if (entity.vehicle != null) {
                 if (!entity.vehicle.dead && entity.vehicle.passenger == entity) {
                     continue;
@@ -1053,7 +1284,7 @@
                     this.getChunkAt(j, k).b(entity);
                 }
 
-                this.entityList.remove(i--);
+                this.entityList.remove(this.tickPosition--); // CraftBukkit - Use field for loop variable
                 this.b(entity);
             }
 
@@ -1062,6 +1293,13 @@
 
         this.methodProfiler.c("blockEntities");
         this.M = true;
+        // CraftBukkit start - From below, clean up tile entities before ticking them
+        if (!this.c.isEmpty()) {
+            this.tileEntityList.removeAll(this.c);
+            this.h.removeAll(this.c);
+            this.c.clear();
+        }
+        // CraftBukkit end
         Iterator iterator = this.tileEntityList.iterator();
 
         while (iterator.hasNext()) {
@@ -1093,11 +1331,13 @@
         }
 
         this.M = false;
+        /* CraftBukkit start - Moved up
         if (!this.c.isEmpty()) {
             this.tileEntityList.removeAll(this.c);
             this.h.removeAll(this.c);
             this.c.clear();
         }
+        // CraftBukkit end */
 
         this.methodProfiler.c("pendingBlockEntities");
         if (!this.b.isEmpty()) {
@@ -1105,9 +1345,11 @@
                 TileEntity tileentity1 = (TileEntity) this.b.get(l);
 
                 if (!tileentity1.x()) {
+                    /* CraftBukkit start - Order matters, moved down
                     if (!this.h.contains(tileentity1)) {
                         this.a(tileentity1);
                     }
+                    // CraftBukkit end */
 
                     if (this.isLoaded(tileentity1.getPosition())) {
                         this.getChunkAtWorldCoords(tileentity1.getPosition()).a(tileentity1.getPosition(), tileentity1);
@@ -1161,7 +1403,10 @@
         int j = MathHelper.floor(entity.locZ);
         byte b0 = 32;
 
-        if (!flag || this.isAreaLoaded(i - b0, 0, j - b0, i + b0, 0, j + b0, true)) {
+        // CraftBukkit start - Use neighbor cache instead of looking up
+        Chunk startingChunk = this.getChunkIfLoaded(i >> 4, j >> 4);
+        if (!flag || (startingChunk != null && startingChunk.areNeighborsLoaded(2)) /* this.isAreaLoaded(i - b0, 0, j - b0, i + b0, 0, j + b0) */) {
+            // CraftBukkit end
             entity.P = entity.locX;
             entity.Q = entity.locY;
             entity.R = entity.locZ;
@@ -1479,10 +1724,18 @@
         }
     }
 
+    public Map<BlockPosition, TileEntity> capturedTileEntities = Maps.newHashMap();
+
     public TileEntity getTileEntity(BlockPosition blockposition) {
         if (!this.isValidLocation(blockposition)) {
             return null;
         } else {
+            // CraftBukkit start
+            if (capturedTileEntities.containsKey(blockposition)) {
+                return capturedTileEntities.get(blockposition);
+            }
+            // CraftBukkit end
+
             TileEntity tileentity = null;
             int i;
             TileEntity tileentity1;
@@ -1517,6 +1770,14 @@
 
     public void setTileEntity(BlockPosition blockposition, TileEntity tileentity) {
         if (tileentity != null && !tileentity.x()) {
+            // CraftBukkit start
+            if (captureBlockStates) {
+                tileentity.a(this);
+                tileentity.a(blockposition);
+                capturedTileEntities.put(blockposition, tileentity);
+                return;
+            }
+            // CraftBukkit end
             if (this.M) {
                 tileentity.a(blockposition);
                 Iterator iterator = this.b.iterator();
@@ -1679,12 +1940,20 @@
                 }
 
                 this.p = MathHelper.a(this.p, 0.0F, 1.0F);
+
+                // CraftBukkit start
+                for (int idx = 0; idx < this.players.size(); ++idx) {
+                    if (((EntityPlayer) this.players.get(idx)).world == this) {
+                        ((EntityPlayer) this.players.get(idx)).tickWeather();
+                    }
+                }
+                // CraftBukkit end
             }
         }
     }
 
     protected void D() {
-        this.chunkTickList.clear();
+        // this.chunkTickList.clear(); // CraftBukkit - removed
         this.methodProfiler.a("buildList");
 
         int i;
@@ -1701,7 +1970,7 @@
 
             for (int i1 = -l; i1 <= l; ++i1) {
                 for (int j1 = -l; j1 <= l; ++j1) {
-                    this.chunkTickList.add(new ChunkCoordIntPair(i1 + j, j1 + k));
+                    this.chunkTickList.add(org.bukkit.craftbukkit.util.LongHash.toLong(i1 + j, j1 + k));
                 }
             }
         }
@@ -1879,7 +2148,10 @@
     }
 
     public boolean c(EnumSkyBlock enumskyblock, BlockPosition blockposition) {
-        if (!this.areChunksLoaded(blockposition, 17, false)) {
+        // CraftBukkit start - Use neighbor cache instead of looking up
+        Chunk chunk = this.getChunkIfLoaded(blockposition.getX() >> 4, blockposition.getZ() >> 4);
+        if (chunk == null || !chunk.areNeighborsLoaded(1) /*!this.areChunksLoaded(blockposition, 17, false)*/) {
+            // CraftBukkit end
             return false;
         } else {
             int i = 0;
@@ -2042,7 +2314,7 @@
         while (iterator.hasNext()) {
             Entity entity = (Entity) iterator.next();
 
-            if (oclass.isAssignableFrom(entity.getClass()) && predicate.apply(entity)) {
+            if (oclass.isAssignableFrom(entity.getClass()) && predicate.apply((T) entity)) { // CraftBukkit - fix decompile error
                 arraylist.add(entity);
             }
         }
@@ -2057,7 +2329,7 @@
         while (iterator.hasNext()) {
             Entity entity = (Entity) iterator.next();
 
-            if (oclass.isAssignableFrom(entity.getClass()) && predicate.apply(entity)) {
+            if (oclass.isAssignableFrom(entity.getClass()) && predicate.apply((T) entity)) { // CraftBukkit - fix decompile error
                 arraylist.add(entity);
             }
         }
@@ -2105,7 +2377,7 @@
             }
         }
 
-        return entity;
+        return (T) entity; // CraftBukkit fix decompile error
     }
 
     public Entity a(int i) {
@@ -2125,8 +2397,17 @@
 
         while (iterator.hasNext()) {
             Entity entity = (Entity) iterator.next();
-
-            if ((!(entity instanceof EntityInsentient) || !((EntityInsentient) entity).isPersistent()) && oclass.isAssignableFrom(entity.getClass())) {
+            // CraftBukkit start - Split out persistent check, don't apply it to special persistent mobs
+            if (entity instanceof EntityInsentient) {
+                EntityInsentient entityinsentient = (EntityInsentient) entity;
+                if (entityinsentient.isTypeNotPersistent() && entityinsentient.isPersistent()) {
+                    continue;
+                }
+            }
+            
+            if (oclass.isAssignableFrom(entity.getClass())) {
+            // if ((!(entity instanceof EntityInsentient) || !((EntityInsentient) entity).isPersistent()) && oclass.isAssignableFrom(entity.getClass())) {
+                // CraftBukkit end
                 ++i;
             }
         }
@@ -2135,12 +2416,18 @@
     }
 
     public void b(Collection<Entity> collection) {
-        this.entityList.addAll(collection);
+        // CraftBukkit start
+        // this.entityList.addAll(collection);
         Iterator iterator = collection.iterator();
 
         while (iterator.hasNext()) {
             Entity entity = (Entity) iterator.next();
 
+            if (entity == null) {
+                continue;
+            }
+            this.entityList.add(entity);
+            // CraftBukkit end
             this.a(entity);
         }
 
@@ -2154,7 +2441,13 @@
         Block block1 = this.getType(blockposition).getBlock();
         AxisAlignedBB axisalignedbb = flag ? null : block.a(this, blockposition, block.getBlockData());
 
-        return axisalignedbb != null && !this.a(axisalignedbb, entity) ? false : (block1.getMaterial() == Material.ORIENTABLE && block == Blocks.ANVIL ? true : block1.getMaterial().isReplaceable() && block.canPlace(this, blockposition, enumdirection, itemstack));
+        // CraftBukkit start - store default return
+        boolean defaultReturn = axisalignedbb != null && !this.a(axisalignedbb, entity) ? false : (block1.getMaterial() == Material.ORIENTABLE && block == Blocks.ANVIL ? true : block1.getMaterial().isReplaceable() && block.canPlace(this, blockposition, enumdirection, itemstack));
+        BlockCanBuildEvent event = new BlockCanBuildEvent(this.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ()), CraftMagicNumbers.getId(block), defaultReturn);
+        this.getServer().getPluginManager().callEvent(event);
+
+        return event.isBuildable();
+        // CraftBukkit end
     }
 
     public int F() {
@@ -2253,6 +2546,11 @@
 
         for (int i = 0; i < this.players.size(); ++i) {
             EntityHuman entityhuman1 = (EntityHuman) this.players.get(i);
+            // CraftBukkit start - Fixed an NPE
+            if (entityhuman1 == null || entityhuman1.dead) {
+                continue;
+            }
+            // CraftBukkit end
 
             if (IEntitySelector.d.apply(entityhuman1)) {
                 double d5 = entityhuman1.e(d0, d1, d2);
@@ -2369,6 +2667,16 @@
 
     public void everyoneSleeping() {}
 
+    // CraftBukkit start
+    // Calls the method that checks to see if players are sleeping
+    // Called by CraftPlayer.setPermanentSleeping()
+    public void checkSleepStatus() {
+        if (!this.isClientSide) {
+            this.everyoneSleeping();
+        }
+    }
+    // CraftBukkit end
+
     public float h(float f) {
         return (this.q + (this.r - this.q) * f) * this.j(f);
     }
@@ -2592,6 +2900,6 @@
         int l = j * 16 + 8 - blockposition.getZ();
         short short0 = 128;
 
-        return k >= -short0 && k <= short0 && l >= -short0 && l <= short0;
+        return k >= -short0 && k <= short0 && l >= -short0 && l <= short0 && this.keepSpawnInMemory; // CraftBukkit - Added 'this.keepSpawnInMemory'
     }
 }