33import com .google .common .collect .Maps ;
44import com .google .common .collect .Sets ;
55import net .minecraft .network .chat .Component ;
6+ import org .apache .commons .lang3 .tuple .Pair ;
67import org .apache .commons .lang3 .tuple .Triple ;
78import org .cyclops .integrateddynamics .api .evaluate .EvaluationException ;
89import org .cyclops .integrateddynamics .api .evaluate .variable .IValue ;
2627public class ScriptingNetwork implements IScriptingNetwork {
2728
2829 private final Set <Integer > disks = Sets .newHashSet ();
30+ private final Map <Pair <Integer , Path >, IScript > scriptCache = Maps .newHashMap ();
2931 private final Map <Triple <Integer , Path , String >, ScriptVariable > variableCache = Maps .newHashMap ();
3032
3133 @ Override
@@ -46,12 +48,21 @@ public Set<Integer> getDisks() {
4648 @ Nullable
4749 @ Override
4850 public IScript getScript (int disk , Path path ) throws EvaluationException {
49- ILanguageHandler languageHandler = LanguageHandlers .REGISTRY .getProvider (path );
50- if (languageHandler == null ) {
51- throw new EvaluationException (Component .translatable ("script.integratedscripting.error.unsupported_language" , path .toString ()));
51+ Pair <Integer , Path > cacheKey = Pair .of (disk , path );
52+ IScript script = scriptCache .get (cacheKey );
53+ if (script == null ) {
54+ ILanguageHandler languageHandler = LanguageHandlers .REGISTRY .getProvider (path );
55+ if (languageHandler == null ) {
56+ throw new EvaluationException (Component .translatable ("script.integratedscripting.error.unsupported_language" , path .toString ()));
57+ }
58+ IScriptFactory scriptFactory = languageHandler .getScriptFactory ();
59+ script = scriptFactory .getScript (disk , path );
60+ if (script != null ) {
61+ script .addInvalidationListener (() -> scriptCache .remove (cacheKey ));
62+ scriptCache .put (cacheKey , script );
63+ }
5264 }
53- IScriptFactory scriptFactory = languageHandler .getScriptFactory ();
54- return scriptFactory .getScript (disk , path );
65+ return script ;
5566 }
5667
5768 @ Override
0 commit comments