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

@ -14,24 +14,22 @@ public class SkriptUtil {
private static final Field EXPR;
static {
Field _FIELD = null;
try {
_FIELD = VariableString.class.getDeclaredField("string");
_FIELD.setAccessible(true);
} catch (NoSuchFieldException e) {
STRING = tryGetOldStringField()
.or(() -> tryGetNewStringField())
.orElseGet(() -> {
Skript.error("Skript's 'string' field could not be resolved.");
e.printStackTrace();
}
STRING = _FIELD;
return null;
});
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();
_FIELD = expressionInfoClass.getDeclaredField("expr");
_FIELD.setAccessible(true);
f = expressionInfoClass.getDeclaredField("expr");
f.setAccessible(true);
} else {
Skript.error("Skript's 'ExpressionInfo' class could not be resolved.");
}
@ -39,7 +37,7 @@ public class SkriptUtil {
e.printStackTrace();
Skript.error("Skript's 'expr' field could not be resolved.");
}
EXPR = _FIELD;
EXPR = f;
}
public static Object[] getTemplateString(VariableString vs) {
@ -58,4 +56,24 @@ public class SkriptUtil {
}
}
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();
}
}
}