2021-06-13 21:29:58 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Fri, 27 Dec 2019 09:42:26 -0800
Subject: [PATCH] Guard against serializing mismatching chunk coordinate
Should help if something dumb happens
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
2024-01-24 13:07:40 +01:00
index 24892ae367e19038625e243bcdf1bb694632ede5..b4ea37dfa90f0c19914ecd32ddedde586d9d5f78 100644
2021-06-13 21:29:58 +02: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
2024-01-24 13:07:40 +01:00
@@ -94,8 +94,20 @@ public class ChunkSerializer {
2021-06-13 21:29:58 +02:00
public ChunkSerializer() {}
+ // Paper start - guard against serializing mismatching coordinates
+ // TODO Note: This needs to be re-checked each update
2024-01-24 13:07:40 +01:00
+ public static ChunkPos getChunkCoordinate(final CompoundTag chunkData) {
2021-11-25 08:47:39 +01:00
+ final int dataVersion = ChunkStorage.getVersion(chunkData);
+ if (dataVersion < 2842) { // Level tag is removed after this version
+ final CompoundTag levelData = chunkData.getCompound("Level");
+ return new ChunkPos(levelData.getInt("xPos"), levelData.getInt("zPos"));
+ } else {
+ return new ChunkPos(chunkData.getInt("xPos"), chunkData.getInt("zPos"));
+ }
2021-06-13 21:29:58 +02:00
+ }
2024-01-21 12:53:04 +01:00
+ // Paper end - guard against serializing mismatching coordinates
2024-01-24 13:07:40 +01:00
public static ProtoChunk read(ServerLevel world, PoiManager poiStorage, ChunkPos chunkPos, CompoundTag nbt) {
2021-11-24 08:37:09 +01:00
- ChunkPos chunkcoordintpair1 = new ChunkPos(nbt.getInt("xPos"), nbt.getInt("zPos"));
2024-01-21 12:53:04 +01:00
+ ChunkPos chunkcoordintpair1 = new ChunkPos(nbt.getInt("xPos"), nbt.getInt("zPos")); // Paper - guard against serializing mismatching coordinates; diff on change, see ChunkSerializer#getChunkCoordinate
2021-06-13 21:29:58 +02:00
2021-11-24 08:37:09 +01:00
if (!Objects.equals(chunkPos, chunkcoordintpair1)) {
2022-03-01 06:43:03 +01:00
ChunkSerializer.LOGGER.error("Chunk file at {} is in the wrong location; relocating. (Expected {}, got {})", new Object[]{chunkPos, chunkPos, chunkcoordintpair1});
2021-06-13 21:29:58 +02:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
2024-01-24 13:07:40 +01:00
index 6c0f87535ffa95cf82ab4b03bb7bf8f2132d275f..ab6b4491d4278b3839b7363d9890bd12a757e4cc 100644
2021-06-13 21:29:58 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
2024-01-24 13:07:40 +01:00
@@ -157,6 +157,13 @@ public class ChunkStorage implements AutoCloseable {
}
2021-06-13 21:29:58 +02:00
2024-01-24 13:07:40 +01:00
public void write(ChunkPos chunkPos, CompoundTag nbt) {
2024-01-21 12:53:04 +01:00
+ // Paper start - guard against serializing mismatching coordinates
2023-06-09 01:12:34 +02:00
+ if (nbt != null && !chunkPos.equals(ChunkSerializer.getChunkCoordinate(nbt))) {
2024-01-24 13:07:40 +01:00
+ final String world = (this instanceof net.minecraft.server.level.ChunkMap) ? ((net.minecraft.server.level.ChunkMap) this).level.getWorld().getName() : null;
+ throw new IllegalArgumentException("Chunk coordinate and serialized data do not have matching coordinates, trying to serialize coordinate " + chunkPos
+ + " but compound says coordinate is " + ChunkSerializer.getChunkCoordinate(nbt) + (world == null ? " for an unknown world" : (" for world: " + world)));
2021-06-13 21:29:58 +02:00
+ }
2024-01-21 12:53:04 +01:00
+ // Paper end - guard against serializing mismatching coordinates
2024-01-24 13:07:40 +01:00
this.worker.store(chunkPos, nbt);
2021-06-13 21:29:58 +02:00
if (this.legacyStructureHandler != null) {
2024-01-24 13:07:40 +01:00
this.legacyStructureHandler.removeIndex(chunkPos.toLong());