2
2
3
3
import ch .njol .skript .Skript ;
4
4
import ch .njol .skript .effects .EffReturn ;
5
- import ch . njol . util . Checker ;
5
+ import org . skriptlang . reflect . syntax . CustomSyntaxStructure ;
6
6
import org .skriptlang .reflect .syntax .condition .elements .CustomCondition ;
7
+ import org .skriptlang .reflect .syntax .condition .elements .StructCustomCondition ;
7
8
import org .skriptlang .reflect .syntax .effect .elements .CustomEffect ;
9
+ import org .skriptlang .reflect .syntax .effect .elements .StructCustomEffect ;
8
10
import org .skriptlang .reflect .syntax .expression .elements .CustomExpression ;
9
11
import com .btk5h .skriptmirror .skript .EffExpressionStatement ;
10
12
import com .btk5h .skriptmirror .skript .custom .ExprMatchedPattern ;
11
13
import com .btk5h .skriptmirror .util .SkriptReflection ;
14
+ import org .skriptlang .reflect .syntax .expression .elements .StructCustomExpression ;
15
+ import org .skriptlang .skript .bukkit .registration .BukkitRegistryKeys ;
16
+ import org .skriptlang .skript .registration .SyntaxInfo ;
17
+ import org .skriptlang .skript .registration .SyntaxRegistry ;
18
+ import org .skriptlang .skript .util .Priority ;
12
19
20
+ import javax .naming .ServiceUnavailableException ;
13
21
import java .util .Collection ;
14
22
import java .util .Optional ;
23
+ import java .util .function .Predicate ;
15
24
16
25
/**
17
26
* Explicitly declares the relative parse orders of different statement types. Classes at the start of the list should
20
29
* This class should only be used to guarantee that skript-mirror's syntax is parsed before other addons. It cannot
21
30
* guarantee that another addon's syntax will be parsed before skript-reflect.
22
31
*/
32
+ @ SuppressWarnings ("UnstableApiUsage" )
23
33
public class ParseOrderWorkarounds {
34
+
35
+ private static final Priority POSITION = Priority .before (SyntaxInfo .PATTERN_MATCHES_EVERYTHING );
36
+
24
37
private static final String [] PARSE_ORDER = {
25
38
EffExpressionStatement .class .getCanonicalName (),
26
39
CustomEffect .class .getCanonicalName (),
@@ -38,22 +51,37 @@ public class ParseOrderWorkarounds {
38
51
39
52
public static void reorderSyntax () {
40
53
for (String c : PARSE_ORDER ) {
41
- ensureLast (Skript . getStatements () , o -> o .getElementClass ().getName ().equals (c ));
42
- ensureLast (Skript . getConditions () , o -> o .getElementClass ().getName ().equals (c ));
43
- ensureLast (Skript . getEffects () , o -> o .getElementClass ().toString ().equals (c ));
44
- ensureLast (SkriptReflection . getExpressions () , o -> o .getElementClass ().getName ().equals (c ));
45
- ensureLast (Skript . getEvents () , o -> o .getElementClass ().getName ().equals (c ));
54
+ ensureLast (SyntaxRegistry . CONDITION , o -> o .type ().getName ().equals (c ));
55
+ ensureLast (SyntaxRegistry . EFFECT , o -> o .type ().getName ().equals (c ));
56
+ ensureLast (SyntaxRegistry . EXPRESSION , o -> o .type ().getName ().equals (c ));
57
+ ensureLast (BukkitRegistryKeys . EVENT , o -> o .type ().getName ().equals (c ));
58
+ ensureLast (SyntaxRegistry . STRUCTURE , o -> o .type ().getName ().equals (c ));
46
59
}
47
60
}
48
61
49
- private static <E > void ensureLast (Collection <E > elements , Checker <E > checker ) {
50
- Optional <E > optionalE = elements .stream ()
51
- .filter (checker ::check )
62
+ private static <T > void ensureLast (SyntaxRegistry .Key <? extends SyntaxInfo <? extends T >> elementKey , Predicate <SyntaxInfo <? extends T >> checker ) {
63
+ SyntaxRegistry syntaxRegistry = SkriptMirror .getAddonInstance ().syntaxRegistry ();
64
+ Optional <? extends SyntaxInfo <? extends T >> optionalE = syntaxRegistry .syntaxes (elementKey ).stream ()
65
+ .filter (checker )
52
66
.findFirst ();
53
67
54
68
optionalE .ifPresent (value -> {
55
- elements .remove (value );
56
- elements .add (value );
69
+ syntaxRegistry .unregister ((SyntaxRegistry .Key ) elementKey , value );
70
+ var newInfo = value .toBuilder ().priority (POSITION ).build ();
71
+ syntaxRegistry .register ((SyntaxRegistry .Key ) elementKey , newInfo );
72
+
73
+ // need to update custom syntax references
74
+ CustomSyntaxStructure .DataTracker <?> tracker = null ;
75
+ if (elementKey == (SyntaxRegistry .Key ) SyntaxRegistry .EFFECT ) {
76
+ tracker = StructCustomEffect .dataTracker ;
77
+ } else if (elementKey == (SyntaxRegistry .Key ) SyntaxRegistry .CONDITION ) {
78
+ tracker = StructCustomCondition .dataTracker ;
79
+ } else if (elementKey == (SyntaxRegistry .Key ) SyntaxRegistry .EXPRESSION ) {
80
+ tracker = StructCustomExpression .dataTracker ;
81
+ }
82
+ if (tracker != null && tracker .getInfo () == value ) {
83
+ tracker .setInfo (newInfo );
84
+ }
57
85
});
58
86
}
59
87
0 commit comments