Class MycelDataFile

java.lang.Object
my.celium.org.data.MycelDataFile

public final class MycelDataFile extends Object
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 Details

    • DEFAULT_MAX_BYTES

      public static final int DEFAULT_MAX_BYTES
      Default size cap: larger files are treated as broken.
      See Also:
  • Method Details

    • at

      public static MycelDataFile at(ModMetadata mod, Path path)
      A file at an explicit path.
    • inConfigDir

      public static MycelDataFile inConfigDir(ModMetadata mod, String fileName)
      A file in the loader's config folder, 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

      public MycelDataFile withGson(com.google.gson.Gson gson)
      The same file, (de)serialized with gson (e.g. with type adapters).
    • withMaxBytes

      public MycelDataFile withMaxBytes(int maxBytes)
      The same file with another size cap in bytes.
    • path

      public Path 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

      public <T> T read(Class<T> type, Supplier<T> fallback)
      Reads the file into type with this file's Gson. Missing or broken files yield fallback.get(); a broken file is moved aside first. Never returns null unless fallback does.
    • read

      public <T> T read(Type type, Supplier<T> fallback)
      Like read(Class, Supplier) for generic types (use a Gson TypeToken).
    • write

      public boolean write(Object value)
      Writes value (serialized with this file's Gson). Returns whether it was saved.
    • quarantine

      public 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. 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 null when nothing moved
    • delete

      public boolean delete()
      Deletes the file if present. Returns whether it is gone.