-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the bulk of the work for returning task data from events.
Credit: kiip1 for suggestion and example.
- Loading branch information
Showing
6 changed files
with
221 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/org/byteskript/skript/runtime/type/EventData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (c) 2022 ByteSkript org (Moderocky) | ||
* View the full licence information and permissions: | ||
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE | ||
*/ | ||
|
||
package org.byteskript.skript.runtime.type; | ||
|
||
import org.byteskript.skript.api.Event; | ||
import org.byteskript.skript.runtime.threading.ScriptFinishFuture; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
|
||
public final class EventData<Type extends Event> { | ||
private final boolean run; | ||
private final Type event; | ||
private final ScriptFinishFuture[] tasks; | ||
private CompletableFuture<?> all, any; | ||
private CompletableFuture<?>[] futures; | ||
|
||
public EventData(boolean run, Type event, ScriptFinishFuture[] tasks) { | ||
this.run = run; | ||
this.event = event; | ||
this.tasks = tasks; | ||
} | ||
|
||
private void prepareFutures() { | ||
this.futures = new CompletableFuture[tasks.length]; | ||
for (int i = 0; i < tasks.length; i++) futures[i] = CompletableFuture.supplyAsync(tasks[i]); | ||
} | ||
|
||
public CompletableFuture<?> all() { | ||
if (all != null) return all; | ||
if (this.futures == null) this.prepareFutures(); | ||
return all = CompletableFuture.allOf(futures); | ||
} | ||
|
||
public CompletableFuture<?> any() { | ||
if (any != null) return any; | ||
if (this.futures == null) this.prepareFutures(); | ||
return any = CompletableFuture.anyOf(futures); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Class<Type> getType() { | ||
return (Class<Type>) event.getClass(); | ||
} | ||
|
||
public boolean run() { | ||
return run; | ||
} | ||
|
||
public Type event() { | ||
return event; | ||
} | ||
|
||
public ScriptFinishFuture[] tasks() { | ||
return tasks; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) return true; | ||
if (obj == null || obj.getClass() != this.getClass()) return false; | ||
var that = (EventData) obj; | ||
return this.run == that.run && | ||
Objects.equals(this.event, that.event) && | ||
Objects.equals(this.tasks, that.tasks); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(run, event, tasks); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "EventData[" + | ||
"run=" + run + ", " + | ||
"event=" + event + ", " + | ||
"tasks=" + tasks + ']'; | ||
} | ||
|
||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/test/java/org/byteskript/skript/test/ThreadingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2022 ByteSkript org (Moderocky) | ||
* View the full licence information and permissions: | ||
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE | ||
*/ | ||
|
||
package org.byteskript.skript.test; | ||
|
||
import mx.kenzie.foundation.language.PostCompileClass; | ||
import org.byteskript.skript.api.Event; | ||
import org.byteskript.skript.api.ModifiableLibrary; | ||
import org.byteskript.skript.api.note.EventValue; | ||
import org.byteskript.skript.runtime.Skript; | ||
import org.byteskript.skript.runtime.config.ConfigEntry; | ||
import org.byteskript.skript.runtime.config.ConfigMap; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class ThreadingTest extends SkriptTest { | ||
|
||
private static final Skript skript | ||
= new Skript(); | ||
// = new Skript(new DebugSkriptCompiler(Stream.controller(System.out))); | ||
|
||
private final String test = """ | ||
on test: | ||
trigger: | ||
print "Start" | ||
wait 1 second | ||
print "Finish" | ||
"""; | ||
private static PostCompileClass cls; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
skript.registerLibrary(new ModifiableLibrary("test") {{ // Credit: kiip1 for suggestion. | ||
generateSyntaxFrom(TestEvent.class); | ||
}}); | ||
cls = skript.compileScript( | ||
""" | ||
on test: | ||
trigger: | ||
wait 1 millisecond | ||
set event-thing to 6 | ||
""", "skript.events"); | ||
skript.loadScript(cls); | ||
} | ||
|
||
@Test | ||
public void eventSynchronized() throws Throwable { | ||
final TestEvent event; | ||
skript.runEvent(event = new TestEvent()) | ||
.all() | ||
.get(); | ||
assert event.number == 6; | ||
} | ||
|
||
@org.byteskript.skript.api.note.Event("on test") | ||
public static final class TestEvent extends Event { | ||
public int number = 1; | ||
@EventValue("thing") | ||
public void setThing(Number number) { | ||
this.number = (int) number; | ||
} | ||
} | ||
} |