Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime errors #7155

Draft
wants to merge 8 commits into
base: dev/feature
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/ScriptLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.util.event.EventRegistry;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.script.ScriptWarning;
import org.skriptlang.skript.lang.structure.Structure;
import org.skriptlang.skript.util.event.EventRegistry;

import java.io.File;
import java.io.FileFilter;
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.localization.ArgsMessage;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.Message;
import ch.njol.skript.localization.PluralizingArgsMessage;
Expand All @@ -44,7 +43,12 @@
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.registrations.Feature;
import ch.njol.skript.test.runner.*;
import ch.njol.skript.test.runner.EffObjectives;
import ch.njol.skript.test.runner.SkriptAsyncJUnitTest;
import ch.njol.skript.test.runner.SkriptJUnitTest;
import ch.njol.skript.test.runner.SkriptTestEvent;
import ch.njol.skript.test.runner.TestMode;
import ch.njol.skript.test.runner.TestTracker;
import ch.njol.skript.timings.SkriptTimings;
import ch.njol.skript.update.ReleaseManifest;
import ch.njol.skript.update.ReleaseStatus;
Expand All @@ -66,13 +70,14 @@
import ch.njol.util.StringUtils;
import ch.njol.util.coll.iterator.CheckedIterator;
import ch.njol.util.coll.iterator.EnumerationIterable;

import com.google.common.collect.Lists;
import com.google.gson.Gson;

import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -101,6 +106,7 @@
import org.skriptlang.skript.lang.converter.Converter;
import org.skriptlang.skript.lang.converter.Converters;
import org.skriptlang.skript.lang.entry.EntryValidator;
import org.skriptlang.skript.lang.errors.RuntimeErrorManager;
import org.skriptlang.skript.lang.experiment.ExperimentRegistry;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.structure.Structure;
Expand Down Expand Up @@ -380,6 +386,15 @@ public File getScriptsFolder() {
return scriptsFolder;
}


// ================ RUNTIME ERRORS ================
private static RuntimeErrorManager runtimeErrorManager;

public static RuntimeErrorManager getRuntimeErrorManager() {
return runtimeErrorManager;
}
// =================================================

