Class ConfigManager

java.lang.Object
org.moddingx.libx.config.ConfigManager

public class ConfigManager extends Object
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: If a class uses generics, the ? 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 Details

    • ConfigManager

      public ConfigManager()
  • Method Details

    • registerValueMapper

      public static void registerValueMapper(String modid, ValueMapper<?,?> mapper)
      Registers a new ValueMapper that can be used to serialise config values.
    • registerValueMapper

      public static void registerValueMapper(String modid, GenericValueMapper<?,?,?> mapper)
      Registers a new GenericValueMapper that can be used to serialise config values.
    • registerValueMapperFactory

      public static void registerValueMapperFactory(String modid, MapperFactory<?> factory)
      Registers a new MapperFactory that can be used to create value mappers based on the generic type of the config key.
    • registerConfigValidator

      public static void registerConfigValidator(String modid, ConfigValidator<?,?> validator)
      Registers a new ConfigValidator that can be used to validate config values.
    • registerConfig

      public static void registerConfig(String modid, Class<?> configClass, boolean clientConfig)
      Registers a config. This will register a config with the id modid:config which means the config will be located in config/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 in config/namespace/path.json5. Exception is a path of config. In this case the config will be located at config/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. Use forceResync(ServerPlayer) for this.
    • reloadClient

      public static void reloadClient()
      Forces reload of all client configs.
    • reloadConfig

      public static void reloadConfig(Class<?> configClass)
      Forces reload of one config. This will not sync the config though. Use forceResync(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

      public static void forceResync(@Nullable net.minecraft.server.level.ServerPlayer player)
      Forces a resync of all configs to one player.
    • configs

      public static Set<net.minecraft.resources.ResourceLocation> configs()
      Gets all registered config ids.