Class MycelDataFile
java.lang.Object
my.celium.org.data.MycelDataFile
One JSON file a mod keeps its own data in (pets, highlights, saved lists),
with the safety Mycel's config files have:
- writes are atomic (temp file + move), so a crash mid-save never leaves half-written JSON behind;
- a file that cannot be parsed is moved aside to
<name>.broken(never deleted or overwritten) and reading continues with the fallback, so the next save cannot destroy it; - files over the size cap (16 MiB by default) are refused the same way.
static final MycelDataFile FILE = MycelDataFile.inConfigDir(META, "mymod_data.json");
Root root = FILE.read(Root.class, Root::new); // never null, never throws
root.entries.put(key, value);
FILE.write(root);
Reading and writing are synchronous file IO; for big files call them from
MycelScheduler.runAsync and hop back with runOnServer.
Instances are immutable and thread-safe; callers own the locking of the
data they write.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault size cap: larger files are treated as broken. -
Method Summary
Modifier and TypeMethodDescriptionstatic MycelDataFileat(ModMetadata mod, Path path) A file at an explicit path.booleandelete()Deletes the file if present.booleanexists()static MycelDataFileinConfigDir(ModMetadata mod, String fileName) A file in the loader'sconfigfolder, e.g.static MycelDataFileinWorld(net.minecraft.server.MinecraftServer server, ModMetadata mod, String fileName) A file that belongs to the current world, at<world>/data/<modid>/<fileName>.path()quarantine(String reason) Moves the file aside to<name>.broken(or a timestamped name when that exists already) so the next write cannot overwrite it.<T> TReads the file intotypewith this file's Gson.<T> TLikeread(Class, Supplier)for generic types (use a GsonTypeToken).com.google.gson.JsonObjectReads the file as a JSON object.withGson(com.google.gson.Gson gson) The same file, (de)serialized withgson(e.g.withMaxBytes(int maxBytes) The same file with another size cap in bytes.booleanWritesvalue(serialized with this file's Gson).
-
Field Details
-
DEFAULT_MAX_BYTES
public static final int DEFAULT_MAX_BYTESDefault size cap: larger files are treated as broken.- See Also:
-
-
Method Details
-
at
A file at an explicit path. -
inConfigDir
A file in the loader'sconfigfolder, e.g.config/mymod_data.json. -
inWorld
public static MycelDataFile inWorld(net.minecraft.server.MinecraftServer server, ModMetadata mod, String fileName) A file that belongs to the current world, at<world>/data/<modid>/<fileName>. Use it for state that should travel with the save, not with the player's game folder. -
withGson
The same file, (de)serialized withgson(e.g. with type adapters). -
withMaxBytes
The same file with another size cap in bytes. -
path
-
exists
public boolean exists() -
readObject
public com.google.gson.JsonObject readObject()Reads the file as a JSON object. Missing file: an empty object. Broken file (unreadable, not JSON, not an object, too big): moved aside and an empty object. -
read
-
read
Likeread(Class, Supplier)for generic types (use a GsonTypeToken). -
write
Writesvalue(serialized with this file's Gson). Returns whether it was saved. -
quarantine
Moves the file aside to<name>.broken(or a timestamped name when that exists already) so the next write cannot overwrite it. Call it when the data parsed but your own validation rejected it. Missing files are ignored.- Returns:
- the path the file was moved to, or
nullwhen nothing moved
-
delete
public boolean delete()Deletes the file if present. Returns whether it is gone.
-