-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a44a06
commit 7a7aca2
Showing
2 changed files
with
86 additions
and
43 deletions.
There are no files selected for viewing
102 changes: 77 additions & 25 deletions
102
src/main/java/gregtech/api/persistence/PersistentData.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 |
---|---|---|
@@ -1,56 +1,108 @@ | ||
package gregtech.api.persistence; | ||
|
||
import gregtech.api.GTValues; | ||
import gregtech.api.util.GTLog; | ||
|
||
import net.minecraftforge.common.config.Configuration; | ||
import net.minecraft.nbt.CompressedStreamTools; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraftforge.fml.common.Loader; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public final class PersistentData { | ||
|
||
public static final String CATEGORY_NAME = "persistent data"; | ||
|
||
private static final PersistentData INSTANCE = new PersistentData(); | ||
private static final String FILE_NAME = "persistent_data.cfg"; | ||
|
||
private @Nullable Configuration config; | ||
private @Nullable Path path; | ||
private @Nullable NBTTagCompound tag; | ||
|
||
public static @NotNull PersistentData instance() { | ||
return INSTANCE; | ||
} | ||
|
||
private PersistentData() {} | ||
|
||
@ApiStatus.Internal | ||
public void init() { | ||
this.path = Loader.instance().getConfigDir().toPath() | ||
.resolve(GTValues.MODID) | ||
.resolve("persistent_data.dat"); | ||
} | ||
|
||
/** | ||
* @return the persistent data storage | ||
* @return the stored persistent data | ||
*/ | ||
public @NotNull Configuration getConfig() { | ||
if (config == null) { | ||
Path configFolderPath = Loader.instance().getConfigDir().toPath().resolve(GTValues.MODID); | ||
File file = configFolderPath.resolve(FILE_NAME).toFile(); | ||
config = new Configuration(file); | ||
public synchronized @NotNull NBTTagCompound getTag() { | ||
if (this.tag == null) { | ||
this.tag = read(); | ||
} | ||
return config; | ||
return this.tag; | ||
} | ||
|
||
@ApiStatus.Internal | ||
public void init() { | ||
Configuration configuration = getConfig(); | ||
configuration.load(); | ||
String comment = """ | ||
GregTech Persistent Data. Items in this file will persist across game loads. | ||
Modifications to this file may be overwritten by GT. | ||
If you are a modpack author, you should ship this file in releases."""; | ||
configuration.addCustomCategoryComment(CATEGORY_NAME, comment); | ||
|
||
if (configuration.hasChanged()) { | ||
configuration.save(); | ||
/** | ||
* @return the read NBTTagCompound from disk | ||
*/ | ||
private @NotNull NBTTagCompound read() { | ||
GTLog.logger.debug("Reading persistent data from path {}", path); | ||
if (this.path == null) { | ||
throw new IllegalStateException("Persistent data path cannot be null"); | ||
} | ||
|
||
if (!Files.exists(path)) { | ||
return new NBTTagCompound(); | ||
} | ||
|
||
try (InputStream inputStream = Files.newInputStream(this.path)) { | ||
return CompressedStreamTools.readCompressed(inputStream); | ||
} catch (IOException e) { | ||
GTLog.logger.error("Failed to read persistent data", e); | ||
return new NBTTagCompound(); | ||
} | ||
} | ||
|
||
/** | ||
* Save the GT Persistent data to disk | ||
*/ | ||
public synchronized void save() { | ||
if (this.tag != null) { | ||
write(this.tag); | ||
} | ||
} | ||
|
||
/** | ||
* @param tagCompound the tag compound to save to disk | ||
*/ | ||
private void write(@NotNull NBTTagCompound tagCompound) { | ||
GTLog.logger.debug("Writing persistent data to path {}", path); | ||
if (tagCompound.isEmpty()) { | ||
return; | ||
} | ||
|
||
if (this.path == null) { | ||
throw new IllegalStateException("Persistent data path cannot be null"); | ||
} | ||
|
||
if (!Files.exists(path)) { | ||
try { | ||
Files.createDirectories(path.getParent()); | ||
} catch (IOException e) { | ||
GTLog.logger.error("Could not create persistent data dir", e); | ||
return; | ||
} | ||
} | ||
|
||
try (OutputStream outputStream = Files.newOutputStream(path)) { | ||
CompressedStreamTools.writeCompressed(tagCompound, outputStream); | ||
} catch (IOException e) { | ||
GTLog.logger.error("Failed to write persistent data", e); | ||
} | ||
} | ||
} |
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