2021-06-11 12:02:28 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Sat, 15 Jun 2019 08:54:33 -0700
Subject: [PATCH] Fix World#isChunkGenerated calls
Optimize World#loadChunk() too
This patch also adds a chunk status cache on region files (note that
its only purpose is to cache the status on DISK)
diff --git a/src/main/java/net/minecraft/server/level/ChunkHolder.java b/src/main/java/net/minecraft/server/level/ChunkHolder.java
2021-06-17 19:12:40 +00:00
index ddaf9d682772d5dbbe6ee4891f9995a8ee365329..a3692debdf8c0df1f785794bff29235b95750809 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/level/ChunkHolder.java
+++ b/src/main/java/net/minecraft/server/level/ChunkHolder.java
2021-06-13 13:46:28 +00:00
@@ -111,6 +111,19 @@ public class ChunkHolder {
2021-06-11 12:02:28 +00:00
Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure> either = (Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>) statusFuture.getNow(null);
2021-06-13 13:46:28 +00:00
return (either == null) ? null : (LevelChunk) either.left().orElse(null);
2021-06-11 12:02:28 +00:00
}
+
+ public ChunkAccess getAvailableChunkNow() {
+ // TODO can we just getStatusFuture(EMPTY)?
+ for (ChunkStatus curr = ChunkStatus.FULL, next = curr.getPreviousStatus(); curr != next; curr = next, next = next.getPreviousStatus()) {
+ CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> future = this.getFutureIfPresentUnchecked(curr);
+ Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure> either = future.getNow(null);
+ if (either == null || !either.left().isPresent()) {
+ continue;
+ }
+ return either.left().get();
+ }
+ return null;
+ }
2021-06-13 13:46:28 +00:00
// CraftBukkit end
2021-06-11 12:02:28 +00:00
public CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> getFutureIfPresentUnchecked(ChunkStatus leastStatus) {
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
2021-06-17 19:12:40 +00:00
index 2c2f0497e34c58c358b04e72c9d27ef1eead4a42..39bf531bce103161cbd836d3a1eb882627cd89a6 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
2021-06-13 13:46:28 +00:00
@@ -84,6 +84,7 @@ import net.minecraft.world.level.chunk.ProtoChunk;
import net.minecraft.world.level.chunk.UpgradeData;
import net.minecraft.world.level.chunk.storage.ChunkSerializer;
import net.minecraft.world.level.chunk.storage.ChunkStorage;
+import net.minecraft.world.level.chunk.storage.RegionFile;
import net.minecraft.world.level.entity.ChunkStatusUpdateListener;
import net.minecraft.world.level.levelgen.structure.StructureStart;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
2021-06-16 20:34:49 +00:00
@@ -1081,12 +1082,61 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2021-06-13 13:46:28 +00:00
// Paper end
2021-06-11 12:02:28 +00:00
@Nullable
- private CompoundTag readChunk(ChunkPos pos) throws IOException {
+ public CompoundTag readChunk(ChunkPos pos) throws IOException { // Paper - private -> public
CompoundTag nbttagcompound = this.read(pos);
+ // Paper start - Cache chunk status on disk
+ if (nbttagcompound == null) {
+ return null;
+ }
+
+ nbttagcompound = this.getChunkData(this.level.getTypeKey(), this.overworldDataStorage, nbttagcompound, pos, level); // CraftBukkit
+ if (nbttagcompound == null) {
+ return null;
+ }
+
+ this.updateChunkStatusOnDisk(pos, nbttagcompound);
+
+ return nbttagcompound;
+ // Paper end
+ }
+
+ // Paper start - chunk status cache "api"
+ public ChunkStatus getChunkStatusOnDiskIfCached(ChunkPos chunkPos) {
2021-06-13 13:46:28 +00:00
+ RegionFile regionFile = regionFileCache.getRegionFileIfLoaded(chunkPos);
- return nbttagcompound == null ? null : this.getChunkData(this.level.getTypeKey(), this.overworldDataStorage, nbttagcompound, pos, level); // CraftBukkit
2021-06-11 12:02:28 +00:00
+ return regionFile == null ? null : regionFile.getStatusIfCached(chunkPos.x, chunkPos.z);
2021-06-13 13:46:28 +00:00
}
2021-06-11 12:02:28 +00:00
+ public ChunkStatus getChunkStatusOnDisk(ChunkPos chunkPos) throws IOException {
2021-06-13 13:46:28 +00:00
+ RegionFile regionFile = regionFileCache.getFile(chunkPos, true);
2021-06-11 12:02:28 +00:00
+
2021-06-13 13:46:28 +00:00
+ if (regionFile == null || !regionFileCache.chunkExists(chunkPos)) {
2021-06-11 12:02:28 +00:00
+ return null;
+ }
+
+ ChunkStatus status = regionFile.getStatusIfCached(chunkPos.x, chunkPos.z);
+
+ if (status != null) {
+ return status;
+ }
+
+ this.readChunk(chunkPos);
2021-06-13 13:46:28 +00:00
+
2021-06-11 12:02:28 +00:00
+ return regionFile.getStatusIfCached(chunkPos.x, chunkPos.z);
2021-06-13 13:46:28 +00:00
+ }
+
2021-06-11 12:02:28 +00:00
+ public void updateChunkStatusOnDisk(ChunkPos chunkPos, @Nullable CompoundTag compound) throws IOException {
2021-06-13 13:46:28 +00:00
+ RegionFile regionFile = regionFileCache.getFile(chunkPos, false);
2021-06-11 12:02:28 +00:00
+
+ regionFile.setStatus(chunkPos.x, chunkPos.z, ChunkSerializer.getStatus(compound));
+ }
+
+ public ChunkAccess getUnloadingChunk(int chunkX, int chunkZ) {
+ ChunkHolder chunkHolder = this.pendingUnloads.get(ChunkPos.asLong(chunkX, chunkZ));
+ return chunkHolder == null ? null : chunkHolder.getAvailableChunkNow();
+ }
+ // Paper end
+
2021-06-13 13:46:28 +00:00
boolean noPlayersCloseForSpawning(ChunkPos chunkPos) {
2021-06-11 12:02:28 +00:00
// Spigot start
2021-06-13 13:46:28 +00:00
return this.isOutsideOfRange(chunkPos, false);
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2021-06-17 19:12:40 +00:00
index 672f78d1560ccd45d1a0ca04d6daf8188105c3e9..734b204d53a4b9e9b1112efe3a50b53cf1aa9f47 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2021-06-13 13:46:28 +00:00
@@ -324,6 +324,7 @@ public class ServerChunkCache extends ChunkSource {
2021-06-11 12:02:28 +00:00
}
// Paper end
2021-06-13 13:46:28 +00:00
// Paper start - async chunk io
+ @Nullable
public ChunkAccess getChunkAtImmediately(int x, int z) {
ChunkHolder holder = this.chunkMap.getVisibleChunkIfPresent(ChunkPos.asLong(x, z));
if (holder == null) {
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/ChunkStatus.java b/src/main/java/net/minecraft/world/level/chunk/ChunkStatus.java
2021-06-13 13:46:28 +00:00
index 6e0cf8ee76143301c939fc4af5eeb091abdcbc5c..066f03ee7b4feda9ec2b0984ee7cf63fa0b9e4fc 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/level/chunk/ChunkStatus.java
+++ b/src/main/java/net/minecraft/world/level/chunk/ChunkStatus.java
2021-06-13 13:46:28 +00:00
@@ -204,6 +204,7 @@ public class ChunkStatus {
2021-06-11 12:02:28 +00:00
return this.name;
}
+ public ChunkStatus getPreviousStatus() { return this.getParent(); } // Paper - OBFHELPER
public ChunkStatus getParent() {
return this.parent;
}
2021-06-13 13:46:28 +00:00
@@ -224,6 +225,17 @@ public class ChunkStatus {
2021-06-11 12:02:28 +00:00
return this.chunkType;
}
+ // Paper start
+ public static ChunkStatus getStatus(String name) {
+ try {
+ // We need this otherwise we return EMPTY for invalid names
+ ResourceLocation key = new ResourceLocation(name);
+ return Registry.CHUNK_STATUS.getOptional(key).orElse(null);
+ } catch (Exception ex) {
+ return null; // invalid name
+ }
+ }
+ // Paper end
public static ChunkStatus byName(String id) {
return (ChunkStatus) Registry.CHUNK_STATUS.get(ResourceLocation.tryParse(id));
}
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkSerializer.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkSerializer.java
2021-06-16 09:18:08 +00:00
index 21b3da831cd959e3fd85d437e1ba3c7a6c72502f..1c975b686c1e335d46e63ab12e0a97dd2dcaba13 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkSerializer.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkSerializer.java
2021-06-16 09:18:08 +00:00
@@ -544,6 +544,17 @@ public class ChunkSerializer {
2021-06-13 13:46:28 +00:00
return nbttagcompound;
2021-06-11 12:02:28 +00:00
}
+ // Paper start
+ public static ChunkStatus getStatus(CompoundTag compound) {
+ if (compound == null) {
+ return null;
+ }
+
+ // Note: Copied from below
+ return ChunkStatus.getStatus(compound.getCompound("Level").getString("Status"));
+ }
+ // Paper end
+
2021-06-13 13:46:28 +00:00
public static ChunkStatus.ChunkType getChunkTypeFromTag(@Nullable CompoundTag nbt) {
if (nbt != null) {
ChunkStatus chunkstatus = ChunkStatus.byName(nbt.getCompound("Level").getString("Status"));
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
2021-06-16 06:25:38 +00:00
index 26ad58e7a73e63d2393eb277984be20472157539..3dae5a4d13118c232529d4d5af29bf7b76ac2a7c 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
2021-06-16 06:25:38 +00:00
@@ -27,6 +27,7 @@ import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
2021-06-11 12:02:28 +00:00
import net.minecraft.world.level.ChunkPos;
+import net.minecraft.world.level.chunk.ChunkStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-06-16 06:25:38 +00:00
@@ -54,6 +55,30 @@ public class RegionFile implements AutoCloseable {
2021-06-13 13:46:28 +00:00
public final java.util.concurrent.locks.ReentrantLock fileLock = new java.util.concurrent.locks.ReentrantLock(true); // Paper
2021-06-16 06:25:38 +00:00
public final File regionFile; // Paper
2021-06-11 12:02:28 +00:00
+ // Paper start - Cache chunk status
+ private final ChunkStatus[] statuses = new ChunkStatus[32 * 32];
+
+ private boolean closed;
+
+ // invoked on write/read
+ public void setStatus(int x, int z, ChunkStatus status) {
+ if (this.closed) {
+ // We've used an invalid region file.
+ throw new IllegalStateException("RegionFile is closed");
+ }
+ this.statuses[getChunkLocation(x, z)] = status;
+ }
+
+ public ChunkStatus getStatusIfCached(int x, int z) {
+ if (this.closed) {
+ // We've used an invalid region file.
+ throw new IllegalStateException("RegionFile is closed");
+ }
+ final int location = getChunkLocation(x, z);
+ return this.statuses[location];
+ }
+ // Paper end
+
public RegionFile(File file, File directory, boolean dsync) throws IOException {
this(file.toPath(), directory.toPath(), RegionFileVersion.VERSION_DEFLATE, dsync);
}
2021-06-16 06:25:38 +00:00
@@ -402,6 +427,7 @@ public class RegionFile implements AutoCloseable {
2021-06-11 12:02:28 +00:00
return this.getOffset(pos) != 0;
}
+ private static int getChunkLocation(int x, int z) { return (x & 31) + (z & 31) * 32; } // Paper - OBFHELPER - sort of, mirror of logic below
private static int getOffsetIndex(ChunkPos pos) {
return pos.getRegionLocalX() + pos.getRegionLocalZ() * 32;
}
2021-06-16 06:25:38 +00:00
@@ -412,6 +438,7 @@ public class RegionFile implements AutoCloseable {
2021-06-13 13:46:28 +00:00
synchronized (this) {
try {
// Paper end
2021-06-11 12:02:28 +00:00
+ this.closed = true; // Paper
try {
this.padToFullSector();
} finally {
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java
2021-06-16 06:25:38 +00:00
index f53268f2d7d2d1909d64d06bb6a61086386830e1..e1b9051f8537db6f023cfdeaca4fb89305ece363 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java
2021-06-16 06:25:38 +00:00
@@ -219,6 +219,7 @@ public class RegionFileStorage implements AutoCloseable {
2021-06-11 12:02:28 +00:00
2021-06-13 13:46:28 +00:00
try {
NbtIo.write(nbt, (DataOutput) dataoutputstream);
+ regionfile.setStatus(pos.x, pos.z, ChunkSerializer.getStatus(nbt)); // Paper - cache status on disk
2021-06-16 06:25:38 +00:00
regionfile.setOversized(pos.x, pos.z, false); // Paper - We don't do this anymore, mojang stores differently, but clear old meta flag if it exists to get rid of our own meta file once last oversized is gone
2021-06-13 13:46:28 +00:00
} catch (Throwable throwable) {
if (dataoutputstream != null) {
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2021-06-14 01:06:38 +00:00
index 98b2d054b6436e3fdb8fadd03369a65cf4156843..f9c58de7fa8b3c2ab5ac78cf0b366df69e0b40df 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2021-06-13 13:46:28 +00:00
@@ -20,6 +20,7 @@ import java.util.Objects;
2021-06-11 12:02:28 +00:00
import java.util.Random;
import java.util.Set;
import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import net.minecraft.core.BlockPos;
2021-06-14 01:06:38 +00:00
@@ -418,8 +419,22 @@ public class CraftWorld implements World {
2021-06-11 12:02:28 +00:00
@Override
public boolean isChunkGenerated(int x, int z) {
+ // Paper start - Fix this method
+ if (!Bukkit.isPrimaryThread()) {
+ return CompletableFuture.supplyAsync(() -> {
+ return CraftWorld.this.isChunkGenerated(x, z);
+ }, world.getChunkSource().mainThreadProcessor).join();
+ }
+ ChunkAccess chunk = world.getChunkSource().getChunkAtImmediately(x, z);
+ if (chunk == null) {
+ chunk = world.getChunkSource().chunkMap.getUnloadingChunk(x, z);
+ }
+ if (chunk != null) {
+ return chunk instanceof ImposterProtoChunk || chunk instanceof net.minecraft.world.level.chunk.LevelChunk;
+ }
try {
2021-06-13 13:46:28 +00:00
- return this.world.getChunkSource().getChunkAtIfCachedImmediately(x, z) != null || this.world.getChunkSource().chunkMap.read(new ChunkPos(x, z)) != null; // Paper (TODO check if the first part can be removed)
2021-06-11 12:02:28 +00:00
+ return world.getChunkSource().chunkMap.getChunkStatusOnDisk(new ChunkPos(x, z)) == ChunkStatus.FULL;
+ // Paper end
} catch (IOException ex) {
throw new RuntimeException(ex);
}
2021-06-14 01:06:38 +00:00
@@ -530,20 +545,48 @@ public class CraftWorld implements World {
2021-06-11 12:02:28 +00:00
@Override
public boolean loadChunk(int x, int z, boolean generate) {
org.spigotmc.AsyncCatcher.catchOp("chunk load"); // Spigot
2021-06-13 13:46:28 +00:00
- ChunkAccess chunk = this.world.getChunkSource().getChunk(x, z, generate || isChunkGenerated(x, z) ? ChunkStatus.FULL : ChunkStatus.EMPTY, true); // Paper
2021-06-11 12:02:28 +00:00
+ // Paper start - Optimize this method
+ ChunkPos chunkPos = new ChunkPos(x, z);
- // If generate = false, but the chunk already exists, we will get this back.
- if (chunk instanceof ImposterProtoChunk) {
- // We then cycle through again to get the full chunk immediately, rather than after the ticket addition
2021-06-13 13:46:28 +00:00
- chunk = this.world.getChunkSource().getChunk(x, z, ChunkStatus.FULL, true);
2021-06-11 12:02:28 +00:00
- }
+ if (!generate) {
+ ChunkAccess immediate = world.getChunkSource().getChunkAtImmediately(x, z);
+ if (immediate == null) {
+ immediate = world.getChunkSource().chunkMap.getUnloadingChunk(x, z);
+ }
+ if (immediate != null) {
+ if (!(immediate instanceof ImposterProtoChunk) && !(immediate instanceof net.minecraft.world.level.chunk.LevelChunk)) {
+ return false; // not full status
+ }
+ world.getChunkSource().addRegionTicket(TicketType.PLUGIN, chunkPos, 1, Unit.INSTANCE);
+ world.getChunk(x, z); // make sure we're at ticket level 32 or lower
+ return true;
+ }
- if (chunk instanceof net.minecraft.world.level.chunk.LevelChunk) {
2021-06-13 13:46:28 +00:00
- this.world.getChunkSource().addRegionTicket(TicketType.PLUGIN, new ChunkPos(x, z), 1, Unit.INSTANCE);
2021-06-11 12:02:28 +00:00
- return true;
+ net.minecraft.world.level.chunk.storage.RegionFile file;
+ try {
2021-06-13 13:46:28 +00:00
+ file = world.getChunkSource().chunkMap.regionFileCache.getFile(chunkPos, false);
2021-06-11 12:02:28 +00:00
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ }
+
+ ChunkStatus status = file.getStatusIfCached(x, z);
+ if (!file.hasChunk(chunkPos) || (status != null && status != ChunkStatus.FULL)) {
+ return false;
+ }
+
+ ChunkAccess chunk = world.getChunkSource().getChunk(x, z, ChunkStatus.EMPTY, true);
+ if (!(chunk instanceof ImposterProtoChunk) && !(chunk instanceof net.minecraft.world.level.chunk.LevelChunk)) {
+ return false;
+ }
+
+ // fall through to load
+ // we do this so we do not re-read the chunk data on disk
}
- return false;
+ world.getChunkSource().addRegionTicket(TicketType.PLUGIN, chunkPos, 1, Unit.INSTANCE);
+ world.getChunkSource().getChunk(x, z, ChunkStatus.FULL, true);
+ return true;
+ // Paper end
}
@Override