|
| 1 | +package com.falsepattern.lib.config; |
| 2 | + |
| 3 | +import cpw.mods.fml.client.event.ConfigChangedEvent; |
| 4 | +import cpw.mods.fml.common.eventhandler.SubscribeEvent; |
| 5 | +import lombok.AccessLevel; |
| 6 | +import lombok.NoArgsConstructor; |
| 7 | +import lombok.val; |
| 8 | +import lombok.var; |
| 9 | +import net.minecraft.launchwrapper.Launch; |
| 10 | +import net.minecraftforge.common.MinecraftForge; |
| 11 | +import net.minecraftforge.common.config.Configuration; |
| 12 | +import net.minecraftforge.common.config.Property; |
| 13 | + |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class for controlling the loading of configuration files. |
| 19 | + */ |
| 20 | +@NoArgsConstructor(access = AccessLevel.PRIVATE) |
| 21 | +public class ConfigurationManager { |
| 22 | + private static final Map<String, Set<Class<?>>> configs = new HashMap<>(); |
| 23 | + |
| 24 | + private static final ConfigurationManager instance = new ConfigurationManager(); |
| 25 | + |
| 26 | + private static boolean initialized = false; |
| 27 | + |
| 28 | + private static Path configDir; |
| 29 | + |
| 30 | + /** |
| 31 | + * Registers a configuration class to be loaded. |
| 32 | + * @param config The class to register. |
| 33 | + */ |
| 34 | + public static void registerConfig(Class<?> config) throws IllegalAccessException { |
| 35 | + val cfg = Optional.of(config.getAnnotation(Config.class)).orElseThrow(() -> new IllegalArgumentException("Class " + config.getName() + " does not have a @Config annotation!")); |
| 36 | + val cfgSet = configs.computeIfAbsent(cfg.modid(), (ignored) -> new HashSet<>()); |
| 37 | + cfgSet.add(config); |
| 38 | + processConfig(config); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Internal, do not use. |
| 43 | + */ |
| 44 | + public static void init() { |
| 45 | + if (initialized) return; |
| 46 | + configDir = Launch.minecraftHome.toPath().resolve("config"); |
| 47 | + MinecraftForge.EVENT_BUS.register(instance); |
| 48 | + initialized = true; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Internal, do not use. |
| 53 | + * @param event The event. |
| 54 | + */ |
| 55 | + @SubscribeEvent |
| 56 | + public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) { |
| 57 | + val configClasses = configs.get(event.modID); |
| 58 | + if (configClasses == null) |
| 59 | + return; |
| 60 | + configClasses.forEach((config) -> { |
| 61 | + try { |
| 62 | + processConfig(config); |
| 63 | + } catch (IllegalAccessException e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + private static void processConfig(Class<?> configClass) throws IllegalAccessException { |
| 70 | + val cfg = configClass.getAnnotation(Config.class); |
| 71 | + val category = cfg.category(); |
| 72 | + val rawConfig = new Configuration(configDir.resolve(cfg.name()).toFile()); |
| 73 | + val cat = rawConfig.getCategory(category); |
| 74 | + |
| 75 | + for (val field: configClass.getDeclaredFields()) { |
| 76 | + if (field.getAnnotation(Config.Ignore.class) != null) continue; |
| 77 | + field.setAccessible(true); |
| 78 | + val comment = Optional.of(field.getAnnotation(Config.Comment.class)).map(Config.Comment::value).map((lines) -> String.join("\n", lines)).orElse(""); |
| 79 | + val name = Optional.of(field.getAnnotation(Config.Name.class)).map(Config.Name::value).orElse(field.getName()); |
| 80 | + val langKey = Optional.of(field.getAnnotation(Config.LangKey.class)).map(Config.LangKey::value).orElse(name); |
| 81 | + var boxed = false; |
| 82 | + Property prop = cat.get(name); |
| 83 | + prop.comment = comment; |
| 84 | + prop.setLanguageKey(langKey); |
| 85 | + if ((boxed = field.getType().equals(Integer.class)) || field.getType().equals(int.class)) { |
| 86 | + val range = Optional.of(field.getAnnotation(Config.RangeInt.class)); |
| 87 | + prop.setMinValue(range.map(Config.RangeInt::min).orElse(Integer.MIN_VALUE)); |
| 88 | + prop.setMaxValue(range.map(Config.RangeInt::max).orElse(Integer.MAX_VALUE)); |
| 89 | + prop.setDefaultValue(Optional.of(field.getAnnotation(Config.DefaultInt.class)).map(Config.DefaultInt::value).orElse(boxed ? (Integer)field.get(null) : field.getInt(null))); |
| 90 | + field.setInt(null, prop.getInt()); |
| 91 | + } else if ((boxed = field.getType().equals(Double.class)) || field.getType().equals(double.class)) { |
| 92 | + val range = Optional.of(field.getAnnotation(Config.RangeDouble.class)); |
| 93 | + prop.setMinValue(range.map(Config.RangeDouble::min).orElse(Double.MIN_VALUE)); |
| 94 | + prop.setMaxValue(range.map(Config.RangeDouble::max).orElse(Double.MAX_VALUE)); |
| 95 | + prop.setDefaultValue(Optional.of(field.getAnnotation(Config.DefaultDouble.class)).map(Config.DefaultDouble::value).orElse(boxed ? (Double) field.get(null) : field.getDouble(null))); |
| 96 | + field.setDouble(null, prop.getDouble()); |
| 97 | + } else if ((boxed = field.getType().equals(Boolean.class)) || field.getType().equals(boolean.class)) { |
| 98 | + prop.setDefaultValue(boxed ? (Boolean)field.get(null) : field.getBoolean(null)); |
| 99 | + field.setBoolean(null, prop.getBoolean()); |
| 100 | + } else if (field.getType().equals(String.class)) { |
| 101 | + prop.setDefaultValue((String)field.get(null)); |
| 102 | + field.set(null, prop.getString()); |
| 103 | + } |
| 104 | + if (field.isAnnotationPresent(Config.RequiresMcRestart.class)) { |
| 105 | + cat.setRequiresMcRestart(true); |
| 106 | + } |
| 107 | + if (field.isAnnotationPresent(Config.RequiresWorldRestart.class)) { |
| 108 | + cat.setRequiresWorldRestart(true); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments