Single module modding
This commit is contained in:
@@ -8,12 +8,12 @@ Each argument type reads its part of the text and suggests what could go there.
|
||||
|
||||
## Registering a Command
|
||||
|
||||
In the server script's `setup`:
|
||||
In the server phase:
|
||||
|
||||
```ts
|
||||
import type { Player, ServerContext } from "cubi/server";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
export function server(ctx: ServerContext) {
|
||||
const { literal, argument, types } = ctx.commands;
|
||||
const heal = (player: Player, amount: number) => player.heal(amount);
|
||||
|
||||
@@ -102,7 +102,7 @@ ctx.commands.register("hello", {
|
||||
Command names are `a-z`, `0-9` and `_`. Two mods registering the same name, or a mod registering one of the engine's,
|
||||
stops the server from starting. Every mod command can also be run as `/<mod id>:<name>`, like `/copper_tools:heal`.
|
||||
|
||||
Commands can only be registered during `setup`.
|
||||
Commands can only be registered while the server phase runs.
|
||||
|
||||
## The Engine's Commands
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
A mod's config is its settings: the numbers and switches a server might want different without changing the mod, like
|
||||
how fast a machine works, how far something reaches, or whether a feature is on. The mod lists them with their defaults
|
||||
in `config.json`, a server changes the ones it wants in a file of its own, and the server script reads the result as
|
||||
in `config.json`, a server changes the ones it wants in a file of its own, and the server phase reads the result as
|
||||
`ctx.config`. It's like NeoForge's server configs.
|
||||
|
||||
## `config.json`
|
||||
@@ -26,7 +26,7 @@ can change has to be in this file with its default.
|
||||
|
||||
## Reading It
|
||||
|
||||
`ctx.config` in the server script holds the settings, with the server's changes made:
|
||||
`ctx.config` in the server phase holds the settings, with the server's changes made:
|
||||
|
||||
```ts
|
||||
import type { ServerContext } from "cubi/server";
|
||||
@@ -36,17 +36,17 @@ interface Config {
|
||||
fan: { push_distance: number; pull_distance: number };
|
||||
}
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
export function server(ctx: ServerContext) {
|
||||
const config: Config = ctx.config;
|
||||
ctx.log(`fans push things up to ${config.fan.push_distance} blocks away`);
|
||||
}
|
||||
```
|
||||
|
||||
It's there from `setup` on, and stays the same for as long as the server runs. It's typed `any`, so give it a type of
|
||||
your own like `Config` above.
|
||||
It's there from the server phase on, and stays the same for as long as the server runs. It's typed `any`, so give it a
|
||||
type of your own like `Config` above.
|
||||
|
||||
Only the server script has it. The copy of a mod players download leaves its config out, since a server's settings
|
||||
aren't always the defaults. When players need to see one, the server sends it, like any other
|
||||
Only the server phase has it. The copy of a mod players download leaves its config out, since a server's settings aren't
|
||||
always the defaults. When players need to see one, the server sends it, like any other
|
||||
[server data](../concepts/sides.md#what-each-side-knows): in a block's [client data](../blocks/tiledata.md), an item's
|
||||
`data`, or over a [channel](../networking/index.md#mod-channels).
|
||||
|
||||
|
||||
+13
-12
@@ -21,10 +21,10 @@ runs. When players join from another site, the confirm screen marks mods that ch
|
||||
## Hooking
|
||||
|
||||
```ts
|
||||
// server.ts: broken blocks drop twice
|
||||
// broken blocks drop twice
|
||||
import { engine, hook } from "cubi/engine";
|
||||
|
||||
export function setup() {
|
||||
export function server() {
|
||||
hook(engine.server.GameServer.prototype, "pop_item", {
|
||||
after(game, [x, y, z, stack], entity) {
|
||||
game.spawn_item(entity.x, entity.y, entity.z, stack.clone());
|
||||
@@ -48,16 +48,17 @@ made changes too. It returns a function that unhooks it.
|
||||
outermost, and `after` hooks chain results.
|
||||
- A hook that throws is logged under its mod (`[my_mod] an after hook on GameServer.pop_item threw: ...`) and skipped. A
|
||||
`replace` hook that throws after calling `original` gives what `original` returned, so the method doesn't run twice.
|
||||
- `hook` only works while the mod's script loads or runs `setup`. Unhooking works any time.
|
||||
- `hook` only works while a phase runs. Unhooking works any time. Hook inside a phase, not at the script's top level,
|
||||
which runs once on each side, in whichever phase imports the script first.
|
||||
- Unhooking a method's last hook puts the original back, so a method nobody hooks costs nothing.
|
||||
|
||||
## What's in `engine`
|
||||
|
||||
| Side | Where | Like |
|
||||
| -------- | -------------- | ----------------------------------------------------------------------------------------- |
|
||||
| `common` | every script | `EverythingRegistry`, `ItemStack`, `Container`, `BlockRules`, `NoiseTerrain`, `physics` |
|
||||
| `server` | server scripts | `GameServer`, `ServerWorld`, `ServerPlayer`, `Entity`, `Mob`, `ModRuntime`, the mob goals |
|
||||
| `client` | client scripts | `Client`, `LocalPlayer`, `ClientLevel`, `GameRenderer`, `Camera`, `GuiScreen` |
|
||||
| Side | Where | Like |
|
||||
| -------- | ------------ | ----------------------------------------------------------------------------------------- |
|
||||
| `common` | every phase | `EverythingRegistry`, `ItemStack`, `Container`, `BlockRules`, `NoiseTerrain`, `physics` |
|
||||
| `server` | server phase | `GameServer`, `ServerWorld`, `ServerPlayer`, `Entity`, `Mob`, `ModRuntime`, the mob goals |
|
||||
| `client` | client phase | `Client`, `LocalPlayer`, `ClientLevel`, `GameRenderer`, `Camera`, `GuiScreen` |
|
||||
|
||||
The full lists are in `common/engine_modules.ts`, `server/game/engine_modules.ts` and `client/engine_modules.ts`. The
|
||||
types are the engine's own source, so `check-mods` typechecks against exactly the engine the mod runs on. `cubi/engine`
|
||||
@@ -67,13 +68,13 @@ also exports `game_version`, the version of the game that's running.
|
||||
through: `engine.common.physics.move_body` is how every entity moves, on the server and on clients.
|
||||
- `#private` members can't be hooked. They're kept for things with rules that must hold, like a container's slots.
|
||||
- Hooks reach one JavaScript realm. Chunk workers run their own copy of the engine, so a client hook doesn't change
|
||||
meshing. The server generates chunks in worldgen workers, and each loads the worldgen scripts into its own copy, so
|
||||
hooks on the terrain (`engine.common.NoiseTerrain`) go in the worldgen script. A server script's hooks there only
|
||||
meshing. The server generates chunks in worldgen workers, and each loads the worldgen phases into its own copy, so
|
||||
hooks on the terrain (`engine.common.NoiseTerrain`) go in the worldgen phase. The server phase's hooks there only
|
||||
reach the few chunks the game generates itself, so the world would come out different in places.
|
||||
|
||||
## Performance
|
||||
|
||||
Hooking is cheap to set up: hook in `setup`, before the game is running. Each call to a hooked method then goes through
|
||||
Hooking is cheap to set up: hook in a phase, before the game is running. Each call to a hooked method then goes through
|
||||
a wrapper that runs its hooks, which is fine for something that happens now and then, like a block breaking. On
|
||||
something called many times a tick, like `physics.move_body` or a method of every entity, the wrapper's cost adds up,
|
||||
and the engine can no longer inline the method into its callers.
|
||||
@@ -81,5 +82,5 @@ and the engine can no longer inline the method into its callers.
|
||||
## Security
|
||||
|
||||
A server mod with engine access controls the game worker completely, but the worker still has no Deno permissions. A
|
||||
client mod with it can change anything in the game page, which every client script can already reach. See
|
||||
client mod with it can change anything in the game page, which every client phase can already reach. See
|
||||
[Security](../concepts/security.md).
|
||||
|
||||
@@ -16,7 +16,7 @@ ctx.system.current_tick; // ticks since the server started
|
||||
```
|
||||
|
||||
`run_timeout(fn, 0)` runs on the next tick, which is how a [before event](../concepts/events.md) gets something done
|
||||
after it. Timers aren't saved: they're gone after a restart, so set them up again in `setup`.
|
||||
after it. Timers aren't saved: they're gone after a restart, so set them up again in the server phase.
|
||||
|
||||
For work every tick, subscribe to the `tick` event. For work on a block, use a
|
||||
[component](../blocks/components.md#ticking)'s `on_tick` or `on_second`.
|
||||
|
||||
Reference in New Issue
Block a user