Add client command integration for Build 42: define APIs, event system, hooks, patches, and Lua table converters. Include tests, documentation, and mod support.

This commit is contained in:
Overlord committed 2026-10-03 13:22:47 +02:00
1 parent af7174e128
commit a351fc237c
24 files changed
+870 -48

No files matched your search

@@ -0,0 +1,14 @@
package dev.overlord.zomboidforge.api;
/**
* An event whose default action, what Project Zomboid does after the event, can be prevented.
* Only listeners on the main thread can cancel: the default action runs once every main-thread
* listener returned. Every listener is called, also after one cancelled.
*/
public interface Cancellable
{
/** Prevents the default action. */
void cancel();
boolean isCancelled();
}
@@ -0,0 +1,55 @@
package dev.overlord.zomboidforge.api;
import java.util.Map;
import java.util.Objects;
/**
* A client command arrived: a client called Lua's {@code sendClientCommand(module, command, args)}.
* Posted on the main thread for every client command, including those of Project Zomboid and of
* Lua mods, so listeners filter by {@link #module()}.
*
* <p>Default action: Project Zomboid passes the command to Lua's {@code Events.OnClientCommand}.
* {@link #cancel()} prevents that, for example when a Java mod handles the module itself.
*
* <p>{@link #args()} is the Lua table converted to Java: keys are {@code String} or
* {@code Double} (Lua arrays have the keys {@code 1.0}, {@code 2.0}, ...), values are
* {@code String}, {@code Double}, {@code Boolean}, a nested {@code Map} of the same kind, or a
* game object that Project Zomboid sends with commands (in Build 42 an inventory item, an
* {@code IsoDirections}, or a dead body). The maps cannot be modified.
*/
public final class ClientCommandEvent implements MainThreadEvent, Cancellable
{
private final PlayerRef player;
private final String module;
private final String command;
private final Map<Object, Object> args;
private boolean cancelled;
/** @param args the converted arguments; empty when the client sent none */
public ClientCommandEvent(PlayerRef player, String module, String command, Map<Object, Object> args)
{
this.player = Objects.requireNonNull(player, "player");
this.module = Objects.requireNonNull(module, "module");
this.command = Objects.requireNonNull(command, "command");
this.args = Objects.requireNonNull(args, "args");
}
/** The player who sent the command. */
public PlayerRef player() { return this.player; }
public String module() { return this.module; }
public String command() { return this.command; }
public Map<Object, Object> args() { return this.args; }
@Override
public void cancel() { this.cancelled = true; }
@Override
public boolean isCancelled() { return this.cancelled; }
@Override
public String toString() { return "ClientCommandEvent[" + this.player + ", " + this.module + "." + this.command + ", " + this.args + (this.cancelled ? ", cancelled" : "") + "]"; }
}
@@ -0,0 +1,35 @@
package dev.overlord.zomboidforge.api;
import java.util.Map;
/**
* Server commands to clients, the reply channel to {@link ClientCommandEvent}: a client receives
* them in Lua's {@code Events.OnServerCommand(module, command, args)}. Together, a Java mod on the
* server and a Lua mod on the client exchange commands while players keep the official client.
*
* <p>{@code args} becomes a Lua table: keys are {@code String} or a {@code Number} (sent as a
* Lua number), values are {@code String}, {@code Number}, {@code Boolean}, a nested {@code Map}, a
* {@code List} (a Lua array, indexed from 1), or a game object that Project Zomboid sends with
* commands (see {@link ClientCommandEvent}). {@code null} keys or values, and cycles, are not
* allowed. An empty map sends no arguments; the client then receives {@code nil}.
*
* <p>The methods can be called on any thread. The arguments are checked at once; the command is
* sent on the main thread, at once when called there, otherwise with the mod's queued
* main-thread tasks.
*/
public interface ClientCommands
{
/**
* Sends a command to {@code player}. Nothing is sent when the player is no longer connected.
*
* @throws IllegalArgumentException when {@code args} contains a key or value that cannot be sent
*/
void send(PlayerRef player, String module, String command, Map<?, ?> args);
/**
* Sends a command to every connected client.
*
* @throws IllegalArgumentException when {@code args} contains a key or value that cannot be sent
*/
void sendToAll(String module, String command, Map<?, ?> args);
}
@@ -87,4 +87,7 @@ public abstract class Mod
/** Admin command registration. */
protected final Commands commands() { return this.context.commands(); }
/** Server commands to clients, received by their Lua mods. */
protected final ClientCommands clientCommands() { return this.context.clientCommands(); }
}
@@ -45,4 +45,7 @@ public interface ModContext
/** Admin command registration. */
Commands commands();
/** Server commands to clients, received by their Lua mods. */
ClientCommands clientCommands();
}
@@ -0,0 +1,19 @@
package dev.overlord.zomboidforge.api;
import java.util.Objects;
/**
* A connected player, by value: usable on any thread and after the player left. Project
* Zomboid reuses online ids of players who left, so ZomboidForge matches both the id and the
* username before it acts on a player.
*
* @param onlineId the player's online id while connected
* @param username the player's account name
*/
public record PlayerRef(int onlineId, String username)
{
public PlayerRef
{
Objects.requireNonNull(username, "username");
}
}