Configuration
As of 1.2.3
, this is outdated. Refer to this test class instead for the time being.
Configs can be very useful to the player, since they can be used to customize variables and behaviours present in a mod.
A simple example can be creating a toggle for a block being added to the game.
Creating your configs​
First we have to declare our Configs, which we can do using ConfigBase
.
public static final ConfigBase YOURMOD_CONFIGS = new ConfigBase("yourmodid");
Once that is done, a folder with your modid
will be created in the game's config
directory, and in that folder will be located all the other files necessary for both resources as well as the yourmodid.properties
file.
Such a file can look something like this:
# yourmodid Configs
# Sat Jul 31 15:05:00 CET 2022
# Lock yourmodid configs from being regenerated
yourmodidConfigLocked = true
# Lock yourmodid resources from being regenerated
yourmodidResourceLocked = true
The first 2 properties will always be autogenerated with ConfigBase
and are used to determine, respectively, if the config files and if the resource files are locked (no need to regenerate them on startup).
This can also be utilized by the user, especially when an unintentional edit was made to the config files.
Simply editing the value to false
will have those specific files be regenerated at startup and the property reset back to true
.
Adding properties​
Using our ConfigBase
object we can call different methods, one of which is addProperty()
, used to add properties to both our object and the yourmodid.properties
file.
You can provide a comment to the property, which will appear in the config file, or simply not have one.
YOURMOD_CONFIGS.addProperty("enableRubyOre", true, "Enables the Ruby Ore block")
YOURMOD_CONFIGS.addProperty("enableRubyItem", true)
To register a property we have to follow it up with the method registerProperties()
.
YOURMOD_CONFIGS.addProperty("enableRubyOre", true, "Enables the Ruby Ore block").registerProperties();
You can concatenate multiple addProperty()
and have the method registerProperties()
at the end.
YOURMOD_CONFIGS.addProperty("enableRubyOre", true, "Enables the Ruby Ore block")
.addProperty("enableRubyItem", true)
.registerProperties();
Now your yourmodid.properties
file will look something like this:
# yourmodid Configs
# Sat Jul 31 15:10:00 CET 2022
# Lock yourmodid configs from being regenerated
yourmodidConfigLocked = true
# Lock yourmodid resources from being regenerated
yourmodidResourceLocked = true
# Enables the Ruby Ore block
enableRubyOre = true
enableRubyItem = true
Using properties​
Interacting with your yourmodid.properties
file can be done through the ConfigHandler
class.
One of the available methods is getBooleanOption()
, which returns the Boolean value of the specified property from the config file, or the default value if it isn't present.
if (ConfigHandler.getBooleanOption(YOURMOD_CONFIGS, "enableRubyOre", true)){
// Do something
}
Result​
Your code should now look something like this:
public static final ConfigBase YOURMOD_CONFIGS = new ConfigBase("yourmodid");
@Override
public void onInitialize() {
YOURMOD_CONFIGS.addProperty("enableRubyOre", true, "Enables the Ruby Ore block")
.registerProperties();
if (ConfigHandler.getBooleanOption(YOURMOD_CONFIGS, "enableRubyOre", true)){
// Do something
}
}