--- a/net/minecraft/server/level/PlayerChunkMap.java
+++ b/net/minecraft/server/level/PlayerChunkMap.java
@@ -91,6 +91,8 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+import org.bukkit.entity.Player; // CraftBukkit
+
 public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.e {
 
     private static final byte CHUNK_TYPE_REPLACEABLE = -1;
@@ -129,6 +131,27 @@
     private final Queue<Runnable> unloadQueue;
     int viewDistance;
 
+    // CraftBukkit start - recursion-safe executor for Chunk loadCallback() and unloadCallback()
+    public final CallbackExecutor callbackExecutor = new CallbackExecutor();
+    public static final class CallbackExecutor implements java.util.concurrent.Executor, Runnable {
+
+        private final java.util.Queue<Runnable> queue = new java.util.ArrayDeque<>();
+
+        @Override
+        public void execute(Runnable runnable) {
+            queue.add(runnable);
+        }
+
+        @Override
+        public void run() {
+            Runnable task;
+            while ((task = queue.poll()) != null) {
+                task.run();
+            }
+        }
+    };
+    // CraftBukkit end
+
     public PlayerChunkMap(WorldServer worldserver, Convertable.ConversionSession convertable_conversionsession, DataFixer datafixer, DefinedStructureManager definedstructuremanager, Executor executor, IAsyncTaskHandler<Runnable> iasynctaskhandler, ILightAccess ilightaccess, ChunkGenerator chunkgenerator, WorldLoadListener worldloadlistener, ChunkStatusUpdateListener chunkstatusupdatelistener, Supplier<WorldPersistentData> supplier, int i, boolean flag) {
         super(new File(convertable_conversionsession.a(worldserver.getDimensionKey()), "region"), datafixer, flag);
         this.visibleChunkMap = this.updatingChunkMap.clone();
@@ -279,9 +302,12 @@
 
         return completablefuture1.thenApply((list1) -> {
             List<IChunkAccess> list2 = Lists.newArrayList();
-            final int l1 = 0;
+            // CraftBukkit start - decompile error
+            int cnt = 0;
 
-            for (Iterator iterator = list1.iterator(); iterator.hasNext(); ++l1) {
+            for (Iterator iterator = list1.iterator(); iterator.hasNext(); ++cnt) {
+                final int l1 = cnt;
+                // CraftBukkit end
                 final Either<IChunkAccess, PlayerChunk.Failure> either = (Either) iterator.next();
                 Optional<IChunkAccess> optional = either.left();
 
@@ -389,7 +415,7 @@
             PlayerChunkMap.LOGGER.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", this.storageFolder.getName());
         } else {
             this.visibleChunkMap.values().stream().filter(PlayerChunk::hasBeenLoaded).forEach((playerchunk) -> {
-                IChunkAccess ichunkaccess = (IChunkAccess) playerchunk.getChunkSave().getNow((Object) null);
+                IChunkAccess ichunkaccess = (IChunkAccess) playerchunk.getChunkSave().getNow(null); // CraftBukkit - decompile error
 
                 if (ichunkaccess instanceof ProtoChunkExtension || ichunkaccess instanceof Chunk) {
                     this.saveChunk(ichunkaccess);
@@ -400,7 +426,6 @@
         }
 
     }
-
     protected void unloadChunks(BooleanSupplier booleansupplier) {
         GameProfilerFiller gameprofilerfiller = this.level.getMethodProfiler();
 
@@ -439,7 +464,7 @@
 
     private void a(long i, PlayerChunk playerchunk) {
         CompletableFuture<IChunkAccess> completablefuture = playerchunk.getChunkSave();
-        Consumer consumer = (ichunkaccess) -> {
+        Consumer<IChunkAccess> consumer = (ichunkaccess) -> { // CraftBukkit - decompile error
             CompletableFuture<IChunkAccess> completablefuture1 = playerchunk.getChunkSave();
 
             if (completablefuture1 != completablefuture) {
@@ -615,7 +640,21 @@
 
     private static void a(WorldServer worldserver, List<NBTTagCompound> list) {
         if (!list.isEmpty()) {
-            worldserver.b(EntityTypes.a(list, (World) worldserver));
+            // CraftBukkit start - these are spawned serialized (DefinedStructure) and we don't call an add event below at the moment due to ordering complexities
+            worldserver.b(EntityTypes.a(list, (World) worldserver).filter((entity) -> {
+                boolean needsRemoval = false;
+                net.minecraft.server.dedicated.DedicatedServer server = worldserver.getCraftServer().getServer();
+                if (!server.getSpawnNPCs() && entity instanceof net.minecraft.world.entity.npc.NPC) {
+                    entity.die();
+                    needsRemoval = true;
+                }
+                if (!server.getSpawnAnimals() && (entity instanceof net.minecraft.world.entity.animal.EntityAnimal || entity instanceof net.minecraft.world.entity.animal.EntityWaterAnimal)) {
+                    entity.die();
+                    needsRemoval = true;
+                }
+                return !needsRemoval;
+            }));
+            // CraftBukkit end
         }
 
     }
@@ -843,7 +882,8 @@
                 return ichunkaccess instanceof Chunk ? Optional.of((Chunk) ichunkaccess) : Optional.empty();
             });
 
-            csvwriter.a(chunkcoordintpair.x, chunkcoordintpair.z, playerchunk.getTicketLevel(), optional.isPresent(), optional.map(IChunkAccess::getChunkStatus).orElse((Object) null), optional1.map(Chunk::getState).orElse((Object) null), a(playerchunk.c()), a(playerchunk.a()), a(playerchunk.b()), this.distanceManager.c(entry.getLongKey()), !this.isOutsideOfRange(chunkcoordintpair), optional1.map((chunk) -> {
+            // CraftBukkit - decompile error
+            csvwriter.a(chunkcoordintpair.x, chunkcoordintpair.z, playerchunk.getTicketLevel(), optional.isPresent(), optional.map(IChunkAccess::getChunkStatus).orElse(null), optional1.map(Chunk::getState).orElse(null), a(playerchunk.c()), a(playerchunk.a()), a(playerchunk.b()), this.distanceManager.c(entry.getLongKey()), !this.isOutsideOfRange(chunkcoordintpair), optional1.map((chunk) -> {
                 return chunk.getTileEntities().size();
             }).orElse(0));
         }
@@ -852,7 +892,7 @@
 
     private static String a(CompletableFuture<Either<Chunk, PlayerChunk.Failure>> completablefuture) {
         try {
-            Either<Chunk, PlayerChunk.Failure> either = (Either) completablefuture.getNow((Object) null);
+            Either<Chunk, PlayerChunk.Failure> either = (Either) completablefuture.getNow(null); // CraftBukkit - decompile error
 
             return either != null ? (String) either.map((chunk) -> {
                 return "done";
@@ -870,7 +910,7 @@
     private NBTTagCompound readChunkData(ChunkCoordIntPair chunkcoordintpair) throws IOException {
         NBTTagCompound nbttagcompound = this.read(chunkcoordintpair);
 
-        return nbttagcompound == null ? null : this.getChunkData(this.level.getDimensionKey(), this.overworldDataStorage, nbttagcompound);
+        return nbttagcompound == null ? null : this.getChunkData(this.level.getTypeKey(), this.overworldDataStorage, nbttagcompound, chunkcoordintpair, level); // CraftBukkit
     }
 
     boolean isOutsideOfRange(ChunkCoordIntPair chunkcoordintpair) {
@@ -1233,7 +1273,7 @@
         public final Set<ServerPlayerConnection> seenBy = Sets.newIdentityHashSet();
 
         public EntityTracker(Entity entity, int i, int j, boolean flag) {
-            this.serverEntity = new EntityTrackerEntry(PlayerChunkMap.this.level, entity, j, flag, this::broadcast);
+            this.serverEntity = new EntityTrackerEntry(PlayerChunkMap.this.level, entity, j, flag, this::broadcast, seenBy); // CraftBukkit
             this.entity = entity;
             this.range = i;
             this.lastSectionPos = SectionPosition.a(entity);
@@ -1286,10 +1326,18 @@
 
         public void updatePlayer(EntityPlayer entityplayer) {
             if (entityplayer != this.entity) {
-                Vec3D vec3d = entityplayer.getPositionVector().d(this.serverEntity.b());
+                Vec3D vec3d = entityplayer.getPositionVector().d(this.entity.getPositionVector()); // MC-155077, SPIGOT-5113
                 int i = Math.min(this.b(), (PlayerChunkMap.this.viewDistance - 1) * 16);
                 boolean flag = vec3d.x >= (double) (-i) && vec3d.x <= (double) i && vec3d.z >= (double) (-i) && vec3d.z <= (double) i && this.entity.a(entityplayer);
 
+                // CraftBukkit start - respect vanish API
+                if (this.entity instanceof EntityPlayer) {
+                    Player player = ((EntityPlayer) this.entity).getBukkitEntity();
+                    if (!entityplayer.getBukkitEntity().canSee(player)) {
+                        flag = false;
+                    }
+                }
+                // CraftBukkit end
                 if (flag) {
                     if (this.seenBy.add(entityplayer.connection)) {
                         this.serverEntity.b(entityplayer);
