Compare commits

...
8 Commits
Author SHA1 Message Date
paula 49952676ab Moooooore items 2026-11-02 23:25:26 -03:00
paula 2276d27867 More food ! 2026-11-02 23:16:39 -03:00
paula 45f3b9831c Soooo many items 2026-11-02 23:05:31 -03:00
paula 61e08af16f Textures and more textures 2026-11-02 00:07:25 -03:00
paula 07bf8547d3 Add recipes for bigger chests 2026-11-01 22:54:39 -03:00
paula c1871a7bf7 Make tool upgrades cost money 2026-11-01 22:48:49 -03:00
paula 12702520dd Stricter rules 2026-11-01 21:10:13 -03:00
paulaandClaude Opus 5.5 a7e81b9944 Every recipe type registered from shared scripts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 21:00:44 -03:00
150 changed files with 1162 additions and 394 deletions
+6
View File
@@ -3,6 +3,12 @@
A voxel game in TypeScript on Deno. The base game is itself a mod (`mods/bworld`), written against the same modding API
as any other mod.
**Never under any circumstance commit anything on your own**
**Never under any circumstance generate any textures or audio files**
Always ask before making even the simplest models, unless it's coming from ports/
## Layout
- `server/`: the game server, world, saves and server-side game logic
+5 -1
View File
@@ -7,6 +7,7 @@ import { AIR } from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import type { ClientContext, ClientMenu, InventoryOverlay, KeyMappings, MenuScreen } from "$/common/mod_api/client.ts";
import {
check_recipe_types,
ModListing,
ModLoadError,
recipe_lookup,
@@ -17,7 +18,7 @@ import {
import { type Bmod, read_bmod } from "$/common/bmod.ts";
import { get_state_value, set_state_value, shared_items_api } from "$/common/utils.ts";
import { BlockRules } from "$/common/block_rules.ts";
import { FluidRules } from "$/common/fluids.ts";
import { fluid_exists, FluidRules, same_fluid } from "$/common/fluids.ts";
import { fluid_rules, set_fluid_rules } from "./fluid_rules.ts";
import { covers_face, EMPTY_SHAPE, FULL_SHAPE, is_solid } from "$/common/block_shapes.ts";
import type { SharedContext, SharedWorld } from "$/common/mod_api/shared.ts";
@@ -192,6 +193,7 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
});
}
rules_setup = false;
check_recipe_types(recipe_book);
client_setup = true;
for (const [i, bmod] of bmods.entries()) {
@@ -239,6 +241,8 @@ function shared_context(listing: ModListing): SharedContext {
blocks: blocks_api(mod),
items: shared_items_api(mod),
fluids: {
exists: fluid_exists,
same: same_fluid,
register(id, rules) {
if (!rules_setup) throw new Error(`[${mod}] rules can only be registered during setup`);
fluid_rules.register(mod, id, rules);
+5 -1
View File
@@ -48,7 +48,7 @@ export class FluidRules {
api(): FluidApi {
return {
ids: () => EverythingRegistry.entries("fluids").map(([id]) => id),
exists: (id) => EverythingRegistry.get("fluids", id) !== undefined,
exists: fluid_exists,
name: (fluid) => this.name(fluid),
color: (fluid) => this.color(fluid),
same: same_fluid,
@@ -68,6 +68,10 @@ export class FluidRules {
}
}
export function fluid_exists(id: string): boolean {
return EverythingRegistry.get("fluids", id) !== undefined;
}
// whether they're one fluid, see FluidApi.same
export function same_fluid(a: FluidVariant, b: FluidVariant): boolean {
return a.id === b.id && same_data(a.data, b.data);
+13 -7
View File
@@ -67,13 +67,17 @@ export interface RecipeType<R, I> {
matches(recipe: R, input: I): boolean;
}
// reads a field of a recipe's json for RecipeType.parse, checked like the engine's own recipes' fields. throws naming
// the field when it isn't one, or its item isn't registered
// reads a value from a recipe's json for RecipeType.parse, checked like the engine's own recipes' fields. name is where
// it is, like "result" or "ingredients[0]", for the error thrown when it isn't one, or what it names isn't registered
export interface RecipeReader {
item(json: Record<string, unknown>, field: string): Id;
stack(json: Record<string, unknown>, field: string): { id: Id; count: number };
item(value: unknown, name: string): Id;
stack(value: unknown, name: string): { id: Id; count: number };
// an item, a #tag or a list of them, any of which fits, as every item that does, like the engine's shaped keys
ingredient(json: Record<string, unknown>, field: string): Id[];
ingredient(value: unknown, name: string): Id[];
// { fluid, amount } in mB
fluid_stack(value: unknown, name: string): FluidStack;
// a whole number, at least 1, like a time in ticks
positive_int(value: unknown, name: string): number;
}
// the recipes of registered types, as their parse made them, in load order. not to be changed
@@ -156,7 +160,8 @@ export interface SharedContext {
// what one sells for, from its "base_sell_value", or undefined if it can't be sold
sell_value(id: Id): number | undefined;
};
readonly fluids: {
// every fluid any mod defines, like the server's and clients' ctx.fluids, without what rules decide
readonly fluids: Pick<FluidApi, "exists" | "same"> & {
// the rules of one of the mod's fluids, which the server and every client name and color it by
register(id: Id, rules: FluidRules): void;
};
@@ -172,7 +177,8 @@ export interface SharedContext {
// deno-lint-ignore no-explicit-any
list(type?: string): any[];
// a recipe type of the mod's own, in its namespace, only during setup: its recipes, from files and register,
// are parsed by it then, and find and all read them from there on every side. see RecipeType
// are parsed by it then, and find and all read them from there on every side. a recipe of a type no mod
// registers stops its mod loading once every shared script has set up. see RecipeType
register_type<R, I>(type: Id, rules: RecipeType<R, I>): void;
} & RecipeLookup;
readonly components: {
+1 -1
View File
@@ -605,7 +605,7 @@ const engine_recipes = new Map<string, z.ZodType>([
]);
// a mod's own types, like "bworld:cooking" for its cooking pot, are only stored by the engine. the mod parses them,
// with ctx.recipes.register_type, or reads them with ctx.recipes.list and checks them
// with ctx.recipes.register_type
const recipe_schema = by_kind(
"type",
(type) => engine_recipes.get(type) ?? (ID_PATTERN.test(type) ? json_object : undefined),
+45 -19
View File
@@ -29,7 +29,8 @@ import {
TagRegistry,
validate_recipe,
} from "./mod_data.ts";
import type { RecipeLookup, RecipeReader, RecipeType, SharedContext } from "./mod_api/shared.ts";
import type { FluidStack, RecipeLookup, RecipeReader, RecipeType, SharedContext } from "./mod_api/shared.ts";
import { fluid_exists } from "./fluids.ts";
import { resolve_tags, Tags } from "./tags.ts";
import type { LootTableJson } from "./loot.ts";
import { type BiomeJson, register_biome } from "./worldgen/biomes.ts";
@@ -88,8 +89,9 @@ export interface ModListing {
export class RecipeBook {
shaped: GridRecipe[] = [];
// recipes of mods' own types, by type
// recipes of mods' own types, by type, and the mod with the first one of each, for the error when nothing registers it
by_type = new Map<string, ModRecipeJson[]>();
first_used_by = new Map<string, string>();
// the types shared scripts registered, and their recipes as parse made them, see SharedContext.recipes.register_type
types = new Map<string, RecipeType<unknown, unknown>>();
parsed = new Map<string, unknown[]>();
@@ -191,7 +193,7 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
for (const { id, data } of mods) {
try {
for (const recipe of data.recipes) add_recipe(recipes, recipe);
for (const recipe of data.recipes) add_recipe(recipes, recipe, id);
} catch (e) {
throw new ModLoadError(id, (e as Error).message);
}
@@ -202,12 +204,13 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
return recipes;
}
// puts a recipe in the book, from a mod's recipes/ or a shared script. throws when its registered type's parse throws
export function add_recipe(recipes: RecipeBook, recipe: RecipeJson) {
// puts mod's recipe in the book, from its recipes/ or its shared script. throws when its registered type's parse throws
export function add_recipe(recipes: RecipeBook, recipe: RecipeJson, mod: string) {
recipes.all.push(recipe);
if (is_mod_recipe(recipe)) {
const list = recipes.by_type.get(recipe.type) ?? [];
recipes.by_type.set(recipe.type, list);
if (!recipes.first_used_by.has(recipe.type)) recipes.first_used_by.set(recipe.type, mod);
list.push(recipe);
const type = recipes.types.get(recipe.type);
if (type) recipes.parsed.get(recipe.type)!.push(parse_recipe(type, recipe, list.length - 1));
@@ -225,33 +228,56 @@ function parse_recipe(type: RecipeType<unknown, unknown>, json: ModRecipeJson, i
}
}
// items and tags load before any script sets up, so the ones recipes name can be checked
// items, fluids and tags load before any script sets up, so the ones recipes name can be checked
const recipe_reader: RecipeReader = {
item: (json, field) => checked_item(field, item_problems(json[field]), json[field] as string),
stack(json, field) {
const stack = json[field] as { id: string; count: number } | undefined;
return { id: checked_item(field, stack_problems(stack), stack?.id as string), count: stack!.count };
item: (value, name) => checked_item(name, item_problems(value), value as string),
stack(value, name) {
const stack = value as { id: string; count: number } | undefined;
return { id: checked_item(name, stack_problems(stack), stack?.id as string), count: stack!.count };
},
// a tag can be empty, like one of another mod's items that isn't installed, but it has to exist
ingredient(json, field) {
const choice = json[field] as string | string[];
ingredient(value, name) {
const choice = value as string | string[];
const problems = choice_problems(choice);
if (problems.length > 0) throw new Error(`${field} ${problems[0]}`);
if (problems.length > 0) throw new Error(`${name} ${problems[0]}`);
for (const entry of choice_list(choice)) {
if (!entry.startsWith("#")) checked_item(field, [], entry);
else if (!Tags.exists("items", entry)) throw new Error(`${field}: there's no item tag ${entry}`);
if (!entry.startsWith("#")) checked_item(name, [], entry);
else if (!Tags.exists("items", entry)) throw new Error(`${name}: there's no item tag ${entry}`);
}
return choice_ids(choice);
},
fluid_stack(value, name) {
const stack = value as Partial<FluidStack> | undefined;
const id = stack?.fluid?.id;
if (typeof id !== "string") throw new Error(`${name} must be { fluid: { id, data }, amount }`);
if (!fluid_exists(id)) throw new Error(`${name}: there's no fluid ${id}`);
return { fluid: stack!.fluid!, amount: recipe_reader.positive_int(stack!.amount, `${name} amount`) };
},
positive_int(value, name) {
if (!Number.isInteger(value) || (value as number) < 1) {
throw new Error(`${name} must be a whole number, at least 1`);
}
return value as number;
},
};
// the item a field names, once the field has no problems
function checked_item(field: string, problems: string[], id: string): string {
if (problems.length > 0) throw new Error(`${field} ${problems[0]}`);
if (!is_registered("items", id)) throw new Error(`${field}: there's no item ${id}`);
function checked_item(name: string, problems: string[], id: string): string {
if (problems.length > 0) throw new Error(`${name} ${problems[0]}`);
if (!is_registered("items", id)) throw new Error(`${name}: there's no item ${id}`);
return id;
}
// once every shared script has set up: a recipe of a type no mod registered, which nothing would read, is an error
// for the first mod with one, like a misspelled type
export function check_recipe_types(recipes: RecipeBook) {
for (const [type, mod] of recipes.first_used_by) {
if (!recipes.types.has(type)) {
throw new ModLoadError(mod, `it has ${type} recipes, but no mod registers that type`);
}
}
}
// find and all, for every side's ctx.recipes
export function recipe_lookup(recipes: () => RecipeBook): RecipeLookup {
return {
@@ -280,7 +306,7 @@ export function shared_recipes_api(
const problems = validate_recipe(recipe);
if (problems.length > 0) fail(`recipe of type ${type}: ${problems.join(", ")}`);
try {
add_recipe(recipes(), recipe as RecipeJson);
add_recipe(recipes(), recipe as RecipeJson, mod);
} catch (e) {
fail((e as Error).message);
}
+3
View File
@@ -109,6 +109,9 @@ ctx.fluids.same(birch, { id: "copper_tools:syrup" }); // false, the data is diff
there as plain `copper_tools:syrup`. Client scripts also have `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
[recipe type](recipes.md#custom-recipe-types) that's found by a fluid.
## Containers
An item with `fluid_container` holds one fluid, full or empty, like a bucket or a bottle:
+23 -43
View File
@@ -68,14 +68,13 @@ For the smithing table, which upgrades a tool. It's the base game's own type, re
}
```
An optional `addition` is a third item it also takes. Upgrading uses up the tool, `count` of the material and the
addition, and the result keeps the tool's `data`, like its [durability](../items/tools.md#durability). Two smithing
recipes with the same tool, material and addition are an error.
An optional `addition` is a third item it also takes, as `{ "id", "count" }` like the material. Upgrading uses up the
tool and `count` of the material and the addition, and the result keeps the tool's `data`, like its
[durability](../items/tools.md#durability). Two smithing recipes with the same tool, material and addition are an error.
## Stonecutting: `bworld:stonecutting`
For the stonecutter, which cuts one item into another. It's the base game's own type, so `mods/bworld` checks these
recipes when the server starts rather than the engine:
For the stonecutter, which cuts one item into another. It's the base game's own type, registered by its shared script:
```json
{
@@ -96,7 +95,7 @@ uses up one of the input.
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. It's checked when the server starts, like stonecutting:
juice:
```json
{
@@ -118,7 +117,7 @@ makes. The juicer uses the first recipe that takes the item.
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
can add one for anything else. It's checked when the server starts, like juicing:
can add one for anything else:
```json
{
@@ -179,9 +178,9 @@ export function setup(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) => ({
tool: read.item(json, "tool"),
material: read.stack(json, "material"),
result: read.item(json, "result"),
tool: read.item(json.tool, "tool"),
material: read.stack(json.material, "material"),
result: read.item(json.result, "result"),
}),
// whether what's in the machine makes the recipe
matches: (recipe, input) =>
@@ -193,10 +192,16 @@ export function setup(ctx: SharedContext) {
`parse` runs once for every recipe of the type, from files and from [scripts](#recipes-from-scripts), in load order, on
the server and every client. Throwing stops the mod loading with an error naming the recipe, so a broken one is caught
when the server starts rather than when someone uses the machine. `read.item(json, field)` and `read.stack(json, field)`
check a field the way the engine's own types are checked, an item id or `{ id, count }` of an item that exists, and
throw naming the field. `read.ingredient(json, field)` reads a [choice](#choices) of items, an item, a `#tag` or a list
of them, as every item that fits. Its tags have to exist, but can be empty.
when the server starts rather than when someone uses the machine. `read` checks a value the way the engine's own types
are checked, throwing with the name it's given, like `"result"` or `"ingredients[0]"`:
| Reader | Reads |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `read.item(value, name)` | An item id, of an item that exists. |
| `read.stack(value, name)` | `{ id, count }` of an item that exists. |
| `read.ingredient(value, name)` | A [choice](#choices): an item, a `#tag` or a list of them, as every item that fits. Its tags have to exist, but can be empty. |
| `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
`RecipeManager.getRecipeFor`:
@@ -210,36 +215,11 @@ ctx.recipes.all<SmithingRecipe>("bworld:smithing"); // every one, in load order
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.
Types that aren't registered, like `bworld:cooking` for now, are read as JSON instead:
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.
```json
{
"format_version": 1,
"recipe": {
"type": "bworld:cooking",
"ingredients": ["bworld:cabbage", ["bworld:carrot", "bworld:potato", "bworld:onion"]],
"result": { "id": "bworld:cabbage_rolls", "count": 1 },
"cook_time": 100
}
}
```
The mod's server script reads them in `setup` and checks their fields itself:
```ts
const recipes = ctx.recipes.list("bworld:cooking").map((json, i) => {
if (!Array.isArray(json.ingredients) || json.ingredients.length > 6) {
throw new Error(`cooking recipe ${i + 1}: ingredients must be 1 to 6 items`);
}
// ...check the rest, and turn it into whatever the machine needs. expand turns #tags into their items
return { ingredients: json.ingredients.map((choice) => ctx.tags.expand(choice)) };
});
```
Throwing in `setup` stops the server from starting with the error, like a broken recipe of a registered type does.
`list` returns copies, in load order. Client scripts can read every recipe with `ctx.recipes.list(type?)`, which is how
the recipe viewer shows mods' own types.
`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.
## Recipes From Scripts
+1 -1
View File
@@ -121,7 +121,7 @@ interface RecipeRegistry {
they can't accidentally register different things.
- `register_type` would let `check-mods` and the loader validate mods' own recipe types, instead of each mod checking
its recipes in `setup`. `shaped` becomes short for `bworld:shaped`. Shared scripts can now register types with
`ctx.recipes.register_type`, and `bworld:smithing`, `bworld:smelting` and `bworld:fuel` are.
`ctx.recipes.register_type`, and every one of the base game's and create's are.
### The Freeze and the Registry Hash
+1 -1
View File
@@ -3,7 +3,7 @@
"entity": {
"id": "bworld:chicken",
"model": "bworld:chicken",
"texture": "bworld:chicken",
"texture": "bworld:chicken_entity",
"width": 0.3,
"height": 0.4,
"max_health": 4,
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:bacon",
"texture": "bworld:bacon",
"food": {
"nutrition": 2,
"saturation_modifier": 0.3
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:bacon_sandwich",
"texture": "bworld:bacon_sandwich",
"food": {
"nutrition": 10,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:baked_potato",
"texture": "bworld:baked_potato",
"food": {
"nutrition": 4,
"saturation_modifier": 0.2
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:barbecue_stick",
"texture": "bworld:barbecue_stick",
"food": {
"nutrition": 8,
"saturation_modifier": 0.9
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:beef",
"texture": "bworld:beef",
"food": {
"nutrition": 2,
"saturation_modifier": 0.2
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:beef_patty",
"texture": "bworld:beef_patty",
"food": {
"nutrition": 4,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:beef_stew",
"texture": "bworld:beef_stew",
"food": {
"nutrition": 12,
"saturation_modifier": 0.8,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"format_version": 1,
"item": {
"id": "bworld:bone",
"texture": "bworld:bone",
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:bone_broth",
"texture": "bworld:kelp_roll",
"food": {
"nutrition": 8,
"saturation_modifier": 0.7,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:bread",
"texture": "bworld:bread",
"food": {
"nutrition": 3,
"saturation_modifier": 0.6
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:bread_slice",
"texture": "bworld:bread_slice",
"food": {
"nutrition": 1,
"saturation_modifier": 0.1
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:chicken",
"texture": "bworld:chicken",
"food": {
"nutrition": 1,
"saturation_modifier": 0.2
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:chicken_cuts",
"texture": "bworld:chicken_cuts",
"food": {
"nutrition": 1,
"saturation_modifier": 0.3
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:chicken_sandwich",
"texture": "bworld:chicken_sandwich",
"food": {
"nutrition": 10,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:chicken_soup",
"texture": "bworld:chicken_soup",
"food": {
"nutrition": 12,
"saturation_modifier": 0.8,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_bacon",
"texture": "bworld:cooked_bacon",
"food": {
"nutrition": 4,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_beef",
"texture": "bworld:cooked_beef",
"food": {
"nutrition": 4,
"saturation_modifier": 0.5
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_chicken",
"texture": "bworld:cooked_chicken",
"food": {
"nutrition": 3,
"saturation_modifier": 0.5
},
"base_sell_value": 1
}
}
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_chicken_cuts",
"texture": "bworld:cooked_chicken_cuts",
"food": {
"nutrition": 3,
"saturation_modifier": 0.6
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_mutton",
"texture": "bworld:cooked_mutton",
"food": {
"nutrition": 4,
"saturation_modifier": 0.5
},
"base_sell_value": 1
}
}
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_mutton_chops",
"texture": "bworld:cooked_mutton_chops",
"food": {
"nutrition": 3,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_porkchop",
"texture": "bworld:cooked_porkchop",
"food": {
"nutrition": 4,
"saturation_modifier": 0.5
},
"base_sell_value": 1
}
}
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_salmon_slice",
"texture": "bworld:cooked_salmon_slice",
"food": {
"nutrition": 3,
"saturation_modifier": 0.5
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:dumplings",
"texture": "bworld:dumplings",
"food": {
"nutrition": 8,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"format_version": 1,
"item": {
"id": "bworld:egg",
"texture": "bworld:egg",
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:egg_sandwich",
"texture": "bworld:egg_sandwich",
"food": {
"nutrition": 8,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:fish_roll",
"texture": "bworld:fish_roll",
"food": {
"nutrition": 7,
"saturation_modifier": 0.6
},
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:fish_stew",
"texture": "bworld:fish_stew",
"food": {
"nutrition": 12,
"saturation_modifier": 0.8,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:fried_egg",
"texture": "bworld:fried_egg",
"food": {
"nutrition": 4,
"saturation_modifier": 0.4
},
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:fried_rice",
"texture": "bworld:fried_rice",
"food": {
"nutrition": 12,
"saturation_modifier": 0.8,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:ham",
"texture": "bworld:ham",
"food": {
"nutrition": 5,
"saturation_modifier": 0.3
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:hamburger",
"texture": "bworld:hamburger",
"food": {
"nutrition": 11,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:kelp_roll",
"texture": "bworld:kelp_roll",
"food": {
"nutrition": 12,
"saturation_modifier": 2.4
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:kelp_roll_slice",
"texture": "bworld:kelp_roll_slice",
"food": {
"nutrition": 6,
"saturation_modifier": 0.5
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:minced_beef",
"texture": "bworld:minced_beef",
"food": {
"nutrition": 2,
"saturation_modifier": 0.3
},
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:mixed_salad",
"texture": "bworld:mixed_salad",
"food": {
"nutrition": 6,
"saturation_modifier": 0.6,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:mutton",
"texture": "bworld:mutton",
"food": {
"nutrition": 2,
"saturation_modifier": 0.2
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:mutton_chops",
"texture": "bworld:mutton_chops",
"food": {
"nutrition": 1,
"saturation_modifier": 0.3
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:mutton_wrap",
"texture": "bworld:mutton_wrap",
"food": {
"nutrition": 10,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:onion_soup",
"texture": "bworld:onion_soup",
"food": {
"nutrition": 12,
"saturation_modifier": 0.8,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:pie_crust",
"texture": "bworld:pie_crust",
"food": {
"nutrition": 2,
"saturation_modifier": 0.2
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:porkchop",
"texture": "bworld:porkchop",
"food": {
"nutrition": 2,
"saturation_modifier": 0.2
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:pumpkin_slice",
"texture": "bworld:pumpkin_slice",
"food": {
"nutrition": 3,
"saturation_modifier": 0.3
},
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:pumpkin_soup",
"texture": "bworld:pumpkin_soup",
"food": {
"nutrition": 14,
"saturation_modifier": 0.75,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:raw_pasta",
"texture": "bworld:raw_pasta",
"food": {
"nutrition": 2,
"saturation_modifier": 0.3
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:raw_pie_crust",
"texture": "bworld:raw_pie_crust",
"food": {
"nutrition": 1,
"saturation_modifier": 0
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:smoked_ham",
"texture": "bworld:smoked_ham",
"food": {
"nutrition": 10,
"saturation_modifier": 0.8
},
"base_sell_value": 1
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:vegetable_soup",
"texture": "bworld:vegetable_soup",
"food": {
"nutrition": 12,
"saturation_modifier": 0.8,
"returns": "bworld:bowl"
},
"base_sell_value": 1
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"item": {
"id": "bworld:wheat_dough",
"texture": "bworld:wheat_dough",
"food": {
"nutrition": 2,
"saturation_modifier": 0.3
},
"base_sell_value": 1
}
}
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot",
"count": 5
},
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_axe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot",
"count": 5
},
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_hoe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot",
"count": 5
},
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_knife"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot",
"count": 5
},
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_pickaxe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot",
"count": 5
},
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_shovel"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:cobalt_ingot",
"count": 5
},
"addition": {
"id": "bworld:cobalt_coin",
"count": 2
},
"result": "bworld:cobalt_watering_can"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_axe"
}
}
@@ -0,0 +1,16 @@
{
"format_version": 1,
"recipe": {
"type": "bworld:smithing",
"tool": "bworld:chest",
"material": {
"id": "bworld:copper_ingot",
"count": 10
},
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_chest"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_hoe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_knife"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_pickaxe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_shovel"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:copper_ingot",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 5
},
"result": "bworld:copper_watering_can"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot",
"count": 5
},
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_axe"
}
}
@@ -0,0 +1,16 @@
{
"format_version": 1,
"recipe": {
"type": "bworld:smithing",
"tool": "bworld:iron_chest",
"material": {
"id": "bworld:gold_ingot",
"count": 10
},
"addition": {
"id": "bworld:gold_coin",
"count": 5
},
"result": "bworld:gold_chest"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot",
"count": 5
},
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_hoe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot",
"count": 5
},
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_knife"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot",
"count": 5
},
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_pickaxe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot",
"count": 5
},
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_shovel"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:gold_ingot",
"count": 5
},
"addition": {
"id": "bworld:gold_coin",
"count": 1
},
"result": "bworld:gold_watering_can"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot",
"count": 5
},
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_axe"
}
}
@@ -0,0 +1,16 @@
{
"format_version": 1,
"recipe": {
"type": "bworld:smithing",
"tool": "bworld:copper_chest",
"material": {
"id": "bworld:iron_ingot",
"count": 10
},
"addition": {
"id": "bworld:iron_coin",
"count": 5
},
"result": "bworld:iron_chest"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot",
"count": 5
},
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_hoe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot",
"count": 5
},
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_knife"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot",
"count": 5
},
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_pickaxe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot",
"count": 5
},
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_shovel"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:iron_ingot",
"count": 5
},
"addition": {
"id": "bworld:iron_coin",
"count": 2
},
"result": "bworld:iron_watering_can"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:stone",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_axe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:stone",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_hoe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:stone",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_knife"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:stone",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_pickaxe"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:stone",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_shovel"
}
}
@@ -7,6 +7,10 @@
"id": "bworld:stone",
"count": 5
},
"addition": {
"id": "bworld:brass_coin",
"count": 1
},
"result": "bworld:stone_watering_can"
}
}
+2 -2
View File
@@ -19,7 +19,7 @@ import type {
} from "bworld/client";
import { MINIGAMES } from "./minigames/mod.ts";
import { JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
import { STONECUTTING, stonecutting_options, type StonecuttingRecipe } from "./stonecutting.ts";
const WIDTH = 176;
// like minecraft's 176x166 container screens
@@ -185,7 +185,7 @@ const SCROLLER_OFF_COLOR: Color = [0.78, 0.78, 0.78, 1];
const SELECTED_COLOR: Color = [1, 1, 1, 0.45];
function stonecutter_screen(ctx: ClientContext, menu: ClientMenu): MenuScreen {
const recipes = stonecutting_recipes(ctx);
const recipes = ctx.recipes.all<StonecuttingRecipe>(STONECUTTING);
// the first row shown, and whether the scroller is being dragged
let row = 0;
let dragging = false;
+5 -119
View File
@@ -7,6 +7,7 @@
// CuttingBoardBlockEntity, holds a stack of anything and cuts it one at a time with the right tool
import type { BlockRef, Container, ItemStack, ServerContext } from "bworld/server";
import { COOKING, type CookingRecipe, CUTTING, type CuttingRecipe, INGREDIENT_SLOTS } from "./cooking_recipes.ts";
// stove
@@ -56,30 +57,16 @@ export function register_stove(ctx: ServerContext) {
// cooking pot
// slots 0-5 are the ingredients, 6 the meal waiting to be served, 7 containers for it, 8 the served meal
const INGREDIENT_SLOTS = 6;
const MEAL_SLOT = 6;
const CONTAINER_SLOT = 7;
const OUTPUT_SLOT = 8;
const POT_SIZE = 9;
// ticks, for recipes that don't say
const DEFAULT_COOK_TIME = 200;
// what's under the pot that heats it, and the state that has to be 1 for it to, like minecraft's heat_sources tag
const HEAT_SOURCES: Record<string, string | undefined> = {
"bworld:furnace": "lit",
"bworld:stove": "lit",
};
// a bworld:cooking recipe: the ingredients in any slots. in the json each is an item id or #tag, or a list of
// those any of which will do. here each is every item that will do
interface CookingRecipe {
ingredients: string[][];
result: { id: string; count: number };
// what the meal is served in, like a bowl. without it the meal comes out as soon as it's cooked
container?: string;
cook_time: number;
}
interface PotData {
container: string;
// ticks, counting up to cook_time_total
@@ -89,81 +76,18 @@ interface PotData {
meal_container: string;
}
// an ingredient: an item, or a #tag of items any of which will do
function item_or_tag(ctx: ServerContext, where: string, id: unknown) {
if (typeof id === "string" && id.startsWith("#")) {
if (!ctx.tags.exists("items", id)) throw new Error(`${where}: there's no item tag ${id}`);
} else if (typeof id !== "string" || !ctx.items.exists(id)) {
throw new Error(`${where}: there's no item ${id}`);
}
}
// checked when the server starts, so a broken recipe is a load error naming it
function load_recipes(ctx: ServerContext): CookingRecipe[] {
return ctx.recipes.list("bworld:cooking").map((json, i) => {
const where = `bworld:cooking recipe ${i + 1} (${json.result?.id ?? "no result"})`;
const item = (id: unknown) => {
if (typeof id !== "string" || !ctx.items.exists(id)) throw new Error(`${where}: there's no item ${id}`);
return id;
};
const ingredients = json.ingredients;
if (!Array.isArray(ingredients) || ingredients.length < 1 || ingredients.length > INGREDIENT_SLOTS) {
throw new Error(`${where}: ingredients must be 1 to ${INGREDIENT_SLOTS} items`);
}
for (const ingredient of ingredients) {
if (Array.isArray(ingredient)) {
if (ingredient.length === 0) throw new Error(`${where}: an ingredient's list of items is empty`);
ingredient.forEach((id) => item_or_tag(ctx, where, id));
} else {
item_or_tag(ctx, where, ingredient);
}
}
item(json.result?.id);
if (!Number.isInteger(json.result.count) || json.result.count < 1) {
throw new Error(`${where}: result.count must be a positive whole number`);
}
if (json.container !== undefined) item(json.container);
const cook_time = json.cook_time ?? DEFAULT_COOK_TIME;
if (!Number.isInteger(cook_time) || cook_time < 1) {
throw new Error(`${where}: cook_time must be positive ticks`);
}
return {
ingredients: ingredients.map((ingredient: string | string[]) => ctx.tags.expand(ingredient)),
result: json.result,
container: json.container,
cook_time,
};
});
}
// the recipe whose ingredients are exactly what's in the ingredient slots, one item from each, in any order
function find_recipe(recipes: CookingRecipe[], container: Container): CookingRecipe | undefined {
function find_recipe(ctx: ServerContext, container: Container): CookingRecipe | undefined {
const items: string[] = [];
for (let slot = 0; slot < INGREDIENT_SLOTS; slot++) {
const item = container.get(slot);
if (item) items.push(item.id);
}
if (items.length === 0) return undefined;
return recipes.find((recipe) => recipe.ingredients.length === items.length && matches(recipe.ingredients, items));
}
// pairs every ingredient with a different item, trying each way round, like a shapeless recipe
function matches(ingredients: string[][], items: string[], used = new Set<number>()): boolean {
if (ingredients.length === 0) return true;
const [first, ...rest] = ingredients;
const fits = (id: string) => first.includes(id);
for (let i = 0; i < items.length; i++) {
if (used.has(i) || !fits(items[i])) continue;
used.add(i);
if (matches(rest, items, used)) return true;
used.delete(i);
}
return false;
return ctx.recipes.find<CookingRecipe>(COOKING, items);
}
export function register_cooking_pot(ctx: ServerContext) {
const recipes = load_recipes(ctx);
const pot_data = (block: BlockRef): PotData => {
const existing = block.data?.container && ctx.containers.get(block.data.container);
const container = existing || ctx.containers.create(POT_SIZE);
@@ -277,7 +201,7 @@ export function register_cooking_pot(ctx: ServerContext) {
const data = pot_data(block);
const container = ctx.containers.get(data.container)!;
const heated = is_heated(block);
const recipe = heated ? find_recipe(recipes, container) : undefined;
const recipe = heated ? find_recipe(ctx, container) : undefined;
if (recipe && can_cook(container, recipe)) {
data.cook_time_total = recipe.cook_time;
@@ -297,47 +221,9 @@ export function register_cooking_pot(ctx: ServerContext) {
// cutting board
// a bworld:cutting recipe, like the java mod's: one ingredient (an item, or a list any of which will do), the
// tool type that cuts it, and everything one of it is cut into
interface CuttingRecipe {
ingredient: string[];
tool: string;
result: { id: string; count: number }[];
}
// checked when the server starts, like the cooking pot's
function load_cutting_recipes(ctx: ServerContext): CuttingRecipe[] {
return ctx.recipes.list("bworld:cutting").map((json, i) => {
const where = `bworld:cutting recipe ${i + 1} (${json.ingredients?.[0] ?? "no ingredient"})`;
const item = (id: unknown) => {
if (typeof id !== "string" || !ctx.items.exists(id)) throw new Error(`${where}: there's no item ${id}`);
return id;
};
if (!Array.isArray(json.ingredients) || json.ingredients.length !== 1) {
throw new Error(`${where}: ingredients must be one item, or one list of items any of which will do`);
}
const choice = [json.ingredients[0]].flat();
if (choice.length === 0) throw new Error(`${where}: the ingredient's list of items is empty`);
choice.forEach((id) => item_or_tag(ctx, where, id));
const ingredient = ctx.tags.expand(choice);
if (typeof json.tool !== "string") throw new Error(`${where}: tool must be a tool type, like "knife"`);
if (!Array.isArray(json.result) || json.result.length === 0) {
throw new Error(`${where}: result must be a list of { id, count }`);
}
for (const result of json.result) {
item(result?.id);
if (!Number.isInteger(result.count) || result.count < 1) {
throw new Error(`${where}: every result's count must be a positive whole number`);
}
}
return { ingredient, tool: json.tool, result: json.result };
});
}
// like the java mod's CuttingBoardBlock and CuttingBoardBlockEntity. what's on the board is the item it shows
// (set_block_item), so the client draws it, like its CuttingBoardRenderer
export function register_cutting_board(ctx: ServerContext) {
const recipes = load_cutting_recipes(ctx);
const on_board = (block: BlockRef) => ctx.world.get_block_item(block.x, block.y, block.z);
const set_on_board = (block: BlockRef, item: ItemStack | undefined) =>
ctx.world.set_block_item(block.x, block.y, block.z, item);
@@ -369,7 +255,7 @@ export function register_cutting_board(ctx: ServerContext) {
}
// anything else is a tool for cutting one of what's on it, like processStoredItemUsingTool
const recipe = item && recipes.find((recipe) => recipe.ingredient.includes(item.id));
const recipe = item && ctx.recipes.find<CuttingRecipe>(CUTTING, item.id);
if (item && recipe && ctx.items.tool(held.id) === recipe.tool) {
for (const result of recipe.result) ctx.world.drop_item(block.x, block.y, block.z, { ...result });
set_on_board(block, item.count > 1 ? { ...item, count: item.count - 1 } : undefined);
+85
View File
@@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the kitchen's recipe types, registered by the shared script: what the cooking pot cooks, found by the items in its
// ingredient slots, and what the cutting board cuts, found by the item on it. see cooking.ts
import type { SharedContext } from "bworld/shared";
export const COOKING = "bworld:cooking";
export const CUTTING = "bworld:cutting";
// how many ingredients the pot holds
export const INGREDIENT_SLOTS = 6;
// ticks, for recipes that don't say
const DEFAULT_COOK_TIME = 200;
// a bworld:cooking recipe: the ingredients in any slots. in the json each is an item id or #tag, or a list of
// those any of which will do. here each is every item that will do
export interface CookingRecipe {
ingredients: string[][];
result: { id: string; count: number };
// what the meal is served in, like a bowl. without it the meal comes out as soon as it's cooked
container?: string;
cook_time: number;
}
// a bworld:cutting recipe, like the java mod's: one ingredient (an item, or a list any of which will do), the
// tool type that cuts it, and everything one of it is cut into
export interface CuttingRecipe {
ingredient: string[];
tool: string;
result: { id: string; count: number }[];
}
export function register_cooking_recipes(ctx: SharedContext) {
// found by the ids of what's in the ingredient slots
ctx.recipes.register_type<CookingRecipe, string[]>(COOKING, {
parse(json, read) {
const ingredients = json.ingredients;
if (!Array.isArray(ingredients) || ingredients.length < 1 || ingredients.length > INGREDIENT_SLOTS) {
throw new Error(`ingredients must be 1 to ${INGREDIENT_SLOTS} items`);
}
const recipe: CookingRecipe = {
ingredients: ingredients.map((choice, i) => read.ingredient(choice, `ingredients[${i}]`)),
result: read.stack(json.result, "result"),
cook_time: json.cook_time === undefined
? DEFAULT_COOK_TIME
: read.positive_int(json.cook_time, "cook_time"),
};
if (json.container !== undefined) recipe.container = read.item(json.container, "container");
return recipe;
},
matches: (recipe, items) => recipe.ingredients.length === items.length && pairs_up(recipe.ingredients, items),
});
ctx.recipes.register_type<CuttingRecipe, string>(CUTTING, {
parse(json, read) {
if (!Array.isArray(json.ingredients) || json.ingredients.length !== 1) {
throw new Error("ingredients must be one item, or one list of items any of which will do");
}
if (typeof json.tool !== "string") throw new Error(`tool must be a tool type, like "knife"`);
if (!Array.isArray(json.result) || json.result.length === 0) {
throw new Error("result must be a list of { id, count }");
}
return {
ingredient: read.ingredient(json.ingredients[0], "ingredients[0]"),
tool: json.tool,
result: json.result.map((result: unknown, i: number) => read.stack(result, `result[${i}]`)),
};
},
matches: (recipe, id) => recipe.ingredient.includes(id),
});
}
// pairs every ingredient with a different item, trying each way round, like a shapeless recipe
function pairs_up(ingredients: string[][], items: string[], used = new Set<number>()): boolean {
if (ingredients.length === 0) return true;
const [first, ...rest] = ingredients;
for (let i = 0; i < items.length; i++) {
if (used.has(i) || !first.includes(items[i])) continue;
used.add(i);
if (pairs_up(rest, items, used)) return true;
used.delete(i);
}
return false;
}
+10 -21
View File
@@ -3,9 +3,8 @@
// fermentable recipes: a fluid that ferments into another over a number of ticks, like juice into wine in the keg. the
// shared script registers one for every juice, and mods can add more like any recipe, as files or from their shared
// scripts
import type { FluidStack, SharedContext } from "bworld/shared";
import { fluid_stack, type RecipeReader } from "./juicing.ts";
// scripts. they're found by the fluid that ferments
import type { FluidStack, FluidVariant, SharedContext } from "bworld/shared";
import type { JuiceableParams } from "./shared.ts";
export const FERMENTABLE = "bworld:fermentable";
@@ -22,6 +21,14 @@ export interface FermentableRecipe {
// a recipe for every juice, the one each juiceable item makes, fermenting it into wine from the same fruit
export function register_fermenting(ctx: SharedContext) {
ctx.recipes.register_type<FermentableRecipe, FluidVariant>(FERMENTABLE, {
parse: (json, read) => ({
input: read.fluid_stack(json.input, "input"),
output: read.fluid_stack(json.output, "output"),
ticks: read.positive_int(json.ticks, "ticks"),
}),
matches: (recipe, fluid) => ctx.fluids.same(recipe.input.fluid, fluid),
});
for (const id of ctx.items.ids()) {
if (!(ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined)) continue;
ctx.recipes.register(FERMENTABLE, {
@@ -31,21 +38,3 @@ export function register_fermenting(ctx: SharedContext) {
});
}
}
// every fermentable recipe, in load order. a broken one throws, which stops the server from starting
export function fermentable_recipes(ctx: RecipeReader): FermentableRecipe[] {
return ctx.recipes.list(FERMENTABLE).map((json, i) => {
const where = `${FERMENTABLE} recipe ${i + 1}`;
const input = fluid_stack(ctx, json.input);
const output = fluid_stack(ctx, json.output);
if (!input || !output) {
throw new Error(
`${where}: input and output must each be a { fluid, amount } of a fluid that exists, in mB`,
);
}
if (!Number.isInteger(json.ticks) || json.ticks < 1) {
throw new Error(`${where}: ticks must be a whole number of ticks, at least 1`);
}
return { input, output, ticks: json.ticks };
});
}
+15 -48
View File
@@ -3,9 +3,9 @@
// juicing recipes: what the juicer presses an item into. the shared script registers one for every item with
// bworld:juiceable, its own juice, and mods can add more like any recipe, as files or from their shared scripts.
// whatever makes juice reads them, like the juicer. with create installed, each is also a create:compacting recipe,
// so its press can make juice in a basin
import type { FluidStack, SharedContext, TagApi } from "bworld/shared";
// whatever makes juice finds them by the item, like the juicer. with create installed, each is also a create:compacting
// recipe, so its press can make juice in a basin
import type { FluidStack, SharedContext } from "bworld/shared";
import type { JuiceableParams } from "./shared.ts";
export const JUICING = "bworld:juicing";
@@ -19,6 +19,18 @@ export interface JuicingRecipe {
// a recipe for every juiceable item, pressing one into the juice named after it, as much as its component says
export function register_juicing(ctx: SharedContext) {
ctx.recipes.register_type<JuicingRecipe, string>(JUICING, {
parse(json, read) {
if (!Array.isArray(json.fluid_results) || json.fluid_results.length !== 1) {
throw new Error("fluid_results must be one { fluid, amount }");
}
return {
input: read.ingredient(json.input, "input"),
juice: read.fluid_stack(json.fluid_results[0], "fluid_results[0]"),
};
},
matches: (recipe, id) => recipe.input.includes(id),
});
for (const id of ctx.items.ids()) {
const params = ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined;
if (!params) continue;
@@ -41,48 +53,3 @@ function register_compacting(ctx: SharedContext) {
});
}
}
// what reading them needs, which server scripts have
export interface RecipeReader {
// deno-lint-ignore no-explicit-any
recipes: { list(type: string): any[] };
tags: TagApi;
items: { exists(id: string): boolean };
fluids: { exists(id: string): boolean };
}
// every juicing recipe, in load order. a broken one throws, which stops the server from starting
export function juicing_recipes(ctx: RecipeReader): JuicingRecipe[] {
return ctx.recipes.list(JUICING).map((json, i) => {
const where = `${JUICING} recipe ${i + 1}`;
const choice: unknown = json.input;
const entries = typeof choice === "string" ? [choice] : Array.isArray(choice) ? choice : [];
const exists = (entry: unknown) =>
typeof entry === "string" &&
(entry.startsWith("#") ? ctx.tags.exists("items", entry.slice(1)) : ctx.items.exists(entry));
if (entries.length === 0 || !entries.every(exists)) {
throw new Error(`${where}: input must be an item, a #tag or a list of them, and they must exist`);
}
const results: unknown = json.fluid_results;
const juice = Array.isArray(results) && results.length === 1 ? fluid_stack(ctx, results[0]) : undefined;
if (!juice) {
throw new Error(`${where}: fluid_results must be one { fluid, amount } of a fluid that exists, in mB`);
}
return { input: ctx.tags.expand(choice as string | string[]), juice };
});
}
// a recipe's { fluid, amount } of a fluid that exists and a whole number of mB, undefined when it isn't one
export function fluid_stack(ctx: RecipeReader, json: unknown): FluidStack | undefined {
const stack = json as Partial<FluidStack> | undefined;
const id = stack?.fluid?.id;
if (typeof id !== "string" || !ctx.fluids.exists(id) || !Number.isInteger(stack!.amount) || stack!.amount! < 1) {
return undefined;
}
return { fluid: stack!.fluid!, amount: stack!.amount! };
}
// the first recipe that presses id, undefined when none does
export function juicing_for(recipes: JuicingRecipe[], id: string): JuicingRecipe | undefined {
return recipes.find((recipe) => recipe.input.includes(id));
}
+17 -14
View File
@@ -6,13 +6,13 @@ import type { BlockRef, Container, FluidHandler, FluidStack, Player, Season, Ser
import { register_curios } from "./curios.ts";
import { register_effects } from "./effects.ts";
import { register_fishing } from "./fishing.ts";
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
import { STONECUTTING, stonecutting_options, type StonecuttingRecipe } from "./stonecutting.ts";
import { SMITHING, type SmithingRecipe } from "./smithing.ts";
import { FUEL, type FuelRecipe, SMELTING, type SmeltingRecipe } from "./smelting.ts";
import { register_verdigris } from "./verdigris/server.ts";
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
import { juicing_for, juicing_recipes } from "./juicing.ts";
import { fermentable_recipes } from "./fermenting.ts";
import { JUICING, type JuicingRecipe } from "./juicing.ts";
import { FERMENTABLE, type FermentableRecipe } from "./fermenting.ts";
import { register_cooking_pot, register_cutting_board, register_stove } from "./cooking.ts";
export function setup(ctx: ServerContext) {
@@ -295,7 +295,7 @@ function register_furnace(ctx: ServerContext) {
});
}
// smithing table: a tool, some of a material and maybe a third item make a better tool, see smithing.ts.
// smithing table: a tool, some of a material and maybe some of a third item make a better tool, see smithing.ts.
// every player gets their own slots, and what's left in them goes back to the player when they close it
const TOOL = 0;
@@ -439,7 +439,14 @@ function register_smithing_table(ctx: ServerContext) {
const addition = container.get(ADDITION);
if (!tool || !material) return undefined;
const recipe = ctx.recipes.find<SmithingRecipe>(SMITHING, { tool, material, addition });
return recipe && { result: recipe.result, material_count: recipe.material.count, tool, material, addition };
return recipe && {
result: recipe.result,
material_count: recipe.material.count,
addition_count: recipe.addition?.count ?? 0,
tool,
material,
addition,
};
};
// the result slot shows what taking it would give, the upgraded tool keeps the old one's data
@@ -470,7 +477,7 @@ function register_smithing_table(ctx: ServerContext) {
if (!recipe || recipe.result !== item.id) return false;
recipe.tool.count -= 1;
recipe.material.count -= recipe.material_count;
if (recipe.addition) recipe.addition.count -= 1;
if (recipe.addition) recipe.addition.count -= recipe.addition_count;
return true;
},
});
@@ -502,7 +509,7 @@ const STONECUTTER_INPUT = 0;
const STONECUTTER_RESULT = 1;
function register_stonecutter(ctx: ServerContext) {
const recipes = stonecutting_recipes(ctx);
const recipes = ctx.recipes.all<StonecuttingRecipe>(STONECUTTING);
const menu_type = ctx.menus.register<BlockRef>("bworld:stonecutter", (menu, block, player) => {
const container = ctx.containers.create(2);
@@ -872,8 +879,6 @@ interface JuicerData {
function register_juicer(ctx: ServerContext) {
// what juice is made from and how it looks, read by the juicing recipes and the juice's rules in shared.ts
ctx.components.register_item<JuiceableParams>("bworld:juiceable", {});
// checked now, so a broken recipe stops the server rather than the juicer later
const recipes = juicing_recipes(ctx);
const show = (block: BlockRef, container: Container) => {
const item = container.get(0);
const data = item?.data !== undefined && { data: item.data };
@@ -897,7 +902,7 @@ function register_juicer(ctx: ServerContext) {
const held = player.held_item;
const container = block_container(ctx, block, 1);
const data = block.data as JuicerData;
if (held && juicing_for(recipes, held.id)) {
if (held && ctx.recipes.find(JUICING, held.id)) {
held.count = container.add(held)?.count ?? 0;
show(block, container);
// true even when none fit, so the fruit isn't eaten instead
@@ -909,7 +914,7 @@ function register_juicer(ctx: ServerContext) {
if (held || !player.is_sneaking()) return false;
const item = container.get(0);
const recipe = item && juicing_for(recipes, item.id);
const recipe = item && ctx.recipes.find<JuicingRecipe>(JUICING, item.id);
if (!item || !recipe) return false;
const juice = tank(block);
if (juice.fill(recipe.juice, "simulate") < recipe.juice.amount) return false;
@@ -951,11 +956,9 @@ interface KegData {
type KegTank = "input" | "output";
function register_keg(ctx: ServerContext) {
// checked now, so a broken recipe stops the server rather than the keg later
const recipes = fermentable_recipes(ctx);
const recipe_for = (block: BlockRef) => {
const fluid = (block.data as KegData | undefined)?.input?.fluid;
return fluid && recipes.find((recipe) => ctx.fluids.same(recipe.input.fluid, fluid));
return fluid && ctx.recipes.find<FermentableRecipe>(FERMENTABLE, fluid);
};
const tank = (block: BlockRef, which: KegTank) =>
ctx.fluids.tank_handler({
+4
View File
@@ -6,10 +6,12 @@
// or a door open the moment they click it. and the rules of juice and wine, which name and color them by the item
// they're from, and the recipes that make them. and the base game's recipe types
import type { SharedBlock, SharedContext } from "bworld/shared";
import { register_cooking_recipes } from "./cooking_recipes.ts";
import { register_fermenting } from "./fermenting.ts";
import { register_juicing } from "./juicing.ts";
import { register_smelting } from "./smelting.ts";
import { register_smithing } from "./smithing.ts";
import { register_stonecutting } from "./stonecutting.ts";
import { register_verdigris_rules } from "./verdigris/shared.ts";
export function setup(ctx: SharedContext) {
@@ -22,6 +24,8 @@ export function setup(ctx: SharedContext) {
register_fermenting(ctx);
register_smithing(ctx);
register_smelting(ctx);
register_stonecutting(ctx);
register_cooking_recipes(ctx);
}
// the sides a fence connects on, and the face of the block there it connects to
+7 -10
View File
@@ -23,20 +23,14 @@ export interface FuelRecipe {
}
export function register_smelting(ctx: SharedContext) {
// deno-lint-ignore no-explicit-any
const ticks = (json: any, field: string): number => {
if (!Number.isInteger(json[field]) || json[field] < 1) throw new Error(`${field} must be at least 1 tick`);
return json[field];
};
// two recipes for one item would leave one that can never be made
const smelted = new Set<string>();
ctx.recipes.register_type<SmeltingRecipe, string>(SMELTING, {
parse(json, read) {
const recipe = {
input: read.ingredient(json, "input"),
output: read.stack(json, "output"),
cook_time: ticks(json, "cook_time"),
input: read.ingredient(json.input, "input"),
output: read.stack(json.output, "output"),
cook_time: read.positive_int(json.cook_time, "cook_time"),
};
for (const id of recipe.input) {
if (smelted.has(id)) throw new Error(`there's already one for ${id}`);
@@ -52,7 +46,10 @@ export function register_smelting(ctx: SharedContext) {
const burning = new Map<string, FuelRecipe>();
ctx.recipes.register_type<FuelRecipe, string>(FUEL, {
parse(json, read) {
const recipe = { items: read.ingredient(json, "item"), burn_time: ticks(json, "burn_time") };
const recipe = {
items: read.ingredient(json.item, "item"),
burn_time: read.positive_int(json.burn_time, "burn_time"),
};
for (const id of recipe.items) {
const earlier = burning.get(id);
if (earlier) earlier.items = earlier.items.filter((other) => other !== id);

Some files were not shown because too many files have changed in this diff Show More