Skip to content

Commit

Permalink
reload all JsonConfig instance on plugin reload
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Aug 7, 2024
1 parent 77c4fb5 commit 81b93c4
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/api/java/mcp/mobius/waila/api/IRegistryFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mcp/mobius/waila/config/BlacklistConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/mcp/mobius/waila/config/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,13 +27,9 @@

public class JsonConfig<T> implements IJsonConfig<T> {

private static final Log LOG = Log.create();

@SuppressWarnings("rawtypes")
private static final ToIntFunction DEFAULT_VERSION_GETTER = t -> 0;
public static final Set<JsonConfig<Object>> 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");
Expand All @@ -39,10 +38,13 @@ public class JsonConfig<T> implements IJsonConfig<T> {
private final ConfigIo<T> io;
private final CachedSupplier<T> getter;

@SuppressWarnings("unchecked")
JsonConfig(Path path, Type clazz, Supplier<T> factory, Gson gson, int currentVersion, ToIntFunction<T> versionGetter, ObjIntConsumer<T> 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<Object>) this);
}

private void write(T t, Path path, boolean invalidate) {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/mcp/mobius/waila/config/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public enum PluginConfig implements IPluginConfig {
private static final ConfigIo<Map<String, Map<String, JsonPrimitive>>> IO = new ConfigIo<>(
LOG::warn, LOG::error,
new GsonBuilder().setPrettyPrinting().create(),
new TypeToken<Map<String, Map<String, JsonPrimitive>>>() {
}.getType(),
new TypeToken<Map<String, Map<String, JsonPrimitive>>>() {}.getType(),
LinkedHashMap::new);

private static final Map<ResourceLocation, ConfigEntry<Object>> CONFIGS = new LinkedHashMap<>();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/mcp/mobius/waila/plugin/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 81b93c4

Please sign in to comment.