Skip to content

Commit f8eebdd

Browse files
committed
Fix computed options
1 parent 11fa985 commit f8eebdd

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/main/java/com/btk5h/skriptmirror/skript/custom/expression/CustomConstantSection.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import ch.njol.skript.lang.Trigger;
1010
import ch.njol.skript.lang.TriggerItem;
1111
import ch.njol.skript.lang.parser.ParserInstance;
12+
import ch.njol.skript.structures.StructOptions;
1213
import ch.njol.util.StringUtils;
1314
import com.btk5h.skriptmirror.skript.custom.CustomSyntaxSection;
15+
import com.btk5h.skriptmirror.util.SkriptReflection;
1416
import com.btk5h.skriptmirror.util.SkriptUtil;
1517

1618
import java.util.List;
@@ -88,7 +90,12 @@ protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.Parse
8890
private static void computeOption(String option, Trigger getter) {
8991
ConstantGetEvent constantEvent = new ConstantGetEvent(0, null);
9092
getter.execute(constantEvent);
91-
ParserInstance.get().getCurrentOptions().put(option, StringUtils.join(constantEvent.getOutput()));
93+
// Get result as a string
94+
String result = StringUtils.join(constantEvent.getOutput());
95+
96+
// Get options of current script, and add it to that
97+
SkriptReflection.getOptions(ParserInstance.get().getCurrentScript())
98+
.put(option, result);
9299
}
93100

94101
}

src/main/java/com/btk5h/skriptmirror/util/SkriptReflection.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import ch.njol.skript.lang.SkriptParser;
1313
import ch.njol.skript.lang.SyntaxElementInfo;
1414
import ch.njol.skript.registrations.Classes;
15+
import ch.njol.skript.structures.StructOptions;
1516
import ch.njol.skript.variables.Variables;
1617
import com.btk5h.skriptmirror.SkriptMirror;
1718
import com.btk5h.skriptmirror.skript.custom.event.ExprReplacedEventValue;
1819
import org.bukkit.event.Event;
20+
import org.skriptlang.skript.lang.script.Script;
1921

2022
import java.lang.reflect.Field;
2123
import 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

Comments
 (0)