mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-28 07:35:51 +00:00
Make config options reloadable from file (#342)
Co-authored-by: Caedis <[email protected]>
This commit is contained in:
1 parent
3fef8965cb
commit
7cfa8f01a6
3 files changed
+64
-11
No files matched your search
@@ -31,6 +31,10 @@ public class ExampleConfig {
|
||||
@Config.Entry(ModIDEntry.class)
|
||||
public static String selectionList = "Mod ID";
|
||||
|
||||
@Config.Comment("A config option that can be reloaded to read changes made in config file")
|
||||
@Config.Reloadable("reload")
|
||||
public static String reloadableField = "I should be Reloadable";
|
||||
|
||||
@Config.Comment("This category button should have RED text.")
|
||||
@Config.Entry(RedCategory.class)
|
||||
@Config.Order(0)
|
||||
|
||||
@@ -301,4 +301,31 @@ public @interface Config {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* spotless:off
|
||||
* Marks a config field as reloadable from the backing config file.
|
||||
* Reloadable fields are not automatically refreshed when the file changes. Instead, they are re-read when
|
||||
* {@link ConfigurationManager#reloadConfig(Class, String)} is called with the owning config class and a matching
|
||||
* reload id.
|
||||
* Multiple fields may share the same reload id. Calling reload with that id reloads all matching fields in the
|
||||
* config class.
|
||||
* Example:
|
||||
* <pre>{@code
|
||||
* @Config.Reloadable("runtime")
|
||||
* public static String runtimeValue = "default";
|
||||
*
|
||||
* ConfigurationManager.reloadConfig(MyConfig.class, "runtime");
|
||||
* }</pre>
|
||||
* spotless:on
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface Reloadable {
|
||||
|
||||
/**
|
||||
* @return the id of the config option to reload
|
||||
*/
|
||||
String value();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,6 +48,7 @@ public class ConfigurationManager {
|
||||
private static final Map<Configuration, Set<String>> observedCategories = new HashMap<>();
|
||||
private static final Map<ConfigCategory, Class<?>> configEntries = new HashMap<>();
|
||||
private static final Map<Class<?>, ConfigNode> configNode = new HashMap<>();
|
||||
private static final Map<Config, Map<String, Set<Runnable>>> reloadables = new HashMap<>();
|
||||
|
||||
private final static Path configDir;
|
||||
|
||||
@@ -117,6 +118,22 @@ public class ConfigurationManager {
|
||||
savedConfigs.forEach(Configuration::save);
|
||||
}
|
||||
|
||||
public static void reloadConfig(Class<?> configClass, String id) {
|
||||
Config config = getClassOrBaseAnnotation(configClass, Config.class);
|
||||
if (config == null) {
|
||||
throw new ConfigException("Class " + configClass.getName() + " does not have a @Config annotation!");
|
||||
}
|
||||
Map<String, Set<Runnable>> toReload = reloadables.get(config);
|
||||
if (toReload == null) return;
|
||||
Set<Runnable> reloadables = toReload.get(id);
|
||||
Configuration rawConfig = configs.get(getConfigKey(config));
|
||||
if (reloadables == null || rawConfig == null) return;
|
||||
rawConfig.load();
|
||||
for (Runnable runnable : reloadables) {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
private static void save(Class<?> configClass, Object instance, Configuration rawConfig, String category)
|
||||
throws IllegalAccessException, ConfigException {
|
||||
for (final var field : configClass.getDeclaredFields()) {
|
||||
@@ -228,17 +245,10 @@ public class ConfigurationManager {
|
||||
|
||||
Config.Sync fieldSync = field.getAnnotation(Config.Sync.class);
|
||||
if (fieldSync != null && fieldSync.value() || syncCategory && fieldSync == null) {
|
||||
SyncedConfigElement element = new SyncedConfigElement(instance, field, () -> {
|
||||
try {
|
||||
ConfigFieldParser.loadField(instance, field, rawConfig, category, langKey);
|
||||
} catch (ConfigException e) {
|
||||
LOGGER.error(
|
||||
"Failed to restore synced field {} in class {}",
|
||||
fieldName,
|
||||
field.getDeclaringClass(),
|
||||
e);
|
||||
}
|
||||
});
|
||||
SyncedConfigElement element = new SyncedConfigElement(
|
||||
instance,
|
||||
field,
|
||||
() -> ConfigFieldParser.loadField(instance, field, rawConfig, category, langKey));
|
||||
ConfigSyncHandler.syncedElements.put(field.toString(), element);
|
||||
|
||||
if (!requiresWorldRestart) {
|
||||
@@ -246,6 +256,18 @@ public class ConfigurationManager {
|
||||
}
|
||||
}
|
||||
|
||||
Config.Reloadable reloadable = field.getAnnotation(Config.Reloadable.class);
|
||||
if (reloadable != null) {
|
||||
// Uses the config annotation as key since there can be multiple reloadables
|
||||
// with the same id in config class
|
||||
Config configAnno = getClassOrBaseAnnotation(configClass, Config.class);
|
||||
if (configAnno != null) {
|
||||
reloadables.computeIfAbsent(configAnno, (ignored) -> new HashMap<>())
|
||||
.computeIfAbsent(reloadable.value(), (ignored) -> new HashSet<>())
|
||||
.add(() -> ConfigFieldParser.loadField(instance, field, rawConfig, category, langKey));
|
||||
}
|
||||
}
|
||||
|
||||
if (!requiresMcRestart) {
|
||||
requiresMcRestart = field.isAnnotationPresent(Config.RequiresMcRestart.class);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user