diff --git a/src/main/java/carpet/script/CarpetScriptHost.java b/src/main/java/carpet/script/CarpetScriptHost.java index a25c3c20..ef67e102 100644 --- a/src/main/java/carpet/script/CarpetScriptHost.java +++ b/src/main/java/carpet/script/CarpetScriptHost.java @@ -43,6 +43,7 @@ import net.minecraft.nbt.NbtElement; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; +import net.minecraft.text.BaseText; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; @@ -58,6 +59,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; +import java.util.TreeMap; import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.function.Predicate; @@ -1022,7 +1024,7 @@ public class CarpetScriptHost extends ScriptHost public void setChatErrorSnooper(ServerCommandSource source) { responsibleSource = source; - errorSnooper = (expr, /*Nullable*/ token, message) -> + errorSnooper = (expr, /*Nullable*/ token, ctx, message) -> { try { @@ -1049,13 +1051,13 @@ public class CarpetScriptHost extends ScriptHost Messenger.m(source, "r " + shebang); if (lines.length > 1 && token.lineno > 0) { - Messenger.m(source, "l " + lines[token.lineno - 1]); + Messenger.m(source, withLocals("l", lines[token.lineno - 1], ctx)); } - Messenger.m(source, "l " + lines[token.lineno].substring(0, token.linepos), "r HERE>> ", "l " + - lines[token.lineno].substring(token.linepos)); + Messenger.m(source, withLocals("l", lines[token.lineno].substring(0, token.linepos), ctx), "r HERE>> ", + withLocals("l", lines[token.lineno].substring(token.linepos), ctx)); if (lines.length > 1 && token.lineno < lines.length - 1) { - Messenger.m(source, "l " + lines[token.lineno + 1]); + Messenger.m(source, withLocals("l", lines[token.lineno + 1], ctx)); } } else @@ -1066,6 +1068,59 @@ public class CarpetScriptHost extends ScriptHost }; } + /** + *
Creates a {@link BaseText} using {@link Messenger} that has the locals in the {@code line} snippet with a hover over + * tooltip with the value of the local at that location
+ * @param line The line to find references to locals on + * @param context The {@link Context} to extract the locals from + * @param format The format to apply to each part of the line, without the trailing space + * @return A BaseText of the given line with the given format, that is visibly the same as passing those to Messenger, but with references to the + * locals in the {@link Context} with a hover over tooltip text + * @implNote The implementation of this method is far from perfect, and won't detect actual references to variables, but try to find the strings + * and add the hover effect to anything that equals to any variable name, so short variable names may appear on random positions + */ + private static BaseText withLocals(String format, String line, Context context) + { + format += " "; + List