1212import ch .njol .skript .lang .SkriptParser ;
1313import ch .njol .skript .lang .SyntaxElementInfo ;
1414import ch .njol .skript .registrations .Classes ;
15+ import ch .njol .skript .structures .StructOptions ;
1516import ch .njol .skript .variables .Variables ;
1617import com .btk5h .skriptmirror .SkriptMirror ;
1718import com .btk5h .skriptmirror .skript .custom .event .ExprReplacedEventValue ;
1819import org .bukkit .event .Event ;
20+ import org .skriptlang .skript .lang .script .Script ;
1921
2022import java .lang .reflect .Field ;
2123import java .lang .reflect .InvocationTargetException ;
@@ -36,6 +38,7 @@ public class SkriptReflection {
3638 private static Field PARSED_VALUE ;
3739 private static Method PARSE_I ;
3840 private static Field EXPRESSIONS ;
41+ private static Field OPTIONS ;
3942
4043 static {
4144 Field _FIELD ;
@@ -114,6 +117,14 @@ public class SkriptReflection {
114117 warning ("Skript's expressions field could not be resolved, " +
115118 "therefore you might get syntax conflict problems" );
116119 }
120+
121+ try {
122+ _FIELD = StructOptions .OptionsData .class .getDeclaredField ("options" );
123+ _FIELD .setAccessible (true );
124+ OPTIONS = _FIELD ;
125+ } catch (NoSuchFieldException e ) {
126+ warning ("Skript's options field could not be resolved, computed options won't work" );
127+ }
117128 }
118129
119130 private static void warning (String message ) {
@@ -298,4 +309,30 @@ public static SkriptParser.ParseResult parse_i(SkriptParser skriptParser, String
298309 }
299310 }
300311
312+ /**
313+ * Gets the modifiable options map from an options data object.
314+ *
315+ * @param script the script to get the options from.
316+ * @return the modifiable options map.
317+ *
318+ * @throws NullPointerException if the given options data object is null.
319+ * @throws IllegalStateException if skript-reflect could not find the modifiable options map.
320+ */
321+ public static Map <String , String > getOptions (Script script ) {
322+ if (script == null )
323+ throw new NullPointerException ();
324+
325+ if (OPTIONS == null )
326+ throw new IllegalStateException ("OPTIONS field not initialized, computed options cannot be used" );
327+
328+ StructOptions .OptionsData optionsData = script .getData (StructOptions .OptionsData .class ,
329+ StructOptions .OptionsData ::new );
330+
331+ try {
332+ return (Map <String , String >) OPTIONS .get (optionsData );
333+ } catch (IllegalAccessException e ) {
334+ throw new IllegalStateException (e ); // setAccessible called
335+ }
336+ }
337+
301338}
0 commit comments