From 9fcde10fed0e970f854b72477b9ad2fbc9bc0c1c Mon Sep 17 00:00:00 2001 From: Overlord Date: Wed, 7 Oct 2026 20:02:29 +0200 Subject: [PATCH] Add support for `@Every` and `@EveryNTicks` annotations to schedule periodic and tick-based method execution. --- MOD_DEVELOPMENT.md | 19 ++ .../dev/overlord/zomboidforge/api/Events.java | 15 +- .../dev/overlord/zomboidforge/api/Mod.java | 9 +- .../zomboidforge/api/annotations/Every.java | 48 +++++ .../api/annotations/EveryNTicks.java | 32 +++ .../dev/overlord/zomboidforge/core/Forge.java | 11 +- .../zomboidforge/core/runtime/EventBus.java | 126 +++++++++++- .../core/runtime/ListenerScanner.java | 189 +++++++++++++++--- .../overlord/zomboidforge/core/ForgeTest.java | 25 +++ .../core/runtime/EventBusTest.java | 177 +++++++++++++++- core/src/test/java/testmods/ScheduleMod.java | 39 ++++ 11 files changed, 632 insertions(+), 58 deletions(-) create mode 100644 api/src/main/java/dev/overlord/zomboidforge/api/annotations/Every.java create mode 100644 api/src/main/java/dev/overlord/zomboidforge/api/annotations/EveryNTicks.java create mode 100644 core/src/test/java/testmods/ScheduleMod.java diff --git a/MOD_DEVELOPMENT.md b/MOD_DEVELOPMENT.md index 6c5b676..d969f4c 100644 --- a/MOD_DEVELOPMENT.md +++ b/MOD_DEVELOPMENT.md @@ -240,6 +240,25 @@ void onGreeting(SomeEvent event) { ... } - A method that breaks these rules stops the server with every problem in the log; `register` throws an `IllegalArgumentException` instead. +### Scheduled methods + +```java +@Every(value = 1, unit = ChronoUnit.MINUTES) // on zf-mod-; optional: initialDelay, fixedDelay = true +void report() { ... } + +@EveryNTicks(10) // on the main thread, every 10th frame +void sweep() { ... } +``` + +- They follow the rules of listener methods, with no parameters. `unit` is `MILLIS`, + `SECONDS` (default), `MINUTES`, `HOURS`, or `DAYS`, and also applies to `initialDelay`. +- `@Every` starts after the mod's `onServerStarted()` (also when it threw), or at once for an + object registered later. Without `fixedDelay`, the period counts from the start of each + run; with it, from the end. A run that throws is logged and ends the schedule. +- `@EveryNTicks(n)` is a `TickEvent` listener for the ticks n, 2n, ...: it may touch game + objects and must return quickly. +- Cancelling the object's `Subscription` ends both; stopping the server ends `@Every`. + ## Commands Admin commands work wherever Project Zomboid's own commands work: in the in-game chat diff --git a/api/src/main/java/dev/overlord/zomboidforge/api/Events.java b/api/src/main/java/dev/overlord/zomboidforge/api/Events.java index 04bcfea..92c3a96 100644 --- a/api/src/main/java/dev/overlord/zomboidforge/api/Events.java +++ b/api/src/main/java/dev/overlord/zomboidforge/api/Events.java @@ -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. * *

Each annotated method: *