diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/ExampleConfig.java b/src/main/java/com/gtnewhorizon/gtnhlib/ExampleConfig.java
index 1f80c268..cdacfc07 100644
--- a/src/main/java/com/gtnewhorizon/gtnhlib/ExampleConfig.java
+++ b/src/main/java/com/gtnewhorizon/gtnhlib/ExampleConfig.java
@@ -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)
diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java b/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java
index 0d5f97f8..caf21386 100644
--- a/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java
+++ b/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java
@@ -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:
+ *
{@code
+ * @Config.Reloadable("runtime")
+ * public static String runtimeValue = "default";
+ *
+ * ConfigurationManager.reloadConfig(MyConfig.class, "runtime");
+ * }
+ * spotless:on
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.FIELD)
+ @interface Reloadable {
+
+ /**
+ * @return the id of the config option to reload
+ */
+ String value();
+ }
+
}
diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java b/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java
index 4aa83b87..92cb36ab 100644
--- a/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java
+++ b/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java
@@ -48,6 +48,7 @@ public class ConfigurationManager {
private static final Map> observedCategories = new HashMap<>();
private static final Map> configEntries = new HashMap<>();
private static final Map, ConfigNode> configNode = new HashMap<>();
+ private static final Map>> 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> toReload = reloadables.get(config);
+ if (toReload == null) return;
+ Set 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);
}