Skip to content

Commit

Permalink
🛠 Fix toFormattedString for null nodes (#5303)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyhamAl-Ali authored May 5, 2023
1 parent ad60611 commit fd2425b
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions src/main/java/ch/njol/skript/log/LogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@

import ch.njol.skript.localization.ArgsMessage;
import ch.njol.skript.util.Utils;

import org.bukkit.ChatColor;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Config;
import ch.njol.skript.config.Node;

/**
* @author Peter Güttinger
*/
public class LogEntry {

public final Level level;
public final int quality;

public final String message;

@Nullable
public final Node node;

private final String from;
private final boolean tracked;

Expand All @@ -55,27 +54,27 @@ public class LogEntry {
public LogEntry(Level level, String message) {
this(level, ErrorQuality.SEMANTIC_ERROR.quality(), message, SkriptLogger.getNode());
}

public LogEntry(Level level, int quality, String message) {
this(level, quality, message, SkriptLogger.getNode());
}

public LogEntry(Level level, ErrorQuality quality, String message) {
this(level, quality.quality(), message, SkriptLogger.getNode());
}

public LogEntry(Level level, String message, @Nullable Node node) {
this(level, ErrorQuality.SEMANTIC_ERROR.quality(), message, node);
}

public LogEntry(Level level, ErrorQuality quality, String message, Node node) {
this(level, quality.quality(), message, node);
}

public LogEntry(Level level, int quality, String message, @Nullable Node node) {
this(level, quality, message, node, false);
}

public LogEntry(Level level, int quality, String message, @Nullable Node node, boolean tracked) {
this.level = level;
this.quality = quality;
Expand All @@ -84,9 +83,9 @@ public LogEntry(Level level, int quality, String message, @Nullable Node node, b
this.tracked = tracked;
from = tracked || Skript.debug() ? findCaller() : "";
}

private static final String skriptLogPackageName = "" + SkriptLogger.class.getPackage().getName();

static String findCaller() {
StackTraceElement[] es = new Exception().getStackTrace();
for (int i = 0; i < es.length; i++) {
Expand All @@ -101,38 +100,38 @@ static String findCaller() {
}
return " (from an unknown source)";
}

public Level getLevel() {
return level;
}

public int getQuality() {
return quality;
}

public String getMessage() {
return message;
}
private boolean used = false;

private boolean used;

void discarded(String info) {
used = true;
if (tracked)
SkriptLogger.LOGGER.warning(" # LogEntry '" + message + "'" + from + " discarded" + findCaller() + "; " + (new Exception()).getStackTrace()[1] + "; " + info);
}

void logged() {
used = true;
if (tracked)
SkriptLogger.LOGGER.warning(" # LogEntry '" + message + "'" + from + " logged" + findCaller());
}

@Override
protected void finalize() {
assert used : message + from;
}

@Override
public String toString() {
if (node == null || level.intValue() < Level.WARNING.intValue())
Expand All @@ -143,11 +142,9 @@ public String toString() {
}

public String toFormattedString() {
if (node == null || level.intValue() < Level.WARNING.intValue())
if (level.intValue() < Level.WARNING.intValue())
return message;

Config c = node.getConfig();

ArgsMessage details;
ArgsMessage lineInfo = WARNING_LINE_INFO;
if (level.intValue() == Level.WARNING.intValue()) { // warnings
Expand All @@ -159,15 +156,20 @@ public String toFormattedString() {
details = OTHER_DETAILS;
}

String from = this.from;
if (!from.isEmpty())
from = "§7 " + from + "\n";

// Replace configured messages chat styles without user variables
String lineInfoMsg = replaceNewline(Utils.replaceEnglishChatStyles(lineInfo.getValue() == null ? lineInfo.key : lineInfo.getValue()));
String detailsMsg = replaceNewline(Utils.replaceEnglishChatStyles(details.getValue() == null ? details.key : details.getValue()));
String lineDetailsMsg = replaceNewline(Utils.replaceEnglishChatStyles(LINE_DETAILS.getValue() == null ? LINE_DETAILS.key : LINE_DETAILS.getValue()));

if (node == null)
return String.format(detailsMsg.replaceAll("^\\s+", ""), message); // Remove line beginning spaces

Config c = node.getConfig();
String from = this.from;

if (!from.isEmpty())
from = ChatColor.GRAY + " " + from + "\n";

return
String.format(lineInfoMsg, String.valueOf(node.getLine()), c.getFileName()) + // String.valueOf is to convert the line number (int) to a String
String.format(detailsMsg, message.replaceAll("§", "&")) + from +
Expand All @@ -177,5 +179,5 @@ public String toFormattedString() {
private String replaceNewline(String s) {
return s.replaceAll("\\\\n", "\n");
}

}

0 comments on commit fd2425b

Please sign in to comment.