Annotate public APIs with JetBrains annotations and enforce immutability constraints on collections.

This commit is contained in:
Overlord committed 2026-10-07 11:02:55 +02:00
1 parent 36ac320c9c
commit 7ac442c4f6
16 files changed
+67 -8

No files matched your search

@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@@ -9,6 +11,7 @@ import java.util.concurrent.Executor;
* task, named {@code zf-async-<id>-<n>}. ZomboidForge shuts it down when the server stops; tasks
* submitted after that are rejected with a {@link java.util.concurrent.RejectedExecutionException}.
*/
@ApiStatus.NonExtendable
public interface AsyncExecutor extends Executor
{
/** Runs {@code task} on a new virtual thread. An exception it throws is logged with the mod's id. */
@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.Unmodifiable;
import java.util.Map;
import java.util.Objects;
@@ -42,7 +44,7 @@ public final class ClientCommandEvent implements MainThreadEvent, Cancellable
public String command() { return this.command; }
public Map<Object, Object> args() { return this.args; }
public @Unmodifiable Map<Object, Object> args() { return this.args; }
@Override
public void cancel() { this.cancelled = true; }
@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
import java.util.Map;
/**
@@ -17,6 +19,7 @@ import java.util.Map;
* sent on the main thread, at once when called there, otherwise with the mod's queued
* main-thread tasks.
*/
@ApiStatus.NonExtendable
public interface ClientCommands
{
/**
@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
/**
* Registration of admin commands. A registered command works wherever Project Zomboid's own
* admin commands work: in the in-game chat ({@code /name}), the server console, and RCON. It
@@ -21,6 +23,7 @@ package dev.overlord.zomboidforge.api;
* Project Zomboid creates a new instance for every use and calls its {@code Command()}, whose
* result is the reply.
*/
@ApiStatus.NonExtendable
public interface Commands
{
/**
@@ -1,11 +1,14 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
import java.util.function.Consumer;
/**
* Registration of event listeners. Listeners of one event run in registration order. A listener
* that throws is logged with the mod's id; one that throws repeatedly is removed.
*/
@ApiStatus.NonExtendable
public interface Events
{
/**
@@ -1,5 +1,8 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Range;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@@ -14,6 +17,7 @@ import java.util.concurrent.Executor;
* main thread: {@code submit(...).thenAcceptAsync(result -> ..., modThread())} moves the
* follow-up work back. The main thread must never wait for another thread's result.
*/
@ApiStatus.NonExtendable
public interface MainThread extends Executor
{
/**
@@ -37,7 +41,7 @@ public interface MainThread extends Executor
*
* @throws IllegalArgumentException when {@code ticks} is less than 1
*/
void runLater(int ticks, Runnable task);
void runLater(@Range(from = 1, to = Integer.MAX_VALUE) int ticks, Runnable task);
/** Whether the calling thread is the main thread. */
boolean isMainThread();
@@ -3,6 +3,9 @@ package dev.overlord.zomboidforge.api;
import dev.overlord.zomboidforge.api.internal.ModBinding;
import dev.overlord.zomboidforge.api.storage.ModStorage;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NonBlocking;
import java.nio.file.Path;
/**
@@ -42,6 +45,7 @@ public abstract class Mod
public final String version() { return this.context.version(); }
/** Called once on the main thread, after the instance is created. */
@ApiStatus.OverrideOnly
@SuppressWarnings("RedundantThrows")
public void onLoad() throws Exception {}
@@ -49,12 +53,15 @@ public abstract class Mod
* Called once on the main thread, after the world is loaded, the network port is open, and
* RCON is initialized; directly before the main loop starts.
*/
@ApiStatus.OverrideOnly
@NonBlocking
public void onServerStarted() {}
/**
* Called once on the shutdown thread when the server stops, before the world is saved.
* The main thread may still be finishing a tick. Mods are stopped in reverse load order.
*/
@ApiStatus.OverrideOnly
public void onStopping() {}
/** The running Project Zomboid version. */
@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
import java.util.Optional;
/**
@@ -10,6 +12,7 @@ import java.util.Optional;
* <p>Keys are {@code snake_case}; record components and paths use the same names converted
* from {@code camelCase} ({@code maxBalance} reads {@code max_balance}).
*/
@ApiStatus.NonExtendable
public interface ModConfig
{
/**
@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
import dev.overlord.zomboidforge.api.storage.ModStorage;
import java.nio.file.Path;
@@ -8,6 +10,7 @@ import java.nio.file.Path;
* What ZomboidForge provides to one mod. Mods reach it through the methods of {@link Mod};
* ZomboidForge implements it.
*/
@ApiStatus.NonExtendable
public interface ModContext
{
/** The mod's id from {@code mod.toml}. */
@@ -1,5 +1,7 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
@@ -12,6 +14,7 @@ import java.util.concurrent.ScheduledFuture;
* that are rejected with a {@link java.util.concurrent.RejectedExecutionException}. It must not
* touch game objects; that goes through {@code main()}.
*/
@ApiStatus.NonExtendable
public interface ModThread extends Executor
{
/** Runs {@code task} on the mod thread. An exception it throws is logged with the mod's id. */
@@ -1,6 +1,9 @@
package dev.overlord.zomboidforge.api;
import org.jetbrains.annotations.ApiStatus;
/** A registered event listener. */
@ApiStatus.NonExtendable
public interface Subscription
{
/** Removes the listener; it receives no further events. Calling it again has no effect. */
@@ -1,5 +1,8 @@
package dev.overlord.zomboidforge.api.storage;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Blocking;
import java.util.function.Supplier;
/**
@@ -16,9 +19,11 @@ import java.util.function.Supplier;
* once the server runs they throw an {@link IllegalStateException}, so database I/O never costs
* tick time. Use {@code modThread()} or {@code async()}.
*/
@ApiStatus.NonExtendable
public interface ModStorage
{
/** Opens the table {@code name} with schema version 1 and no migrations. */
@Blocking
<R extends Record> Table<R> table(String name, Class<R> type);
/** A table with a schema version and migrations; see {@link TableDefinition}. */
@@ -32,8 +37,10 @@ public interface ModStorage
* other threads, for example with {@code main().submit(...).join()}. A backup waits at most
* 10 seconds for it and then runs without pausing storage.
*/
@Blocking
void transaction(Runnable work);
/** Like {@link #transaction(Runnable)}, returning the result of {@code work}. */
@Blocking
<T> T transaction(Supplier<T> work);
}
@@ -1,6 +1,8 @@
package dev.overlord.zomboidforge.api.storage;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.Set;
@@ -10,12 +12,13 @@ import java.util.Set;
* numbers as {@code Double}, texts (also UUIDs, instants, and enum constants) as
* {@code String}, bytes as {@code byte[]}.
*/
@ApiStatus.NonExtendable
public interface Row
{
String key();
/** The names of the stored columns. */
Set<String> columns();
@Unmodifiable Set<String> columns();
/** The stored value, or {@code null} when it is null. @throws IllegalArgumentException for an unknown column */
@Nullable Object get(String column);
@@ -1,5 +1,9 @@
package dev.overlord.zomboidforge.api.storage;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.Unmodifiable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -13,6 +17,8 @@ import java.util.function.UnaryOperator;
* unless it runs inside {@link ModStorage#transaction}. Calls block; on the main thread they
* are only allowed during startup.
*/
@ApiStatus.NonExtendable
@Blocking
public interface Table<R extends Record>
{
/** Stores {@code value} under {@code key}, replacing an existing row. */
@@ -24,7 +30,7 @@ public interface Table<R extends Record>
Optional<R> get(String key);
/** The rows of the given keys that exist. */
Map<String, R> getAll(Collection<String> keys);
@Unmodifiable Map<String, R> getAll(Collection<String> keys);
boolean contains(String key);
@@ -35,7 +41,7 @@ public interface Table<R extends Record>
int deleteAll(Collection<String> keys);
/** Every key, sorted. */
List<String> keys();
@Unmodifiable List<String> keys();
/**
* Calls {@code action} for every row, sorted by key, without loading all rows at once.
@@ -1,5 +1,9 @@
package dev.overlord.zomboidforge.api.storage;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.Range;
import java.util.function.Function;
/**
@@ -12,10 +16,11 @@ import java.util.function.Function;
* .open();
* }</pre>
*/
@ApiStatus.NonExtendable
public interface TableDefinition<R extends Record>
{
/** Schema version of the record, at least 1. Default: 1. */
TableDefinition<R> version(int version);
TableDefinition<R> version(@Range(from = 1, to = Integer.MAX_VALUE) int version);
/**
* How to turn a row stored with schema version {@code from} into the current record. Needed
@@ -29,5 +34,6 @@ public interface TableDefinition<R extends Record>
*
* @throws StorageException when the stored table does not match and no migration applies
*/
@Blocking
Table<R> open();
}
@@ -117,7 +117,7 @@ final class SqlTable<R extends Record> implements Table<R>
return found;
});
return found;
return Collections.unmodifiableMap(found);
}
@Override
@@ -168,7 +168,7 @@ final class SqlTable<R extends Record> implements Table<R>
while (rows.next()) keys.add(rows.getString(1));
}
return keys;
return Collections.unmodifiableList(keys);
});
}