diff --git a/src/main/java/ch/njol/skript/expressions/ExprDebugInfo.java b/src/main/java/ch/njol/skript/expressions/ExprDebugInfo.java new file mode 100644 index 00000000000..2f637bcd15e --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprDebugInfo.java @@ -0,0 +1,46 @@ +package ch.njol.skript.expressions; + +import ch.njol.skript.classes.ClassInfo; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Example; +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 org.jetbrains.annotations.Nullable; + +@Name("Debug Info") +@Description(""" + Returns a string version of the given objects, but with their type attached: + debug info of 1, "a", 0.5 -> 1 (long), "a" (string), 0.5 (double) + This is intended to make debugging easier, not as a reliable method of getting the type of a value. + """) +@Example("broadcast debug info of {list::*}") +@Since("INSERT VERSION") +public class ExprDebugInfo extends SimplePropertyExpression { + + static { + register(ExprDebugInfo.class, String.class, "debug info[rmation]", "objects"); + } + + @Override + public @Nullable String convert(Object from) { + String toString = Classes.toString(from); + ClassInfo classInfo = Classes.getSuperClassInfo(from.getClass()); + String typeName = classInfo.getName().toString(); + if (from instanceof String) + toString = "\"" + toString + "\""; + return toString + " (" + typeName + ")"; + } + + @Override + public Class getReturnType() { + return String.class; + } + + @Override + protected String getPropertyName() { + return "debug info"; + } + +}