From e2db3c9f830c968c43b28cff494b1a6b62f747f2 Mon Sep 17 00:00:00 2001 From: LatvianModder <latvianmodder@gmail.com> Date: Fri, 8 Dec 2023 16:50:47 +0200 Subject: [PATCH] Fixed warnings being shown as errors --- .../latvian/mods/kubejs/DevProperties.java | 2 + .../mods/kubejs/client/KubeJSClient.java | 6 +- .../mods/kubejs/core/MinecraftClientKJS.java | 7 ++ .../mixin/common/MinecraftClientMixin.java | 11 +++ .../mixin/common/MinecraftServerMixin.java | 2 +- .../mods/kubejs/script/ScriptType.java | 2 - .../latvian/mods/kubejs/util/ConsoleJS.java | 83 ++++++++----------- 7 files changed, 62 insertions(+), 51 deletions(-) diff --git a/common/src/main/java/dev/latvian/mods/kubejs/DevProperties.java b/common/src/main/java/dev/latvian/mods/kubejs/DevProperties.java index c8e37598a..c1ef85a53 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/DevProperties.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/DevProperties.java @@ -38,6 +38,7 @@ public static void reload() { public boolean logSkippedPlugins = true; public boolean logGeneratedData = false; public boolean strictTags = false; + public boolean alwaysCaptureErrors = false; private DevProperties() { properties = new Properties(); @@ -66,6 +67,7 @@ private DevProperties() { logSkippedPlugins = get("logSkippedPlugins", true); logGeneratedData = get("logGeneratedData", false); strictTags = get("strictTags", false); + alwaysCaptureErrors = get("alwaysCaptureErrors", false); KubeJSPlugins.forEachPlugin(this, KubeJSPlugin::loadDevProperties); diff --git a/common/src/main/java/dev/latvian/mods/kubejs/client/KubeJSClient.java b/common/src/main/java/dev/latvian/mods/kubejs/client/KubeJSClient.java index 665c10eca..b160ef8ff 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/client/KubeJSClient.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/client/KubeJSClient.java @@ -65,7 +65,11 @@ public void reloadClientInternal() { public static void reloadClientScripts() { KubeJSClientEventHandler.staticItemTooltips = null; - KubeJS.getClientScriptManager().reload(Minecraft.getInstance() == null ? null : Minecraft.getInstance().getResourceManager()); + var mc = Minecraft.getInstance(); + + if (mc != null) { + KubeJS.getClientScriptManager().reload(mc.getResourceManager()); + } } public static void copyDefaultOptionsFile(File optionsFile) { diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java b/common/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java index 55670fc8a..2d31f0769 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java @@ -6,6 +6,7 @@ import dev.latvian.mods.kubejs.item.ItemClickedEventJS; import dev.latvian.mods.kubejs.net.FirstClickMessage; import dev.latvian.mods.kubejs.script.ScriptType; +import dev.latvian.mods.kubejs.util.ConsoleJS; import dev.latvian.mods.rhino.util.HideFromJS; import dev.latvian.mods.rhino.util.RemapPrefixForJS; import net.minecraft.client.Minecraft; @@ -107,4 +108,10 @@ public interface MinecraftClientKJS extends MinecraftEnvironmentKJS { new FirstClickMessage(1).sendToServer(); } + + @HideFromJS + default void kjs$afterResourcesLoaded(boolean reload) { + ConsoleJS.CLIENT.setCapturingErrors(false); + ConsoleJS.CLIENT.info("Client resource reload complete!"); + } } diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/MinecraftClientMixin.java b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/MinecraftClientMixin.java index 9201cb587..55b6b6308 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/MinecraftClientMixin.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/MinecraftClientMixin.java @@ -20,6 +20,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.List; +import java.util.concurrent.CompletableFuture; @Mixin(Minecraft.class) @RemapPrefixForJS("kjs$") @@ -27,6 +28,11 @@ public abstract class MinecraftClientMixin implements MinecraftClientKJS { @Unique private ScheduledEvents kjs$scheduledEvents; + @Inject(method = "<init>", at = @At("RETURN")) + private void kjs$init(CallbackInfo ci) { + CompletableFuture.runAsync(() -> kjs$afterResourcesLoaded(false), kjs$self()); + } + @Inject(method = "createTitle", at = @At("HEAD"), cancellable = true) private void kjs$createTitle(CallbackInfoReturnable<String> ci) { var s = ClientProperties.get().title; @@ -79,4 +85,9 @@ public abstract class MinecraftClientMixin implements MinecraftClientKJS { return kjs$scheduledEvents; } + + @Inject(method = "reloadResourcePacks(Z)Ljava/util/concurrent/CompletableFuture;", at = @At("TAIL")) + private void kjs$endResourceReload(boolean bl, CallbackInfoReturnable<CompletableFuture<Void>> cir) { + CompletableFuture.runAsync(() -> kjs$afterResourcesLoaded(true), kjs$self()); + } } \ No newline at end of file diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/MinecraftServerMixin.java b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/MinecraftServerMixin.java index 369c2a537..1e09324d0 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/MinecraftServerMixin.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/MinecraftServerMixin.java @@ -111,7 +111,7 @@ public abstract class MinecraftServerMixin implements MinecraftServerKJS { public abstract void stopServer(); @Inject(method = "reloadResources", at = @At("TAIL")) - private void endResourceReload(Collection<String> collection, CallbackInfoReturnable<CompletableFuture<Void>> cir) { + private void kjs$endResourceReload(Collection<String> collection, CallbackInfoReturnable<CompletableFuture<Void>> cir) { CompletableFuture.runAsync(() -> kjs$afterResourcesLoaded(true), kjs$self()); } } diff --git a/common/src/main/java/dev/latvian/mods/kubejs/script/ScriptType.java b/common/src/main/java/dev/latvian/mods/kubejs/script/ScriptType.java index 36bf6452d..8ffcfac82 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/script/ScriptType.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/script/ScriptType.java @@ -90,8 +90,6 @@ public boolean isStartup() { @HideFromJS public void unload() { - console.errors.clear(); - console.warnings.clear(); console.resetFile(); for (var group : EventGroup.getGroups().values()) { diff --git a/common/src/main/java/dev/latvian/mods/kubejs/util/ConsoleJS.java b/common/src/main/java/dev/latvian/mods/kubejs/util/ConsoleJS.java index 3a20a48e4..6c702bff8 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/util/ConsoleJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/util/ConsoleJS.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.ConcurrentLinkedDeque; -import java.util.function.Consumer; import java.util.function.Function; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -37,21 +36,6 @@ public class ConsoleJS { public static ConsoleJS SERVER; public static ConsoleJS CLIENT; - private record LogFunc(ConsoleJS console, LogType type) implements Consumer<ConsoleLine> { - @Override - public void accept(ConsoleLine line) { - type.callback.accept(console.logger, line.getText()); - - if (console.capturingErrors) { - if (type == LogType.ERROR) { - console.errors.add(line); - } else if (type == LogType.WARN) { - console.warnings.add(line); - } - } - } - } - public static ConsoleJS getCurrent(ConsoleJS def) { Context cx = ScriptManager.getCurrentContext(); return cx == null ? def : getCurrent(cx); @@ -79,11 +63,11 @@ public static ConsoleJS getCurrent(@Nullable Context cx) { }; public final ScriptType scriptType; - private transient boolean capturingErrors; public final transient Collection<ConsoleLine> errors; public final transient Collection<ConsoleLine> warnings; - private final Logger logger; + public final transient Logger logger; private final Path logFile; + private boolean capturingErrors; private String group; private boolean muted; private boolean debugEnabled; @@ -91,28 +75,19 @@ public static ConsoleJS getCurrent(@Nullable Context cx) { private final List<String> writeQueue; private final Calendar calendar; - public final Consumer<ConsoleLine> debugLogFunction; - public final Consumer<ConsoleLine> infoLogFunction; - public Consumer<ConsoleLine> warnLogFunction; - public Consumer<ConsoleLine> errorLogFunction; - public ConsoleJS(ScriptType m, Logger log) { this.scriptType = m; this.errors = new ConcurrentLinkedDeque<>(); this.warnings = new ConcurrentLinkedDeque<>(); this.logger = log; this.logFile = m.getLogFile(); + this.capturingErrors = DevProperties.get().alwaysCaptureErrors; this.group = ""; this.muted = false; this.debugEnabled = false; this.writeToFile = true; this.writeQueue = new LinkedList<>(); this.calendar = Calendar.getInstance(); - - this.debugLogFunction = new LogFunc(this, LogType.DEBUG); - this.infoLogFunction = new LogFunc(this, LogType.INFO); - this.warnLogFunction = new LogFunc(this, LogType.WARN); - this.errorLogFunction = new LogFunc(this, LogType.ERROR); } public Logger getLogger() { @@ -148,18 +123,24 @@ public synchronized boolean getWriteToFile() { } public synchronized void setCapturingErrors(boolean enabled) { - capturingErrors = enabled; - - if (DevProperties.get().debugInfo) { - if (enabled) { - logger.info("Capturing errors for " + scriptType.name + " scripts enabled"); - } else { - logger.info("Capturing errors for " + scriptType.name + " scripts disabled"); + if (DevProperties.get().alwaysCaptureErrors) { + capturingErrors = true; + } else if (capturingErrors != enabled) { + capturingErrors = enabled; + + if (DevProperties.get().debugInfo) { + if (capturingErrors) { + logger.info("Capturing errors for " + scriptType.name + " scripts enabled"); + } else { + logger.info("Capturing errors for " + scriptType.name + " scripts disabled"); + } } } } - public void resetFile() { + public synchronized void resetFile() { + errors.clear(); + warnings.clear(); scriptType.executor.execute(() -> { try { Files.write(logFile, List.of()); @@ -222,16 +203,24 @@ private ConsoleLine line(LogType type, Object object, @Nullable Throwable error) return line; } - private ConsoleLine log(Consumer<ConsoleLine> logFunction, LogType type, @Nullable Throwable error, Object message) { + private ConsoleLine log(LogType type, @Nullable Throwable error, Object message) { if (shouldPrint()) { - var s = line(type, message, error); - logFunction.accept(s); + var line = line(type, message, error); + type.callback.accept(logger, line.getText()); + + if (capturingErrors) { + if (type == LogType.ERROR) { + errors.add(line); + } else if (type == LogType.WARN) { + warnings.add(line); + } + } if (writeToFile) { - writeToFile(type, s.timestamp, s.getText()); + writeToFile(type, line.timestamp, line.getText()); } - return s; + return line; } return null; @@ -299,7 +288,7 @@ public void log(Object... message) { } public ConsoleLine info(Object message) { - return log(infoLogFunction, LogType.INFO, null, message); + return log(LogType.INFO, null, message); } public ConsoleLine infof(String message, Object... args) { @@ -307,12 +296,12 @@ public ConsoleLine infof(String message, Object... args) { } public ConsoleLine warn(Object message) { - return log(warnLogFunction, LogType.WARN, null, message); + return log(LogType.WARN, null, message); } public ConsoleLine warn(String message, Throwable error, @Nullable Pattern exitPattern) { if (shouldPrint()) { - var l = log(errorLogFunction, LogType.WARN, error, message.isEmpty() ? error.getMessage() : (message + ": " + error.getMessage())); + var l = log(LogType.WARN, error, message.isEmpty() ? error.getMessage() : (message + ": " + error.getMessage())); handleError(l, error, exitPattern, !capturingErrors); return l; } @@ -329,12 +318,12 @@ public ConsoleLine warnf(String message, Object... args) { } public ConsoleLine error(Object message) { - return log(errorLogFunction, LogType.ERROR, null, message); + return log(LogType.ERROR, null, message); } public ConsoleLine error(String message, Throwable error, @Nullable Pattern exitPattern) { if (shouldPrint()) { - var l = log(errorLogFunction, LogType.ERROR, error, message.isEmpty() ? error.getMessage() : (message + ": " + error.getMessage())); + var l = log(LogType.ERROR, error, message.isEmpty() ? error.getMessage() : (message + ": " + error.getMessage())); handleError(l, error, exitPattern, true); return l; } @@ -356,7 +345,7 @@ public boolean shouldPrintDebug() { public ConsoleLine debug(Object message) { if (shouldPrintDebug()) { - return log(debugLogFunction, LogType.DEBUG, null, message); + return log(LogType.DEBUG, null, message); } return null;