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 ;
9- import ch .njol .skript .lang .Section ;
10- import ch .njol .skript .lang .SkriptParser ;
11- import ch .njol .skript .lang .Statement ;
12- import ch .njol .skript .lang .TriggerItem ;
8+ import ch .njol .skript .lang .*;
139import ch .njol .skript .lang .parser .ParserInstance ;
1410import ch .njol .skript .log .CountingLogHandler ;
1511import ch .njol .skript .log .LogEntry ;
2218import ch .njol .skript .util .Task ;
2319import ch .njol .skript .util .Timespan ;
2420import ch .njol .skript .variables .HintManager ;
25- import ch .njol .util .NonNullPair ;
2621import ch .njol .util .OpenCloseable ;
2722import ch .njol .util .StringUtils ;
2823import org .bukkit .Bukkit ;
@@ -528,18 +523,19 @@ private static CompletableFuture<ScriptInfo> loadScripts(List<Config> configs, O
528523
529524 // build sorted list
530525 // this nest of pairs is terrible, but we need to keep the reference to the modifiable structures list
531- List <NonNullPair <LoadingScriptInfo , Structure >> pairs = scripts .stream ()
526+ record LoadingStructure (LoadingScriptInfo loadingScriptInfo , Structure structure ) {}
527+ List <LoadingStructure > loadingStructures = scripts .stream ()
532528 .flatMap (info -> { // Flatten each entry down to a stream of Script-Structure pairs
533529 return info .structures .stream ()
534- .map (structure -> new NonNullPair <> (info , structure ));
530+ .map (structure -> new LoadingStructure (info , structure ));
535531 })
536- .sorted (Comparator .comparing (pair -> pair .getSecond ().getPriority ()))
532+ .sorted (Comparator .comparing (pair -> pair .structure ().getPriority ()))
537533 .collect (Collectors .toCollection (ArrayList ::new ));
538534
539535 // pre-loading
540- pairs .removeIf (pair -> {
541- LoadingScriptInfo loadingInfo = pair . getFirst ();
542- Structure structure = pair . getSecond ();
536+ loadingStructures .removeIf (loadingStructure -> {
537+ LoadingScriptInfo loadingInfo = loadingStructure . loadingScriptInfo ();
538+ Structure structure = loadingStructure . structure ();
543539
544540 parser .setActive (loadingInfo .script );
545541 parser .setCurrentStructure (structure );
@@ -566,9 +562,9 @@ private static CompletableFuture<ScriptInfo> loadScripts(List<Config> configs, O
566562 // Until these reworks happen, limiting main loading to asynchronous (not parallel) is the only choice we have.
567563
568564 // loading
569- pairs .removeIf (pair -> {
570- LoadingScriptInfo loadingInfo = pair . getFirst ();
571- Structure structure = pair . getSecond ();
565+ loadingStructures .removeIf (loadingStructure -> {
566+ LoadingScriptInfo loadingInfo = loadingStructure . loadingScriptInfo ();
567+ Structure structure = loadingStructure . structure ();
572568
573569 parser .setActive (loadingInfo .script );
574570 parser .setCurrentStructure (structure );
@@ -590,9 +586,9 @@ private static CompletableFuture<ScriptInfo> loadScripts(List<Config> configs, O
590586 parser .setInactive ();
591587
592588 // post-loading
593- pairs .removeIf (pair -> {
594- LoadingScriptInfo loadingInfo = pair . getFirst ();
595- Structure structure = pair . getSecond ();
589+ loadingStructures .removeIf (loadingStructure -> {
590+ LoadingScriptInfo loadingInfo = loadingStructure . loadingScriptInfo ();
591+ Structure structure = loadingStructure . structure ();
596592
597593 parser .setActive (loadingInfo .script );
598594 parser .setCurrentStructure (structure );
@@ -870,35 +866,53 @@ public static ScriptInfo unloadScripts(Set<Script> scripts) {
870866 }
871867
872868 ParserInstance parser = getParser ();
873-
874- // initial unload stage
869+ record UnloadingStructure (Script script , Structure structure ) {}
870+ Comparator <UnloadingStructure > unloadComparator = Comparator .comparing (unloadingStructure -> unloadingStructure .structure ().getPriority ());
871+ unloadComparator = unloadComparator .reversed ();
872+
873+ List <UnloadingStructure > unloadingStructures = scripts .stream ()
874+ .flatMap (script -> { // Flatten each entry down to a stream of Script-Structure pairs
875+ return script .getStructures ().stream ()
876+ .map (structure -> new UnloadingStructure (script , structure ));
877+ })
878+ .sorted (unloadComparator )
879+ .collect (Collectors .toCollection (ArrayList ::new ));
880+
881+ // trigger unload event before unloading scripts
875882 for (Script script : scripts ) {
876- parser .setActive (script );
877-
878- // trigger unload event before beginning
879883 eventRegistry ().events (ScriptUnloadEvent .class )
880- .forEach (event -> event .onUnload (parser , script ));
884+ .forEach (event -> event .onUnload (parser , script ));
881885 script .eventRegistry ().events (ScriptUnloadEvent .class )
882- .forEach (event -> event .onUnload (parser , script ));
886+ .forEach (event -> event .onUnload (parser , script ));
887+ }
888+
889+ // initial unload stage
890+ for (UnloadingStructure unloadingStructure : unloadingStructures ) {
891+ Script script = unloadingStructure .script ();
892+ Structure structure = unloadingStructure .structure ();
883893
884- for ( Structure structure : script . getStructures ())
885- structure .unload ();
894+ parser . setActive ( script );
895+ structure .unload ();
886896 }
887897
888898 parser .setInactive ();
889899
890- // finish unloading + data collection
900+ // finish unloading of structures + data collection
891901 ScriptInfo info = new ScriptInfo ();
892- for (Script script : scripts ) {
893- List <Structure > structures = script .getStructures ();
902+ for (UnloadingStructure unloadingStructure : unloadingStructures ) {
903+ Script script = unloadingStructure .script ();
904+ Structure structure = unloadingStructure .structure ();
894905
895- info .files ++;
896- info .structures += structures .size ();
906+ info .structures ++;
897907
898908 parser .setActive (script );
899- for (Structure structure : structures )
900- structure .postUnload ();
901- parser .setInactive ();
909+ structure .postUnload ();
910+ }
911+ parser .setInactive ();
912+
913+ // finish unloading of scripts + data collection
914+ for (Script script : scripts ) {
915+ info .files ++;
902916
903917 script .clearData ();
904918 script .invalidate ();
0 commit comments