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 neoforge 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
StringOptional<?>List<?>Set<?>Map<String, ?>ResourceLocationComponentResourceListUUID- 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Set<net.minecraft.resources.ResourceLocation> configs()Gets all registered config ids.static voidregisterConfig(String modid, Class<?> configClass, boolean clientConfig) Registers a config.static voidregisterConfig(net.minecraft.resources.ResourceLocation location, Class<?> configClass, boolean clientConfig) Registers a config.static voidregisterConfigValidator(String modid, ConfigValidator<?, ?> validator) Registers a newConfigValidatorthat can be used to validate config values.static voidregisterValueMapper(String modid, GenericValueMapper<?, ?, ?> mapper) Registers a newGenericValueMapperthat can be used to serialise config values.static voidregisterValueMapper(String modid, ValueMapper<?, ?> mapper) Registers a newValueMapperthat can be used to serialise config values.static voidregisterValueMapperFactory(String modid, MapperFactory<?> factory) Registers a newMapperFactorythat can be used to create value mappers based on the generic type of the config key.static voidForces reload of all client configs.static voidForces reload of all common configs.static voidreloadConfig(Class<?> configClass) Forces reload of one config.static voidsynchronize(net.minecraft.server.level.ServerPlayer player) Forces synchronisation of all configs to one player.static voidsynchronize(net.minecraft.server.level.ServerPlayer player, Class<?> configClass) Forces synchronisation of one config to one player.static voidsynchronize(net.minecraft.server.MinecraftServer server) Forces synchronisation of all configs to all players.static voidsynchronize(net.minecraft.server.MinecraftServer server, Class<?> configClass) Forces synchronisation of one config to all players.
-
Constructor Details
-
ConfigManager
public ConfigManager()
-
-
Method Details
-
registerValueMapper
Registers a newValueMapperthat can be used to serialise config values. -
registerValueMapper
Registers a newGenericValueMapperthat can be used to serialise config values. -
registerValueMapperFactory
Registers a newMapperFactorythat can be used to create value mappers based on the generic type of the config key. -
registerConfigValidator
Registers a newConfigValidatorthat can be used to validate config values. -
registerConfig
Registers a config. This will register a config with the idmodid:configwhich 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. Usesynchronize(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. Usesynchronize(ServerPlayer, Class)for this. -
synchronize
Forces synchronisation of one config to all players. -
synchronize
public static void synchronize(net.minecraft.server.MinecraftServer server) Forces synchronisation of all configs to all players. -
synchronize
public static void synchronize(net.minecraft.server.level.ServerPlayer player, Class<?> configClass) Forces synchronisation of one config to one player. -
synchronize
public static void synchronize(net.minecraft.server.level.ServerPlayer player) Forces synchronisation of all configs to one player. -
configs
Gets all registered config ids.
-