Proper cutting board
This commit is contained in:
@@ -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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user