Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+20 -53
View File
@@ -2,7 +2,7 @@
// 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. 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";
import type { BlockRef, Container, ItemStack, ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
register_stove(ctx);
@@ -91,27 +91,6 @@ interface PotData {
meal_container: string;
}
const POT_BARS = [
{
x: 2.2,
y: 2,
value: "heat",
max: "heat_max",
direction: "up" as const,
empty_texture: "bworld:fire_empty",
full_texture: "bworld:fire_full",
},
{
x: 4.6,
y: 0.5,
value: "progress",
max: "progress_max",
direction: "right" as const,
empty_texture: "bworld:arrow_empty",
full_texture: "bworld:arrow_full",
},
];
// an ingredient: an item, or a #tag of items any of which will do
function item_or_tag(ctx: ServerContext, where: string, id: unknown) {
if (typeof id === "string" && id.startsWith("#")) {
@@ -186,9 +165,6 @@ function matches(ingredients: string[][], items: string[], used = new Set<number
function register_cooking_pot(ctx: ServerContext) {
const recipes = load_recipes(ctx);
// who's looking at each pot, by position, to keep their bars moving
const viewers = new Map<string, Set<ScreenHandle>>();
const key = (block: BlockRef) => `${block.x},${block.y},${block.z}`;
const pot_data = (block: BlockRef): PotData => {
const existing = block.data?.container && ctx.containers.get(block.data.container);
@@ -204,12 +180,24 @@ function register_cooking_pot(ctx: ServerContext) {
return state === undefined || ctx.world.get_state(block.x, block.y - 1, block.z, state) === 1;
};
const show = (screen: ScreenHandle, data: PotData, heated: boolean) => {
screen.set_property("progress", data.cook_time);
screen.set_property("progress_max", data.cook_time_total);
screen.set_property("heat", heated ? 1 : 0);
screen.set_property("heat_max", 1);
};
// like the java mod's CookingPotMenu, in gui pixels: the ingredients in 2 rows of 3, the meal, and the containers
// and served meal under it. the screen is in scripts/client.ts
const menu_type = ctx.menus.register<BlockRef>("farmersdelight:cooking_pot", (menu, block) => {
const container = ctx.containers.get(pot_data(block).container)!;
menu.title = "Cooking Pot";
for (let slot = 0; slot < INGREDIENT_SLOTS; slot++) {
menu.add_slot(container, slot, { x: 29 + (slot % 3) * 18, y: 16 + Math.floor(slot / 3) * 18 });
}
// the meal stays in the pot until it's served
menu.add_slot(container, MEAL_SLOT, { x: 122, y: 25, output_only: true, on_take: () => false });
menu.add_slot(container, CONTAINER_SLOT, { x: 90, y: 52 });
menu.add_slot(container, OUTPUT_SLOT, { x: 122, y: 52, output_only: true });
menu.add_player_inventory(7, 84);
menu.add_data("progress", () => (block.data as PotData | undefined)?.cook_time ?? 0);
menu.add_data("progress_max", () => (block.data as PotData | undefined)?.cook_time_total ?? 0);
menu.add_data("heated", () => is_heated(block) ? 1 : 0);
menu.still_valid_near(block);
});
// there's room in the meal slot for what it makes
const can_cook = (container: Container, recipe: CookingRecipe) => {
@@ -275,26 +263,7 @@ function register_cooking_pot(ctx: ServerContext) {
return true;
}
const screen = ctx.ui.open_container(player, {
container,
layout: [
...Array.from(
{ length: INGREDIENT_SLOTS },
(_, slot) => ({ slot, x: 1.2 + slot % 3, y: Math.floor(slot / 3) }),
),
// the meal stays in the pot until it's served
{ slot: MEAL_SLOT, x: 6.4, y: 0.5, output_only: true, on_take: () => false },
{ slot: CONTAINER_SLOT, x: 4.6, y: 2 },
{ slot: OUTPUT_SLOT, x: 6.4, y: 2, output_only: true },
],
rows: 3,
bars: POT_BARS,
});
show(screen, data, is_heated(block));
const screens = viewers.get(key(block)) ?? new Set();
viewers.set(key(block), screens);
screens.add(screen);
screen.on_close(() => screens.delete(screen));
menu_type.open(player, block);
return true;
},
on_break(block) {
@@ -324,8 +293,6 @@ function register_cooking_pot(ctx: ServerContext) {
data.cook_time = Math.max(0, data.cook_time - 2);
}
serve(data, container);
for (const screen of viewers.get(key(block)) ?? []) show(screen, data, heated);
},
});
}