diff --git a/src/api/java/mcp/mobius/waila/api/IJsonConfig.java b/src/api/java/mcp/mobius/waila/api/IJsonConfig.java index b0c0a31d..30585b9e 100644 --- a/src/api/java/mcp/mobius/waila/api/IJsonConfig.java +++ b/src/api/java/mcp/mobius/waila/api/IJsonConfig.java @@ -6,7 +6,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.nio.file.Path; -import java.util.function.Function; import java.util.function.ObjIntConsumer; import java.util.function.Supplier; import java.util.function.ToIntFunction; @@ -87,7 +86,7 @@ default void backup() { /** * Adds comment for this value. * - * @see Builder1#commenter(Function) + * @see Builder1#commenter(Commenter) */ @Target({ElementType.TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @@ -100,6 +99,13 @@ default void backup() { } + interface Commenter { + + @Nullable + String getComment(String path); + + } + interface Builder0 { Builder1 file(File file); @@ -116,7 +122,7 @@ interface Builder1 { Builder1 json5(); - Builder1 commenter(Function commenter); + Builder1 commenter(Commenter commenter); Builder1 gson(Gson gson); diff --git a/src/main/java/mcp/mobius/waila/Waila.java b/src/main/java/mcp/mobius/waila/Waila.java index 14e0784e..3d8f2c84 100644 --- a/src/main/java/mcp/mobius/waila/Waila.java +++ b/src/main/java/mcp/mobius/waila/Waila.java @@ -48,6 +48,7 @@ public abstract class Waila { .file(WailaConstants.NAMESPACE + "/blacklist") .version(BlacklistConfig.VERSION, BlacklistConfig::getConfigVersion, BlacklistConfig::setConfigVersion) .json5() + .commenter(BlacklistConfig.COMMENTER) .gson(new GsonBuilder() .setPrettyPrinting() .registerTypeAdapter(BlacklistConfig.class, new BlacklistConfig.Adapter()) diff --git a/src/main/java/mcp/mobius/waila/config/AnnotationCommenter.java b/src/main/java/mcp/mobius/waila/config/AnnotationCommenter.java index f1cd2039..1e22bd13 100644 --- a/src/main/java/mcp/mobius/waila/config/AnnotationCommenter.java +++ b/src/main/java/mcp/mobius/waila/config/AnnotationCommenter.java @@ -4,7 +4,6 @@ import java.lang.reflect.Type; import java.util.Arrays; import java.util.HashSet; -import java.util.function.Function; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; @@ -14,11 +13,11 @@ public record AnnotationCommenter( Type type, Gson gson, - Function extra -) implements Function { + IJsonConfig.Commenter extra +) implements IJsonConfig.Commenter { @Override - public @Nullable String apply(String path) { + public @Nullable String getComment(String path) { String annotationComment = null; if (type instanceof Class cls) { AnnotatedElement element = null; @@ -53,7 +52,7 @@ public record AnnotationCommenter( } } - var extraComment = extra.apply(path); + var extraComment = extra.getComment(path); if (annotationComment == null) return extraComment; if (extraComment == null) return annotationComment; diff --git a/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java b/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java index 259a2dcb..9e445ecc 100644 --- a/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java +++ b/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java @@ -14,6 +14,7 @@ import com.google.gson.JsonSerializer; import mcp.mobius.waila.Waila; import mcp.mobius.waila.api.IBlacklistConfig; +import mcp.mobius.waila.api.IJsonConfig; import mcp.mobius.waila.api.IRegistryFilter; import mcp.mobius.waila.util.Log; import net.minecraft.core.Registry; @@ -32,11 +33,20 @@ public class BlacklistConfig { private static final String BLACKLIST_TAG = "#" + Waila.id("blacklist"); public static final int VERSION = 0; + public static final IJsonConfig.Commenter COMMENTER = path -> !path.equals("$") ? null : """ + 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); public final LinkedHashSet blocks = new LinkedHashSet<>(); public final LinkedHashSet blockEntityTypes = new LinkedHashSet<>(); public final LinkedHashSet entityTypes = new LinkedHashSet<>(); + @IJsonConfig.Comment("\nThe values below are used internally by WTHIT, you SHOULD NOT modify it!") private int configVersion = 0; public int[] pluginHash = {0, 0, 0}; @@ -119,20 +129,6 @@ public static class Adapter implements JsonSerializer, JsonDese public JsonElement serialize(BlacklistConfig src, Type typeOfSrc, JsonSerializationContext context) { var object = new JsonObject(); - 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"); - - var commentArray = new JsonArray(); - for (var line : comments) commentArray.add(line); - object.add("_comment", commentArray); - src.addBlacklistTags(); object.add("blocks", context.serialize(src.blocks)); diff --git a/src/main/java/mcp/mobius/waila/config/JsonConfig.java b/src/main/java/mcp/mobius/waila/config/JsonConfig.java index 70642ed5..f7ed3d83 100644 --- a/src/main/java/mcp/mobius/waila/config/JsonConfig.java +++ b/src/main/java/mcp/mobius/waila/config/JsonConfig.java @@ -11,7 +11,6 @@ import java.util.Date; import java.util.Set; import java.util.WeakHashMap; -import java.util.function.Function; import java.util.function.ObjIntConsumer; import java.util.function.Supplier; import java.util.function.ToIntFunction; @@ -24,7 +23,6 @@ import mcp.mobius.waila.mcless.config.ConfigIo; import mcp.mobius.waila.util.CachedSupplier; import mcp.mobius.waila.util.Log; -import org.apache.commons.io.FilenameUtils; import org.jetbrains.annotations.Nullable; public class JsonConfig implements IJsonConfig { @@ -41,9 +39,11 @@ public class JsonConfig implements IJsonConfig { private final CachedSupplier getter; @SuppressWarnings("unchecked") - JsonConfig(Path path, Type type, Supplier factory, boolean json5, Function commenter, Gson gson, int currentVersion, ToIntFunction versionGetter, ObjIntConsumer versionSetter) { + JsonConfig(Path path, Type type, Supplier factory, boolean json5, Commenter commenter, Gson gson, int currentVersion, ToIntFunction versionGetter, ObjIntConsumer versionSetter) { this.path = path.toAbsolutePath(); - this.io = new ConfigIo<>(LOG::warn, LOG::error, json5, new AnnotationCommenter(type, gson, commenter), gson, type, factory, currentVersion, versionGetter, versionSetter); + + var annotationCommenter = new AnnotationCommenter(type, gson, commenter); + this.io = new ConfigIo<>(LOG::warn, LOG::error, json5, annotationCommenter::getComment, gson, type, factory, currentVersion, versionGetter, versionSetter); this.getter = new CachedSupplier<>(() -> io.read(this.path)); INSTANCES.add((JsonConfig) this); @@ -103,7 +103,7 @@ public static class Builder implements Builder0, Builder1 { final Type type; Supplier path; boolean json5; - Function commenter; + Commenter commenter; Gson gson; int currentVersion; ToIntFunction versionGetter; @@ -178,7 +178,7 @@ public Builder1 json5() { } @Override - public Builder1 commenter(Function commenter) { + public Builder1 commenter(Commenter commenter) { this.commenter = commenter; return this; } diff --git a/src/minecraftless/java/mcp/mobius/waila/mcless/json5/Json5Writer.java b/src/minecraftless/java/mcp/mobius/waila/mcless/json5/Json5Writer.java index 14e53377..58430c0e 100644 --- a/src/minecraftless/java/mcp/mobius/waila/mcless/json5/Json5Writer.java +++ b/src/minecraftless/java/mcp/mobius/waila/mcless/json5/Json5Writer.java @@ -2,6 +2,7 @@ // - Remove unused members // - Directly extends GSON's JsonWriter // - Added generic commenter function +// - Fixed issue with character replacement table (https://github.com/QuiltMC/quilt-parsers/pull/5) // https://github.com/QuiltMC/quilt-parsers/blob/00803c4e70fb0cf93765593eaae5c781b1505bee/json/src/main/java/org/quiltmc/parsers/json/JsonWriter.java // @formatter:off @@ -182,8 +183,8 @@ public final class Json5Writer extends JsonWriter { private String deferredName; private String deferredComment; - private final Function commenter; - private String[] pathNames = new String[32]; + private final Function commenter; + private String[] pathNames = new String[32]; // API methods @@ -191,7 +192,7 @@ public Json5Writer(Writer out, Function commenter) { super(Writer.nullWriter()); Objects.requireNonNull(out, "Writer cannot be null"); this.out = out; - this.commenter = commenter; + this.commenter = commenter; } /** @@ -211,8 +212,8 @@ public Json5Writer name(String name) throws IOException { throw new IllegalStateException("JsonWriter is closed."); } deferredName = name; - pathNames[stackSize - 1] = name; - comment(commenter.apply(getPath())); + pathNames[stackSize - 1] = name; + comment(commenter.apply(getPath())); return this; } @@ -455,7 +456,7 @@ private Json5Writer close(int empty, int nonempty, char closeBracket) } stackSize--; - pathNames[stackSize] = null; + pathNames[stackSize] = null; if (context == nonempty) { commentAndNewline(); } @@ -465,9 +466,9 @@ private Json5Writer close(int empty, int nonempty, char closeBracket) private void push(int newTop) { if (stackSize == stack.length) { - int newLength = stackSize * 2; + int newLength = stackSize * 2; stack = Arrays.copyOf(stack, newLength); - pathNames = Arrays.copyOf(pathNames, newLength); + pathNames = Arrays.copyOf(pathNames, newLength); } stack[stackSize++] = newTop; } @@ -536,10 +537,6 @@ private void string(String value, boolean quotes, boolean escapeQuotes) throws I out.write('\"'); } - if (!escapeQuotes) { - replacements['\"'] = null; - } - int last = 0; int length = value.length(); @@ -547,6 +544,9 @@ private void string(String value, boolean quotes, boolean escapeQuotes) throws I char c = value.charAt(i); String replacement; if (c < 128) { + if (c == '"' && !escapeQuotes) { + continue; + } replacement = replacements[c]; if (replacement == null) { continue; @@ -611,7 +611,7 @@ private void beforeValue() throws IOException { "JSON must have only one top-level value."); // fall-through case Json5Scope.EMPTY_DOCUMENT: // first in document - comment(commenter.apply("$")); + comment(commenter.apply("$")); writeDeferredComment(); replaceTop(Json5Scope.NONEMPTY_DOCUMENT); break; @@ -636,29 +636,29 @@ private void beforeValue() throws IOException { } } - public String getPath() { - StringBuilder result = new StringBuilder().append('$'); - for (int i = 0; i < stackSize; i++) { - switch (stack[i]) { - case Json5Scope.EMPTY_ARRAY: - case Json5Scope.NONEMPTY_ARRAY: - break; - - case Json5Scope.EMPTY_OBJECT: - case Json5Scope.DANGLING_NAME: - case Json5Scope.NONEMPTY_OBJECT: - result.append('.'); - if (pathNames[i] != null) { - result.append(pathNames[i]); - } - break; - - case Json5Scope.NONEMPTY_DOCUMENT: - case Json5Scope.EMPTY_DOCUMENT: - case Json5Scope.CLOSED: - break; - } - } - return result.toString(); - } + public String getPath() { + StringBuilder result = new StringBuilder().append('$'); + for (int i = 0; i < stackSize; i++) { + switch (stack[i]) { + case Json5Scope.EMPTY_ARRAY: + case Json5Scope.NONEMPTY_ARRAY: + break; + + case Json5Scope.EMPTY_OBJECT: + case Json5Scope.DANGLING_NAME: + case Json5Scope.NONEMPTY_OBJECT: + result.append('.'); + if (pathNames[i] != null) { + result.append(pathNames[i]); + } + break; + + case Json5Scope.NONEMPTY_DOCUMENT: + case Json5Scope.EMPTY_DOCUMENT: + case Json5Scope.CLOSED: + break; + } + } + return result.toString(); + } } 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 5e11a7e2..91018e3f 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 @@ -11,6 +11,7 @@ import com.google.gson.JsonParseException; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; +import mcp.mobius.waila.api.IJsonConfig; import mcp.mobius.waila.api.IRegistryFilter; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; @@ -33,6 +34,17 @@ public View getView() { return view; } + public static IJsonConfig.Commenter commenter(ResourceLocation tag) { + return p -> !p.equals("$") ? null : """ + 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(), tag); + } + public class View { public final IRegistryFilter blockFilter; @@ -59,20 +71,6 @@ public Adapter(ResourceLocation tagId) { public JsonElement serialize(ExtraBlacklistConfig src, Type typeOfSrc, JsonSerializationContext context) { var object = new JsonObject(); - 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(), tagRule) - .split("\n"); - - var commentArray = new JsonArray(); - for (var line : comments) commentArray.add(line); - object.add("_comment", commentArray); - object.add("blocks", context.serialize(src.blocks)); object.add("blockEntityTypes", context.serialize(src.blockEntityTypes)); object.add("entityTypes", context.serialize(src.entityTypes)); diff --git a/src/pluginExtra/java/mcp/mobius/waila/plugin/extra/provider/DataProvider.java b/src/pluginExtra/java/mcp/mobius/waila/plugin/extra/provider/DataProvider.java index 99a1a545..e1ac1ee3 100644 --- a/src/pluginExtra/java/mcp/mobius/waila/plugin/extra/provider/DataProvider.java +++ b/src/pluginExtra/java/mcp/mobius/waila/plugin/extra/provider/DataProvider.java @@ -51,6 +51,7 @@ protected DataProvider(IData.Type type, StreamCodec