Single module modding
This commit is contained in:
@@ -48,8 +48,8 @@ a tank.
|
||||
|
||||
## Rules
|
||||
|
||||
A fluid's name, color and value can depend on its data. The mod's [shared script](../concepts/sides.md#shared-scripts)
|
||||
gives one of its fluids rules for that, like NeoForge's `FluidType.getDescription` and `FluidTintSource.colorAsStack`:
|
||||
A fluid's name, color and value can depend on its data. The mod's [shared phase](../concepts/sides.md#shared) gives one
|
||||
of its fluids rules for that, like NeoForge's `FluidType.getDescription` and `FluidTintSource.colorAsStack`:
|
||||
|
||||
```ts
|
||||
import type { SharedContext } from "cubi/shared";
|
||||
@@ -58,7 +58,7 @@ interface SyrupData {
|
||||
tree?: string;
|
||||
}
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
export function shared(ctx: SharedContext) {
|
||||
ctx.fluids.register("copper_tools:syrup", {
|
||||
name(data) {
|
||||
const { tree } = (data ?? {}) as SyrupData;
|
||||
@@ -86,15 +86,15 @@ export function setup(ctx: SharedContext) {
|
||||
|
||||
`data` is the variant's, `undefined` when it has none, and mustn't be changed. A rule that throws, or returns something
|
||||
that isn't a name, a color or a value, is logged and the JSON's is used. A fluid has one set of rules, from its own mod,
|
||||
and they can only be registered during `setup`.
|
||||
and they can only be registered while the shared phase runs.
|
||||
|
||||
The rules are in the shared script because both sides need them: the server for tooltips, and clients to draw the fluid.
|
||||
Shared scripts have `ctx.items.exists(id)`, `ctx.items.name(id)`, `ctx.items.components(id)` and
|
||||
The rules are in the shared phase because both sides need them: the server for tooltips, and clients to draw the fluid.
|
||||
The shared phase has `ctx.items.exists(id)`, `ctx.items.name(id)`, `ctx.items.components(id)` and
|
||||
`ctx.items.sell_value(id, data?)` for rules that read an item, like syrup named after its tree.
|
||||
|
||||
## Using Fluids
|
||||
|
||||
Server and client scripts have `ctx.fluids`, which goes through the rules:
|
||||
Server and client phases have `ctx.fluids`, which goes through the rules:
|
||||
|
||||
```ts
|
||||
const birch = { id: "copper_tools:syrup", data: { tree: "bworld:birch_log" } };
|
||||
@@ -106,10 +106,10 @@ ctx.fluids.same(birch, { id: "copper_tools:syrup" }); // false, the data is diff
|
||||
```
|
||||
|
||||
`ids` gives each fluid once, without data, so a fluid that's many things by its data, like syrup from each tree, is only
|
||||
there as plain `copper_tools:syrup`. Client scripts also have `ctx.fluids.mod(id)`, the name of the mod that added it,
|
||||
there as plain `copper_tools:syrup`. The client phase also has `ctx.fluids.mod(id)`, the name of the mod that added it,
|
||||
like `ctx.items.mod`.
|
||||
|
||||
Shared scripts have `ctx.fluids.exists` and `ctx.fluids.same` too, which don't depend on rules, like a
|
||||
The shared phase has `ctx.fluids.exists` and `ctx.fluids.same` too, which don't depend on rules, like a
|
||||
[recipe type](recipes.md#custom-recipe-types) that's found by a fluid.
|
||||
|
||||
## Containers
|
||||
@@ -146,11 +146,11 @@ tooltip says what's in it and how much, like "Birch Log Syrup, 250 mB".
|
||||
|
||||
A full one sells for its own `base_sell_value` and what its fluid is worth on top: the fluid's value for 1000 mB times
|
||||
how much it holds, rounded up to a whole number. A jar of maple syrup worth 40 a bucket sells for 10, and the jar's own
|
||||
value when it has one. It can't be sold if neither has a value. Server scripts get this from
|
||||
`ctx.items.sell_value(id, data)`, which the base game's shipping bin sells by. Client and shared scripts have it too,
|
||||
and the base game shows it in [tooltips](../gui/index.md#item-tooltips).
|
||||
value when it has one. It can't be sold if neither has a value. The server phase gets this from
|
||||
`ctx.items.sell_value(id, data)`, which the base game's shipping bin sells by. Client and shared phases have it too, and
|
||||
the base game shows it in [tooltips](../gui/index.md#item-tooltips).
|
||||
|
||||
A server script fills and empties one through its [fluid handler](#fluid-handlers), and `ctx.items.fluid_capacity(id)`
|
||||
The server phase fills and empties one through its [fluid handler](#fluid-handlers), and `ctx.items.fluid_capacity(id)`
|
||||
says how much one holds.
|
||||
|
||||
## Fluid Handlers
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
A loot table says what something gives, as data: a block when it's broken, a mob when it dies, a fishing rod when it
|
||||
reels something in, a crop when it's picked, and a container a script fills, like a chest in a ruin. They work like
|
||||
Minecraft's, and mods can add their own conditions and functions from a server script.
|
||||
Minecraft's, and mods can add their own conditions and functions from the server phase.
|
||||
|
||||
## Files and Ids
|
||||
|
||||
@@ -174,12 +174,12 @@ Those become the `bworld:season`, `bworld:biome` and `bworld:time` conditions, w
|
||||
|
||||
## From a Script
|
||||
|
||||
Server scripts roll tables, fill containers, and add conditions, functions and changes of their own:
|
||||
The server phase rolls tables, fills containers, and adds conditions, functions and changes of its own:
|
||||
|
||||
```ts
|
||||
import type { ServerContext } from "cubi/server";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
export function server(ctx: ServerContext) {
|
||||
// "condition": "my_mod:night" in any table
|
||||
ctx.loot.register_condition("my_mod:night", (_params, context) => ctx.world.time % 24000 > 13000);
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ pattern fits anywhere in the grid.
|
||||
|
||||
## Smelting: `bworld:smelting` and `bworld:fuel`
|
||||
|
||||
For the furnace. They're the base game's own types, registered by its shared script like
|
||||
For the furnace. They're the base game's own types, registered by its shared phase like
|
||||
[any mod's](#custom-recipe-types):
|
||||
|
||||
```json
|
||||
@@ -53,7 +53,7 @@ long something burns.
|
||||
|
||||
## Smithing: `bworld:smithing`
|
||||
|
||||
For the smithing table, which upgrades a tool. It's the base game's own type, registered by its shared script like
|
||||
For the smithing table, which upgrades a tool. It's the base game's own type, registered by its shared phase like
|
||||
[any mod's](#custom-recipe-types):
|
||||
|
||||
```json
|
||||
@@ -76,7 +76,7 @@ are an error.
|
||||
|
||||
## Stonecutting: `bworld:stonecutting`
|
||||
|
||||
For the stonecutter, which cuts one item into another. It's the base game's own type, registered by its shared script:
|
||||
For the stonecutter, which cuts one item into another. It's the base game's own type, registered by its shared phase:
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -95,9 +95,8 @@ uses up one of the input.
|
||||
## Juicing: `bworld:juicing`
|
||||
|
||||
For the juicer, which presses an item into a fluid. The base game registers one from its
|
||||
[shared script](#recipes-from-scripts) for every item with [`bworld:juiceable`](../items/food.md#juice), making the
|
||||
juice named after it, so most mods never write one. A file can add one for anything else, or make something other than
|
||||
juice:
|
||||
[shared phase](#recipes-from-scripts) for every item with [`bworld:juiceable`](../items/food.md#juice), making the juice
|
||||
named after it, so most mods never write one. A file can add one for anything else, or make something other than juice:
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -118,7 +117,7 @@ makes. The juicer uses the first recipe that takes the item.
|
||||
## Fermenting: `bworld:fermentable`
|
||||
|
||||
A fluid that ferments into another over time, like juice into wine. The base game registers one from its
|
||||
[shared script](#recipes-from-scripts) for every [juice](../items/food.md#juice), into wine from the same item. A file
|
||||
[shared phase](#recipes-from-scripts) for every [juice](../items/food.md#juice), into wine from the same item. A file
|
||||
can add one for anything else:
|
||||
|
||||
```json
|
||||
@@ -165,7 +164,7 @@ Use a tag rather than listing ids when other mods might have more of the same th
|
||||
|
||||
Any `type` with a namespace is a mod's own, like `bworld:smithing` for the smithing table. The engine only stores these
|
||||
recipes. What their fields mean is up to the mod, which registers the type from its
|
||||
[shared script](../concepts/sides.md#shared-scripts), like NeoForge's `RecipeType` with its serializer:
|
||||
[shared phase](../concepts/sides.md#shared), like NeoForge's `RecipeType` with its serializer:
|
||||
|
||||
```ts
|
||||
import type { ItemStack, SharedContext } from "cubi/shared";
|
||||
@@ -176,7 +175,7 @@ interface SmithingRecipe {
|
||||
result: string;
|
||||
}
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
export function shared(ctx: SharedContext) {
|
||||
ctx.recipes.register_type<SmithingRecipe, { tool: ItemStack; material: ItemStack }>("bworld:smithing", {
|
||||
// the json checked and turned into what matches and the machine use
|
||||
parse: (json, read) => ({
|
||||
@@ -205,7 +204,7 @@ are checked, throwing with the name it's given, like `"result"` or `"ingredients
|
||||
| `read.fluid_stack(value, name)` | `{ fluid: { id, data }, amount }` of a fluid that exists, in mB. |
|
||||
| `read.positive_int(value, name)` | A whole number, at least 1, like a time in ticks. |
|
||||
|
||||
Then server, client and shared scripts look recipes up by what's in the machine, like NeoForge's
|
||||
Then server, client and shared phases look recipes up by what's in the machine, like NeoForge's
|
||||
`RecipeManager.getRecipeFor`:
|
||||
|
||||
```ts
|
||||
@@ -214,25 +213,25 @@ ctx.recipes.all<SmithingRecipe>("bworld:smithing"); // every one, in load order
|
||||
```
|
||||
|
||||
`find` gives the first recipe, in load order, that `matches` says the input makes. Both give the recipes as `parse` made
|
||||
them, not copies, so they aren't to be changed. A type can only be registered during `setup`, in the mod's own
|
||||
namespace. Other mods can add recipes of your type too, as long as they depend on your mod.
|
||||
them, not copies, so they aren't to be changed. A type can only be registered while the shared phase runs, in the mod's
|
||||
own namespace. Other mods can add recipes of your type too, as long as they depend on your mod.
|
||||
|
||||
A recipe of a type no mod registers, like a misspelled one, stops the first mod with one from loading once every shared
|
||||
script has set up, since nothing would ever read it.
|
||||
|
||||
`ctx.recipes.list(type?)` gives recipes as their JSON instead, copies in load order. It's how the recipe viewer shows
|
||||
every mod's types, and how a shared script makes recipes from another type's, see below.
|
||||
every mod's types, and how the shared phase makes recipes from another type's, see below.
|
||||
|
||||
## Recipes From Scripts
|
||||
|
||||
A recipe that follows from other data, like one for every item with a component, can be made in the mod's
|
||||
[shared script](../concepts/sides.md#shared-scripts) instead of written out as files. `ctx.recipes.register(type, json)`
|
||||
adds one like a file in `recipes/` with that `type` would, with the rest of its fields from `json`:
|
||||
[shared phase](../concepts/sides.md#shared) instead of written out as files. `ctx.recipes.register(type, json)` adds one
|
||||
like a file in `recipes/` with that `type` would, with the rest of its fields from `json`:
|
||||
|
||||
```ts
|
||||
import type { SharedContext } from "cubi/shared";
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
export function shared(ctx: SharedContext) {
|
||||
// a sieve recipe for every item that says what it sifts into
|
||||
for (const id of ctx.items.ids()) {
|
||||
const sifts = ctx.items.components(id)["copper_tools:siftable"] as { into: string } | undefined;
|
||||
@@ -241,15 +240,15 @@ export function setup(ctx: SharedContext) {
|
||||
}
|
||||
```
|
||||
|
||||
Shared scripts run on the server and on every player's client, from the same data, so every side makes the same recipes
|
||||
and nothing is sent: the crafting grid, `ctx.recipes.list` in server and client scripts, and recipe viewers see them
|
||||
like any other. That's also why it's only in shared scripts. Recipes can only be registered during `setup`, before any
|
||||
server or client script sets up, and one is checked like a file would be: a broken one, or a second smelting recipe for
|
||||
the same input, throws, which stops the mod loading.
|
||||
The shared phase runs on the server and on every player's client, from the same data, so every side makes the same
|
||||
recipes and nothing is sent: the crafting grid, `ctx.recipes.list` in server and client phases, and recipe viewers see
|
||||
them like any other. That's also why it's only in the shared phase. Recipes can only be registered while it runs, before
|
||||
any server or client phase sets up, and one is checked like a file would be: a broken one, or a second smelting recipe
|
||||
for the same input, throws, which stops the mod loading.
|
||||
|
||||
`ctx.recipes.list(type?)` reads the recipes there are so far, copies in load order, like the client's: every mod's
|
||||
`recipes/` files, and what the shared scripts that set up before this one registered. Dependencies set up first, so a
|
||||
mod can make recipes of its own from another mod's, like a machine of its own for every one of the base game's juicing
|
||||
`recipes/` files, and what the shared phases that set up before this one registered. Dependencies set up first, so a mod
|
||||
can make recipes of its own from another mod's, like a machine of its own for every one of the base game's juicing
|
||||
recipes:
|
||||
|
||||
```ts
|
||||
|
||||
@@ -89,7 +89,7 @@ A tag nobody defines is a check-mods error there.
|
||||
|
||||
## Using Tags in Scripts
|
||||
|
||||
Server and client scripts both have `ctx.tags`. Tags can be written with or without their `#`:
|
||||
Server and client phases both have `ctx.tags`. Tags can be written with or without their `#`:
|
||||
|
||||
```ts
|
||||
ctx.tags.has("items", "bworld:planks", item.id); // whether it's in the tag
|
||||
@@ -102,7 +102,7 @@ ctx.tags.expand(["#bworld:planks", "bworld:stick"]); // every item a recipe's ch
|
||||
`has` and `get` treat a tag nobody defines as empty.
|
||||
|
||||
Mods' own [recipe types](recipes.md#custom-recipe-types) should take tags like the base game's do. `expand` turns a
|
||||
choice into the items it stands for, so the mod's server script can check and match them:
|
||||
choice into the items it stands for, so the mod's server phase can check and match them:
|
||||
|
||||
```ts
|
||||
const ingredient = ctx.tags.expand(json.ingredient); // "#bworld:planks" -> ["bworld:oak_planks", "bworld:spruce_planks", ...]
|
||||
|
||||
Reference in New Issue
Block a user