@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, this);
Expand Down Expand Up @@ -565,6 +580,8 @@ public void onEnable() {

Commands.registerListeners();

runtimeErrorManager = new RuntimeErrorManager();

if (logNormal())
info(" " + Language.get("skript.copyright"));

Expand Down Expand Up @@ -1965,6 +1982,7 @@ public static void error(final CommandSender sender, final String error) {
sender.sendMessage(Utils.replaceEnglishChatStyles(getSkriptPrefix() + ChatColor.DARK_RED + error));
}


/**
* Gets the updater instance currently used by Skript.
* @return SkriptUpdater instance.
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
Expand All @@ -32,8 +33,10 @@ public class ExprSkullOwner extends SimplePropertyExpression<Block, OfflinePlaye
@Override
public @Nullable OfflinePlayer convert(Block block) {
BlockState state = block.getState();
if (!(state instanceof Skull))
if (!(state instanceof Skull)) {
error("Block '" + Classes.toString(block) + "' is not a skull!");
return null;
}
return ((Skull) state).getOwningPlayer();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package ch.njol.skript.expressions.base;

import ch.njol.skript.classes.Converter;
import ch.njol.skript.config.Node;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.errors.RuntimeErrorProducer;

/**
* A base class for property expressions that requires only few overridden methods
Expand All @@ -15,7 +17,10 @@
* @see PropertyExpression#register(Class, Class, String, String)
*/
@SuppressWarnings("deprecation") // for backwards compatibility
public abstract class SimplePropertyExpression<F, T> extends PropertyExpression<F, T> implements Converter<F, T> {
public abstract class SimplePropertyExpression<F, T> extends PropertyExpression<F, T> implements Converter<F, T>, RuntimeErrorProducer {

private Node node;
private String rawExpr;

@Override
@SuppressWarnings("unchecked")
Expand All @@ -25,6 +30,8 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
return LiteralUtils.canInitSafely(getExpr());
}
setExpr((Expression<? extends F>) expressions[0]);
node = getParser().getNode();
rawExpr = parseResult.expr;
return true;
}

Expand All @@ -37,6 +44,16 @@ protected T[] get(Event event, F[] source) {
return super.get(source, this);
}

@Override
public Node getNode() {
return node;
}

@Override
public String toUnderline() {
return rawExpr;
}

/**
* Used to collect the property type used in the register method.
* This forms the toString of this SimplePropertyExpression.
Expand Down
103 changes: 103 additions & 0 deletions src/main/java/org/skriptlang/skript/lang/errors/Frame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.skriptlang.skript.lang.errors;

import ch.njol.skript.config.Node;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.util.Utils;
import org.bukkit.Bukkit;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class Frame {

final static String plural = "s were";
final static String singular= " was";

final String type;
int totalLimit;
int lineLimit;
int lineTimeoutLimit;

int skipped;
int printed;
Map<Node, Integer> lineTotals;
Map<Node, Integer> lineSkipped;
Map<Node, Integer> timeouts;

public Frame(String type, int totalLimit, int lineLimit, int lineTimeoutLimit) {
this.type = type;
this.totalLimit = totalLimit;
this.lineLimit = lineLimit;
this.lineTimeoutLimit = lineTimeoutLimit;
lineTotals = new ConcurrentHashMap<>();
lineSkipped = new ConcurrentHashMap<>();
timeouts = new ConcurrentHashMap<>();
}

boolean add(Node node) {
// increment counter
int lineTotal = lineTotals.compute(node, (key, count) -> (count == null ? 1 : count + 1));

// don't print if in timeout
if (timeouts.containsKey(node)) {
skipped++;
lineSkipped.compute(node, (key, count) -> (count == null ? 1 : count + 1));
return false;
}

// decide whether to print
if (printed < totalLimit && lineTotal <= lineLimit) {
printed++;
return true;
} else {
skipped++;
lineSkipped.compute(node, (key, count) -> (count == null ? 1 : count + 1));
if (lineTotal == lineTimeoutLimit) {
timeouts.put(node, 10);
}
return false;
}
}

void nextFrame() {
skipped = 0;
printed = 0;
lineTotals.clear();
lineSkipped.clear();
for (Iterator<Map.Entry<Node, Integer>> it = timeouts.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<Node, Integer> entry = it.next();
if (entry.getValue() > 0)
entry.setValue(entry.getValue() - 1);
else
it.remove();
}
}

void printFrame() {
if (skipped > 0) {
String linesNotDisplayed = lineSkipped.entrySet().stream()
.map(entry -> "'"+ entry.getKey().getConfig().getFileName() + "' line " + entry.getKey().getLine() + " (" + entry.getValue() + ")")
.collect(Collectors.joining(", "));

SkriptLogger.sendFormatted(Bukkit.getConsoleSender(), Utils.replaceEnglishChatStyles(
"<gold>" + (skipped) + "<light red> runtime " + type + (skipped > 1 ? plural : singular) +
" thrown in the last second but not displayed. From: " +
"<gray>" + linesNotDisplayed + "\n \n"));
}

for (Map.Entry<Node, Integer> entry : timeouts.entrySet()) {
if (entry.getValue() == 10) {
Node node = entry.getKey();
SkriptLogger.sendFormatted(Bukkit.getConsoleSender(), Utils.replaceEnglishChatStyles(
"<gold>Line " + node.getLine() + "<light red> of script '<gray>" + node.getConfig().getFileName() +
"<light red>' produced too many runtime errors (<gray>" + lineTimeoutLimit +
"<light red> allowed per second). No errors from this line will be printed for 10 seconds.\n \n"));
}
}

// todo: send notif to online players with permission. "Script x.sk produced a runtime error! Check console."

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.skriptlang.skript.lang.errors;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.log.SkriptLogger;
import org.bukkit.Bukkit;

/**
* Handles duplicate and spammed runtime errors.
*/
public class RuntimeErrorManager {

private final Frame errorFrame, warningFrame;

public RuntimeErrorManager() {
errorFrame = new Frame("error", 8, 2, 4);
warningFrame = new Frame("warning", 8, 2, 4);
Bukkit.getScheduler().scheduleSyncRepeatingTask(Skript.getInstance(), () -> {
errorFrame.printFrame();
errorFrame.nextFrame();

warningFrame.printFrame();
warningFrame.nextFrame();
}, 20, 20);
}

void error(Node node, String message) {
// print if < limit
if (errorFrame.add(node)) {
SkriptLogger.sendFormatted(Bukkit.getConsoleSender(), message);
}
}

void warning(Node node, String message){
// print if < limit
if (warningFrame.add(node)) {
SkriptLogger.sendFormatted(Bukkit.getConsoleSender(), message);
}
}

public Frame getErrorFrame() {
return errorFrame;
}

public Frame getWarningFrame() {
return warningFrame;
}
}
Loading