Shipping bin block
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user