From 8c5be166861b662d0b4aa775f0e38c8acb9a30f1 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Sun, 19 Dec 2021 09:15:22 -0800
Subject: [PATCH] Only write chunk data to disk if it serializes without
 throwing

This ensures at least a valid version of the chunk exists
on disk, even if outdated
---
 ...-data-to-disk-if-it-serializes-witho.patch | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 patches/server/0832-Only-write-chunk-data-to-disk-if-it-serializes-witho.patch

diff --git a/patches/server/0832-Only-write-chunk-data-to-disk-if-it-serializes-witho.patch b/patches/server/0832-Only-write-chunk-data-to-disk-if-it-serializes-witho.patch
new file mode 100644
index 000000000..f6365f90b
--- /dev/null
+++ b/patches/server/0832-Only-write-chunk-data-to-disk-if-it-serializes-witho.patch
@@ -0,0 +1,38 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Spottedleaf <Spottedleaf@users.noreply.github.com>
+Date: Sun, 19 Dec 2021 09:13:41 -0800
+Subject: [PATCH] Only write chunk data to disk if it serializes without
+ throwing
+
+This ensures at least a valid version of the chunk exists
+on disk, even if outdated
+
+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
+index 2dde10324e515bd58fc6ba7e93156ae783492cc2..c7216cf3317cbd49b032c44feb370c50928dd21e 100644
+--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java
++++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileStorage.java
+@@ -298,10 +298,11 @@ public class RegionFileStorage implements AutoCloseable {
+                 NbtIo.write(nbt, (DataOutput) dataoutputstream);
+                 regionfile.setStatus(pos.x, pos.z, ChunkSerializer.getStatus(nbt)); // Paper - cache status on disk
+                 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
++                dataoutputstream.close(); // Paper - only write if successful
+             } catch (Throwable throwable) {
+                 if (dataoutputstream != null) {
+                     try {
+-                        dataoutputstream.close();
++                        //dataoutputstream.close(); // Paper - don't write garbage data to disk if writing serialization fails
+                     } catch (Throwable throwable1) {
+                         throwable.addSuppressed(throwable1);
+                     }
+@@ -309,10 +310,7 @@ public class RegionFileStorage implements AutoCloseable {
+ 
+                 throw throwable;
+             }
+-
+-            if (dataoutputstream != null) {
+-                dataoutputstream.close();
+-            }
++            // Paper - move into try block to only write if successfully serialized
+         }
+ 
+             // Paper start