Proper cutting board

This commit is contained in:
2026-09-29 00:04:56 -03:00
parent a51adba46f
commit 445148e812
26 changed files with 549 additions and 16 deletions
+18 -1
View File
@@ -6,12 +6,29 @@
"textures": {
"main": "farmersdelight:cutting_board"
},
"variants": {
"facing=1": { "y": 90 },
"facing=2": { "y": 180 },
"facing=3": { "y": 270 }
},
"render_layer": "cutout",
"collision": false,
"mining": {
"toughness": 2,
"tool": "axe"
},
"drops": "farmersdelight:cutting_board"
"drops": "farmersdelight:cutting_board",
"interactive": true,
"item_display": { "surface": 1 },
"states": [
{
"name": "facing",
"bits": 2,
"default": 0
}
],
"components": {
"farmersdelight:cutting_board": {}
}
}
}
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "farmersdelight:cabbage_leaf",
"texture": "farmersdelight:cabbage_leaf",
"food": {
"nutrition": 1,
"saturation_modifier": 0.4
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "farmersdelight:cod_slice",
"texture": "farmersdelight:cod_slice",
"food": {
"nutrition": 1,
"saturation_modifier": 0.1
}
}
}
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "farmersdelight:salmon_slice",
"texture": "farmersdelight:salmon_slice",
"food": {
"nutrition": 1,
"saturation_modifier": 0.1
}
}
}
@@ -0,0 +1,20 @@
{
"format_version": 1,
"recipe": {
"type": "shaped",
"pattern": ["SPP", "SPP"],
"key": {
"S": "bworld:stick",
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
]
},
"result": { "id": "farmersdelight:cutting_board", "count": 1 }
}
}
@@ -0,0 +1,9 @@
{
"format_version": 1,
"recipe": {
"type": "farmersdelight:cutting",
"ingredients": ["farmersdelight:cabbage"],
"tool": "knife",
"result": [{ "id": "farmersdelight:cabbage_leaf", "count": 2 }]
}
}
@@ -0,0 +1,9 @@
{
"format_version": 1,
"recipe": {
"type": "farmersdelight:cutting",
"ingredients": ["bworld:cod"],
"tool": "knife",
"result": [{ "id": "farmersdelight:cod_slice", "count": 2 }, { "id": "bworld:bone_meal", "count": 1 }]
}
}
@@ -0,0 +1,9 @@
{
"format_version": 1,
"recipe": {
"type": "farmersdelight:cutting",
"ingredients": ["bworld:salmon"],
"tool": "knife",
"result": [{ "id": "farmersdelight:salmon_slice", "count": 2 }, { "id": "bworld:bone_meal", "count": 1 }]
}
}
+104 -2
View File
@@ -1,11 +1,13 @@
// farmer's delight's kitchen. the stove is a heat source, like its StoveBlock. the cooking pot, like its
// CookingPotBlockEntity, cooks its recipes while something hot is under it, keeps the meal, and serves it into the
// container the meal needs (a bowl), or straight out when it needs none
import type { BlockRef, Container, ScreenHandle, ServerContext } from "bworld/server";
// container the meal needs (a bowl), or straight out when it needs none. the cutting board, like its
// CuttingBoardBlockEntity, holds a stack of anything and cuts it one at a time with the right tool
import type { BlockRef, Container, ItemStack, ScreenHandle, ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
register_stove(ctx);
register_cooking_pot(ctx);
register_cutting_board(ctx);
}
// stove
@@ -312,3 +314,103 @@ function register_cooking_pot(ctx: ServerContext) {
},
});
}
// cutting board
// its facing state, the way its front points: north, east, south or west, like the java mod's HORIZONTAL_FACING
const FACINGS = 4;
// a farmersdelight: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("farmersdelight:cutting").map((json, i) => {
const where = `farmersdelight: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 ingredient = [json.ingredients[0]].flat();
if (ingredient.length === 0) throw new Error(`${where}: the ingredient's list of items is empty`);
ingredient.forEach(item);
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
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);
// placed facing whoever placed it, like getStateForPlacement. they look along (-sin yaw, -cos yaw), so the way
// back to them is (sin yaw, cos yaw), and north (-z) is 0 going clockwise
ctx.events.after.block_place.subscribe(({ player, block }) => {
if (block.id !== "farmersdelight:cutting_board") return;
const quarter = Math.round((Math.PI - player.yaw) / (Math.PI / 2));
const facing = ((quarter % FACINGS) + FACINGS) % FACINGS;
ctx.world.set_state(block.x, block.y, block.z, "facing", facing);
});
ctx.components.register_block("farmersdelight:cutting_board", {
on_interact(block, _params, player) {
const held = player.inventory.get(player.selected_slot);
const item = on_board(block);
// an empty hand takes everything back
if (!held) {
if (!item) return false;
set_on_board(block, undefined);
player.give_item(item.id, item.count, item.data);
return true;
}
// anything goes on, as much of it as fits, like canAddItem and addItem. a knife on an empty board too
const room = !item
? ctx.items.max_stack(held.id)
: item.id === held.id && JSON.stringify(item.data) === JSON.stringify(held.data)
? ctx.items.max_stack(item.id) - item.count
: 0;
if (room > 0) {
const count = Math.min(room, held.count);
set_on_board(block, { id: held.id, count: (item?.count ?? 0) + count, data: held.data });
held.count -= count;
return true;
}
// 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));
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);
}
// like the java mod, the click is used up either way, nothing gets placed or eaten
return true;
},
on_break(block) {
const item = on_board(block);
if (item) ctx.world.drop_item(block.x, block.y, block.z, item);
},
});
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 386 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 B

