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:
1 parent
af7174e128
commit
a351fc237c
24 files changed
+870
-48
No files matched your search
+66
-14
@@ -164,20 +164,21 @@ public final class ShopMod extends Mod
|
||||
|
||||
Methods of `Mod`, usable from the constructor on (also in field initializers):
|
||||
|
||||
| Method | Returns | Use |
|
||||
|-----------------|-----------------|---------------------------------------------------------------|
|
||||
| `id()` | `String` | the mod's id from `mod.toml` |
|
||||
| `version()` | `String` | the mod's version from `mod.toml` |
|
||||
| `gameVersion()` | `GameVersion` | the running Project Zomboid version |
|
||||
| `config()` | `ModConfig` | settings from `config.toml`, bound to records or read by path |
|
||||
| `logger()` | `System.Logger` | messages in the Project Zomboid log, marked with the mod's id |
|
||||
| `directory()` | `Path` | the mod's folder, `<forge>/mods/<id>` |
|
||||
| `events()` | `Events` | event listeners |
|
||||
| `main()` | `MainThread` | code that touches game objects |
|
||||
| `modThread()` | `ModThread` | the mod's own work, one task at a time |
|
||||
| `async()` | `AsyncExecutor` | blocking work such as HTTP calls or file I/O |
|
||||
| `storage()` | `ModStorage` | the mod's tables in the world's `zomboidforge.db` |
|
||||
| `commands()` | `Commands` | admin commands for chat, the server console, and RCON |
|
||||
| Method | Returns | Use |
|
||||
|--------------------|------------------|---------------------------------------------------------------|
|
||||
| `id()` | `String` | the mod's id from `mod.toml` |
|
||||
| `version()` | `String` | the mod's version from `mod.toml` |
|
||||
| `gameVersion()` | `GameVersion` | the running Project Zomboid version |
|
||||
| `config()` | `ModConfig` | settings from `config.toml`, bound to records or read by path |
|
||||
| `logger()` | `System.Logger` | messages in the Project Zomboid log, marked with the mod's id |
|
||||
| `directory()` | `Path` | the mod's folder, `<forge>/mods/<id>` |
|
||||
| `events()` | `Events` | event listeners |
|
||||
| `main()` | `MainThread` | code that touches game objects |
|
||||
| `modThread()` | `ModThread` | the mod's own work, one task at a time |
|
||||
| `async()` | `AsyncExecutor` | blocking work such as HTTP calls or file I/O |
|
||||
| `storage()` | `ModStorage` | the mod's tables in the world's `zomboidforge.db` |
|
||||
| `commands()` | `Commands` | admin commands for chat, the server console, and RCON |
|
||||
| `clientCommands()` | `ClientCommands` | server commands to clients, received by their Lua mods |
|
||||
|
||||
## Threads
|
||||
|
||||
@@ -259,6 +260,57 @@ public void onLoad() { this.commands().register(HelloCommand.class); }
|
||||
that the work started.
|
||||
- Project Zomboid's commands are looked up first; mods' commands follow in load order.
|
||||
|
||||
## Client commands
|
||||
|
||||
A Java mod on the server and a Lua mod on the client (for example from the Workshop) talk
|
||||
through Project Zomboid's command channel, so players keep the official client.
|
||||
|
||||
Client to server: the Lua mod calls `sendClientCommand`, and the server posts
|
||||
`ClientCommandEvent` on the main thread.
|
||||
|
||||
```lua
|
||||
sendClientCommand(getPlayer(), "Shop", "buy", { item = "Base.Axe", count = 2 })
|
||||
```
|
||||
|
||||
```java
|
||||
this.events().onMain(ClientCommandEvent.class, event ->
|
||||
{
|
||||
if (!event.module().equals("Shop")) return;
|
||||
event.cancel(); // Lua's OnClientCommand does not see it
|
||||
|
||||
String item = (String) event.args().get("item");
|
||||
int count = ((Double) event.args().get("count")).intValue();
|
||||
// ...
|
||||
this.clientCommands().send(event.player(), "Shop", "bought", Map.of("item", item, "count", count));
|
||||
});
|
||||
```
|
||||
|
||||
Server to client: `clientCommands().send(player, ...)` and `sendToAll(...)`; the Lua mod
|
||||
receives them in `Events.OnServerCommand`.
|
||||
|
||||
```lua
|
||||
Events.OnServerCommand.Add(function(module, command, args)
|
||||
if module == "Shop" and command == "bought" then print(args.item, args.count) end
|
||||
end)
|
||||
```
|
||||
|
||||
- **Every** client command is posted, including those of Project Zomboid (`vehicle`,
|
||||
`player`, ...) and of other mods; filter by `module()`. The event is a `MainThreadEvent`.
|
||||
- **`cancel()`** stops Project Zomboid from passing the command to Lua's `OnClientCommand`.
|
||||
Every listener still runs; `isCancelled()` tells whether another one cancelled.
|
||||
- **`args()`** is the Lua table as an unmodifiable map: keys are `String` or `Double` (Lua
|
||||
arrays have the keys `1.0`, `2.0`, ...); values are `String`, `Double` (every Lua number),
|
||||
`Boolean`, nested maps, or game objects (inventory items, directions, dead bodies). It is
|
||||
empty when the client sent no table.
|
||||
- **`player()`** is a `PlayerRef` (online id and username), a value that stays valid on other
|
||||
threads and after the player left. Sending to a player who left sends nothing.
|
||||
- **Sending** works from any thread. The arguments are copied and checked at once
|
||||
(`IllegalArgumentException` for unsupported types, `null`, or cycles); numbers become Lua
|
||||
numbers, lists become Lua arrays. The command is sent on the main thread: at once when
|
||||
called there, otherwise as one of the mod's queued main-thread tasks.
|
||||
- Client commands come from players and can be forged by a modified client. Check every
|
||||
argument and the player's rights before acting on it.
|
||||
|
||||
## Storage
|
||||
|
||||
Mod data belongs in `storage()`: tables in `zomboidforge.db`, a SQLite database in the world's
|
||||
|
||||
Reference in new issue
Block a user