Add cleanup task registration and execution in ScriptApi
All checks were successful
Build JStom / build (push) Successful in 1m24s

This commit is contained in:
2026-01-28 22:37:59 +00:00
parent c01026b7c1
commit 13cfc35de8

View File

@@ -17,6 +17,7 @@ import java.lang.reflect.Modifier;
public class ScriptApi {
private final List<EventNode<Event>> registeredNodes = new ArrayList<>();
private final List<Entity> trackedEntities = new ArrayList<>();
private final List<Value> cleanupTasks = new ArrayList<>();
private static final Map<String, EntityType> entityTypes = new HashMap<>();
static {
@@ -32,6 +33,18 @@ public class ScriptApi {
}
}
/**
* Registers a cleanup task.
* Usage in JS: server.onCleanup(() => { timer.cancel(); });
*/
public void onCleanup(Value callback) {
if (callback.canExecute()) {
synchronized (cleanupTasks) {
cleanupTasks.add(callback);
}
}
}
/**
* Registers an event listener from JavaScript.
* Usage in JS: server.on('net.minestom.server.event.player.PlayerLoginEvent', (event) => { ... });
@@ -82,6 +95,19 @@ public class ScriptApi {
}
public void cleanup() {
System.out.println("[JStom] Running " + cleanupTasks.size() + " cleanup tasks...");
synchronized (cleanupTasks) {
for (Value task : cleanupTasks) {
try {
task.executeVoid();
} catch (Exception e) {
System.err.println("[JStom] Error in cleanup task:");
e.printStackTrace();
}
}
cleanupTasks.clear();
}
System.out.println("[JStom] Cleaning up " + registeredNodes.size() + " event nodes...");
for (var node : registeredNodes) {
MinecraftServer.getGlobalEventHandler().removeChild(node);