55import ch .njol .skript .config .SectionNode ;
66import ch .njol .skript .config .SimpleNode ;
77import ch .njol .skript .events .bukkit .PreScriptLoadEvent ;
8+ import ch .njol .skript .lang .ExecutionIntent ;
89import ch .njol .skript .lang .Section ;
910import ch .njol .skript .lang .SkriptParser ;
1011import ch .njol .skript .lang .Statement ;
2021import ch .njol .skript .util .SkriptColor ;
2122import ch .njol .skript .util .Task ;
2223import ch .njol .skript .util .Timespan ;
23- import ch .njol .skript .variables .TypeHints ;
24+ import ch .njol .skript .variables .HintManager ;
2425import ch .njol .util .NonNullPair ;
2526import ch .njol .util .OpenCloseable ;
2627import ch .njol .util .StringUtils ;
@@ -951,6 +952,14 @@ public static ArrayList<TriggerItem> loadItems(SectionNode node) {
951952
952953 ArrayList <TriggerItem > items = new ArrayList <>();
953954
955+ // Begin local variable type hints
956+ parser .getHintManager ().enterScope (true );
957+ // Track if the scope has been frozen
958+ // This is the case when a statement that stops further execution is encountered
959+ // Further statements would not run, meaning the hints from them are inaccurate
960+ // When true, before exiting the scope, its hints are cleared to avoid passing them up
961+ boolean freezeScope = false ;
962+
954963 boolean executionStops = false ;
955964 for (Node subNode : node ) {
956965 parser .setNode (subNode );
@@ -983,11 +992,18 @@ public static ArrayList<TriggerItem> loadItems(SectionNode node) {
983992
984993 items .add (item );
985994 } else if (subNode instanceof SectionNode subSection ) {
986- TypeHints .enterScope (); // Begin conditional type hints
987995
988996 RetainingLogHandler handler = SkriptLogger .startRetainingLog ();
989997 find_section :
990998 try {
999+ // enter capturing scope
1000+ // it is possible that the line may successfully parse and initialize (via init), but some other issue
1001+ // prevents it from being able to load. for example:
1002+ // - a statement with a section that has no expression to claim the section
1003+ // - a statement with a section that has multiple expressions attempting to claim the section
1004+ // thus, hints may be added, but we do not want to save them as the line is invalid
1005+ parser .getHintManager ().enterScope (false );
1006+
9911007 item = Section .parse (expr , "Can't understand this section: " + expr , subSection , items );
9921008 if (item != null )
9931009 break find_section ;
@@ -1014,6 +1030,13 @@ public static ArrayList<TriggerItem> loadItems(SectionNode node) {
10141030 }
10151031 continue ;
10161032 } finally {
1033+ // exit hint scope (see above)
1034+ HintManager hintManager = parser .getHintManager ();
1035+ if (item == null ) { // unsuccessful, wipe out hints
1036+ hintManager .clearScope (0 , false );
1037+ }
1038+ hintManager .exitScope ();
1039+
10171040 RetainingLogHandler afterParse = handler .backup ();
10181041 handler .clear ();
10191042 handler .printLog ();
@@ -1023,9 +1046,6 @@ public static ArrayList<TriggerItem> loadItems(SectionNode node) {
10231046 }
10241047
10251048 items .add (item );
1026-
1027- // Destroy these conditional type hints
1028- TypeHints .exitScope ();
10291049 } else {
10301050 continue ;
10311051 }
@@ -1037,7 +1057,24 @@ public static ArrayList<TriggerItem> loadItems(SectionNode node) {
10371057 Skript .warning ("Unreachable code. The previous statement stops further execution." );
10381058 }
10391059 executionStops = item .executionIntent () != null ;
1060+
1061+ if (executionStops && !freezeScope ) {
1062+ freezeScope = true ;
1063+ // Execution might stop for some sections but not all
1064+ // We want to pass hints up to the scope that execution resumes in
1065+ if (item .executionIntent () instanceof ExecutionIntent .StopSections intent ) {
1066+ parser .getHintManager ().mergeScope (0 , intent .levels (), true );
1067+ }
1068+ }
1069+ }
1070+
1071+ // If the scope was frozen, then we need to clear it to prevent passing up inaccurate hints
1072+ // They will have already been copied as necessary
1073+ if (freezeScope ) {
1074+ parser .getHintManager ().clearScope (0 , false );
10401075 }
1076+ // Destroy local variable type hints for this section
1077+ parser .getHintManager ().exitScope ();
10411078
10421079 for (int i = 0 ; i < items .size () - 1 ; i ++)
10431080 items .get (i ).setNext (items .get (i + 1 ));
0 commit comments