11package mekanism .common .lib ;
22
3- import java .io .File ;
4- import java .io .IOException ;
5- import java .nio .file .AtomicMoveNotSupportedException ;
6- import java .nio .file .Files ;
7- import java .nio .file .Path ;
8- import java .nio .file .StandardCopyOption ;
93import java .util .function .Supplier ;
104import mekanism .common .Mekanism ;
115import net .minecraft .core .HolderLookup ;
@@ -20,31 +14,6 @@ public abstract class MekanismSavedData extends SavedData {
2014
2115 public abstract void load (@ NotNull CompoundTag nbt , @ NotNull HolderLookup .Provider provider );
2216
23- @ Override
24- public void save (@ NotNull File file , @ NotNull HolderLookup .Provider provider ) {
25- if (isDirty ()) {
26- //This is loosely based on Refined Storage's RSSavedData's system of saving first to a temp file
27- // to reduce the odds of corruption if the user's computer crashes while the file is being written
28- Path targetPath = file .toPath ();
29- Path tempPath = file .toPath ().getParent ().resolve (file .getName () + ".tmp" );
30- File tempFile = tempPath .toFile ();
31- super .save (tempFile , provider );
32- //Based on Applied Energistics' AESavedData by starting to try with using an atomic move, and then only falling back to the replacing
33- if (tempFile .exists ()) {
34- //Note: We check that the temp file exists, as if it doesn't that means we failed to write it and super will log the failure
35- try {
36- try {
37- Files .move (tempPath , targetPath , StandardCopyOption .ATOMIC_MOVE );
38- } catch (AtomicMoveNotSupportedException ignored ) {
39- Files .move (tempPath , targetPath , StandardCopyOption .REPLACE_EXISTING );
40- }
41- } catch (IOException e ) {
42- Mekanism .logger .error ("Could not replace save data {} with new value" , this , e );
43- }
44- }
45- }
46- }
47-
4817 /**
4918 * Note: This should only be called from the server side
5019 */
0 commit comments