forked from Limework/skript-db
Compare commits
No commits in common. "master" and "master" have entirely different histories.
@ -1,85 +1,94 @@
|
|||||||
package com.btk5h.skriptdb;
|
package com.btk5h.skriptdb;
|
||||||
|
|
||||||
import ch.njol.skript.Skript;
|
|
||||||
import ch.njol.skript.lang.Expression;
|
|
||||||
import ch.njol.skript.lang.VariableString;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
|
||||||
|
import ch.njol.skript.ScriptLoader;
|
||||||
|
import ch.njol.skript.Skript;
|
||||||
|
import ch.njol.skript.lang.Expression;
|
||||||
|
import ch.njol.skript.lang.VariableString;
|
||||||
|
import ch.njol.skript.lang.parser.ParserInstance;
|
||||||
|
|
||||||
public class SkriptUtil {
|
public class SkriptUtil {
|
||||||
private static final Field STRING;
|
|
||||||
private static final Field EXPR;
|
|
||||||
|
|
||||||
static {
|
private static final Field STRING;
|
||||||
Field stringField = null;
|
private static final Field EXPR;
|
||||||
Field exprField = null;
|
|
||||||
|
|
||||||
String[] possibleStringFields = {"string", "stringValue", "rawString"};
|
static {
|
||||||
for (String fieldName : possibleStringFields) {
|
STRING = tryGetOldStringField()
|
||||||
try {
|
.or(() -> tryGetNewStringField())
|
||||||
stringField = VariableString.class.getDeclaredField(fieldName);
|
.orElseGet(() -> {
|
||||||
stringField.setAccessible(true);
|
Skript.error("Skript's 'string' field could not be resolved.");
|
||||||
break;
|
return null;
|
||||||
} catch (NoSuchFieldException ignored) {}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (stringField == null) {
|
Field f = null;
|
||||||
for (Field field : VariableString.class.getDeclaredFields()) {
|
try {
|
||||||
if (field.getType().isArray() && field.getType().getComponentType() == Object.class) {
|
Optional<Class<?>> expressionInfo = Arrays.stream(VariableString.class.getDeclaredClasses())
|
||||||
try {
|
.filter(cls -> cls.getSimpleName().equals("ExpressionInfo"))
|
||||||
field.setAccessible(true);
|
.findFirst();
|
||||||
stringField = field;
|
if (expressionInfo.isPresent()) {
|
||||||
break;
|
Class<?> expressionInfoClass = expressionInfo.get();
|
||||||
} catch (SecurityException ignored) {}
|
f = expressionInfoClass.getDeclaredField("expr");
|
||||||
}
|
f.setAccessible(true);
|
||||||
}
|
} else {
|
||||||
}
|
Skript.error("Skript's 'ExpressionInfo' class could not be resolved.");
|
||||||
|
}
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Skript.error("Skript's 'expr' field could not be resolved.");
|
||||||
|
}
|
||||||
|
EXPR = f;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
public static Object[] getTemplateString(VariableString vs) {
|
||||||
Optional<Class<?>> expressionInfo = Arrays.stream(VariableString.class.getDeclaredClasses())
|
try {
|
||||||
.filter(cls -> cls.getSimpleName().equals("ExpressionInfo"))
|
return (Object[]) STRING.get(vs);
|
||||||
.findFirst();
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (expressionInfo.isPresent()) {
|
public static Expression<?> getExpressionFromInfo(Object o) {
|
||||||
Class<?> expressionInfoClass = expressionInfo.get();
|
try {
|
||||||
try {
|
return (Expression<?>) EXPR.get(o);
|
||||||
exprField = expressionInfoClass.getDeclaredField("expr");
|
} catch (IllegalAccessException e) {
|
||||||
exprField.setAccessible(true);
|
throw new RuntimeException(e);
|
||||||
} catch (NoSuchFieldException e) {
|
}
|
||||||
for (Field field : expressionInfoClass.getDeclaredFields()) {
|
}
|
||||||
if (Expression.class.isAssignableFrom(field.getType())) {
|
|
||||||
field.setAccessible(true);
|
@SuppressWarnings("deprecation")
|
||||||
exprField = field;
|
public static boolean isCurrentEvent(Class<? extends Event> event) {
|
||||||
break;
|
try {
|
||||||
}
|
Class.forName("ch.njol.skript.lang.parser.ParserInstance");
|
||||||
}
|
return ParserInstance.get().isCurrentEvent(event);
|
||||||
}
|
} catch (ClassNotFoundException e) {
|
||||||
}
|
return ScriptLoader.isCurrentEvent(event);
|
||||||
} catch (Exception ignored) {}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Optional<Field> tryGetOldStringField() {
|
||||||
|
try {
|
||||||
|
Field f = VariableString.class.getDeclaredField("string");
|
||||||
|
f.setAccessible(true);
|
||||||
|
return Optional.of(f);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Optional<Field> tryGetNewStringField() {
|
||||||
|
try {
|
||||||
|
Field f = VariableString.class.getDeclaredField("strings");
|
||||||
|
f.setAccessible(true);
|
||||||
|
return Optional.of(f);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
STRING = stringField;
|
}
|
||||||
EXPR = exprField;
|
|
||||||
|
|
||||||
if (STRING == null && EXPR == null) {
|
|
||||||
Skript.error("Failed to initialize SkriptUtil: Could not find required fields");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Object[] getTemplateString(VariableString vs) {
|
|
||||||
try {
|
|
||||||
return STRING != null ? (Object[]) STRING.get(vs) : new Object[]{vs.toString()};
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
return new Object[]{vs.toString()};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Expression<?> getExpressionFromInfo(Object o) {
|
|
||||||
try {
|
|
||||||
return EXPR != null ? (Expression<?>) EXPR.get(o) : null;
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user