-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
83 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...n/java/com/refinedmods/refinedstorage2/platform/common/support/AbstractSafeSavedData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.refinedmods.refinedstorage2.platform.common.support; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.AtomicMoveNotSupportedException; | ||
import java.nio.file.Files; | ||
import java.nio.file.StandardCopyOption; | ||
|
||
import com.mojang.logging.LogUtils; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.NbtIo; | ||
import net.minecraft.nbt.NbtUtils; | ||
import net.minecraft.world.level.saveddata.SavedData; | ||
import org.slf4j.Logger; | ||
|
||
public abstract class AbstractSafeSavedData extends SavedData { | ||
private static final Logger LOGGER = LogUtils.getLogger(); | ||
|
||
@Override | ||
public void save(final File file) { | ||
if (!isDirty()) { | ||
return; | ||
} | ||
final var targetPath = file.toPath().toAbsolutePath(); | ||
final var tempFile = targetPath.getParent().resolve(file.getName() + ".temp"); | ||
final CompoundTag compoundTag = new CompoundTag(); | ||
compoundTag.put("data", this.save(new CompoundTag())); | ||
NbtUtils.addCurrentDataVersion(compoundTag); | ||
try { | ||
// Write to temp file first. | ||
NbtIo.writeCompressed(compoundTag, tempFile); | ||
// Try atomic move | ||
try { | ||
Files.move(tempFile, targetPath, StandardCopyOption.ATOMIC_MOVE); | ||
} catch (final AtomicMoveNotSupportedException ignored) { | ||
Files.move(tempFile, targetPath, StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
} catch (final IOException e) { | ||
LOGGER.error("Could not save data {}", this, e); | ||
} | ||
setDirty(false); | ||
} | ||
} |