Add register listener methods annotated with @OnMain and @OnModThread; validate methods before mod loading.

This commit is contained in:
Overlord committed 2026-10-07 11:58:02 +02:00
1 parent 7ac442c4f6
commit b4d70c3d3a
15 files changed
+605 -13

No files matched your search

@@ -1,5 +1,8 @@
package dev.overlord.zomboidforge.api;
import dev.overlord.zomboidforge.api.annotations.OnMain;
import dev.overlord.zomboidforge.api.annotations.OnModThread;
import org.jetbrains.annotations.ApiStatus;
import java.util.function.Consumer;
@@ -24,4 +27,33 @@ public interface Events
* @throws IllegalArgumentException when {@code type} is a {@link MainThreadEvent}
*/
<E extends Event> Subscription onModThread(Class<E> type, Consumer<? super E> listener);
/**
* Registers the methods of {@code listener} annotated with {@link OnMain} or
* {@link OnModThread}, as {@link #onMain} and {@link #onModThread} do. ZomboidForge registers
* the mod's entry point this way after its constructor and before {@link Mod#onLoad()}; other
* objects opt in through this method.
*
* <p>Each annotated method:
* <ul>
* <li>returns {@code void} and has one parameter, a class that implements {@link Event}
* and is neither abstract nor an interface, because listeners are matched by the
* event's exact class;</li>
* <li>carries one of the two annotations; {@link OnModThread} not for a
* {@link MainThreadEvent};</li>
* <li>may be static, private, and declared in a superclass of {@code listener}'s class.
* An instance method that a subclass overrides listens only when the override carries
* the annotation itself.</li>
* </ul>
*
* Superclass methods are registered before subclass methods, and the methods of one class
* sorted by name, then by parameter type.
*
* @return one subscription for every listener method of {@code listener}; cancelling it
* allows registering {@code listener} again
* @throws IllegalArgumentException listing every method that breaks a rule, when a class
* the methods name cannot be loaded, or when
* {@code listener} is registered already
*/
Subscription register(Object listener);
}
@@ -18,12 +18,17 @@ import java.nio.file.Path;
*
* <p>Lifecycle, in this order:
* <ol>
* <li>Listener methods: the methods annotated with
* {@link dev.overlord.zomboidforge.api.annotations.OnMain} or
* {@link dev.overlord.zomboidforge.api.annotations.OnModThread} are registered (see
* {@link Events#register(Object)}).</li>
* <li>{@link #onLoad()}: registration and setup. The server database, Lua, and the world do
* not exist yet.</li>
* <li>{@link #onServerStarted()}: the world is loaded and the network port is open.</li>
* <li>{@link #onStopping()}: the server stops; the world has not been saved yet.</li>
* </ol>
* An exception thrown by the constructor or {@link #onLoad()} stops the server.
* An exception thrown by the constructor or {@link #onLoad()}, or a listener method that breaks
* the rules, stops the server.
*/
public abstract class Mod
{
@@ -0,0 +1,19 @@
package dev.overlord.zomboidforge.api.annotations;
import dev.overlord.zomboidforge.api.Events;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Registers the method with {@link Events#onMain} for the event type of its only parameter. Read
* on the entry point before {@code onLoad()}, and on objects passed to
* {@link Events#register(Object)}, which lists the rules for the method.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface OnMain {}
@@ -0,0 +1,19 @@
package dev.overlord.zomboidforge.api.annotations;
import dev.overlord.zomboidforge.api.Events;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Registers the method with {@link Events#onModThread} for the event type of its only parameter.
* Read on the entry point before {@code onLoad()}, and on objects passed to
* {@link Events#register(Object)}, which lists the rules for the method.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface OnModThread {}
@@ -0,0 +1,10 @@
/**
* Annotations that stand for calls a mod can also make through the services of
* {@link dev.overlord.zomboidforge.api.Mod}.
*
* @noinspection UnstableApiUsage, RedundantSuppression
*/
@NotNullByDefault
package dev.overlord.zomboidforge.api.annotations;
import org.jetbrains.annotations.NotNullByDefault;