Smithing table

This commit is contained in:
2026-09-25 23:48:29 -03:00
parent 9237152aa1
commit 8f32a59bd6
15 changed files with 279 additions and 13 deletions
+3 -2
View File
@@ -12,7 +12,7 @@ import {
TICKS_PER_SECOND,
} from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { click_slot, Container, ItemData, ItemStack, take_output } from "$/common/inventory.ts";
import { can_take_output, click_slot, Container, ItemData, ItemStack, take_output } from "$/common/inventory.ts";
import {
AIR_ID,
BlockChange,
@@ -744,7 +744,8 @@ export class GameServer {
}
} else if (key === "screen" && player.screen?.layout.slots.find((s) => s.index === index)?.output) {
const item = container.get_item(index);
if (item && take_output(item, player.cursor)) {
const allowed = (item: ItemStack) => player.screen?.on_take.get(index)?.(item) ?? true;
if (item && can_take_output(item, player.cursor) && allowed(item) && take_output(item, player.cursor)) {
container.set_item(index, undefined);
}
} else if (
+19 -2
View File
@@ -391,6 +391,12 @@ export class ModRuntime {
fuel_value: (item) => game().recipes.fuel.get(item) ?? 0,
is_fuel: (item) => game().recipes.fuel.has(item),
is_smeltable: (item) => game().recipes.furnace.has(item),
smithing: (tool, material, addition) => {
const recipe = game().recipes.smithing.find((r) =>
r.tool === tool && r.material.id === material && r.addition === addition
);
return recipe && { result: recipe.result, material_count: recipe.material.count };
},
},
containers: {
create: (size) => {
@@ -442,7 +448,11 @@ export class ModRuntime {
}
const filters = new Map<number, (item: EngineItemStack) => boolean>();
for (const { slot, filter } of options.layout) {
const on_take = new Map<number, (item: EngineItemStack) => boolean>();
for (const { slot, filter, on_take: take } of options.layout) {
if (take) {
on_take.set(slot, (item) => run_guarded(mod, "an on_take", () => take(item_api(item))) === true);
}
if (!Number.isInteger(slot) || slot < 0 || slot >= container.size) {
throw new Error(`slot ${slot} isn't in the container, it has ${container.size}`);
}
@@ -474,7 +484,14 @@ export class ModRuntime {
};
let open = true;
const screen: OpenScreen = { container, layout, properties: {}, filters, on_close: [() => open = false] };
const screen: OpenScreen = {
container,
layout,
properties: {},
filters,
on_take,
on_close: [() => open = false],
};
game.open_screen(server_player, screen);
return {
+2
View File
@@ -12,6 +12,8 @@ export interface OpenScreen {
properties: Record<string, number>;
// what can go into each slot, checked when the player clicks
filters: Map<number, (item: ItemStack) => boolean>;
// asked before a player takes from an output slot
on_take: Map<number, (item: ItemStack) => boolean>;
on_close: (() => void)[];
}