@@ -516,67 +516,89 @@ private static CompletableFuture<ScriptInfo> loadScripts(List<Config> configs, O
516
516
try {
517
517
openCloseable .open ();
518
518
519
- scripts . stream ()
520
- . flatMap ( pair -> { // Flatten each entry down to a stream of Script-Structure pairs
521
- return pair . getSecond () .stream ()
522
- . map ( structure -> new NonNullPair <>( pair , structure ));
523
- } )
524
- . sorted ( Comparator . comparing ( pair -> pair . getSecond (). getPriority ()))
525
- . forEach ( pair -> {
526
- Script script = pair .getFirst ().getFirst ();
527
- Structure structure = pair . getSecond ( );
528
-
529
- parser . setActive ( script );
530
- parser . setCurrentStructure ( structure );
531
- parser . setNode ( structure . getEntryContainer (). getSource () );
532
-
533
- try {
534
- if (! structure . preLoad ())
535
- pair . getFirst (). getSecond ().remove ( structure );
536
- } catch ( Exception e ) {
537
- //noinspection ThrowableNotThrown
538
- Skript . exception ( e , "An error occurred while trying to load a Structure." );
519
+ // build sorted list
520
+ // this nest of pairs is terrible, but we need to keep the reference to the modifiable structures list
521
+ List < NonNullPair < NonNullPair < Script , List < Structure >>, Structure >> pairs = scripts .stream ()
522
+ . flatMap ( pair -> { // Flatten each entry down to a stream of Script-Structure pairs
523
+ return pair . getSecond (). stream ( )
524
+ . map ( structure -> new NonNullPair <>( pair , structure ));
525
+ })
526
+ . sorted ( Comparator . comparing ( pair -> pair .getSecond ().getPriority ()))
527
+ . collect ( Collectors . toCollection ( ArrayList :: new ) );
528
+
529
+ // pre-loading
530
+ pairs . removeIf ( pair -> {
531
+ Structure structure = pair . getSecond ( );
532
+
533
+ parser . setActive ( pair . getFirst (). getFirst ());
534
+ parser . setCurrentStructure ( structure );
535
+ parser . setNode ( structure . getEntryContainer ().getSource () );
536
+
537
+ try {
538
+ if (! structure . preLoad ()) {
539
539
pair .getFirst ().getSecond ().remove (structure );
540
+ return true ;
540
541
}
541
- });
542
-
542
+ } catch (Exception e ) {
543
+ //noinspection ThrowableNotThrown
544
+ Skript .exception (e , "An error occurred while trying to preLoad a Structure." );
545
+ pair .getFirst ().getSecond ().remove (structure );
546
+ return true ;
547
+ }
548
+ return false ;
549
+ });
543
550
parser .setInactive ();
544
551
545
- // TODO in the future, Structure#load should be split across multiple threads if parallel loading is enabled.
552
+ // TODO in the future, Structure#load/Structure#postLoad should be split across multiple threads if parallel loading is enabled.
546
553
// However, this is not possible right now as reworks in multiple areas will be needed.
547
554
// For example, the "Commands" class still uses a static list for currentArguments that is cleared between loads.
548
555
// Until these reworks happen, limiting main loading to asynchronous (not parallel) is the only choice we have.
549
- for (NonNullPair <Script , List <Structure >> pair : scripts ) {
550
- parser .setActive (pair .getFirst ());
551
- pair .getSecond ().removeIf (structure -> {
552
- parser .setCurrentStructure (structure );
553
- parser .setNode (structure .getEntryContainer ().getSource ());
554
- try {
555
- return !structure .load ();
556
- } catch (Exception e ) {
557
- //noinspection ThrowableNotThrown
558
- Skript .exception (e , "An error occurred while trying to load a Structure." );
556
+
557
+ // loading
558
+ pairs .removeIf (pair -> {
559
+ Structure structure = pair .getSecond ();
560
+
561
+ parser .setActive (pair .getFirst ().getFirst ());
562
+ parser .setCurrentStructure (structure );
563
+ parser .setNode (structure .getEntryContainer ().getSource ());
564
+
565
+ try {
566
+ if (!structure .load ()) {
567
+ pair .getFirst ().getSecond ().remove (structure );
559
568
return true ;
560
569
}
561
- });
562
- }
563
-
570
+ } catch (Exception e ) {
571
+ //noinspection ThrowableNotThrown
572
+ Skript .exception (e , "An error occurred while trying to load a Structure." );
573
+ pair .getFirst ().getSecond ().remove (structure );
574
+ return true ;
575
+ }
576
+ return false ;
577
+ });
564
578
parser .setInactive ();
565
579
566
- for (NonNullPair <Script , List <Structure >> pair : scripts ) {
567
- parser .setActive (pair .getFirst ());
568
- pair .getSecond ().removeIf (structure -> {
569
- parser .setCurrentStructure (structure );
570
- parser .setNode (structure .getEntryContainer ().getSource ());
571
- try {
572
- return !structure .postLoad ();
573
- } catch (Exception e ) {
574
- //noinspection ThrowableNotThrown
575
- Skript .exception (e , "An error occurred while trying to load a Structure." );
580
+ // post-loading
581
+ pairs .removeIf (pair -> {
582
+ Structure structure = pair .getSecond ();
583
+
584
+ parser .setActive (pair .getFirst ().getFirst ());
585
+ parser .setCurrentStructure (structure );
586
+ parser .setNode (structure .getEntryContainer ().getSource ());
587
+
588
+ try {
589
+ if (!structure .postLoad ()) {
590
+ pair .getFirst ().getSecond ().remove (structure );
576
591
return true ;
577
592
}
578
- });
579
- }
593
+ } catch (Exception e ) {
594
+ //noinspection ThrowableNotThrown
595
+ Skript .exception (e , "An error occurred while trying to postLoad a Structure." );
596
+ pair .getFirst ().getSecond ().remove (structure );
597
+ return true ;
598
+ }
599
+ return false ;
600
+ });
601
+ parser .setInactive ();
580
602
581
603
return scriptInfo ;
582
604
} catch (Exception e ) {
@@ -593,7 +615,7 @@ private static CompletableFuture<ScriptInfo> loadScripts(List<Config> configs, O
593
615
/**
594
616
* Creates a script and loads the provided config into it.
595
617
* @param config The config to load into a script.
596
- * @return The script that was loaded.
618
+ * @return A pair containing the script that was loaded and a modifiable version of the structures list .
597
619
*/
598
620
// Whenever you call this method, make sure to also call PreScriptLoadEvent
599
621
private static NonNullPair <Script , List <Structure >> loadScript (Config config ) {
@@ -1009,20 +1031,6 @@ public static FileFilter getDisabledScriptsFilter() {
1009
1031
* by new methods in this class.
1010
1032
*/
1011
1033
1012
- /**
1013
- * Reloads a single script.
1014
- * @param scriptFile The file representing the script to reload.
1015
- * @return Future of statistics of the newly loaded script.
1016
- * @deprecated Use {@link #reloadScript(Script, OpenCloseable)}.
1017
- */
1018
- @ Deprecated
1019
- public static CompletableFuture <ScriptInfo > reloadScript (File scriptFile , OpenCloseable openCloseable ) {
1020
- Script script = getScript (scriptFile );
1021
- if (script == null )
1022
- return CompletableFuture .completedFuture (new ScriptInfo ());
1023
- return reloadScript (script , openCloseable );
1024
- }
1025
-
1026
1034
/**
1027
1035
* Unloads the provided script.
1028
1036
* @param scriptFile The file representing the script to unload.
@@ -1049,6 +1057,18 @@ private static ScriptInfo unloadScripts(File folder) {
1049
1057
return unloadScripts (getScripts (folder ));
1050
1058
}
1051
1059
1060
+ /**
1061
+ * Reloads a single script.
1062
+ * @param scriptFile The file representing the script to reload.
1063
+ * @return Future of statistics of the newly loaded script.
1064
+ * @deprecated Use {@link #reloadScript(Script, OpenCloseable)}.
1065
+ */
1066
+ @ Deprecated
1067
+ public static CompletableFuture <ScriptInfo > reloadScript (File scriptFile , OpenCloseable openCloseable ) {
1068
+ unloadScript (scriptFile );
1069
+ return loadScripts (scriptFile , openCloseable );
1070
+ }
1071
+
1052
1072
/**
1053
1073
* Reloads all scripts in the given folder and its subfolders.
1054
1074
* @param folder A folder.
@@ -1058,7 +1078,7 @@ private static ScriptInfo unloadScripts(File folder) {
1058
1078
@ Deprecated
1059
1079
public static CompletableFuture <ScriptInfo > reloadScripts (File folder , OpenCloseable openCloseable ) {
1060
1080
unloadScripts (folder );
1061
- return loadScripts (loadStructures ( folder ) , openCloseable );
1081
+ return loadScripts (folder , openCloseable );
1062
1082
}
1063
1083
1064
1084
/**
0 commit comments