A simple and readable way to manage mod settings is by using TOML files. This format allows you to organize settings into categories and entries, with comments that make it easier to read and edit manually.
public static final String MOD_ID = "modid";
public static TomlConfigHandler CFG;
private static final Toml TOML = new Toml("A comment"); // General comment for the TOML file
// Add a category called "IDs" with two entries
TOML.addCategory("IDs")
.addEntry("starting_item_id", 19000)
.addEntry("starting_block_id", 11000);
// Initialize the config handler with the MOD_ID and the TOML configuration
CFG = new TomlConfigHandler(MOD_ID, TOML);
.toml
output:# A comment
[IDs] # Category
starting_item_id = 19000 # Values
starting_block_id = 11000
.minecraft\config\yourmodid.cfg
project\run\config\yourmodid.cfg
You can read values using getters matching the data type:
getBoolean()
getDouble()
getFloat()
getLong()
getInt()
Example to get an integer value:
int value = CFG.getInt("CategoryName.value_name");
Gamerules provide a simple, accessible way to add configurable options that players can change directly in-game. They have several advantages:
Minecraft provides different gamerule types in the package net.minecraft.core.data.gamerule
.
For example, to create a boolean gamerule:
public static GameRuleBoolean MY_GAMERULE = null;
static {
// Register the gamerule with a unique name and a default value (true here)
MY_GAMERULE = GameRules.register(new GameRuleBoolean("myGameruleName", true));
}
GameRuleBoolean
) — for toggling options on or off.GameRuleInteger
) — for numeric values.GameRuleFloat
) — for decimal values.