Skip to content

Creating A Config

RedstoneParadox edited this page Dec 24, 2020 · 5 revisions

Creating the Config Class

To setup your config, you will first need to create an object that extends RootConfigCategory. You will need to pass the name of the config file to the superclass constructor, including the file extension. Paradox Config provides support for json configs out of the box and json5 configs if you have Jankson Fabric installed. For this tutorial, we will be making a json5 config since it's generally preferred over json for this purpose.

object MyConfig: ConfigCategory("my_config.json5") {

}

Setting up loading and saving

Next you will need to go fabric.mod.json and add the paradoxconfig key with an object containing the package your config classes are in and an array with the config classes themselves (in case you want to have more than one).

  "custom": {
    "paradoxconfig": {
        "package": "my.config.package",
        "configs": [ "MyConfig" ]
    }
  }

After this, you should be able to run the game and ParadoxConfig will create your config file.

Adding Options

ParadoxConfig uses Kotlin delegates to provide option functionality. To create an option delegate, simply call the option method in your config class and pass a default value, option key, and comment if you wish (not all file formats support comments). If your option holds a numeric value, you can additionally pass a range after the default option to limit values to that range.

object MyConfig: ConfigCategory("my_config.json5") {
    var bool: Boolean by option(true, "bool", "I'm a boolean!")
    var number: Long by option(1L, "int")
    // Fun fact, this calls a different overload of the option function
    var ranged: Long by option(1L, 1L..3L, "ranged", "Power Level")
    // So does this.
    var fruits: MutableList<String> by option(mutableListOf("pear", "apple", "orange", "tomato"), "fruits", "Yummy fruits!")
    // And so does this.
    var dictionary: Map<String, Long> by option(mapOf("one" to 1L, "two" to 2L, "three" to 3L), "dictionary", "Map words to numbers.")
}

If you're using Jankson, your config file should now look like this:

{ 
	// I'm a boolean! [Values: true/false]
	"bool": true,
	// [Values: any number]
	"int": 1,
	// Power level [Values: any number in 1..3]
	"ranged": 1
	// Yummy fruits! [Collection of any string]
	"fruits": [ 
		"pear",
		"apple",
		"orange",
		"tomato"
	],
	// Map words to numbers. [Keys: any string, Values: any number]
	"dictionary": { 
		"two": 2,
		"three": 3,
		"one": 1
	},
}

Subcategories

Subcategories can be used to better organize the config file. To create a subcategory, add an inner object class of to a ConfigCategory that also extends ConfigCategory.

object MyConfig: ConfigCategory("my_config.json5") {
    // ...
    object Greetings: ConfigCategory("greetings", "A Bunch of Greetings!") {
        var english: MutableList<String> by option(mutableListOf("Hi!", "Hey!"), "english")
        var spanish: MutableList<String> by option(mutableListOf("Hola!"), "spanish")
    }
}

After running the game, your config file should now have a subcategory:

{ 
	// I'm a boolean! [Values: true/false]
	"bool": true,
	// [Values: any number]
	"int": 1,
	// Power level [Values: any number in 1..3]
	"ranged": 1
	// Yummy fruits! [Collection of any string]
	"fruits": [ 
		"pear",
		"apple",
		"orange",
		"tomato"
	],
	// Map words to numbers. [Keys: any string, Values: any number]
	"dictionary": { 
		"two": 2,
		"three": 3,
		"one": 1
	},

	// A Bunch of Greetings!
	"greetings": { 
		//  [Collection of any string]
		"english": [ 
			"Hi!",
			"Hey!"
		],
		//  [Collection of any string]
		"spanish": [ 
			"Hola!"
		]
	}
}

The full config class should look like this:

object MyConfig: ConfigCategory("my_config.json5") {
    var bool: Boolean by option(true, "bool")
    var number: Long by option(1L, "int")
    // Fun fact, this calls a different overload of the option function
    var ranged: Long by option(1L, 1L..3L, "ranged")
    // So does this.
    var fruits: MutableList<String> by option(mutableListOf("pear", "apple", "orange", "tomato"), "fruits")
    // And so does this.
    var dictionary: HashMap<String, Long> by option(hashMapOf("one" to 1L, "two" to 2L, "three" to 3L), "dictionary")

    object Greetings: ConfigCategory("my_sub_category", "A Bunch of Greetings!") {
        var english: MutableList<String> by option(mutableListOf("Hi!", "Hey!"), "english")
        var spanish: MutableList<String> by option(mutableListOf("Hola!"), "spanish")
    }
}
Clone this wiki locally