Skip to content

Custom Cloning

GMalvestiti edited this page Aug 31, 2026 · 2 revisions

Cloning defaults to a JSON round-trip through the config model, which is correct for any model but adds overhead proportional to the number of fields. Replace it with a hand-written copy when profiling shows copy() is on a hot path:

public final class MyModConfigCloner implements StateCloner<MyModConfig> {

    @Override
    public MyModConfig copy(MyModConfig source) {
        MyModConfig copy = new MyModConfig();
        copy.hudScale = source.hudScale;
        copy.showHints = source.showHints;
        copy.hiddenHints = new ArrayList<>(source.hiddenHints);   // deep-copy the list
        return copy;
    }
}
@Config(
    name = "mymod",
    stateCloner = MyModConfigCloner.class
)
public final class MyModConfig {
    public int hudScale;
}
LiteConfig.holder(MyModConfig.class)
    .modId("mymod")
    .create();

A custom StateCloner needs a no-argument constructor and must preserve every persisted field in a fully independent object. The cloner belongs to the config declaration, so every holder uses the same copy behavior regardless of its runtime base directory. Deep-copy mutable children such as lists, maps, and nested settings; immutable values may be reused safely.

Clone this wiki locally