From 75e0449b11f33b386a0370deb470aa46a842445d Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Tue, 24 Dec 2024 14:16:16 -0800 Subject: [PATCH] Fix forcedchunk.dat saving (should be uncompressed) --- .../hodgepodge/core/HodgepodgeCore.java | 14 ++++++++--- .../MixinForgeChunkManager_threadedIO.java | 2 +- .../MixinSaveHandler_threadedIO.java | 2 +- .../MixinMazeHandler_threadedIO.java | 2 +- .../hodgepodge/util/WorldDataSaver.java | 24 ++++++++++--------- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/mitchej123/hodgepodge/core/HodgepodgeCore.java b/src/main/java/com/mitchej123/hodgepodge/core/HodgepodgeCore.java index 54276d6f..483dbd9b 100644 --- a/src/main/java/com/mitchej123/hodgepodge/core/HodgepodgeCore.java +++ b/src/main/java/com/mitchej123/hodgepodge/core/HodgepodgeCore.java @@ -44,11 +44,19 @@ public class HodgepodgeCore implements IFMLLoadingPlugin, IEarlyMixinLoader { } public static void saveWorldData(File file, NBTTagCompound tag) { - WorldDataSaver.INSTANCE.saveData(file, tag); + WorldDataSaver.INSTANCE.saveData(file, tag, true, false); } - public static void saveWorldData(File file, NBTTagCompound tag, boolean backup) { - WorldDataSaver.INSTANCE.saveData(file, tag, backup); + public static void saveWorldDataUncompressed(File file, NBTTagCompound tag) { + WorldDataSaver.INSTANCE.saveData(file, tag, false, false); + } + + public static void saveWorldDataBackup(File file, NBTTagCompound tag) { + WorldDataSaver.INSTANCE.saveData(file, tag, true, true); + } + + public static void savfeWorldDataUncompressedBackup(File file, NBTTagCompound tag) { + WorldDataSaver.INSTANCE.saveData(file, tag, false, true); } private String[] transformerClasses; diff --git a/src/main/java/com/mitchej123/hodgepodge/mixins/early/forge/MixinForgeChunkManager_threadedIO.java b/src/main/java/com/mitchej123/hodgepodge/mixins/early/forge/MixinForgeChunkManager_threadedIO.java index 90d27d96..14fea9c4 100644 --- a/src/main/java/com/mitchej123/hodgepodge/mixins/early/forge/MixinForgeChunkManager_threadedIO.java +++ b/src/main/java/com/mitchej123/hodgepodge/mixins/early/forge/MixinForgeChunkManager_threadedIO.java @@ -21,6 +21,6 @@ public class MixinForgeChunkManager_threadedIO { value = "INVOKE", target = "Lnet/minecraft/nbt/CompressedStreamTools;write(Lnet/minecraft/nbt/NBTTagCompound;Ljava/io/File;)V")) private static void redirectWrite(NBTTagCompound forcedChunkNBTData, File chunkLoaderFile) throws IOException { - HodgepodgeCore.saveWorldData(chunkLoaderFile, forcedChunkNBTData); + HodgepodgeCore.saveWorldDataUncompressed(chunkLoaderFile, forcedChunkNBTData); } } diff --git a/src/main/java/com/mitchej123/hodgepodge/mixins/early/minecraft/MixinSaveHandler_threadedIO.java b/src/main/java/com/mitchej123/hodgepodge/mixins/early/minecraft/MixinSaveHandler_threadedIO.java index 9e4f3e5a..952214ce 100644 --- a/src/main/java/com/mitchej123/hodgepodge/mixins/early/minecraft/MixinSaveHandler_threadedIO.java +++ b/src/main/java/com/mitchej123/hodgepodge/mixins/early/minecraft/MixinSaveHandler_threadedIO.java @@ -28,7 +28,7 @@ public class MixinSaveHandler_threadedIO { private void injectSaveWorldDataWithPlayer(WorldInfo worldInfo, NBTTagCompound playerTag, CallbackInfo ci, @Local(ordinal = 2) NBTTagCompound nbttagcompound2) { File file = new File(((SaveHandler) (Object) this).getWorldDirectory(), "level.dat"); - HodgepodgeCore.saveWorldData(file, nbttagcompound2, true); + HodgepodgeCore.saveWorldDataBackup(file, nbttagcompound2); ci.cancel(); } } diff --git a/src/main/java/com/mitchej123/hodgepodge/mixins/late/thaumcraft/MixinMazeHandler_threadedIO.java b/src/main/java/com/mitchej123/hodgepodge/mixins/late/thaumcraft/MixinMazeHandler_threadedIO.java index baabb619..b3328cf3 100644 --- a/src/main/java/com/mitchej123/hodgepodge/mixins/late/thaumcraft/MixinMazeHandler_threadedIO.java +++ b/src/main/java/com/mitchej123/hodgepodge/mixins/late/thaumcraft/MixinMazeHandler_threadedIO.java @@ -41,7 +41,7 @@ public static void saveMaze(World world) { final File file = new File(world.getSaveHandler().getWorldDirectory(), filename); - HodgepodgeCore.saveWorldData(file, parentTag, true); + HodgepodgeCore.saveWorldDataBackup(file, parentTag); } } diff --git a/src/main/java/com/mitchej123/hodgepodge/util/WorldDataSaver.java b/src/main/java/com/mitchej123/hodgepodge/util/WorldDataSaver.java index fa25ffa7..8745876f 100644 --- a/src/main/java/com/mitchej123/hodgepodge/util/WorldDataSaver.java +++ b/src/main/java/com/mitchej123/hodgepodge/util/WorldDataSaver.java @@ -23,10 +23,12 @@ public class WorldDataSaver implements IThreadedFileIO { static class WrappedNBTTagCompound { public final NBTTagCompound tag; + public final boolean compressed; public final boolean backup; - public WrappedNBTTagCompound(NBTTagCompound tag, boolean backup) { + public WrappedNBTTagCompound(NBTTagCompound tag, boolean compressed, boolean backup) { this.tag = tag; + this.compressed = compressed; this.backup = backup; } } @@ -42,6 +44,7 @@ public boolean writeNextIO() { final File file; final WrappedNBTTagCompound wrapped; final NBTTagCompound data; + final boolean compressed; final boolean backup; synchronized (pendingData) { Iterator> it = pendingData.entrySet().iterator(); @@ -53,6 +56,7 @@ public boolean writeNextIO() { wrapped = entry.getValue(); data = wrapped.tag; backup = wrapped.backup; + compressed = wrapped.compressed; it.remove(); } @@ -65,10 +69,12 @@ public boolean writeNextIO() { } try { - LOGGER.debug("Writing data to file {}", file); - FileOutputStream fileoutputstream = new FileOutputStream(file); - CompressedStreamTools.writeCompressed(data, fileoutputstream); - fileoutputstream.close(); + LOGGER.info("Writing data to file {}{}", file, compressed ? " (compressed)" : ""); + if (compressed) { + try (FileOutputStream fileoutputstream = new FileOutputStream(file)) { + CompressedStreamTools.writeCompressed(data, fileoutputstream); + } + } else CompressedStreamTools.write(data, file); } catch (IOException e) { LOGGER.error("Failed to write data to file {}", file, e); @@ -77,12 +83,8 @@ public boolean writeNextIO() { return true; } - public void saveData(File file, NBTTagCompound parentTag) { - saveData(file, parentTag, false); - } - - public void saveData(File file, NBTTagCompound parentTag, boolean backup) { - WrappedNBTTagCompound wrapped = new WrappedNBTTagCompound(parentTag, backup); + public void saveData(File file, NBTTagCompound parentTag, boolean compressed, boolean backup) { + WrappedNBTTagCompound wrapped = new WrappedNBTTagCompound(parentTag, compressed, backup); if (pendingData.containsKey(file)) { pendingData.replace(file, wrapped); } else {