Package org.moddingx.libx.config
Class ConfigManager
java.lang.Object
org.moddingx.libx.config.ConfigManager
Provides a config system for configuration files that is meant to be more easy and powerful than
the system by forge based on
NightConfig
. This system creates
json files with comments based on a class. That class may contain fields with @Config
annotations. Each field with a config annotation will get one value in the config file. To create sub
groups, you can create static nested classes inside the base class. Suppose you have the following
class structure:
public class ExampleConfig {
@Config
("A value")
public static int
value = 23;
@Config
({"Multiline Comments", "are also possible"})
@DoubleRange
(min
= 0)
public static double
another_value;
@Config
("A component")
public static Component
component = Component
.literal
("LibX is fancy");
public static class SubGroup {
@Config
public static List
<Integer
> valueList = List
.of
(1, 5, 23);
}
}
This would create the following config file:
{
// Multiline Comments
// are also possible
// Minimum: 0
"another_value": 0.0,
// A component
"tc": {
"text": "LibX is fancy"
},
// A value
"value": 23,
"SubGroup": {
"coolValues": [
1, 5, 23
]
}
}
The values of the fields are the default values for the config.
Fields can have any type you want as long as you provide a ValueMapper
for that type.
You need to register that type via registerValueMapper(String, ValueMapper)
(or
registerValueMapper(String, GenericValueMapper)
for generic value mappers).
Then you can use that type in a config. Custom registered value mappers are unique for each mod, so
you and another mod can add different value mappers for the same class. However, you can't add two
value mappers for the same class in one mod.
By default the following types are supported:
- boolean
- byte
- short
- int
- long
- float
- double
String
Optional<?>
List<?>
Set<?>
Map<String, ?>
ResourceLocation
Ingredient
IngredientStack
Component
ResourceList
UUID
- Any
enum
- Any
record
- Any
Pair<?, ?>
- Any
Triple<?, ?, ?>
?
can be any type that is supported by the config system. So
you can also use a List
<Pair
<List
<Integer
>, String
>>
.
Each config field can also have one validator annotation applied to validate a value. You can
find builtin validator annotations in org.moddingx.libx.config.validator
. To register
you own validator, use registerConfigValidator(String, ConfigValidator)
.
Configs come in two different types: Common configs and client configs. Common configs are loaded on
both the dedicated server and the client and are synced from server to client. Client configs are
only loaded on the client.
A config is registered with registerConfig(ResourceLocation, Class, boolean)
.
You can then just use the values in the config class. Make sure to not modify them as the results
are unpredictable.
Config values may never be null in the code. However value mappers are allowed to produce json-null
values. If you need a nullable value in the config, use an Optional. Empty Optionals will translate
to null in the JSON.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Set<net.minecraft.resources.ResourceLocation>
configs()
Gets all registered config ids.static void
forceResync
(net.minecraft.server.level.ServerPlayer player) Forces a resync of all configs to one player.static void
forceResync
(net.minecraft.server.level.ServerPlayer player, Class<?> configClass) Forces a resync of one config to one player.static void
registerConfig
(String modid, Class<?> configClass, boolean clientConfig) Registers a config.static void
registerConfig
(net.minecraft.resources.ResourceLocation location, Class<?> configClass, boolean clientConfig) Registers a config.static void
registerConfigValidator
(String modid, ConfigValidator<?, ?> validator) Registers a newConfigValidator
that can be used to validate config values.static void
registerValueMapper
(String modid, GenericValueMapper<?, ?, ?> mapper) Registers a newGenericValueMapper
that can be used to serialise config values.static void
registerValueMapper
(String modid, ValueMapper<?, ?> mapper) Registers a newValueMapper
that can be used to serialise config values.static void
registerValueMapperFactory
(String modid, MapperFactory<?> factory) Registers a newMapperFactory
that can be used to create value mappers based on the generic type of the config key.static void
Forces reload of all client configs.static void
Forces reload of all common configs.static void
reloadConfig
(Class<?> configClass) Forces reload of one config.
-
Constructor Details
-
ConfigManager
public ConfigManager()
-
-
Method Details
-
registerValueMapper
Registers a newValueMapper
that can be used to serialise config values. -
registerValueMapper
Registers a newGenericValueMapper
that can be used to serialise config values. -
registerValueMapperFactory
Registers a newMapperFactory
that can be used to create value mappers based on the generic type of the config key. -
registerConfigValidator
Registers a newConfigValidator
that can be used to validate config values. -
registerConfig
Registers a config. This will register a config with the idmodid:config
which means the config will be located inconfig/modid.json5
.- Parameters:
modid
- The modid of the mod.configClass
- The base class for the config.clientConfig
- Whether this is a client config.
-
registerConfig
public static void registerConfig(net.minecraft.resources.ResourceLocation location, Class<?> configClass, boolean clientConfig) Registers a config.- Parameters:
location
- The id of the config. The config will be located inconfig/namespace/path.json5
. Exception is a path ofconfig
. In this case the config will be located atconfig/modid.json5
.configClass
- The base class for the config.clientConfig
- Whether this is a client config.
-
reloadCommon
public static void reloadCommon()Forces reload of all common configs. This will not sync the config though. UseforceResync(ServerPlayer)
for this. -
reloadClient
public static void reloadClient()Forces reload of all client configs. -
reloadConfig
Forces reload of one config. This will not sync the config though. UseforceResync(ServerPlayer, Class)
for this. -
forceResync
public static void forceResync(@Nullable net.minecraft.server.level.ServerPlayer player, Class<?> configClass) Forces a resync of one config to one player. -
forceResync
Forces a resync of all configs to one player. -
configs
Gets all registered config ids.
-