Add cleanup task registration and execution in ScriptApi
All checks were successful
Build JStom / build (push) Successful in 1m24s
All checks were successful
Build JStom / build (push) Successful in 1m24s
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user