Configuration

Create a simple Config file

General

Registered Config files will be saved in the default config directory, most times config/.

  1. General
  2. Structure
  3. Registering
  4. Synchronized Values
  5. Useful methods

Structure

Effectively every class that implements Config can be read as Configuration. It’s highly recommended to override the getName() method to save performance!

The following example configuration class…

public final class ImplConfig implements Config {
    public boolean someBoolean = true;
    public List<String> someList = new ArrayList<>() {
        {
            add("123");
        }
    };
    @Synchronize
    public String syncedValue = "";

    @Override
    public String getName() {
        return TestMod.MODID;
    }
}

…will result in the following json file:

{
  "someBoolean": true,
  "someList": [
    "123"
  ],
  "syncedValue": ""
}

You’ll notice, the fields will be read and interpreted as keys. The values you defined will be the default values and will be overwritten by the values of the generated json file.

Registering

You also need to register the config file for craftedcore, to be recognized:

class TestMod {
    public static final String MODID = "testmod";
    public static final ImplConfig CONFIG = ConfigLoader.read(MODID, ImplConfig.class);
}

To avoid duplicate configs, most modders use their modid as config name!

Synchronized Values

By annotating config files with @Synchronize, the config value will be send to the clients when they join. After they leave, their client value will be restored.

Useful methods

  • CONFIG.getName() — returns the config name
  • CONFIG.getPath() — returns the path where the config file is saved
  • CONFIG.save() — saves the current values to the json file, recommended, if the values are changed on the runtime
  • CONFIG.sendToPlayer(player) — re-syncs the @Synchronize fields to the specified player
  • CONFIG.sendToPlayers(serverLavel) — re-syncs the @Synchronize fields to every player registered in the ServerLevel
  • ConfigLoader.getConfigSyncTag(config) — returns the CompoundTag reads the @Synchronize fields in the specified config and creates a CompoundTag containing them
  • ConfigLoader.sendConfigSyncPackages(player) — rre-syncs every config to the specified player
  • ConfigLoader.getConfigByName(name) — returns the config, registered with the specified name or null, if no config was found
  • ConfigLoader.getConfigNames(config) — returns every config name, that matches the specified config UNSTABLE

Continue with Networking