diff --git a/pom.xml b/pom.xml
index f9bc153..7048eba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
org.byteskript
byteskript
- 1.0.34
+ 1.0.35
ByteSkript
A compiled JVM implementation of the Skript language.
diff --git a/src/main/java/org/byteskript/skript/runtime/Skript.java b/src/main/java/org/byteskript/skript/runtime/Skript.java
index 3d4afdf..c0ff3bc 100644
--- a/src/main/java/org/byteskript/skript/runtime/Skript.java
+++ b/src/main/java/org/byteskript/skript/runtime/Skript.java
@@ -24,6 +24,7 @@
import org.byteskript.skript.runtime.internal.*;
import org.byteskript.skript.runtime.threading.*;
import org.byteskript.skript.runtime.type.Converter;
+import org.byteskript.skript.runtime.type.EventData;
import org.byteskript.skript.runtime.type.OperatorFunction;
import java.io.*;
@@ -463,15 +464,15 @@ public Future> runScript(final ScriptRunner runner, final Event event) {
final Runnable runnable = () -> {
final ScriptThread thread = (ScriptThread) Thread.currentThread();
future.thread = thread;
- thread.variables.clear();
+ thread.variables.clear(); // Some threads get regurgitated and will have shadow variables from their previous run.
thread.initiator = runner.owner();
thread.event = event;
try {
runner.run();
- future.value(runner.result());
} catch (ThreadDeath ignore) {
- // This is likely from an exit the current process effect, we don't want to make noise
+ // This is likely from an exit the current process effect, we don't want to make noise.
} finally {
+ future.value(runner.result());
future.finish();
}
};
@@ -485,15 +486,16 @@ public Future> runScript(final ScriptRunner runner, final Event event) {
This will trigger only the given script.
""")
@GenerateExample
- public boolean runEvent(final Event event, final Script script) {
+ public EventData> runEvent(final Event event, final Script script) {
boolean run = false;
+ final List futures = new ArrayList<>();
for (Map.Entry, EventHandler> entry : events.entrySet()) {
final Class extends Event> key = entry.getKey();
if (!key.isAssignableFrom(event.getClass())) continue;
run = true;
- entry.getValue().run(this, event, script);
+ futures.addAll(Arrays.asList(entry.getValue().run(this, event, script)));
}
- return run;
+ return new EventData<>(run, event, futures.toArray(new ScriptFinishFuture[0]));
}
@Description("""
@@ -777,15 +779,16 @@ public void unloadScript(Script script) {
Each handler will spawn its own process.
""")
@GenerateExample
- public boolean runEvent(final Event event) {
+ public EventData> runEvent(final Event event) {
boolean run = false;
+ final List futures = new ArrayList<>();
for (Map.Entry, EventHandler> entry : events.entrySet()) {
final Class extends Event> key = entry.getKey();
if (!key.isAssignableFrom(event.getClass())) continue;
run = true;
- entry.getValue().run(this, event);
+ futures.addAll(Arrays.asList(entry.getValue().run(this, event)));
}
- return run;
+ return new EventData<>(run, event, futures.toArray(new ScriptFinishFuture[0]));
}
@Description("""
diff --git a/src/main/java/org/byteskript/skript/runtime/internal/EventHandler.java b/src/main/java/org/byteskript/skript/runtime/internal/EventHandler.java
index d678cfd..0a0b1b3 100644
--- a/src/main/java/org/byteskript/skript/runtime/internal/EventHandler.java
+++ b/src/main/java/org/byteskript/skript/runtime/internal/EventHandler.java
@@ -10,10 +10,12 @@
import org.byteskript.skript.api.Event;
import org.byteskript.skript.runtime.Script;
import org.byteskript.skript.runtime.Skript;
+import org.byteskript.skript.runtime.threading.ScriptFinishFuture;
import org.byteskript.skript.runtime.threading.ScriptRunner;
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.Future;
@Ignore
public class EventHandler {
@@ -24,21 +26,38 @@ public List getTriggers() {
return triggers;
}
- public void run(final Skript skript, final Event event) {
- for (final ScriptRunner trigger : triggers) {
- skript.runScript(trigger, event);
+ public ScriptFinishFuture[] run(final Skript skript, final Event event) {
+ final ScriptFinishFuture[] futures = new ScriptFinishFuture[triggers.size()];
+ int count = 0;
+ synchronized (triggers) { // Reduce the chance of comodification.
+ for (final ScriptRunner trigger : triggers) {
+ futures[count] = (ScriptFinishFuture) skript.runScript(trigger, event);
+ assert futures[count] != null: "Script trigger didn't produce a task.";
+ count++;
+ }
}
+ return futures;
}
- public void run(final Skript skript, final Event event, final Script script) {
- for (final ScriptRunner trigger : triggers) {
- if (trigger.owner() == script.mainClass())
- skript.runScript(trigger, event);
+ public ScriptFinishFuture[] run(final Skript skript, final Event event, final Script script) {
+ int count = 0;
+ synchronized (triggers) {
+ final List triggers = new ArrayList<>(this.triggers);
+ triggers.removeIf(trigger -> trigger.owner() != script.mainClass());
+ final ScriptFinishFuture[] futures = new ScriptFinishFuture[triggers.size()];
+ for (final ScriptRunner trigger : triggers) {
+ futures[count] = (ScriptFinishFuture) skript.runScript(trigger, event);
+ assert futures[count] != null: "Script trigger didn't produce a task.";
+ count++;
+ }
+ return futures;
}
}
public void add(final ScriptRunner runner) {
- this.triggers.add(runner);
+ synchronized (triggers) {
+ this.triggers.add(runner);
+ }
}
}
diff --git a/src/main/java/org/byteskript/skript/runtime/threading/ScriptFinishFuture.java b/src/main/java/org/byteskript/skript/runtime/threading/ScriptFinishFuture.java
index 87714ac..5feaddd 100644
--- a/src/main/java/org/byteskript/skript/runtime/threading/ScriptFinishFuture.java
+++ b/src/main/java/org/byteskript/skript/runtime/threading/ScriptFinishFuture.java
@@ -13,11 +13,13 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
+import java.util.function.Supplier;
-public class ScriptFinishFuture implements Future