Add support for @Every and @EveryNTicks annotations to schedule periodic and tick-based method execution.
This commit is contained in:
1 parent
b10504e9af
commit
9fcde10fed
11 files changed
+632
-58
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package dev.overlord.zomboidforge.api;
|
||||
|
||||
import dev.overlord.zomboidforge.api.annotations.Every;
|
||||
import dev.overlord.zomboidforge.api.annotations.EveryNTicks;
|
||||
import dev.overlord.zomboidforge.api.annotations.OnClientCommand;
|
||||
import dev.overlord.zomboidforge.api.annotations.OnMain;
|
||||
import dev.overlord.zomboidforge.api.annotations.OnModThread;
|
||||
@@ -32,14 +34,15 @@ public interface Events
|
||||
/**
|
||||
* Registers the methods of {@code listener} annotated with {@link OnMain},
|
||||
* {@link OnModThread}, or {@link OnClientCommand}, as {@link #onMain} and {@link #onModThread}
|
||||
* do. ZomboidForge registers the mod's entry point this way after its constructor and before
|
||||
* do, and schedules those annotated with {@link Every} or {@link EveryNTicks}. 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>returns {@code void}. A listener 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; a scheduled method has none;</li>
|
||||
* <li>carries one of the annotations; {@link OnModThread} not for a
|
||||
* {@link MainThreadEvent}, {@link OnClientCommand} only for a
|
||||
* {@link ClientCommandEvent};</li>
|
||||
@@ -51,8 +54,8 @@ public interface Events
|
||||
* 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
|
||||
* @return one subscription for every listener and scheduled 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
|
||||
|
||||
@@ -20,12 +20,15 @@ import java.nio.file.Path;
|
||||
* <ol>
|
||||
* <li>Listener methods: the methods annotated with
|
||||
* {@link dev.overlord.zomboidforge.api.annotations.OnMain OnMain},
|
||||
* {@link dev.overlord.zomboidforge.api.annotations.OnModThread OnModThread}, or
|
||||
* {@link dev.overlord.zomboidforge.api.annotations.OnClientCommand OnClientCommand} are registered (see
|
||||
* {@link dev.overlord.zomboidforge.api.annotations.OnModThread OnModThread},
|
||||
* {@link dev.overlord.zomboidforge.api.annotations.OnClientCommand OnClientCommand}, or
|
||||
* {@link dev.overlord.zomboidforge.api.annotations.EveryNTicks EveryNTicks} 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 #onServerStarted()}: the world is loaded and the network port is open. Once it
|
||||
* returned, the methods annotated with
|
||||
* {@link dev.overlord.zomboidforge.api.annotations.Every Every} start.</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()}, or a listener method that breaks
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package dev.overlord.zomboidforge.api.annotations;
|
||||
|
||||
import dev.overlord.zomboidforge.api.Events;
|
||||
import dev.overlord.zomboidforge.api.ModThread;
|
||||
|
||||
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;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
/**
|
||||
* Runs the method periodically on the mod's own thread, as {@link ModThread#scheduleAtFixedRate}
|
||||
* or {@link ModThread#scheduleWithFixedDelay} do. The schedule starts once the mod's
|
||||
* {@code onServerStarted()} returned (also when it threw); for an object registered later with
|
||||
* {@link Events#register(Object)}, at once. A run that throws is logged and ends the schedule;
|
||||
* the schedule ends as well when the server stops or the object's subscription is cancelled.
|
||||
*
|
||||
* <p>The method has no parameters and returns {@code void}; otherwise the rules of
|
||||
* {@link Events#register(Object)} apply. Like any mod-thread work, it must not touch game
|
||||
* objects.
|
||||
*
|
||||
* <pre>{@code
|
||||
* @Every(value = 1, unit = ChronoUnit.MINUTES)
|
||||
* void report() { ... }
|
||||
* }</pre>
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Every
|
||||
{
|
||||
/** The period, at least 1. */
|
||||
long value();
|
||||
|
||||
/** The unit of {@link #value} and {@link #initialDelay}: {@code MILLIS}, {@code SECONDS}, {@code MINUTES}, {@code HOURS}, or {@code DAYS}. */
|
||||
ChronoUnit unit() default ChronoUnit.SECONDS;
|
||||
|
||||
/** The time before the first run, at least 0. */
|
||||
long initialDelay() default 0;
|
||||
|
||||
/**
|
||||
* {@code false}: the period counts from the start of each run (fixed rate); {@code true}: from
|
||||
* the end of each run (fixed delay).
|
||||
*/
|
||||
boolean fixedDelay() default false;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package dev.overlord.zomboidforge.api.annotations;
|
||||
|
||||
import dev.overlord.zomboidforge.api.Events;
|
||||
import dev.overlord.zomboidforge.api.TickEvent;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Runs the method on the main thread every n-th frame of the main loop: a {@link TickEvent}
|
||||
* listener for the ticks that {@link #value} divides (n, 2n, ...). It may touch game objects and,
|
||||
* like every main-thread listener, must return quickly.
|
||||
*
|
||||
* <p>The method has no parameters and returns {@code void}; otherwise the rules of
|
||||
* {@link Events#register(Object)} apply.
|
||||
*
|
||||
* <pre>{@code
|
||||
* @EveryNTicks(10)
|
||||
* void sweep() { ... }
|
||||
* }</pre>
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface EveryNTicks
|
||||
{
|
||||
/** n, at least 1. */
|
||||
int value();
|
||||
}
|
||||
Reference in new issue
Block a user