diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/ScriptHolder.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/ScriptHolder.java index f35d1239..83dc877a 100644 --- a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/ScriptHolder.java +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/ScriptHolder.java @@ -29,7 +29,7 @@ public class ScriptHolder { private Scriptable scope; - public boolean isActive = false; + public long failTime = 0; public double lastExecuteTime = 0; public void load(Map scripts) { @@ -87,8 +87,6 @@ public void load(Map scripts) { rhinoCtx.evaluateString(scope, entry.getValue(), entry.getKey().toString(), 1, null); ScriptResourceUtil.relativeBase = null; } - - isActive = true; } catch (Exception ex) { Main.LOGGER.error("Error in NTE Resource Pack JavaScript", ex); } finally { @@ -97,7 +95,7 @@ public void load(Map scripts) { } public Future callTrainFunction(String function, TrainScriptContext trainCtx) { - if (!isActive) return null; + if (duringFailTimeout()) return null; return SCRIPT_THREAD.submit(() -> { if (Thread.currentThread().isInterrupted()) return; Context rhinoCtx = Context.enter(); @@ -112,7 +110,7 @@ public Future callTrainFunction(String function, TrainScriptContext trainCtx) } } catch (Exception ex) { Main.LOGGER.error("Error in NTE Resource Pack JavaScript", ex); - isActive = false; + failTime = System.currentTimeMillis(); } finally { Context.exit(); } @@ -120,7 +118,7 @@ public Future callTrainFunction(String function, TrainScriptContext trainCtx) } public Future callEyeCandyFunction(String function, EyeCandyScriptContext eyeCandyCtx) { - if (!isActive) return null; + if (duringFailTimeout()) return null; return SCRIPT_THREAD.submit(() -> { if (Thread.currentThread().isInterrupted()) return; Context rhinoCtx = Context.enter(); @@ -135,10 +133,14 @@ public Future callEyeCandyFunction(String function, EyeCandyScriptContext eye } } catch (Exception ex) { Main.LOGGER.error("Error in NTE Resource Pack JavaScript", ex); - isActive = false; + failTime = System.currentTimeMillis(); } finally { Context.exit(); } }); } + + private boolean duringFailTimeout() { + return (System.currentTimeMillis() - failTime) < 4000; + } }