diff --git a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovySandbox.java b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovySandbox.java index 7c4abe4aa..21f525bb5 100644 --- a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovySandbox.java +++ b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovySandbox.java @@ -168,7 +168,8 @@ protected void runScript(Script script){ } public T runClosure(Closure closure, Object... args) { - startRunning(); + boolean wasRunning = isRunning(); + if (!wasRunning) startRunning(); T result = null; try { result = closure.call(args); @@ -176,7 +177,7 @@ public T runClosure(Closure closure, Object... args) { GroovyScript.LOGGER.error("Caught an exception trying to run a closure:"); e.printStackTrace(); } finally { - stopRunning(); + if (!wasRunning) stopRunning(); } return result; } diff --git a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java index ac97f4abb..279c0e6b9 100644 --- a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java +++ b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java @@ -178,7 +178,8 @@ public void load() throws Exception { @ApiStatus.Internal @Override public T runClosure(Closure closure, Object... args) { - startRunning(); + boolean wasRunning = isRunning(); + if (!wasRunning) startRunning(); T result = null; try { result = runClosureInternal(closure, args); @@ -189,7 +190,7 @@ public T runClosure(Closure closure, Object... args) { return new AtomicInteger(); }).addAndGet(1); } finally { - stopRunning(); + if (!wasRunning) stopRunning(); } return result; }