From 81b93c4aedfcd4e2e32e3f2b5c7721384fdda884 Mon Sep 17 00:00:00 2001 From: deirn Date: Wed, 7 Aug 2024 16:55:11 +0700 Subject: [PATCH] reload all JsonConfig instance on plugin reload --- .editorconfig | 2 +- .../mcp/mobius/waila/api/IRegistryFilter.java | 4 ++-- .../mobius/waila/config/BlacklistConfig.java | 4 ++-- .../mcp/mobius/waila/config/JsonConfig.java | 18 ++++++++++-------- .../mcp/mobius/waila/config/PluginConfig.java | 3 +-- .../mcp/mobius/waila/plugin/PluginLoader.java | 3 +++ .../mobius/waila/mcless/config/ConfigIo.java | 5 ++++- .../extra/config/ExtraBlacklistConfig.java | 7 ++++--- 8 files changed, 27 insertions(+), 19 deletions(-) diff --git a/.editorconfig b/.editorconfig index 0715af829..579d0d2d7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -118,7 +118,7 @@ ij_java_keep_indents_on_empty_lines = false ij_java_keep_line_breaks = true ij_java_keep_multiple_expressions_in_one_line = false ij_java_keep_simple_blocks_in_one_line = false -ij_java_keep_simple_classes_in_one_line = false +ij_java_keep_simple_classes_in_one_line = true ij_java_keep_simple_lambdas_in_one_line = true ij_java_keep_simple_methods_in_one_line = false ij_java_label_indent_absolute = false diff --git a/src/api/java/mcp/mobius/waila/api/IRegistryFilter.java b/src/api/java/mcp/mobius/waila/api/IRegistryFilter.java index ea35d0834..70cfe35ef 100644 --- a/src/api/java/mcp/mobius/waila/api/IRegistryFilter.java +++ b/src/api/java/mcp/mobius/waila/api/IRegistryFilter.java @@ -51,11 +51,11 @@ static String getHeader() { #tag - include objects based on data pack tags /regex/ - include objects based on regular expression default - include objects with specific ID - + An exclamation mark (!) prefix can be added which negates the pattern. Any entries matching previous rules will be removed from it. Can be combined with other rule to exclude what matches the rule. - + Example: @aether - include all block from the aether namespace #minecraft:planks - include all blocks in the planks tag diff --git a/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java b/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java index 9d52c6b6e..259a2dcbe 100644 --- a/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java +++ b/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java @@ -122,9 +122,9 @@ public JsonElement serialize(BlacklistConfig src, Type typeOfSrc, JsonSerializat var comments = """ Run /waila reload to apply changes server-wide. Run /wailac reload to apply changes to only your client. - + %s - + The %s tag rule can not be removed""" .formatted(IRegistryFilter.getHeader(), BLACKLIST_TAG) .split("\n"); diff --git a/src/main/java/mcp/mobius/waila/config/JsonConfig.java b/src/main/java/mcp/mobius/waila/config/JsonConfig.java index 209641ca0..85a0d1630 100644 --- a/src/main/java/mcp/mobius/waila/config/JsonConfig.java +++ b/src/main/java/mcp/mobius/waila/config/JsonConfig.java @@ -7,7 +7,10 @@ import java.nio.file.Paths; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Collections; import java.util.Date; +import java.util.Set; +import java.util.WeakHashMap; import java.util.function.ObjIntConsumer; import java.util.function.Supplier; import java.util.function.ToIntFunction; @@ -24,13 +27,9 @@ public class JsonConfig implements IJsonConfig { - private static final Log LOG = Log.create(); - - @SuppressWarnings("rawtypes") - private static final ToIntFunction DEFAULT_VERSION_GETTER = t -> 0; + public static final Set> INSTANCES = Collections.newSetFromMap(Collections.synchronizedMap(new WeakHashMap<>())); - @SuppressWarnings("rawtypes") - private static final ObjIntConsumer DEFAULT_VERSION_SETTER = (t, v) -> {}; + private static final Log LOG = Log.create(); private static final Gson DEFAULT_GSON = new GsonBuilder().setPrettyPrinting().create(); private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); @@ -39,10 +38,13 @@ public class JsonConfig implements IJsonConfig { private final ConfigIo io; private final CachedSupplier getter; + @SuppressWarnings("unchecked") JsonConfig(Path path, Type clazz, Supplier factory, Gson gson, int currentVersion, ToIntFunction versionGetter, ObjIntConsumer versionSetter) { this.path = path.toAbsolutePath(); this.io = new ConfigIo<>(LOG::warn, LOG::error, gson, clazz, factory, currentVersion, versionGetter, versionSetter); this.getter = new CachedSupplier<>(() -> io.read(this.path)); + + INSTANCES.add((JsonConfig) this); } private void write(T t, Path path, boolean invalidate) { @@ -109,8 +111,8 @@ public Builder(Type type) { this.type = type; this.gson = DEFAULT_GSON; this.currentVersion = 0; - this.versionGetter = DEFAULT_VERSION_GETTER; - this.versionSetter = DEFAULT_VERSION_SETTER; + this.versionGetter = t -> 0; + this.versionSetter = (t, v) -> {}; if (type instanceof Class clazz) this.factory = () -> { try { diff --git a/src/main/java/mcp/mobius/waila/config/PluginConfig.java b/src/main/java/mcp/mobius/waila/config/PluginConfig.java index c6569563f..f2feaf349 100644 --- a/src/main/java/mcp/mobius/waila/config/PluginConfig.java +++ b/src/main/java/mcp/mobius/waila/config/PluginConfig.java @@ -31,8 +31,7 @@ public enum PluginConfig implements IPluginConfig { private static final ConfigIo>> IO = new ConfigIo<>( LOG::warn, LOG::error, new GsonBuilder().setPrettyPrinting().create(), - new TypeToken>>() { - }.getType(), + new TypeToken>>() {}.getType(), LinkedHashMap::new); private static final Map> CONFIGS = new LinkedHashMap<>(); diff --git a/src/main/java/mcp/mobius/waila/plugin/PluginLoader.java b/src/main/java/mcp/mobius/waila/plugin/PluginLoader.java index 15f519b7f..993442571 100644 --- a/src/main/java/mcp/mobius/waila/plugin/PluginLoader.java +++ b/src/main/java/mcp/mobius/waila/plugin/PluginLoader.java @@ -15,6 +15,7 @@ import mcp.mobius.waila.Waila; import mcp.mobius.waila.api.IPluginInfo; import mcp.mobius.waila.api.__internal__.Internals; +import mcp.mobius.waila.config.JsonConfig; import mcp.mobius.waila.config.PluginConfig; import mcp.mobius.waila.mcless.version.VersionRanges; import mcp.mobius.waila.network.common.s2c.BlacklistSyncCommonS2CPacket; @@ -169,6 +170,8 @@ public final void loadPlugins() { Registrar.get().lock(); PluginConfig.reload(); + + JsonConfig.INSTANCES.forEach(it -> it.write(it.get(), true)); } private void initialize(IPluginInfo info) { diff --git a/src/minecraftless/java/mcp/mobius/waila/mcless/config/ConfigIo.java b/src/minecraftless/java/mcp/mobius/waila/mcless/config/ConfigIo.java index 640abc191..30a6d939e 100644 --- a/src/minecraftless/java/mcp/mobius/waila/mcless/config/ConfigIo.java +++ b/src/minecraftless/java/mcp/mobius/waila/mcless/config/ConfigIo.java @@ -96,6 +96,9 @@ public T read(Path path) { if (init) { versionSetter.accept(config, currentVersion); write(path, config); + + config = read(path); + write(path, config); } return config; } @@ -105,7 +108,7 @@ public boolean write(Path path, T value) { writer.write(gson.toJson(value)); return true; } catch (IOException e) { - e.printStackTrace(); + error.accept("Exception when writing config file " + path, e); return false; } } diff --git a/src/pluginExtra/java/mcp/mobius/waila/plugin/extra/config/ExtraBlacklistConfig.java b/src/pluginExtra/java/mcp/mobius/waila/plugin/extra/config/ExtraBlacklistConfig.java index 7b2fd345c..5e11a7e22 100644 --- a/src/pluginExtra/java/mcp/mobius/waila/plugin/extra/config/ExtraBlacklistConfig.java +++ b/src/pluginExtra/java/mcp/mobius/waila/plugin/extra/config/ExtraBlacklistConfig.java @@ -60,10 +60,11 @@ public JsonElement serialize(ExtraBlacklistConfig src, Type typeOfSrc, JsonSeria var object = new JsonObject(); var comments = """ - The game needs to be restarted for the changes to apply. - + Run /waila reload to apply changes server-wide. + Run /wailac reload to apply changes to only your client. + %s - + The %s tag rule can not be removed""" .formatted(IRegistryFilter.getHeader(), tagRule) .split("\n");