Add support for @RegisterCommand, @Key, @Min, and @Max annotations for command registration, custom keys, and configuration bounds.

This commit is contained in:
Overlord committed 2026-10-07 23:02:14 +02:00
1 parent 9fcde10fed
commit 9af951b4be
17 files changed
+577 -33

No files matched your search

@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import dev.overlord.zomboidforge.api.annotations.RegisterCommand;
import org.jetbrains.annotations.ApiStatus;
/**
@@ -28,7 +30,8 @@ public interface Commands
{
/**
* Registers {@code command}. Only on the main thread while the mod loads: in the entry point's
* constructor or {@link Mod#onLoad()}.
* constructor or {@link Mod#onLoad()}. Classes annotated with {@link RegisterCommand} are
* registered without this call.
*
* @throws IllegalArgumentException when {@code command} does not meet the rules above, or is
* registered already
@@ -1,5 +1,9 @@
package dev.overlord.zomboidforge.api;
import dev.overlord.zomboidforge.api.annotations.Key;
import dev.overlord.zomboidforge.api.annotations.Max;
import dev.overlord.zomboidforge.api.annotations.Min;
import org.jetbrains.annotations.ApiStatus;
import java.util.Optional;
@@ -10,7 +14,8 @@ import java.util.Optional;
* default. The table {@code [forge]} belongs to ZomboidForge and is not part of these settings.
*
* <p>Keys are {@code snake_case}; record components and paths use the same names converted
* from {@code camelCase} ({@code maxBalance} reads {@code max_balance}).
* from {@code camelCase} ({@code maxBalance} reads {@code max_balance}), or the name of the
* component's {@link Key}.
*/
@ApiStatus.NonExtendable
public interface ModConfig
@@ -21,9 +26,13 @@ public interface ModConfig
* wrappers), enums (by constant name), records (TOML tables), {@code List<T>},
* {@code Map<String, T>} (tables with free keys), and {@code Optional<T>} for values that
* may be missing. Every other component must have a value. Keys without a component are
* logged as warnings.
* logged as warnings. {@link Min} and {@link Max} bound numeric components.
*
* @throws IllegalStateException listing every missing or mistyped value
* @throws IllegalStateException listing every missing, mistyped, or out-of-bounds value
* @throws IllegalArgumentException when {@code type} has a component that configuration
* values cannot have, an invalid or repeated {@link Key},
* or {@link Min}/{@link Max} on a component that is not
* numeric or with the minimum above the maximum
*/
<T extends Record> T bind(Class<T> type);
@@ -0,0 +1,32 @@
package dev.overlord.zomboidforge.api.annotations;
import dev.overlord.zomboidforge.api.ModConfig;
import dev.overlord.zomboidforge.api.storage.ModStorage;
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;
/**
* The key of a record component, instead of the one converted from its name ({@code maxBalance}
* to {@code max_balance}): the {@code config.toml} key for {@link ModConfig#bind}, the column
* name for {@link ModStorage} tables. A component keeps its key when it is renamed:
*
* <pre>{@code
* record Settings(@Key("max_hp_regen") int healthRegenPerHour) {}
* }</pre>
*
* The key is {@code snake_case}: lowercase letters, digits, and single underscores, starting with
* a letter. Two components of a record cannot have the same key. For a storage record, giving an
* existing component another key renames its column, which the table treats as a removed and a
* new column, as when the component is renamed without {@code @Key}.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.RECORD_COMPONENT)
public @interface Key
{
String value();
}
@@ -0,0 +1,21 @@
package dev.overlord.zomboidforge.api.annotations;
import dev.overlord.zomboidforge.api.ModConfig;
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;
/**
* The largest value a numeric component of a configuration record accepts; see {@link Min}.
* {@link ModConfig#bind} reports a larger value in {@code config.toml} with its key.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.RECORD_COMPONENT)
public @interface Max
{
double value();
}
@@ -0,0 +1,28 @@
package dev.overlord.zomboidforge.api.annotations;
import dev.overlord.zomboidforge.api.ModConfig;
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;
/**
* The smallest value a numeric component of a configuration record accepts. {@link ModConfig#bind}
* reports a smaller value in {@code config.toml} with its key, together with every other
* problem. On a {@code List}, {@code Map}, or {@code Optional} component, it applies to each
* value. The bound may be a fraction ({@code @Min(0.1)}); it is not {@code NaN}. Values are
* compared as {@code double}. Storage records ignore it.
*
* <pre>{@code
* record Settings(@Min(1) @Max(100) int maxPlayers) {}
* }</pre>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.RECORD_COMPONENT)
public @interface Min
{
double value();
}
@@ -0,0 +1,28 @@
package dev.overlord.zomboidforge.api.annotations;
import dev.overlord.zomboidforge.api.Commands;
import dev.overlord.zomboidforge.api.Mod;
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 class as an admin command, as {@link Commands#register} does, while the mod
* loads: after the entry point's listener methods and before {@link Mod#onLoad()}. Only classes
* in the mod's own jar are found, not those of its libraries; several are registered in the
* order of their names. The class follows the rules of {@link Commands}.
*
* <pre>{@code
* @RegisterCommand
* @CommandName(name = "zfhello")
* @RequiredCapability(requiredCapability = Capability.LoginOnServer)
* public class HelloCommand extends CommandBase { ... }
* }</pre>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RegisterCommand {}