Configuration
Create a simple Config file
General
Registered Config files will be saved in the default config directory, most times config/
.
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 nameCONFIG.getPath()
— returns the path where the config file is savedCONFIG.save()
— saves the current values to the json file, recommended, if the values are changed on the runtimeCONFIG.sendToPlayer(player)
— re-syncs the@Synchronize
fields to the specified playerCONFIG.sendToPlayers(serverLavel)
— re-syncs the@Synchronize
fields to every player registered in theServerLevel
ConfigLoader.getConfigSyncTag(config)
— returns theCompoundTag
reads the@Synchronize
fields in the specified config and creates aCompoundTag
containing themConfigLoader.sendConfigSyncPackages(player)
— rre-syncs every config to the specified playerConfigLoader.getConfigByName(name)
— returns the config, registered with the specified name ornull
, if no config was foundConfigLoader.getConfigNames(config)
— returns every config name, that matches the specified config UNSTABLE
Continue with Networking