Add support for @OnClientCommand annotation to register and filter client command event handlers.

This commit is contained in:
Overlord committed 2026-10-07 13:09:27 +02:00
1 parent b4d70c3d3a
commit 1664bb6f28
6 files changed
+177 -24

No files matched your search

@@ -1,5 +1,6 @@
package dev.overlord.zomboidforge.api;
import dev.overlord.zomboidforge.api.annotations.OnClientCommand;
import dev.overlord.zomboidforge.api.annotations.OnMain;
import dev.overlord.zomboidforge.api.annotations.OnModThread;
@@ -29,18 +30,19 @@ public interface Events
<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.
* 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
* {@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>carries one of the annotations; {@link OnModThread} not for a
* {@link MainThreadEvent}, {@link OnClientCommand} only for a
* {@link ClientCommandEvent};</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>
@@ -19,8 +19,9 @@ 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 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 Events#register(Object)}).</li>
* <li>{@link #onLoad()}: registration and setup. The server database, Lua, and the world do
* not exist yet.</li>
@@ -0,0 +1,39 @@
package dev.overlord.zomboidforge.api.annotations;
import dev.overlord.zomboidforge.api.ClientCommandEvent;
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 {@link ClientCommandEvent}s of one
* module, and optionally one command. The method has one parameter, a {@code ClientCommandEvent};
* otherwise the rules of {@link Events#register(Object)} apply, and the method carries none of
* the other listener annotations.
*
* <pre>{@code
* @OnClientCommand(module = "Shop", command = "buy", cancel = true)
* void buy(ClientCommandEvent event) { ... }
* }</pre>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface OnClientCommand
{
/** The Lua module, matched exactly; not blank. */
String module();
/** The command, matched exactly; empty matches every command of the module. */
String command() default "";
/**
* Cancels the event before the method runs, so Lua's {@code OnClientCommand} does not
* receive the command, also when the method throws.
*/
boolean cancel() default false;
}