Saving Runnable: A callback method that triggers immediately when the user confirms (saves) the changes on the layout config screen. Use this block to run your mod's file serialization methods.
Serializing and Deserializing Data
While FramingLib provides configuration UI utilities, it remains completely format-agnostic and does not lock you into a single config format.
Because the library uses factory creation patterns (like AlignmentSizeOffset.create()), standard reflective serialization doesn't work. You must handle serializing and deserializing these objects yourself.
The example below uses Google's GSON library, which is the preferred approach for many projects, including mine. However, this is just an example, and you are entirely free to serialize the data into TOML, YAML, or any other format you prefer.
Saving and Loading Alignments/Alignment Size Offset with GSON
Configuration Class
To handle your layout data with GSON, you must first define a config class for your mod's configuration.
Declare the values you want to store as instance fields. You must provide the fields with default values, or include a no-argument default constructor for this class to define and initialize the default values when no configuration file exists on the disk yet (first launch, or if the file is deleted, or if there's an error loading the file). You can optionally include getter and setter methods to access the fields.
publicclassModConfig{// The alignment size offset object we will store in our configprivateAlignmentSizeOffsetlayoutAlignmentSizeOffset;// Default constructor to initialize our config with default valuespublicModConfig(){this.layoutAlignmentSizeOffset=AlignmentSizeOffset.create(40,100,16,16,Alignments.create(Alignments.HAlignment.MIDDLE,Alignments.VAlignment.TOP),Alignments.create(Alignments.HAlignment.LEFT,Alignments.VAlignment.TOP));}// Getter for the alignment size offsetpublicAlignmentSizeOffsetgetAlignmentSizeOffset(){returnthis.layoutAlignmentSizeOffset;}}
Custom GSON Type Adapter Implementation
To handle your layout data with GSON, you must create a custom type adapter that implements both JsonSerializer<AlignmentSizeOffset> and JsonDeserializer<AlignmentSizeOffset>.
When saving, extract properties directly from your persistent AlignmentSizeOffset object. When loading, read the fields from the JSON structure, and pass them back through the create() method to re-initialize your persistent AlignmentSizeOffset object.
Now, when your mod initializes, you can simply load your config using ModConfigManager.initializeConfig(). You can pass ModConfigManager.save() to your layout config screen builder to save the changes to a .json file when the user clicks Save.
You can also use the ModConfigManager.getConfig() or ModConfigManager.getDefault() methods throughout your mod to gain quick access to either the current config, or the default fallback config.