+15 -6
View File
@@ -23,6 +23,7 @@ const CATALYSTS: Record<string, string> = {
fuel: "bworld:furnace",
smithing: "bworld:smithing_table",
"farmersdelight:cooking": "farmersdelight:cooking_pot",
"farmersdelight:cutting": "farmersdelight:cutting_board",
};
const CATEGORY_NAMES: Record<string, string> = {
shaped: "Crafting",
@@ -255,7 +256,7 @@ class RecipeViewer {
break;
default: {
// a mod's own type: the items in its lists in a grid, single items (like a bowl to serve in) under
// the arrow, then what it makes
// the arrow, then what it makes, one under the other
const { grid, extras } = mod_recipe_inputs(recipe);
grid.slice(0, 9).forEach((ingredient, i) =>
cell(ingredient, x + (i % 3) * CELL, top + Math.floor(i / 3) * CELL)
@@ -264,7 +265,10 @@ class RecipeViewer {
extras.slice(0, 1).forEach((ingredient) =>
cell(ingredient, x + 3.5 * CELL, top + CELL * 1.5, ["Served in"])
);
outputs(recipe).slice(0, 1).forEach((output) => cell(output, x + 5 * CELL, top + CELL / 2));
const made = outputs(recipe).slice(0, 3);
made.forEach((output, i) =>
cell(output, x + 5 * CELL, made.length > 1 ? top + i * CELL : top + CELL / 2)
);
if (typeof recipe.cook_time === "number") {
g.text(seconds(recipe.cook_time), x + 5 * CELL, top + CELL * 1.5 + (CELL - TEXT) / 2, {
color: LABEL_COLOR,
@@ -280,7 +284,9 @@ class RecipeViewer {
function card_height(recipe: Recipe): number {
let rows = 2;
if (recipe.type === "shaped") rows = 3;
else if (!(recipe.type in CATEGORY_NAMES)) rows = Math.max(2, Math.ceil(mod_recipe_inputs(recipe).grid.length / 3));
else if (!(recipe.type in CATEGORY_NAMES)) {
rows = Math.max(2, Math.ceil(mod_recipe_inputs(recipe).grid.length / 3), outputs(recipe).length);
}
return LINE + Math.min(rows, 3) * CELL + GAP;
}
@@ -317,10 +323,13 @@ export function outputs(recipe: Recipe): Ingredient[] {
case "smithing":
return [{ ids: [recipe.result], count: 1 }];
default: {
// one item or stack, or a list of them, like what a cutting board cuts something into
const output = recipe.result ?? recipe.output;
if (typeof output === "string") return [{ ids: [output], count: 1 }];
if (output && typeof output.id === "string") return [stack(output)];
return [];
return [output].flat().flatMap((output): Ingredient[] => {
if (typeof output === "string") return [{ ids: [output], count: 1 }];
if (output && typeof output.id === "string") return [stack(output)];
return [];
});
}
}
}