--- a/net/minecraft/server/PlayerChunkMap.java +++ b/net/minecraft/server/PlayerChunkMap.java @@ -42,6 +42,7 @@ import org.apache.commons.lang3.mutable.MutableBoolean; 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.d { @@ -72,6 +73,31 @@ private final Queue A; private 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 Runnable queued; + + @Override + public void execute(Runnable runnable) { + if (queued != null) { + throw new IllegalStateException("Already queued"); + } + queued = runnable; + } + + @Override + public void run() { + Runnable task = queued; + queued = null; + if (task != null) { + task.run(); + } + } + }; + // CraftBukkit end + public PlayerChunkMap(WorldServer worldserver, File file, DataFixer datafixer, DefinedStructureManager definedstructuremanager, Executor executor, IAsyncTaskHandler iasynctaskhandler, ILightAccess ilightaccess, ChunkGenerator chunkgenerator, WorldLoadListener worldloadlistener, Supplier supplier, int i) { super(new File(worldserver.getWorldProvider().getDimensionManager().a(file), "region"), datafixer); this.visibleChunks = this.updatingChunks.clone(); @@ -191,9 +217,12 @@ return completablefuture1.thenApply((list1) -> { List 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 either = (Either) iterator.next(); Optional optional = either.left(); @@ -291,7 +320,7 @@ PlayerChunkMap.LOGGER.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", this.x.getName()); } else { this.visibleChunks.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); @@ -302,7 +331,6 @@ } } - protected void unloadChunks(BooleanSupplier booleansupplier) { GameProfilerFiller gameprofilerfiller = this.world.getMethodProfiler(); @@ -341,7 +369,7 @@ private void a(long i, PlayerChunk playerchunk) { CompletableFuture completablefuture = playerchunk.getChunkSave(); - Consumer consumer = (ichunkaccess) -> { + Consumer consumer = (ichunkaccess) -> { // CraftBukkit - decompile error CompletableFuture completablefuture1 = playerchunk.getChunkSave(); if (completablefuture1 != completablefuture) { @@ -490,7 +518,7 @@ return CompletableFuture.completedFuture(Either.right(playerchunk_failure)); }); }, (runnable) -> { - this.mailboxWorldGen.a((Object) ChunkTaskQueueSorter.a(playerchunk, runnable)); + this.mailboxWorldGen.a(ChunkTaskQueueSorter.a(playerchunk, runnable)); // CraftBukkit - decompile error }); } @@ -548,8 +576,15 @@ while (iterator.hasNext()) { Entity entity = (Entity) iterator.next(); + // CraftBukkit start - these are spawned serialized (DefinedStructure) and we don't call an add event below at the moment due to ordering complexities + boolean needsRemoval = false; + if (chunk.needsDecoration && !this.world.getServer().getServer().getSpawnNPCs() && entity instanceof NPC) { + entity.die(); + needsRemoval = true; + } - if (!(entity instanceof EntityHuman) && !this.world.addEntityChunk(entity)) { + if (!(entity instanceof EntityHuman) && (needsRemoval || !this.world.addEntityChunk(entity))) { + // CraftBukkit end if (list == null) { list = Lists.newArrayList(new Entity[]{entity}); } else { @@ -571,7 +606,7 @@ long i = playerchunk.i().pair(); playerchunk.getClass(); - mailbox.a((Object) ChunkTaskQueueSorter.a(runnable, i, playerchunk::getTicketLevel)); + mailbox.a(ChunkTaskQueueSorter.a(runnable, i, playerchunk::getTicketLevel)); // CraftBukkit - decompile error }); } @@ -588,7 +623,7 @@ return Either.left(chunk); }); }, (runnable) -> { - this.mailboxMain.a((Object) ChunkTaskQueueSorter.a(playerchunk, runnable)); + this.mailboxMain.a(ChunkTaskQueueSorter.a(playerchunk, runnable)); // CraftBukkit - decompile error }); completablefuture1.thenAcceptAsync((either) -> { @@ -602,7 +637,7 @@ return Either.left(chunk); }); }, (runnable) -> { - this.mailboxMain.a((Object) ChunkTaskQueueSorter.a(playerchunk, runnable)); + this.mailboxMain.a(ChunkTaskQueueSorter.a(playerchunk, runnable)); // CraftBukkit - decompile error }); return completablefuture1; } @@ -616,7 +651,7 @@ return chunk; }); }, (runnable) -> { - this.mailboxMain.a((Object) ChunkTaskQueueSorter.a(playerchunk, runnable)); + this.mailboxMain.a(ChunkTaskQueueSorter.a(playerchunk, runnable)); // CraftBukkit - decompile error }); } @@ -740,7 +775,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.chunkDistanceManager.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.chunkDistanceManager.c(entry.getLongKey()), !this.isOutsideOfRange(chunkcoordintpair), optional1.map((chunk) -> { return Stream.of(chunk.getEntitySlices()).mapToInt(EntitySlice::size).sum(); }).orElse(0), optional1.map((chunk) -> { return chunk.getTileEntities().size(); @@ -751,7 +787,7 @@ private static String a(CompletableFuture> completablefuture) { try { - Either either = (Either) completablefuture.getNow((Object) null); + Either either = (Either) completablefuture.getNow(null); // Craftbukkit - decompile error return either != null ? (String) either.map((chunk) -> { return "done"; @@ -769,7 +805,7 @@ private NBTTagCompound readChunkData(ChunkCoordIntPair chunkcoordintpair) throws IOException { NBTTagCompound nbttagcompound = this.read(chunkcoordintpair); - return nbttagcompound == null ? null : this.getChunkData(this.world.getWorldProvider().getDimensionManager(), this.m, nbttagcompound); + return nbttagcompound == null ? null : this.getChunkData(this.world.getWorldProvider().getDimensionManager(), this.m, nbttagcompound, chunkcoordintpair, world); // CraftBukkit } boolean isOutsideOfRange(ChunkCoordIntPair chunkcoordintpair) { @@ -1101,7 +1137,7 @@ public final Set trackedPlayers = Sets.newHashSet(); public EntityTracker(Entity entity, int i, int j, boolean flag) { - this.trackerEntry = new EntityTrackerEntry(PlayerChunkMap.this.world, entity, j, flag, this::broadcast); + this.trackerEntry = new EntityTrackerEntry(PlayerChunkMap.this.world, entity, j, flag, this::broadcast, trackedPlayers); // CraftBukkit this.tracker = entity; this.trackingDistance = i; this.e = SectionPosition.a(entity); @@ -1154,7 +1190,7 @@ public void updatePlayer(EntityPlayer entityplayer) { if (entityplayer != this.tracker) { - Vec3D vec3d = (new Vec3D(entityplayer.locX, entityplayer.locY, entityplayer.locZ)).d(this.trackerEntry.b()); + Vec3D vec3d = (new Vec3D(entityplayer.locX, entityplayer.locY, entityplayer.locZ)).d(this.tracker.getPositionVector()); // MC-155077, SPIGOT-5113 int i = Math.min(this.trackingDistance, (PlayerChunkMap.this.viewDistance - 1) * 16); boolean flag = vec3d.x >= (double) (-i) && vec3d.x <= (double) i && vec3d.z >= (double) (-i) && vec3d.z <= (double) i && this.tracker.a(entityplayer); @@ -1170,6 +1206,17 @@ } } + // CraftBukkit start - respect vanish API + if (this.tracker instanceof EntityPlayer) { + Player player = ((EntityPlayer) this.tracker).getBukkitEntity(); + if (!entityplayer.getBukkitEntity().canSee(player)) { + flag1 = false; + } + } + + entityplayer.removeQueue.remove(Integer.valueOf(this.tracker.getId())); + // CraftBukkit end + if (flag1 && this.trackedPlayers.add(entityplayer)) { this.trackerEntry.b(entityplayer); }