Single module modding
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Block Components
|
||||
|
||||
A block's JSON says what it is. Components say what it _does_. A component is a set of handlers a server script
|
||||
A block's JSON says what it is. Components say what it _does_. A component is a set of handlers the server phase
|
||||
registers under an id. Any block that lists that id in its `components` gets that behavior, with its own parameters.
|
||||
|
||||
This is how every block that does something works, including the base game's chests, furnaces and crops. One component
|
||||
@@ -20,7 +20,7 @@ A block can list several. Their handlers run in the listed order.
|
||||
|
||||
## Registering a Component
|
||||
|
||||
In the server script:
|
||||
In the server phase:
|
||||
|
||||
```ts
|
||||
import type { ServerContext } from "cubi/server";
|
||||
@@ -30,7 +30,7 @@ interface OxidizesParams {
|
||||
into: string;
|
||||
}
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
export function server(ctx: ServerContext) {
|
||||
ctx.components.register_block<OxidizesParams>("copper_tools:oxidizes", {
|
||||
on_create(block) {
|
||||
block.data = { age: 0 };
|
||||
@@ -53,9 +53,9 @@ export function setup(ctx: ServerContext) {
|
||||
|
||||
Items have components the same way, and so do [status effects](../entities/effects.md#effect-components).
|
||||
|
||||
Components can only be registered during `setup`, and only in the mod's own namespace. Once every mod's `setup` has run,
|
||||
a block or item that lists a component nobody registered stops the server from starting, naming the block and the
|
||||
component.
|
||||
Components can only be registered while the server phase runs, and only in the mod's own namespace. Once every mod's
|
||||
server phase has run, a block or item that lists a component nobody registered stops the server from starting, naming
|
||||
the block and the component.
|
||||
|
||||
## Handlers
|
||||
|
||||
@@ -145,14 +145,14 @@ not in memory counts as loaded, and reading it reads it from the save.
|
||||
## Rules
|
||||
|
||||
A component's rules decide a block's states from where it's placed, what's around it and being right clicked. They're
|
||||
registered in the mod's [shared script](../concepts/sides.md#shared-scripts) instead, under the same id, because clients
|
||||
run them too: that's how a player sees a fence join its neighbors the moment they place it, instead of a moment later
|
||||
when the server answers. A component can have rules, handlers, or both, and a block can list one that only has rules.
|
||||
registered in the mod's [shared phase](../concepts/sides.md#shared) instead, under the same id, because clients run them
|
||||
too: that's how a player sees a fence join its neighbors the moment they place it, instead of a moment later when the
|
||||
server answers. A component can have rules, handlers, or both, and a block can list one that only has rules.
|
||||
|
||||
```ts
|
||||
import type { SharedContext } from "cubi/shared";
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
export function shared(ctx: SharedContext) {
|
||||
ctx.components.register_block("copper_tools:pipe", {
|
||||
state_for_placement(placement, params) {/* ... */},
|
||||
on_neighbor_change(block, params, neighbor) {/* ... */},
|
||||
@@ -167,7 +167,7 @@ export function setup(ctx: SharedContext) {
|
||||
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | |
|
||||
| `on_interact(block, params, interaction)` | A player right clicks it. | `true` if it did something |
|
||||
|
||||
Here `block` is `{ id, x, y, z }`, without tile data, which clients never have. `ctx.world` in a shared script has
|
||||
Here `block` is `{ id, x, y, z }`, without tile data, which clients never have. `ctx.world` in the shared phase has
|
||||
`get_block`, `get_state`, `set_state`, `is_face_sturdy`, `biome_at` and `season_at` (see
|
||||
[Seasons](../worldgen/biomes.md#seasons)), and only works inside rules. `ctx.blocks.components(id)` gives any block's
|
||||
components and their params, like on the server, for rules that look at what's around them, like a shaft lining up with
|
||||
@@ -329,7 +329,7 @@ catch up. If it's further behind than that, it skips the rest and logs a warning
|
||||
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
|
||||
| `bworld:water_source` | none | Refills the watering can. |
|
||||
| `bworld:fishable` | `loot_table`? | The fishing rod's hook floats on it and fish bite there when it lands in it. What's caught is `loot_table`, `bworld:gameplay/fishing` when not given, built from the fish whose `fluid` is this block. |
|
||||
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (both are rules, in the shared script). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
|
||||
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (both are rules, in the shared phase). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
|
||||
| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. |
|
||||
|
||||
Item components work the same way, see [Items](../items/index.md#item-components).
|
||||
|
||||
@@ -4,7 +4,7 @@ Blocks make up the world. Each kind of block is defined once, in a JSON file in
|
||||
world is made of references to those definitions at different positions.
|
||||
|
||||
A block's definition says how it looks, how it's mined and what it drops. What it _does_ comes from
|
||||
[components](components.md), which server scripts register.
|
||||
[components](components.md), which server phases register.
|
||||
|
||||
## A Basic Block
|
||||
|
||||
@@ -27,7 +27,7 @@ side, takes 5 seconds to break by hand, breaks faster with a pickaxe, and only d
|
||||
of tier 1 (stone or copper) or better.
|
||||
|
||||
A mod with many similar blocks, like every block of every kind of rock, can register them in code from a
|
||||
[content script](../concepts/sides.md#content-scripts) instead, with the same object a file holds under `"block"`.
|
||||
[content phase](../concepts/sides.md#content) instead, with the same object a file holds under `"block"`.
|
||||
|
||||
## Properties
|
||||
|
||||
@@ -225,8 +225,8 @@ plains' color.
|
||||
|
||||
## Raycasts
|
||||
|
||||
Server scripts find the first block on a line with `ctx.world.raycast`, like Minecraft's `Level.clip`. The fishing rod's
|
||||
hook uses it to see where it lands, and with a player's
|
||||
The server phase finds the first block on a line with `ctx.world.raycast`, like Minecraft's `Level.clip`. The fishing
|
||||
rod's hook uses it to see where it lands, and with a player's
|
||||
[`eye_position` and `look_direction`](../entities/players.md#the-player-api) it finds what they're looking at:
|
||||
|
||||
```ts
|
||||
|
||||
@@ -21,7 +21,7 @@ the defaults when it's placed or generated.
|
||||
|
||||
## Reading and Changing States
|
||||
|
||||
Server scripts use `ctx.world`:
|
||||
The server phase uses `ctx.world`:
|
||||
|
||||
```ts
|
||||
const age = ctx.world.get_state(x, y, z, "age") ?? 0;
|
||||
|
||||
@@ -78,7 +78,7 @@ ctx.components.register_block("copper_tools:shaft", {
|
||||
changes often doesn't resend the others, and setting a field to what it already is sends nothing.
|
||||
- `undefined` removes a field. All of them go when the block is broken or replaced.
|
||||
- It's saved with the world. `get_client_data` and `set_client_data` copy the value, so change it by setting it again.
|
||||
- Client scripts read it with `ctx.world.get_client_data<T>(x, y, z, field)`, see [GUI](../gui/index.md). A block's
|
||||
- Client phases read it with `ctx.world.get_client_data<T>(x, y, z, field)`, see [GUI](../gui/index.md). A block's
|
||||
[visual](visuals.md) reads it with `block.get_client_data`, and its `update` is called when a field changes.
|
||||
|
||||
Like NeoForge's `getUpdateTag`, but per field. Keep it to what clients need to draw the block: everything in it is sent
|
||||
@@ -92,7 +92,7 @@ A block with `item_display` draws one item lying on top of it, like the food on
|
||||
"item_display": { "surface": 1 }
|
||||
```
|
||||
|
||||
`surface` is the height in pixels the item lies at, 0 to 16. The server script sets the item:
|
||||
`surface` is the height in pixels the item lies at, 0 to 16. The server phase sets the item:
|
||||
|
||||
```ts
|
||||
ctx.world.set_block_item(x, y, z, { id: "bworld:cabbage", count: 1 });
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Visuals
|
||||
|
||||
A visual draws a block with code, for blocks that move or show what's in them: a shaft that turns, a press coming down,
|
||||
the items and fluid in a machine. A client script registers one for a block, and every block of that kind in a shown
|
||||
the items and fluid in a machine. The client phase registers one for a block, and every block of that kind in a shown
|
||||
chunk gets its own. It's bworld's version of Minecraft's block entity renderers, shaped like the Flywheel visuals Create
|
||||
draws its machines with.
|
||||
|
||||
@@ -10,7 +10,7 @@ draws its machines with.
|
||||
```ts
|
||||
import type { ClientContext } from "cubi/client";
|
||||
|
||||
export function setup(ctx: ClientContext) {
|
||||
export function client(ctx: ClientContext) {
|
||||
ctx.render.register_visual("copper_tools:shaft", (block, instances) => {
|
||||
const shaft = instances.rotating();
|
||||
const turn = () => shaft.set_rotation("y", block.get_client_data<number>("speed") ?? 0);
|
||||
@@ -20,7 +20,7 @@ export function setup(ctx: ClientContext) {
|
||||
}
|
||||
```
|
||||
|
||||
- Visuals can only be registered during `setup`, and a block has at most one.
|
||||
- Visuals can only be registered while the client phase runs, and a block has at most one.
|
||||
- With `replaces_model: true`, the block's model is left out of the terrain, for a visual that draws all of the block,
|
||||
like the shaft above. Without it, the model is drawn as usual and the visual adds to it, like a press's head over its
|
||||
casing. The model is still what the block's item, its outline and the cracks on it while it's broken are drawn with.
|
||||
@@ -145,7 +145,7 @@ ctx.render.register_visual("copper_tools:crank", (block, instances) => {
|
||||
});
|
||||
```
|
||||
|
||||
Only visuals in chunks in view get `on_frame`, so keep anything that has to happen out of view in the server script.
|
||||
Only visuals in chunks in view get `on_frame`, so keep anything that has to happen out of view in the server phase.
|
||||
|
||||
## Cost
|
||||
|
||||
|
||||
Reference in New Issue
Block a user