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