package com.btk5h.skriptdb; import java.lang.reflect.Field; import java.util.Arrays; 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 { private static final Field STRING; private static final Field EXPR; static { STRING = tryGetOldStringField() .or(() -> tryGetNewStringField()) .orElseGet(() -> { Skript.error("Skript's 'string' field could not be resolved."); return null; }); Field f = null; try { Optional> 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; } 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); } } @SuppressWarnings("deprecation") public static boolean isCurrentEvent(Class event) { try { Class.forName("ch.njol.skript.lang.parser.ParserInstance"); return ParserInstance.get().isCurrentEvent(event); } catch (ClassNotFoundException e) { return ScriptLoader.isCurrentEvent(event); } } private static Optional tryGetOldStringField() { try { Field f = VariableString.class.getDeclaredField("string"); f.setAccessible(true); return Optional.of(f); } catch (NoSuchFieldException e) { return Optional.empty(); } } private static Optional tryGetNewStringField() { try { Field f = VariableString.class.getDeclaredField("strings"); f.setAccessible(true); return Optional.of(f); } catch (NoSuchFieldException e) { return Optional.empty(); } } }