Shipping bin block

This commit is contained in:
2026-10-04 16:18:03 -03:00
parent 6a6abae0d5
commit 4cdf48c9fb
8 changed files with 147 additions and 3 deletions
+2
View File
@@ -337,6 +337,8 @@ export interface ItemApi {
max_stack(id: Id): number;
// the tool type it is, like "pickaxe" or "shovel", from the item's "tool"
tool(id: Id): string | undefined;
// what one of it sells for, from the item's "base_sell_value", or undefined if it can't be sold
sell_value(id: Id): number | undefined;
// the components its json lists and their params, a copy. a mod can read params of its components this way
// without a stack, like the curios slots do for bworld:curio_modifiers
components(id: Id): Record<Id, unknown>;
+1
View File
@@ -156,6 +156,7 @@ catch up. If it's further behind than that, it skips the rest and logs a warning
| Component | Params | What it does |
| ----------------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
| `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it with a `base_sell_value` for coins. |
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
+1
View File
@@ -167,6 +167,7 @@ watering can uses `bworld:water_source`, or subscribe to the `block_interact` [e
ctx.items.exists("bworld:carrot"); // true
ctx.items.max_stack("bworld:wood_pickaxe"); // 1
ctx.items.tool("bworld:wood_pickaxe"); // "pickaxe", or undefined for items that aren't tools
ctx.items.sell_value("bworld:copper_coin"); // 1, or undefined for items without a base_sell_value
player.give_item("bworld:carrot", 3); // what doesn't fit drops at their feet
```
+18
View File
@@ -0,0 +1,18 @@
{
"format_version": 1,
"block": {
"id": "bworld:shipping_bin",
"textures": "bworld:oak_planks",
"mining": {
"toughness": 8,
"tool": "axe"
},
"drops": "bworld:shipping_bin",
"interactive": true,
"components": {
"bworld:shipping_bin": {
"rows": 3
}
}
}
}
@@ -0,0 +1,19 @@
{
"format_version": 1,
"recipe": {
"type": "shaped",
"pattern": [
"PLP",
"P P",
"PPP"
],
"key": {
"P": "#bworld:planks",
"L": "#bworld:logs"
},
"result": {
"id": "bworld:shipping_bin",
"count": 1
}
}
}
+67 -3
View File
@@ -5,7 +5,8 @@ import { register_effects } from "./effects.ts";
export function setup(ctx: ServerContext) {
register_hoeable(ctx);
register_storage(ctx);
const storage_menu = register_storage(ctx);
register_shipping_bin(ctx, storage_menu);
register_furnace(ctx);
register_smithing_table(ctx);
register_trapdoor(ctx);
@@ -71,12 +72,19 @@ function drop_contents(ctx: ServerContext, block: BlockRef) {
// inventory goes at the bottom, its rows from INVENTORY_Y and the hotbar under them
const INVENTORY_Y = 84;
interface StorageArgs {
block: BlockRef;
rows: number;
// "Chest" when not set
title?: string;
}
// a chest, like minecraft's ChestMenu: rows of 9 over the inventory
function register_storage(ctx: ServerContext) {
const menu_type = ctx.menus.register<{ block: BlockRef; rows: number }>("bworld:storage", (menu, args) => {
const menu_type = ctx.menus.register<StorageArgs>("bworld:storage", (menu, args) => {
const { block, rows } = args;
const container = block_container(ctx, block, rows * 9);
menu.title = "Chest";
menu.title = args.title ?? "Chest";
menu.open_data = { rows };
for (let slot = 0; slot < rows * 9; slot++) {
menu.add_slot(container, slot, { x: 7 + (slot % 9) * 18, y: 17 + Math.floor(slot / 9) * 18 });
@@ -97,6 +105,62 @@ function register_storage(ctx: ServerContext) {
drop_contents(ctx, block);
},
});
return menu_type;
}
// a shipping bin, like stardew valley's: a chest that every morning at 06:00 sells everything in it with a
// base_sell_value, and pays for it in as few coins as it can
const COINS = [
"bworld:cobalt_coin",
"bworld:gold_coin",
"bworld:iron_coin",
"bworld:brass_coin",
"bworld:nickel_coin",
"bworld:copper_coin",
];
function register_shipping_bin(ctx: ServerContext, storage_menu: ReturnType<typeof register_storage>) {
// the biggest first, by their own sell values
const coin_value = (id: string) => ctx.items.sell_value(id) ?? 0;
const coins = [...COINS].sort((a, b) => coin_value(b) - coin_value(a));
ctx.components.register_block<{ rows: number }>("bworld:shipping_bin", {
on_create(block, params) {
block_container(ctx, block, params.rows * 9);
},
on_interact(block, params, player) {
storage_menu.open(player, { block, rows: params.rows, title: "Shipping Bin" });
return true;
},
on_break(block) {
drop_contents(ctx, block);
},
on_new_day(block, params) {
const container = block_container(ctx, block, params.rows * 9);
let total = 0;
for (let slot = 0; slot < container.size; slot++) {
const item = container.get(slot);
const value = item && ctx.items.sell_value(item.id);
if (value === undefined) continue;
total += value * item!.count;
container.set(slot, undefined);
}
for (const coin of coins) {
const value = coin_value(coin);
if (value <= 0) continue;
let count = Math.floor(total / value);
total -= count * value;
while (count > 0) {
const stack = Math.min(count, ctx.items.max_stack(coin));
count -= stack;
// with unsold items in the way, what doesn't fit falls out
const left = container.add({ id: coin, count: stack });
if (left) ctx.world.drop_item(block.x, block.y, block.z, left);
}
}
},
});
}
// furnace
+5
View File
@@ -670,6 +670,11 @@ export class ModRuntime {
if (!item) throw new Error(`unknown item ${id}`);
return item.tool?.type;
},
sell_value: (id) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`unknown item ${id}`);
return item.base_sell_value;
},
components: (id) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`unknown item ${id}`);
+34
View File
@@ -1,6 +1,7 @@
// the base game's block and item behavior: hoeing, chests, furnaces, crops and the watering can
import { assert, assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { CYCLE_TICKS } from "$/common/time.ts";
import { display_name, get_state_value } from "$/common/utils.ts";
import { menu_data, menu_items, opened_menu, test_game } from "./helpers.ts";
@@ -99,6 +100,39 @@ Deno.test("a chest keeps items, shows them to everyone, and drops them when brok
);
});
Deno.test("a shipping bin sells what's in it every morning for as few coins as it can", async () => {
// oak planks sell for 2, so 70 of them and 5 copper are 145: 2 brass, 2 nickel and 1 copper
const t = await setup([["bworld:oak_planks", 64], ["bworld:oak_planks", 6], ["bworld:copper_coin", 5]]);
assertEquals(EverythingRegistry.get<ItemRegistry>("items", "bworld:oak_planks")?.base_sell_value, 2);
t.game.set_block(1, 100, 0, "bworld:shipping_bin");
const messages = t.take(1);
const inventory = last_container(messages, "inventory");
const planks = inventory.flatMap((item: Msg, slot: number) => item?.id === "bworld:oak_planks" ? [slot] : []);
const copper = slot_of(messages, "bworld:copper_coin");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const opened = opened_menu(t.take(1));
assertEquals(opened.menu_type, "bworld:storage");
assertEquals(opened.title, "Shipping Bin");
assertEquals(opened.slots.length, 27 + 36);
move_to_menu(t, opened, planks[0], 0);
move_to_menu(t, opened, planks[1], 1);
move_to_menu(t, opened, copper, 2);
t.send(1, { type: "close_menu", menu: opened.id });
t.take(1);
t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1);
t.game.tick();
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const sold = menu_items(t.take(1)).slice(0, 27).filter((item: Msg) => item);
assertEquals(sold, [
{ id: "bworld:brass_coin", count: 2 },
{ id: "bworld:nickel_coin", count: 2 },
{ id: "bworld:copper_coin", count: 1 },
]);
});
Deno.test("a furnace burns fuel to smelt, shows its progress, and only lets the result be taken", async () => {
const t = await setup([["bworld:iron_ore", 2], ["bworld:coal", 1]]);
t.game.set_block(1, 100, 0, "bworld:furnace");