-
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.
adjust recipe input expected cache size dynamically (#2331)
- Loading branch information
1 parent
7e01d33
commit 251a9ac
Showing
3 changed files
with
210 additions
and
24 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
108 changes: 108 additions & 0 deletions
108
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package gregtech.api.persistence; | ||
|
||
import gregtech.api.GTValues; | ||
import gregtech.api.util.GTLog; | ||
|
||
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.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public final class PersistentData { | ||
|
||
private static final PersistentData INSTANCE = new PersistentData(); | ||
|
||
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 stored persistent data | ||
*/ | ||
public synchronized @NotNull NBTTagCompound getTag() { | ||
if (this.tag == null) { | ||
this.tag = read(); | ||
} | ||
return this.tag; | ||
} | ||
|
||
/** | ||
* @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