Worm bin working with both worms and grubs

This commit is contained in:
2026-10-12 02:22:44 -03:00
parent ff5fb971da
commit df26248f4a
13 changed files with 222 additions and 60 deletions
+1 -1
View File
@@ -242,7 +242,7 @@ export abstract class GuiMenuScreen extends GuiScreen {
// output slots depend on server side things like recipes, and where a shift click goes on slot filters only the
// server has, just wait for the server
if (!slot.output && !shift) {
click_slot(slot.container, slot.index, this.inventories.cursor, button);
click_slot(slot.container, slot.index, this.inventories.cursor, button, slot.max_count);
}
}
}
+8 -1
View File
@@ -15,6 +15,8 @@ export interface ClientSlot {
output: boolean;
// a texture drawn while it's empty, like minecraft's armor slots
icon: string | undefined;
// the most it holds of anything, when that's less than a stack
max_count?: number;
}
export class ClientMenu {
@@ -56,6 +58,7 @@ export class ClientMenu {
y: slot.y,
output: slot.output === true,
icon: slot.icon,
max_count: slot.max_count,
})),
);
}
@@ -84,7 +87,11 @@ export function player_menu(inventories: ClientInventories): ClientMenu {
slots.push(slot(inventories.crafting, CRAFTING_RESULT_SLOT, 151, 34, true));
if (slots.length !== PLAYER_MENU_EXTRA) throw new Error("menu 0 has the wrong slots");
for (const [i, info] of inventories.extra_slots.entries()) {
slots.push({ ...slot(inventories.extra, i, info.x, info.y, info.output === true), icon: info.icon });
slots.push({
...slot(inventories.extra, i, info.x, info.y, info.output === true),
icon: info.icon,
max_count: info.max_count,
});
}
return new ClientMenu(PLAYER_MENU, "", "Crafting", null, slots);
}
+37 -16
View File
@@ -203,12 +203,13 @@ export interface Cursor {
export const LEFT_CLICK = 0;
export const RIGHT_CLICK = 2;
// what clicking a normal slot does, the server runs this for real and clients run it to predict
export function click_slot(container: Container, index: number, cursor: Cursor, button: number) {
// what clicking a normal slot does, the server runs this for real and clients run it to predict. limit is the most
// the slot holds of anything, like minecraft's Slot.getMaxStackSize
export function click_slot(container: Container, index: number, cursor: Cursor, button: number, limit = Infinity) {
if (button === LEFT_CLICK) {
left_click(container, index, cursor);
left_click(container, index, cursor, limit);
} else if (button === RIGHT_CLICK) {
right_click(container, index, cursor);
right_click(container, index, cursor, limit);
}
if (cursor.item && cursor.item.amount <= 0) {
cursor.item = undefined;
@@ -222,7 +223,7 @@ function swap_with_cursor(container: Container, index: number, cursor: Cursor) {
slot.set_item(original);
}
function left_click(container: Container, index: number, cursor: Cursor) {
function left_click(container: Container, index: number, cursor: Cursor, limit: number) {
const holding = cursor.item;
const slot = container.get_slot(index);
const slot_item = slot.get_item();
@@ -237,8 +238,8 @@ function left_click(container: Container, index: number, cursor: Cursor) {
// if you are holding something and slot type equals holding type
// try to add to stack
if (slot_item && slot.type_id === holding.type_id) {
const space_left = slot.max_amount! - slot_item.amount;
const amount_to_add = Math.min(space_left, holding.amount);
const space_left = Math.min(slot.max_amount!, limit) - slot_item.amount;
const amount_to_add = Math.max(0, Math.min(space_left, holding.amount));
slot_item.amount += amount_to_add;
holding.amount -= amount_to_add;
@@ -246,12 +247,23 @@ function left_click(container: Container, index: number, cursor: Cursor) {
return;
}
// more than the slot holds: an empty one takes as many as it can, a full one keeps what it has
if (holding.amount > limit) {
if (!slot_item) {
const placed = holding.clone();
placed.amount = limit;
slot.set_item(placed);
holding.amount -= limit;
}
return;
}
// if something on hand but not the same
// swap
swap_with_cursor(container, index, cursor);
}
function right_click(container: Container, index: number, cursor: Cursor) {
function right_click(container: Container, index: number, cursor: Cursor, limit: number) {
const holding = cursor.item;
const slot = container.get_slot(index);
const slot_item = slot.get_item();
@@ -261,7 +273,7 @@ function right_click(container: Container, index: number, cursor: Cursor) {
// and slot type equals holding type
// add 1 to matching stack
if (slot_item && slot.type_id === holding.type_id) {
if (slot_item.amount < slot_item.max_amount) {
if (slot_item.amount < Math.min(slot_item.max_amount, limit)) {
slot_item.amount += 1;
holding.amount -= 1;
}
@@ -278,8 +290,8 @@ function right_click(container: Container, index: number, cursor: Cursor) {
}
// if something on hand but not the same
// swap
swap_with_cursor(container, index, cursor);
// swap, when all of it fits
if (holding.amount <= limit) swap_with_cursor(container, index, cursor);
return;
}
@@ -303,6 +315,13 @@ function right_click(container: Container, index: number, cursor: Cursor) {
export interface SlotRef {
container: Container;
index: number;
// the most it holds of anything, when that's less than a stack
max_count?: number;
}
// how many of item the slot holds
function slot_cap(slot: SlotRef, item: ItemStack): number {
return Math.min(item.max_amount, slot.max_count ?? Infinity);
}
// shift clicking, like minecraft's moveItemStackTo: tops up stacks of the same item in these slots, in order, then
@@ -312,7 +331,7 @@ export function move_into(item: ItemStack, slots: SlotRef[], accepts: (slot: Slo
const there = slot.container.get_item(slot.index);
if (item.amount <= 0) return;
if (there && there !== item && there.type_id === item.type_id && accepts(slot)) {
const moving = Math.min(there.max_amount - there.amount, item.amount);
const moving = Math.max(0, Math.min(slot_cap(slot, there) - there.amount, item.amount));
there.amount += moving;
item.amount -= moving;
}
@@ -320,8 +339,10 @@ export function move_into(item: ItemStack, slots: SlotRef[], accepts: (slot: Slo
for (const slot of slots) {
if (item.amount <= 0) return;
if (!slot.container.get_item(slot.index) && accepts(slot)) {
slot.container.set_item(slot.index, item.clone());
item.amount = 0;
const moved = item.clone();
moved.amount = Math.min(item.amount, slot_cap(slot, item));
slot.container.set_item(slot.index, moved);
item.amount -= moved.amount;
}
}
}
@@ -332,8 +353,8 @@ export function room_for(item: ItemStack, slots: SlotRef[], accepts: (slot: Slot
for (const slot of slots) {
const there = slot.container.get_item(slot.index);
if (!accepts(slot)) continue;
if (!there) room += item.max_amount;
else if (there.type_id === item.type_id) room += there.max_amount - there.amount;
if (!there) room += slot_cap(slot, item);
else if (there.type_id === item.type_id) room += Math.max(0, slot_cap(slot, there) - there.amount);
}
return room;
}
+2
View File
@@ -494,6 +494,8 @@ export interface SlotOptions {
on_take?: (item: ItemStack) => boolean;
// a texture drawn in the slot while it's empty, like minecraft's armor slots
icon?: Id;
// the most it holds, when that's less than a stack, like minecraft's Slot.getMaxStackSize
max_count?: number;
}
// what a menu type's build function gets, like neoforge's server menu constructor
+2
View File
@@ -77,6 +77,8 @@ export interface MenuSlotInfo {
y: number;
output?: boolean;
icon?: string;
// the most it holds of anything, when that's less than a stack
max_count?: number;
}
// clients send what the player is trying to do, the server decides what happens
+4 -3
View File
@@ -61,14 +61,15 @@ returns the slot's **menu slot index**: slots are numbered in the order they're
| `output_only` | Can only be taken from, all at once, like the furnace result. |
| `on_take` | For `output_only` slots: runs when a player takes from it, before they get the item. Return `false` to stop them. |
| `icon` | A texture drawn in the slot while it's empty, like Minecraft's armor slots. |
| `max_count` | The most the slot holds, when that's less than a stack, like Minecraft's `Slot.getMaxStackSize`. |
`add_player_inventory(x, y)` adds the player's 36 slots, like NeoForge's `addStandardInventorySlots`: the rest of the
inventory in 3 rows from `x` and `y`, and the hotbar 58 pixels under them. They're added in inventory order, so
inventory slot `i` is the returned index plus `i`.
`filter`, `output_only` and `on_take` run **on the server**, so players can't put the wrong items in by editing their
client. `on_take` is the place to use up a recipe's inputs, so taking the result and spending the inputs happen in the
same step, like the smithing table does.
`filter`, `output_only`, `on_take` and `max_count` run **on the server**, so players can't put the wrong items in by
editing their client. `on_take` is the place to use up a recipe's inputs, so taking the result and spending the inputs
happen in the same step, like the smithing table does.
### Shift Clicking
+3 -3
View File
@@ -3,14 +3,14 @@
"recipe": {
"type": "shaped",
"pattern": [
"CWC",
"I I",
"CDC",
"CCC"
],
"key": {
"I": "#c:ingots/iron",
"C": "#c:ingots/copper",
"D": "bworld:dirt",
"W": "bworld:worms"
"D": "bworld:dirt"
},
"result": {
"id": "bworld:worm_bin",
+8 -1
View File
@@ -10,7 +10,7 @@ export function setup(ctx: ClientContext) {
ctx.ui.register_menu_screen("bworld:storage", chest_screen);
ctx.ui.register_menu_screen("bworld:furnace", furnace_screen);
ctx.ui.register_menu_screen("bworld:crabpot", (menu) => container_screen(menu, HEIGHT));
ctx.ui.register_menu_screen("bworld:worm_bin", (menu) => container_screen(menu, HEIGHT));
ctx.ui.register_menu_screen("bworld:worm_bin", worm_bin_screen);
ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen);
ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen);
}
@@ -44,6 +44,13 @@ function furnace_screen(menu: ClientMenu): MenuScreen {
});
}
// its input on the left, an arrow to what it makes on the right
function worm_bin_screen(menu: ClientMenu): MenuScreen {
return container_screen(menu, HEIGHT, (g) => {
g.texture("bworld:arrow_empty", 85, 34, 18, 18);
});
}
function smithing_screen(menu: ClientMenu): MenuScreen {
return container_screen(menu, HEIGHT, (g) => {
g.texture("bworld:arrow_empty", 88, 34, 18, 18);
+37 -10
View File
@@ -39,10 +39,15 @@ function register_hoeable(ctx: ServerContext) {
// existed don't have one yet
function block_container(ctx: ServerContext, block: BlockRef, size: number): Container {
const existing = block.data?.container && ctx.containers.get(block.data.container);
if (existing) {
if (existing && existing.size >= size) {
return existing;
}
// one from before the block had more slots moves into a bigger one, keeping what's in it
const container = ctx.containers.create(size);
if (existing) {
for (let slot = 0; slot < existing.size; slot++) container.set(slot, existing.get(slot));
ctx.containers.delete(existing.id);
}
block.data = { ...block.data, container: container.id };
return container;
}
@@ -329,14 +334,29 @@ function register_crabpot(ctx: ServerContext) {
});
}
// the worm bin makes worms every morning, for the crab pot's bait, into its one slot, which players can only take from.
// they pile up to a full stack
// the worm bin breeds bait: one #bworld:worm_binable item in its input makes 5 more of it every morning, into its
// output, which players can only take from. they pile up to a full stack, and the input is never used up. the output
// is slot 0, where worm bins from before the input kept their worms
const WORM_BIN_OUTPUT = 0;
const WORM_BIN_INPUT = 1;
const WORM_BIN_SIZE = 2;
const WORM_BINABLE = "bworld:worm_binable";
const WORMS_A_DAY = 5;
function register_worm_bin(ctx: ServerContext) {
const binable = (id: string) => ctx.tags.has("items", WORM_BINABLE, id);
const menu_type = ctx.menus.register<BlockRef>("bworld:worm_bin", (menu, block) => {
const container = block_container(ctx, block, WORM_BIN_SIZE);
menu.title = "Worm Bin";
menu.add_slot(block_container(ctx, block, 1), 0, { x: 79, y: 34, output_only: true });
menu.add_slot(container, WORM_BIN_INPUT, {
x: 55,
y: 34,
filter: (item) => binable(item.id),
max_count: 1,
icon: "bworld:empty_slot_bait",
});
menu.add_slot(container, WORM_BIN_OUTPUT, { x: 115, y: 34, output_only: true });
menu.add_player_inventory(7, INVENTORY_Y);
menu.still_valid_near(block);
});
@@ -344,7 +364,7 @@ function register_worm_bin(ctx: ServerContext) {
ctx.components.register_block("bworld:worm_bin", {
// its container is its tile data, so its mornings come anywhere in the world, like a crop's
on_create(block) {
block_container(ctx, block, 1);
block_container(ctx, block, WORM_BIN_SIZE);
},
on_interact(block, _params, player) {
menu_type.open(player, block);
@@ -354,11 +374,18 @@ function register_worm_bin(ctx: ServerContext) {
drop_contents(ctx, block);
},
on_new_day(block) {
const container = block_container(ctx, block, 1);
const worms = container.get(0);
const max = ctx.items.max_stack(CRABPOT_WORMS);
if (!worms) container.set(0, { id: CRABPOT_WORMS, count: WORMS_A_DAY });
else if (worms.id === CRABPOT_WORMS) worms.count = Math.min(max, worms.count + WORMS_A_DAY);
const container = block_container(ctx, block, WORM_BIN_SIZE);
const input = container.get(WORM_BIN_INPUT);
if (!input || !binable(input.id)) {
return;
}
// an output of something else, from before the input was swapped, has to be taken out first
const output = container.get(WORM_BIN_OUTPUT);
if (!output) {
container.set(WORM_BIN_OUTPUT, { id: input.id, count: WORMS_A_DAY });
} else if (output.id === input.id) {
output.count = Math.min(ctx.items.max_stack(input.id), output.count + WORMS_A_DAY);
}
},
});
}
@@ -0,0 +1,9 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:worms",
"bworld:grubs"
]
}
}
+5 -2
View File
@@ -35,6 +35,8 @@ export interface MenuSlot {
on_take?: (item: ItemStack) => boolean;
// drawn in the slot while it's empty
icon?: string;
// the most it holds of anything, when that's less than a stack, like minecraft's Slot.getMaxStackSize
max_count?: number;
// one of the player's own inventory slots, this one
inventory?: number;
}
@@ -79,7 +81,7 @@ export class Menu {
// the slot doesn't take what the cursor holds
return;
} else {
click_slot(slot.container, slot.index, cursor, button);
click_slot(slot.container, slot.index, cursor, button, slot.max_count);
}
this.on_changed?.();
}
@@ -138,11 +140,12 @@ export class Menu {
}
slot_info(): MenuSlotInfo[] {
return this.slots.map(({ x, y, output, icon }) => ({
return this.slots.map(({ x, y, output, icon, max_count }) => ({
x,
y,
...(output ? { output: true } : {}),
...(icon ? { icon } : {}),
...(max_count !== undefined ? { max_count } : {}),
}));
}
+6
View File
@@ -1154,6 +1154,12 @@ function menu_slot(
if (options.output_only) added.output = true;
if (on_take) added.on_take = (item) => guarded("an on_take", () => on_take(item_api(item))) === true;
if (options.icon) added.icon = String(options.icon);
if (options.max_count !== undefined) {
if (!Number.isInteger(options.max_count) || options.max_count < 1) {
throw new Error(`slot ${slot}'s max_count must be a whole number, at least 1`);
}
added.max_count = options.max_count;
}
if (engine === player.inventory) added.inventory = slot;
return added;
}
+100 -23
View File
@@ -1,51 +1,128 @@
// the worm bin: 5 worms every morning into its one slot, for the crab pot's bait
// the worm bin: one worm_binable item in its input makes 5 more of it every morning, into its output
import { assert, assertEquals } from "@std/assert";
import { CYCLE_TICKS } from "$/common/time.ts";
import { ItemStack } from "$/common/inventory.ts";
import { opened_menu, test_game } from "./helpers.ts";
import { Container, ItemStack } from "$/common/inventory.ts";
import { menu_items, opened_menu, test_game } from "./helpers.ts";
async function setup() {
// the container's slots, see register_worm_bin
const OUTPUT = 0;
const INPUT = 1;
// the menu's: the input, the output, then the inventory
const MENU_INPUT = 0;
const MENU_OUTPUT = 1;
const inventory_slot = (index: number) => 2 + index;
// a worm bin at 0 100 0 next to alice, with the items given in her inventory from the first hotbar slot on
async function setup(items: [string, number][] = []) {
const t = await test_game("mods");
t.join(1, "alice");
t.game.set_block(0, 99, 0, "bworld:stone");
t.game.set_block(0, 100, 0, "bworld:worm_bin");
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
const data = t.game.world.get_tile(0, 100, 0)!.mod_data as { container: string };
const container = t.game.containers.get(data.container)!;
for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` });
const tile = t.game.world.get_tile(0, 100, 0)!;
const container = () => t.game.containers.get((tile.mod_data as { container: string }).container)!;
const next_morning = () => {
t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1);
t.game.tick();
};
return { t, container, next_morning };
const open = () => {
t.take(1);
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
return opened_menu(t.take(1));
};
const click = (menu: { id: number }, slot: number, shift = false) =>
t.send(1, { type: "click", menu: menu.id, slot, button: 0, shift });
return { t, tile, container, next_morning, open, click };
}
Deno.test("right clicking a worm bin opens its one output slot", async () => {
const { t } = await setup();
t.take(1);
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
const opened = opened_menu(t.take(1));
Deno.test("right clicking a worm bin opens its input, an arrow's width from its output", async () => {
const { open } = await setup();
const opened = open();
assertEquals([opened.menu_type, opened.title], ["bworld:worm_bin", "Worm Bin"]);
assertEquals(opened.slots.length, 1 + 36);
assertEquals(opened.slots[0], { x: 79, y: 34, output: true });
assertEquals(opened.slots.length, 2 + 36);
assertEquals(opened.slots[MENU_INPUT], { x: 55, y: 34, icon: "bworld:empty_slot_bait", max_count: 1 });
assertEquals(opened.slots[MENU_OUTPUT], { x: 115, y: 34, output: true });
});
Deno.test("a worm bin makes 5 worms every morning, up to a full stack", async () => {
Deno.test("the input takes one worm_binable item, and nothing else", async () => {
const { t, container, open, click } = await setup([["bworld:worms", 10], ["bworld:coal", 3], ["bworld:grubs", 4]]);
const menu = open();
// coal isn't worm_binable
click(menu, inventory_slot(1));
click(menu, MENU_INPUT);
assertEquals(container().get_item(INPUT), undefined);
click(menu, inventory_slot(1));
// of a stack of worms, one goes in and the rest stay on the cursor
click(menu, inventory_slot(0));
click(menu, MENU_INPUT);
assertEquals(container().get_item(INPUT)?.to_data(), { id: "bworld:worms", count: 1 });
click(menu, MENU_INPUT);
assertEquals(container().get_item(INPUT)?.amount, 1);
click(menu, inventory_slot(0));
assertEquals(menu_items(t.take(1))[inventory_slot(0)], { id: "bworld:worms", count: 9 });
// a stack of grubs doesn't swap with it, it wouldn't fit
click(menu, inventory_slot(2));
click(menu, MENU_INPUT);
assertEquals(container().get_item(INPUT)?.type_id, "bworld:worms");
click(menu, inventory_slot(2));
// shift clicking moves just one in too, once it's empty
click(menu, MENU_INPUT, true);
assertEquals(container().get_item(INPUT), undefined);
click(menu, inventory_slot(2), true);
assertEquals(container().get_item(INPUT)?.to_data(), { id: "bworld:grubs", count: 1 });
assertEquals(menu_items(t.take(1))[inventory_slot(2)], { id: "bworld:grubs", count: 3 });
});
Deno.test("every morning a worm bin makes 5 of what's in its input, up to a full stack", async () => {
const { container, next_morning } = await setup();
assertEquals(container.get_item(0), undefined);
// an empty input makes nothing
next_morning();
assertEquals(container.get_item(0)?.to_data(), { id: "bworld:worms", count: 5 });
assertEquals(container().get_item(OUTPUT), undefined);
container().set_item(INPUT, new ItemStack("bworld:worms"));
next_morning();
assertEquals(container.get_item(0)?.amount, 10);
container.set_item(0, new ItemStack("bworld:worms", 62));
assertEquals(container().get_item(OUTPUT)?.to_data(), { id: "bworld:worms", count: 5 });
next_morning();
assertEquals(container.get_item(0)?.amount, 64);
assertEquals(container().get_item(OUTPUT)?.amount, 10);
container().set_item(OUTPUT, new ItemStack("bworld:worms", 62));
next_morning();
assertEquals(container().get_item(OUTPUT)?.amount, 64);
// the input is never used up
assertEquals(container().get_item(INPUT)?.to_data(), { id: "bworld:worms", count: 1 });
// grubs make grubs, once the worms are taken out
container().set_item(INPUT, new ItemStack("bworld:grubs"));
next_morning();
assertEquals(container().get_item(OUTPUT)?.to_data(), { id: "bworld:worms", count: 64 });
container().set_item(OUTPUT, undefined);
next_morning();
assertEquals(container().get_item(OUTPUT)?.to_data(), { id: "bworld:grubs", count: 5 });
});
Deno.test("breaking a worm bin drops it and its worms", async () => {
const { t, next_morning } = await setup();
Deno.test("a worm bin from before the input keeps its worms", async () => {
const { t, tile, container, next_morning } = await setup();
const old = new Container(1);
old.set_item(0, new ItemStack("bworld:worms", 20));
t.game.containers.set("old_worm_bin", old);
tile.mod_data = { container: "old_worm_bin" };
next_morning();
assertEquals(container().size, 2);
assertEquals(container().get_item(OUTPUT)?.to_data(), { id: "bworld:worms", count: 20 });
assertEquals(t.game.containers.has("old_worm_bin"), false);
});
Deno.test("breaking a worm bin drops it, its input and what it made", async () => {
const { t, container, next_morning } = await setup();
container().set_item(INPUT, new ItemStack("bworld:worms"));
next_morning();
t.send(1, { type: "break_block", x: 0, y: 100, z: 0 });
const dropped = t.game.item_entities().map((e) => `${e.item.type_id} ${e.item.amount}`).sort();
assertEquals(dropped, ["bworld:worm_bin 1", "bworld:worms 5"]);
assertEquals(dropped, ["bworld:worm_bin 1", "bworld:worms 1", "bworld:worms 5"]);
assert(t.game.world.get_block_id(0, 100, 0) === "bworld:air");
});