Refactor ScriptManager to schedule loading and unloading of scripts on the Minecraft server's tick thread
All checks were successful
Build JStom / build (push) Successful in 1m21s
All checks were successful
Build JStom / build (push) Successful in 1m21s
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package net.jstom.script;
|
package net.jstom.script;
|
||||||
|
|
||||||
|
import net.minestom.server.MinecraftServer;
|
||||||
import org.graalvm.polyglot.Context;
|
import org.graalvm.polyglot.Context;
|
||||||
import org.graalvm.polyglot.HostAccess;
|
import org.graalvm.polyglot.HostAccess;
|
||||||
import org.graalvm.polyglot.Source;
|
import org.graalvm.polyglot.Source;
|
||||||
@@ -8,6 +9,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class ScriptManager {
|
public class ScriptManager {
|
||||||
private final File scriptsDir;
|
private final File scriptsDir;
|
||||||
@@ -18,6 +20,10 @@ public class ScriptManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void load() {
|
public void load() {
|
||||||
|
MinecraftServer.getSchedulerManager().scheduleNextTick(this::doLoad);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doLoad() {
|
||||||
if (!scriptsDir.exists()) {
|
if (!scriptsDir.exists()) {
|
||||||
scriptsDir.mkdirs();
|
scriptsDir.mkdirs();
|
||||||
}
|
}
|
||||||
@@ -30,13 +36,14 @@ public class ScriptManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadScript(File file) {
|
private void loadScript(File file) {
|
||||||
String fileName = file.getName();
|
String fileName = file.getName();
|
||||||
unloadScript(fileName); // Ensure clean slate if reloading
|
unloadScript(fileName); // Ensure clean slate if reloading
|
||||||
|
|
||||||
System.out.println("[JStom] Loading script: " + fileName);
|
System.out.println("[JStom] Loading script: " + fileName);
|
||||||
|
|
||||||
ScriptApi api = new ScriptApi();
|
ScriptApi api = new ScriptApi();
|
||||||
|
// Context created on the Tick Thread (because loadScript is called from scheduled tasks)
|
||||||
Context context = Context.newBuilder("js")
|
Context context = Context.newBuilder("js")
|
||||||
.allowHostAccess(HostAccess.ALL)
|
.allowHostAccess(HostAccess.ALL)
|
||||||
.allowHostClassLookup(s -> true)
|
.allowHostClassLookup(s -> true)
|
||||||
@@ -63,7 +70,7 @@ public class ScriptManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unloadScript(String fileName) {
|
private void unloadScript(String fileName) {
|
||||||
ScriptEnv env = loadedScripts.remove(fileName);
|
ScriptEnv env = loadedScripts.remove(fileName);
|
||||||
if (env != null) {
|
if (env != null) {
|
||||||
env.api.cleanup();
|
env.api.cleanup();
|
||||||
@@ -73,24 +80,32 @@ public class ScriptManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void unload() {
|
public void unload() {
|
||||||
|
MinecraftServer.getSchedulerManager().scheduleNextTick(this::doUnload);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doUnload() {
|
||||||
// Copy keys to avoid ConcurrentModificationException
|
// Copy keys to avoid ConcurrentModificationException
|
||||||
for (String fileName : new java.util.ArrayList<>(loadedScripts.keySet())) {
|
for (String fileName : new ArrayList<>(loadedScripts.keySet())) {
|
||||||
unloadScript(fileName);
|
unloadScript(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reload() {
|
public void reload() {
|
||||||
unload();
|
MinecraftServer.getSchedulerManager().scheduleNextTick(() -> {
|
||||||
load();
|
doUnload();
|
||||||
|
doLoad();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reload(String fileName) {
|
public void reload(String fileName) {
|
||||||
File file = new File(scriptsDir, fileName);
|
MinecraftServer.getSchedulerManager().scheduleNextTick(() -> {
|
||||||
if (file.exists()) {
|
File file = new File(scriptsDir, fileName);
|
||||||
loadScript(file);
|
if (file.exists()) {
|
||||||
} else {
|
loadScript(file);
|
||||||
System.err.println("[JStom] File not found: " + fileName);
|
} else {
|
||||||
}
|
System.err.println("[JStom] File not found: " + fileName);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ScriptEnv {
|
private static class ScriptEnv {
|
||||||
@@ -105,4 +120,3 @@ public class ScriptManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user