Introduce event-driven API, modular threading, and enhanced server lifecycle tools
Add `Events` and `EventBus` for mod-driven event handling with support for main-thread and mod-thread execution. Introduce `MainThread` and `AsyncExecutor` for modular threading and background task management. Add `ForgeSettings` to manage global mod configuration through `forge.toml`. Include tests for events, threading, and configuration parsing to improve robustness and maintainability.
This commit is contained in:
1 parent
3b21fc3aa6
commit
a0d1ec2feb
30 files changed
+1683
-19
No files matched your search
@@ -0,0 +1,20 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* Virtual threads for a mod's blocking work, such as HTTP calls or file I/O: one thread per
|
||||
* task, named {@code zf-async-<id>-<n>}. ZomboidForge shuts it down when the server stops; tasks
|
||||
* submitted after that are rejected with a {@link java.util.concurrent.RejectedExecutionException}.
|
||||
*/
|
||||
public interface AsyncExecutor extends Executor
|
||||
{
|
||||
/** Runs {@code task} on a new virtual thread. An exception it throws is logged with the mod's id. */
|
||||
@Override
|
||||
void execute(Runnable task);
|
||||
|
||||
/** Runs {@code task} on a new virtual thread; the future completes with its result or exception. */
|
||||
<T> CompletableFuture<T> submit(Callable<T> task);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
/**
|
||||
* Something that happened in the server, delivered to listeners registered with
|
||||
* {@link Events}. Listeners are matched by the event's exact class.
|
||||
*/
|
||||
public interface Event {}
|
||||
@@ -0,0 +1,24 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Registration of event listeners. Listeners of one event run in registration order. A listener
|
||||
* that throws is logged with the mod's id; one that throws repeatedly is removed.
|
||||
*/
|
||||
public interface Events
|
||||
{
|
||||
/**
|
||||
* {@code listener} runs on the main thread while the event is posted. It may touch game
|
||||
* objects and must return quickly: it runs inside the tick.
|
||||
*/
|
||||
<E extends Event> Subscription onMain(Class<E> type, Consumer<? super E> listener);
|
||||
|
||||
/**
|
||||
* {@code listener} runs later on the mod's own thread. It must not touch game objects;
|
||||
* main-thread work goes through {@code main()}.
|
||||
*
|
||||
* @throws IllegalArgumentException when {@code type} is a {@link MainThreadEvent}
|
||||
*/
|
||||
<E extends Event> Subscription onModThread(Class<E> type, Consumer<? super E> listener);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* Runs a mod's code on the server's main thread, which owns every game object. Tasks from other
|
||||
* threads are queued and run during the next ticks, within a time budget per tick
|
||||
* ({@code forge.toml}); a task is never interrupted. Called on the main thread itself,
|
||||
* {@link #execute} and {@link #submit} run the task immediately.
|
||||
*
|
||||
* <p>Callbacks of the returned futures run on the thread that completes them, which is the
|
||||
* main thread: {@code submit(...).thenAcceptAsync(result -> ..., modThread())} moves the
|
||||
* follow-up work back. The main thread must never wait for another thread's result.
|
||||
*/
|
||||
public interface MainThread extends Executor
|
||||
{
|
||||
/**
|
||||
* Runs {@code task} on the main thread. An exception it throws is logged with the mod's id.
|
||||
*
|
||||
* @throws java.util.concurrent.RejectedExecutionException after the server started stopping
|
||||
*/
|
||||
@Override
|
||||
void execute(Runnable task);
|
||||
|
||||
/**
|
||||
* Runs {@code task} on the main thread; the future completes with its result or exception.
|
||||
*
|
||||
* @throws java.util.concurrent.RejectedExecutionException after the server started stopping
|
||||
*/
|
||||
<T> CompletableFuture<T> submit(Callable<T> task);
|
||||
|
||||
/**
|
||||
* Queues {@code task} to run on the main thread {@code ticks} frames from now, also when
|
||||
* called on the main thread.
|
||||
*
|
||||
* @throws IllegalArgumentException when {@code ticks} is less than 1
|
||||
*/
|
||||
void runLater(int ticks, Runnable task);
|
||||
|
||||
/** Whether the calling thread is the main thread. */
|
||||
boolean isMainThread();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
/**
|
||||
* An event that carries live game state or only makes sense during the tick. Listeners for it
|
||||
* run on the main thread only; {@link Events#onModThread} rejects it.
|
||||
*/
|
||||
public interface MainThreadEvent extends Event {}
|
||||
@@ -67,4 +67,17 @@ public abstract class Mod
|
||||
|
||||
/** The mod's settings from {@code config.toml}. */
|
||||
protected final ModConfig config() { return this.context.config(); }
|
||||
|
||||
/** Event listener registration. */
|
||||
protected final Events events() { return this.context.events(); }
|
||||
|
||||
/** The server's main thread, for everything that touches game objects. */
|
||||
@SuppressWarnings("ConfusingMainMethod")
|
||||
protected final MainThread main() { return this.context.main(); }
|
||||
|
||||
/** The mod's own platform thread, {@code zf-mod-<id>}: one task at a time, in order. */
|
||||
protected final ModThread modThread() { return this.context.modThread(); }
|
||||
|
||||
/** Virtual threads for blocking work such as HTTP calls or file I/O. */
|
||||
protected final AsyncExecutor async() { return this.context.async(); }
|
||||
}
|
||||
@@ -25,4 +25,16 @@ public interface ModContext
|
||||
|
||||
/** The mod's settings from {@code config.toml}. */
|
||||
ModConfig config();
|
||||
|
||||
/** Event listener registration. */
|
||||
Events events();
|
||||
|
||||
/** The server's main thread, for everything that touches game objects. */
|
||||
MainThread main();
|
||||
|
||||
/** The mod's own platform thread, {@code zf-mod-<id>}: one task at a time, in order. */
|
||||
ModThread modThread();
|
||||
|
||||
/** Virtual threads for blocking work such as HTTP calls or file I/O. */
|
||||
AsyncExecutor async();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
/**
|
||||
* The mod's own platform thread, {@code zf-mod-<id>}: runs one task at a time, in the order
|
||||
* they were submitted. ZomboidForge shuts it down when the server stops; tasks submitted after
|
||||
* that are rejected with a {@link java.util.concurrent.RejectedExecutionException}. It must not
|
||||
* touch game objects; that goes through {@code main()}.
|
||||
*/
|
||||
public interface ModThread extends Executor
|
||||
{
|
||||
/** Runs {@code task} on the mod thread. An exception it throws is logged with the mod's id. */
|
||||
@Override
|
||||
void execute(Runnable task);
|
||||
|
||||
/** Runs {@code task} on the mod thread; the future completes with its result or exception. */
|
||||
<T> CompletableFuture<T> submit(Callable<T> task);
|
||||
|
||||
/** Runs {@code task} once after {@code delay}. An exception it throws is logged. */
|
||||
ScheduledFuture<?> schedule(Runnable task, Duration delay);
|
||||
|
||||
/**
|
||||
* Runs {@code task} after {@code initialDelay}, then every {@code period}, measured from the
|
||||
* start of each run. A run that throws is logged and ends the schedule.
|
||||
*/
|
||||
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration initialDelay, Duration period);
|
||||
|
||||
/**
|
||||
* Runs {@code task} after {@code initialDelay}, then again {@code delay} after each run
|
||||
* finished. A run that throws is logged and ends the schedule.
|
||||
*/
|
||||
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration initialDelay, Duration delay);
|
||||
|
||||
/** Whether the calling thread is this mod thread. */
|
||||
boolean isModThread();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
/** A registered event listener. */
|
||||
public interface Subscription
|
||||
{
|
||||
/** Removes the listener; it receives no further events. Calling it again has no effect. */
|
||||
void cancel();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
/**
|
||||
* Posted on the main thread once per frame of the main loop, before the queued main-thread
|
||||
* tasks run.
|
||||
*
|
||||
* @param tick number of the frame, counted from 1 at the first frame after the server started
|
||||
*/
|
||||
public record TickEvent(long tick) implements MainThreadEvent {}
|
||||
Reference in new issue
Block a user