|
| 1 | +package ch.njol.skript.expressions; |
| 2 | + |
| 3 | +import ch.njol.skript.classes.ClassInfo; |
| 4 | +import ch.njol.skript.doc.Description; |
| 5 | +import ch.njol.skript.doc.Example; |
| 6 | +import ch.njol.skript.doc.Name; |
| 7 | +import ch.njol.skript.doc.Since; |
| 8 | +import ch.njol.skript.expressions.base.SimplePropertyExpression; |
| 9 | +import ch.njol.skript.registrations.Classes; |
| 10 | +import org.jetbrains.annotations.Nullable; |
| 11 | + |
| 12 | +@Name("Debug Info") |
| 13 | +@Description(""" |
| 14 | + Returns a string version of the given objects, but with their type attached: |
| 15 | + debug info of 1, "a", 0.5 -> 1 (long), "a" (string), 0.5 (double) |
| 16 | + This is intended to make debugging easier, not as a reliable method of getting the type of a value. |
| 17 | + """) |
| 18 | +@Example("broadcast debug info of {list::*}") |
| 19 | +@Since("INSERT VERSION") |
| 20 | +public class ExprDebugInfo extends SimplePropertyExpression<Object, String> { |
| 21 | + |
| 22 | + static { |
| 23 | + register(ExprDebugInfo.class, String.class, "debug info[rmation]", "objects"); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public @Nullable String convert(Object from) { |
| 28 | + String toString = Classes.toString(from); |
| 29 | + ClassInfo<?> classInfo = Classes.getSuperClassInfo(from.getClass()); |
| 30 | + String typeName = classInfo.getName().toString(); |
| 31 | + if (from instanceof String) |
| 32 | + toString = "\"" + toString + "\""; |
| 33 | + return toString + " (" + typeName + ")"; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public Class<? extends String> getReturnType() { |
| 38 | + return String.class; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected String getPropertyName() { |
| 43 | + return "debug info"; |
| 44 | + } |
| 45 | + |
| 46 | +} |
0 commit comments