Fixed missing 'string' field in Skript 2.8

This commit is contained in:
szumielxd 2024-07-24 14:31:49 +02:00
parent 2dc1f70f4f
commit 41881dbb7d
Signed by: szumielxd
GPG Key ID: 5D0CCFC4CB110524

View File

@ -10,52 +10,70 @@ import java.util.Optional;
public class SkriptUtil { public class SkriptUtil {
private static final Field STRING; private static final Field STRING;
private static final Field EXPR; private static final Field EXPR;
static { static {
Field _FIELD = null; STRING = tryGetOldStringField()
try { .or(() -> tryGetNewStringField())
_FIELD = VariableString.class.getDeclaredField("string"); .orElseGet(() -> {
_FIELD.setAccessible(true); Skript.error("Skript's 'string' field could not be resolved.");
} catch (NoSuchFieldException e) { return null;
Skript.error("Skript's 'string' field could not be resolved."); });
e.printStackTrace();
}
STRING = _FIELD;
try { Field f = null;
Optional<Class<?>> expressionInfo = Arrays.stream(VariableString.class.getDeclaredClasses()) try {
.filter(cls -> cls.getSimpleName().equals("ExpressionInfo")) Optional<Class<?>> expressionInfo = Arrays.stream(VariableString.class.getDeclaredClasses())
.findFirst(); .filter(cls -> cls.getSimpleName().equals("ExpressionInfo"))
if (expressionInfo.isPresent()) { .findFirst();
Class<?> expressionInfoClass = expressionInfo.get(); if (expressionInfo.isPresent()) {
_FIELD = expressionInfoClass.getDeclaredField("expr"); Class<?> expressionInfoClass = expressionInfo.get();
_FIELD.setAccessible(true); f = expressionInfoClass.getDeclaredField("expr");
} else { f.setAccessible(true);
Skript.error("Skript's 'ExpressionInfo' class could not be resolved."); } else {
} Skript.error("Skript's 'ExpressionInfo' class could not be resolved.");
} catch (NoSuchFieldException e) { }
e.printStackTrace(); } catch (NoSuchFieldException e) {
Skript.error("Skript's 'expr' field could not be resolved."); e.printStackTrace();
Skript.error("Skript's 'expr' field could not be resolved.");
}
EXPR = f;
} }
EXPR = _FIELD;
}
public static Object[] getTemplateString(VariableString vs) { public static Object[] getTemplateString(VariableString vs) {
try { try {
return (Object[]) STRING.get(vs); return (Object[]) STRING.get(vs);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
}
} }
}
public static Expression<?> getExpressionFromInfo(Object o) { public static Expression<?> getExpressionFromInfo(Object o) {
try { try {
return (Expression<?>) EXPR.get(o); return (Expression<?>) EXPR.get(o);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
}
}
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();
}
} }
}
} }