Skip to content

Commit

Permalink
Fixed warnings being shown as errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Dec 8, 2023
1 parent 6e6a22a commit e2db3c9
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Mixin(Minecraft.class)
@RemapPrefixForJS("kjs$")
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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
83 changes: 36 additions & 47 deletions common/src/main/java/dev/latvian/mods/kubejs/util/ConsoleJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -79,40 +63,31 @@ 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;
private boolean writeToFile;
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() {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -299,20 +288,20 @@ 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) {
return info(message.formatted(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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down

0 comments on commit e2db3c9

Please sign in to comment.