Skip to content

Commit

Permalink
Add script exception timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Sep 5, 2023
1 parent e8b1f63 commit e0550b4
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceLocation, String> scripts) {
Expand Down Expand Up @@ -87,8 +87,6 @@ public void load(Map<ResourceLocation, String> 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 {
Expand All @@ -97,7 +95,7 @@ public void load(Map<ResourceLocation, String> 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();
Expand All @@ -112,15 +110,15 @@ 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();
}
});
}

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();
Expand All @@ -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;
}
}

0 comments on commit e0550b4

Please sign in to comment.