Remove duplicate code

This commit is contained in:
2026-10-13 11:57:43 -03:00
parent dfc6a6d77b
commit 5165698fe3
50 changed files with 307 additions and 812 deletions
+2 -5
View File
@@ -5,10 +5,7 @@
import { assertEquals } from "@std/assert";
import { ATTRIBUTES } from "$/server/game/entity/attributes.ts";
import { PLAYER_MENU_EXTRA } from "$/common/protocol.ts";
import { test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
import { container_items, test_game } from "./helpers.ts";
async function setup() {
const t = await test_game("mods");
@@ -39,7 +36,7 @@ Deno.test("the power glove and feral claws go in the hands slot, and only work w
Deno.test("worn artifacts work again after rejoining, and say what they do", async () => {
const t = await setup();
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
const worn = t.take(1).findLast((m: Msg) => m.type === "container" && m.container === "extra").items;
const worn = container_items(t.take(1), "extra");
assertEquals(worn[t.hands - PLAYER_MENU_EXTRA].lore, "When worn:\n+4 Attack Damage");
t.game.on_disconnect(1);
+9 -14
View File
@@ -6,22 +6,17 @@ 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";
import { container_items, menu_data, menu_items, opened_menu, stone_floor, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
type Game = Awaited<ReturnType<typeof test_game>>;
const last_container = (messages: Msg[], container: string) =>
messages.filter((m) => m.type === "container" && m.container === container).at(-1)?.items;
// a player standing on a stone floor at y 99, high above the terrain, with the items given in their inventory
async function setup(items: [string, number][] = []) {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
stone_floor(t.game);
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` });
return t;
@@ -29,7 +24,7 @@ async function setup(items: [string, number][] = []) {
// the inventory slot holding an item, from what the server sent last
function slot_of(messages: Msg[], id: string) {
const slot = (last_container(messages, "inventory") ?? []).findIndex((i: Msg) => i?.id === id);
const slot = (container_items(messages, "inventory") ?? []).findIndex((i: Msg) => i?.id === id);
assert(slot >= 0, `no ${id} in the inventory`);
return slot;
}
@@ -109,7 +104,7 @@ Deno.test("a shipping bin sells what's in it every morning for as few coins as i
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 inventory = container_items(messages, "inventory");
const planks = inventory.flatMap((item: Msg, slot: number) => item?.id === "bworld:oak_planks" ? [slot] : []);
const copper = slot_of(messages, "bworld:copper_coin");
@@ -190,7 +185,7 @@ Deno.test("bone meal grows wheat until it's fully grown", async () => {
assertEquals(age(), 1);
for (let i = 0; i < 7; i++) t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
assertEquals(age(), 7);
assertEquals(last_container(t.take(1), "inventory")[slot]?.count, 1);
assertEquals(container_items(t.take(1), "inventory")[slot]?.count, 1);
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air");
});
@@ -200,12 +195,12 @@ Deno.test("the last bone meal is used up", async () => {
const slot = slot_of(t.take(1), "bworld:bone_meal");
t.send(1, { type: "select_slot", slot });
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
assertEquals(last_container(t.take(1), "inventory")[slot], null);
assertEquals(container_items(t.take(1), "inventory")[slot], null);
});
Deno.test("a new watering can starts empty, and players are told so in its lore", async () => {
const t = await setup([["bworld:copper_watering_can", 1]]);
const can = last_container(t.take(1), "inventory").find((i: Msg) => i?.id === "bworld:copper_watering_can");
const can = container_items(t.take(1), "inventory").find((i: Msg) => i?.id === "bworld:copper_watering_can");
assertEquals(can.data, { water: 0, max_water: 32 });
assertEquals(can.lore, "Water: 0/32");
@@ -259,7 +254,7 @@ Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone, using up exa
// closing gives back the leftover stone, and the cursor's pickaxe goes into the inventory
t.send(1, { type: "close_menu", menu: opened.id });
const inventory = last_container(t.take(1), "inventory").filter((i: Msg) => i);
const inventory = container_items(t.take(1), "inventory").filter((i: Msg) => i);
assertEquals(
inventory.sort((a: Msg, b: Msg) => a.id.localeCompare(b.id)),
[{ id: "bworld:stone", count: 2 }, { id: "bworld:stone_pickaxe", count: 1 }],
@@ -339,7 +334,7 @@ Deno.test("a stonecutter cuts its input into the recipe picked with a button, on
assertEquals(menu_data(latest).selected, -1);
assertEquals(menu_items(latest)[1], null);
t.send(1, { type: "close_menu", menu: opened.id });
const inventory = last_container(t.take(1), "inventory").filter((i: Msg) => i);
const inventory = container_items(t.take(1), "inventory").filter((i: Msg) => i);
assertEquals(
inventory.map((i: Msg) => i.id).sort(),
["bworld:cobblestone", "bworld:dirt", result.id].sort(),
+2 -1
View File
@@ -3,7 +3,8 @@
import { assertEquals } from "@std/assert";
import { join } from "@std/path";
import { CLIENT_MANIFEST, client_manifest, sha256, update_client } from "$/desktop/client_updates.ts";
import { CLIENT_MANIFEST, client_manifest, update_client } from "$/desktop/client_updates.ts";
import { sha256 } from "$/common/utils.ts";
const PROTOCOL = 7;
+2 -4
View File
@@ -7,7 +7,7 @@ import { ItemStack } from "$/common/inventory.ts";
import { bake_model } from "$/common/block_models.ts";
import { validate_recipe } from "$/common/mod_data.ts";
import { get_state_value, set_state_value } from "$/common/utils.ts";
import { test_game } from "./helpers.ts";
import { stone_floor, test_game } from "./helpers.ts";
const POT = [0, 101, 0] as const;
const FURNACE = [0, 100, 0] as const;
@@ -15,9 +15,7 @@ const FURNACE = [0, 100, 0] as const;
async function setup() {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
stone_floor(t.game);
t.send(1, { type: "move", x: 2.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.game.set_block(...FURNACE, "bworld:furnace");
t.game.set_block(...POT, "farmersdelight:cooking_pot");
+6 -9
View File
@@ -5,16 +5,13 @@
// mods/bworld/scripts/curios.ts
import { assert, assertEquals } from "@std/assert";
import { PLAYER_MENU_EXTRA, PROTOCOL_VERSION } from "$/common/protocol.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { container_items, copy_dir, test_game } from "./helpers.ts";
import type { WorldSave } from "$/server/game/saves.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
type Game = Awaited<ReturnType<typeof test_game>>;
const last_container = (messages: Msg[], container: string) =>
messages.filter((m) => m.type === "container" && m.container === container).at(-1)?.items;
// the base game, and a mod with a ring and an amulet that goes in any slot
async function setup(save?: WorldSave) {
const dir = Deno.makeTempDirSync({ prefix: "bworld_curios_" });
@@ -58,7 +55,7 @@ async function setup(save?: WorldSave) {
}
// what's in the curios slots, the ones the base game added to the player's own menu, as last synced
const curios_items = (messages: Msg[]) => last_container(messages, "extra");
const curios_items = (messages: Msg[]) => container_items(messages, "extra");
// which of those is a slot type, found by its icon. which types there are depends on what items every mod has
function curio_slot(t: Game, type: string) {
@@ -86,8 +83,8 @@ Deno.test("items only go in the slots they fit, and fall out when the player die
// #bworld:curios/curio items in any slot, the curio slot first
for (const slot of [2, 0, 1]) t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
const messages = t.take(1);
assertEquals(last_container(messages, "inventory").slice(0, 3), [null, null, null]);
assertEquals(last_container(messages, "inventory")[9], { id: "bworld:coal", count: 1 });
assertEquals(container_items(messages, "inventory").slice(0, 3), [null, null, null]);
assertEquals(container_items(messages, "inventory")[9], { id: "bworld:coal", count: 1 });
const [curio, ring] = [curio_slot(t, "curio"), curio_slot(t, "ring")];
assertEquals(curios_items(messages)[curio], { id: "jewelry:amulet", count: 1 });
assertEquals(curios_items(messages)[ring], { id: "jewelry:ring", count: 1 });
@@ -125,7 +122,7 @@ Deno.test("curios worn when curios was a mod of its own are still worn", async (
Deno.test("the giant chestplate goes in the body slot and gives 20 more health while worn", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give bworld:giant_chestplate" });
const slot = last_container(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
const slot = container_items(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
const alice = t.game.players()[0];
t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
// what curios give is worked out each tick, from what changed
@@ -161,7 +158,7 @@ Deno.test("the giant chestplate goes in the body slot and gives 20 more health w
Deno.test("the giant chestplate gives regeneration III after 10 seconds without being hurt", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give bworld:giant_chestplate" });
const slot = last_container(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
const slot = container_items(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
const alice = t.game.players()[0];
t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
t.game.damage_player(alice, 2, "test");
+2 -4
View File
@@ -4,16 +4,14 @@
// farmer's delight's cutting board: holds a stack of anything, shows it on top, and a knife cuts it one at a time
import { assert, assertEquals } from "@std/assert";
import { get_state_value } from "$/common/utils.ts";
import { test_game } from "./helpers.ts";
import { stone_floor, test_game } from "./helpers.ts";
const BOARD = [0, 100, 0] as const;
async function setup(yaw = 0) {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
stone_floor(t.game);
t.send(1, { type: "move", x: 2.5, y: 100, z: 2.5, yaw, pitch: 0 });
t.send(1, { type: "chat", text: "/give farmersdelight:cutting_board" });
t.send(1, { type: "select_slot", slot: 0 });
+3 -6
View File
@@ -7,7 +7,7 @@ import { assert, assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { CYCLE_TICKS } from "$/common/time.ts";
import { get_state_value } from "$/common/utils.ts";
import { test_game } from "./helpers.ts";
import { container_items, stone_floor, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
@@ -16,13 +16,10 @@ type Msg = any;
async function setup(items: string[] = []) {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
stone_floor(t.game);
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
for (const item of items) t.send(1, { type: "chat", text: `/give ${item}` });
const inventory = () =>
t.take(1).filter((m) => m.type === "container" && m.container === "inventory").at(-1)?.items ?? [];
const inventory = () => container_items(t.take(1), "inventory") ?? [];
const hold = (id: string) => {
const slot = inventory_now().findIndex((i: Msg) => i?.id === id);
assert(slot >= 0 && slot < 9, `${id} isn't in the hotbar`);
+2 -4
View File
@@ -10,7 +10,7 @@ import { collision_shape_at, covers_face, FULL_SHAPE, shape_at } from "$/common/
import { move_body } from "$/common/physics.ts";
import { get_state_value } from "$/common/utils.ts";
import { update_crafting_result } from "$/server/game/crafting.ts";
import { test_game } from "./helpers.ts";
import { stone_floor, test_game } from "./helpers.ts";
const Y = 100;
@@ -108,9 +108,7 @@ Deno.test("six planks and two sticks make three fences of that wood", async () =
Deno.test("clicking the ground beside a fence tells the client it's still joined, not a bare post", async () => {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
stone_floor(t.game);
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.game.tick();
t.game.set_block(0, 100, 0, "bworld:oak_fence");
+6 -8
View File
@@ -6,6 +6,7 @@
import { AIR, ID_MASK } from "$/common/constants.ts";
import type { RawChunk } from "$/common/generation.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { sha256 } from "$/common/utils.ts";
export const FIXTURE = new URL("./fixtures/worldgen_golden.json", import.meta.url);
export const SEEDS = ["golden-1", "golden-2", "golden-3"];
@@ -40,12 +41,12 @@ export function raw_hash(raw: RawChunk, name: (value: number) => string): Promis
for (let i = 0; i < raw.spills.length; i += 4) {
spills.push(raw.spills[i], raw.spills[i + 1], raw.spills[i + 2], name(raw.spills[i + 3]));
}
return sha256(blocks_text(raw.blocks, name), JSON.stringify(spills), raw.biomes.join(","));
return hash(blocks_text(raw.blocks, name), JSON.stringify(spills), raw.biomes.join(","));
}
// what the fixture's "final" is: the chunk with its neighbors' spills
export function final_hash(blocks: Uint32Array, name: (value: number) => string): Promise<string> {
return sha256(blocks_text(blocks, name));
return hash(blocks_text(blocks, name));
}
// the blocks as a palette of names in the order they first appear, then each block's place in it
@@ -63,7 +64,8 @@ function blocks_text(blocks: Uint32Array, name: (value: number) => string): (str
return [JSON.stringify([...palette.keys()].map(name)), new Uint8Array(indices.buffer)];
}
async function sha256(...parts: (string | Uint8Array | (string | Uint8Array)[])[]) {
// the sha-256 of the parts one after another, strings each on their own line
function hash(...parts: (string | Uint8Array | (string | Uint8Array)[])[]) {
const bytes = parts.flat().map((part) => typeof part === "string" ? new TextEncoder().encode(part + "\n") : part);
const all = new Uint8Array(bytes.reduce((sum, part) => sum + part.length, 0));
let offset = 0;
@@ -71,9 +73,5 @@ async function sha256(...parts: (string | Uint8Array | (string | Uint8Array)[])[
all.set(part, offset);
offset += part.length;
}
let hex = "";
for (const byte of new Uint8Array(await crypto.subtle.digest("SHA-256", all))) {
hex += byte.toString(16).padStart(2, "0");
}
return hex;
return sha256(all);
}
+13 -19
View File
@@ -4,6 +4,7 @@
import { resolve, toFileUrl } from "@std/path";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import { load_and_check, load_order } from "$/tools/check_mods.ts";
import { mod_data } from "$/common/mod_check.ts";
import type { ServerModSource } from "$/server/game/load_mods.ts";
import { GameServer } from "$/server/game/game_server.ts";
import { start_game } from "$/server/game/load_mods.ts";
@@ -41,25 +42,7 @@ function read_mod_sources(mods_dir: string): ServerModSource[] {
size: 0,
...(mod.manifest?.engine_access === true ? { engine_access: true } : {}),
},
data: {
blocks: mod.blocks.map((b) => b.json),
models: mod.models.map((m) => m.json),
items: mod.items.map((i) => i.json),
recipes: mod.recipes.map((r) => r.json),
ores: mod.ores.map((o) => o.json),
spawns: mod.spawns.map((s) => s.json),
entities: mod.entities.map((e) => e.json),
entity_models: mod.entity_models.map((m) => m.json),
effects: mod.effects.map((e) => e.json),
tags: mod.tags.map((t) => t.json),
loot_tables: mod.loot_tables.map((t) => t.json),
biomes: mod.biomes.map((b) => b.json),
noises: mod.noises.map((n) => n.json),
density_functions: mod.density_functions.map((d) => d.json),
noise_settings: mod.noise_settings.map((n) => n.json),
biome_sources: mod.biome_sources.map((b) => b.json),
features: mod.features.map((f) => f.json),
},
data: mod_data(mod),
server_url: url(scripts.server),
worldgen_url: url(scripts.worldgen),
shared_url: url(scripts.shared),
@@ -155,6 +138,13 @@ export function sent_block(messages: any[], join: { blocks: string[] }, x: numbe
return [join.blocks[value & ID_MASK], value >>> 16];
}
// a stone floor at y 99 from -radius to radius on x and z, high above the terrain, for players to stand on at y 100
export function stone_floor(game: GameServer, radius = 3) {
for (let x = -radius; x <= radius; x++) {
for (let z = -radius; z <= radius; z++) game.set_block(x, 99, z, "bworld:stone");
}
}
export function copy_dir(from: string, to: string) {
if (Deno.statSync(from).isDirectory) {
Deno.mkdirSync(to, { recursive: true });
@@ -174,5 +164,9 @@ export const opened_menu = (messages: Msg[]) => messages.findLast((m) => m.type
// what the open menu's slots last held among messages, by menu slot index
export const menu_items = (messages: Msg[]) => messages.findLast((m) => m.type === "menu_content")?.items;
// what a container, like "inventory", last held among messages
export const container_items = (messages: Msg[], container: string) =>
messages.findLast((m) => m.type === "container" && m.container === container)?.items;
// what the open menu's data last was among messages
export const menu_data = (messages: Msg[]) => messages.findLast((m) => m.type === "menu_data")?.data;
+4 -6
View File
@@ -4,7 +4,7 @@
import { assert, assertEquals } from "@std/assert";
import { CRAFTING_RESULT_SLOT, PLAYER_MENU_CRAFTING } from "$/common/protocol.ts";
import { ItemStack } from "$/common/inventory.ts";
import { test_game } from "./helpers.ts";
import { container_items, test_game } from "./helpers.ts";
// a stone floor at y 99 so drops have something to land on, high above the generated terrain
function build_floor(game: Awaited<ReturnType<typeof test_game>>["game"]) {
@@ -15,11 +15,9 @@ function build_floor(game: Awaited<ReturnType<typeof test_game>>["game"]) {
}
}
const inventory_count = (
messages: { type: string; container?: string; items?: ({ id: string; count: number } | null)[] }[],
id: string,
) => (messages.filter((m) => m.type === "container" && m.container === "inventory").at(-1)?.items ?? [])
.reduce((sum, item) => sum + (item?.id === id ? item.count : 0), 0);
const inventory_count = (messages: unknown[], id: string) =>
((container_items(messages, "inventory") ?? []) as ({ id: string; count: number } | null)[])
.reduce((sum, item) => sum + (item?.id === id ? item.count : 0), 0);
Deno.test("breaking a block drops its item, which lands and gets picked up by walking to it", async () => {
const { game, take, send, join } = await test_game("mods");
+2 -2
View File
@@ -6,7 +6,7 @@ import { assert, assertEquals, assertStringIncludes } from "@std/assert";
import { entity_model_from_geo, validate_entity } from "$/common/mod_data.ts";
import { Mob } from "$/server/game/entity/mob.ts";
import { create_entity } from "$/server/game/entity/entity_types.ts";
import { test_game } from "./helpers.ts";
import { container_items, test_game } from "./helpers.ts";
Deno.test("a bedrock geo.json becomes an entity model with only what the engine uses", () => {
const geo = JSON.parse(Deno.readTextFileSync("mods/bworld/models/chicken.geo.json"));
@@ -95,7 +95,7 @@ Deno.test("solid blocks can't be placed where a mob or player is", async () => {
game.set_block(1, 100, 1, "bworld:stone");
send(1, { type: "move", x: 3.5, y: 101, z: 3.5, yaw: 0, pitch: 0 });
send(1, { type: "chat", text: "/give bworld:stone 2" });
const inventory = take(1).filter((m) => m.type === "container" && m.container === "inventory").at(-1).items;
const inventory = container_items(take(1), "inventory");
send(1, { type: "select_slot", slot: inventory.findIndex((i: { id: string } | null) => i?.id === "bworld:stone") });
// the chicken's box pokes into the block above the stone, even from the one next to it
+2 -3
View File
@@ -10,7 +10,7 @@ import { block_variant } from "$/common/block_models.ts";
import { load_worldgen } from "$/common/worldgen_loader.ts";
import { create_mod } from "$/tools/new_mod.ts";
import { PLAYER_MENU_CRAFTING, PROTOCOL_VERSION } from "$/common/protocol.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { container_items, copy_dir, test_game } from "./helpers.ts";
// mods/bworld plus a mod made from the template, in a temp folder
function mods_with_template() {
@@ -22,8 +22,7 @@ function mods_with_template() {
// deno-lint-ignore no-explicit-any
type Msg = any;
const inventory_of = (messages: Msg[]) =>
messages.filter((m) => m.type === "container" && m.container === "inventory").at(-1)?.items ?? [];
const inventory_of = (messages: Msg[]) => container_items(messages, "inventory") ?? [];
Deno.test("the base game loads from mods/bworld", async () => {
const { game } = await test_game("mods");
+2 -4
View File
@@ -5,15 +5,13 @@
import { assert, assertEquals } from "@std/assert";
import { create_entity } from "$/server/game/entity/entity_types.ts";
import { Mob } from "$/server/game/entity/mob.ts";
import { test_game } from "./helpers.ts";
import { stone_floor, test_game } from "./helpers.ts";
// a stone floor with alice on it, and a rotwalker 3 blocks away
async function setup() {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -8; x <= 8; x++) {
for (let z = -8; z <= 8; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
stone_floor(t.game, 8);
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
const rotwalker = create_entity(t.game.entities, "undergarden:rotwalker") as Mob;
rotwalker.set_position(0.5, 100, 3.5);
+13 -18
View File
@@ -4,21 +4,16 @@
// shift clicking a slot moves its stack to the other part of the screen, like minecraft's quick move
import { assertEquals } from "@std/assert";
import { CRAFTING_RESULT_SLOT, PLAYER_MENU_CRAFTING } from "$/common/protocol.ts";
import { menu_items, opened_menu, test_game } from "./helpers.ts";
import { container_items, menu_items, opened_menu, stone_floor, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
const last_container = (messages: Msg[], container: string) =>
messages.filter((m) => m.type === "container" && m.container === container).at(-1)?.items;
// a player standing on a stone floor, with the items given in their inventory from the first hotbar slot on
async function setup(items: [string, number][] = []) {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
stone_floor(t.game);
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` });
t.take(1);
@@ -28,13 +23,13 @@ async function setup(items: [string, number][] = []) {
Deno.test("without a container open, shift clicking moves between the hotbar and the rest of the inventory", async () => {
const t = await setup([["bworld:coal", 10], ["bworld:stick", 5]]);
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
let inventory = last_container(t.take(1), "inventory");
let inventory = container_items(t.take(1), "inventory");
assertEquals(inventory[0], null);
assertEquals(inventory[9], { id: "bworld:coal", count: 10 });
// back down it goes to the first free hotbar slot, the sticks are in the second
t.send(1, { type: "click", menu: 0, slot: 9, button: 0, shift: true });
inventory = last_container(t.take(1), "inventory");
inventory = container_items(t.take(1), "inventory");
assertEquals(inventory[0], { id: "bworld:coal", count: 10 });
assertEquals(inventory[1], { id: "bworld:stick", count: 5 });
assertEquals(inventory[9], null);
@@ -46,7 +41,7 @@ Deno.test("shift clicking tops up stacks of the same item before using empty slo
t.send(1, { type: "click", menu: 0, slot: 0, button: 2 });
t.send(1, { type: "click", menu: 0, slot: 9, button: 0 });
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
const inventory = last_container(t.take(1), "inventory");
const inventory = container_items(t.take(1), "inventory");
assertEquals(inventory[0], null);
assertEquals(inventory[9], { id: "bworld:coal", count: 20 });
assertEquals(inventory[10], null);
@@ -61,14 +56,14 @@ Deno.test("with a chest open, shift clicking moves between it and the inventory"
// the chest's 27 slots, then the inventory's
t.send(1, { type: "click", menu: id, slot: 27, button: 0, shift: true });
let messages = t.take(1);
assertEquals(last_container(messages, "inventory")[0], null);
assertEquals(container_items(messages, "inventory")[0], null);
assertEquals(menu_items(messages)[0], { id: "bworld:coal", count: 10 });
// like minecraft, out of a container it fills the hotbar from the right
t.send(1, { type: "click", menu: id, slot: 0, button: 0, shift: true });
messages = t.take(1);
assertEquals(menu_items(messages)[0], null);
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:coal", count: 10 });
assertEquals(container_items(messages, "inventory")[8], { id: "bworld:coal", count: 10 });
});
Deno.test("shift clicking into a furnace goes by its slot filters, and takes the result out", async () => {
@@ -86,13 +81,13 @@ Deno.test("shift clicking into a furnace goes by its slot filters, and takes the
{ id: "bworld:coal", count: 1 },
null,
]);
assertEquals(last_container(messages, "inventory")[2], { id: "bworld:stick", count: 1 });
assertEquals(container_items(messages, "inventory")[2], { id: "bworld:stick", count: 1 });
for (let i = 0; i < 210; i++) t.game.tick();
t.send(1, { type: "click", menu: id, slot: 2, button: 0, shift: true });
messages = t.take(1);
assertEquals(menu_items(messages)[2], null);
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:iron_ingot", count: 1 });
assertEquals(container_items(messages, "inventory")[8], { id: "bworld:iron_ingot", count: 1 });
});
Deno.test("shift clicking the crafting result crafts as many as fit", async () => {
@@ -102,8 +97,8 @@ Deno.test("shift clicking the crafting result crafts as many as fit", async () =
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING, button: 0 });
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING + CRAFTING_RESULT_SLOT, button: 0, shift: true });
const messages = t.take(1);
assertEquals(last_container(messages, "crafting").every((item: Msg) => item === null), true);
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:oak_planks", count: 6 });
assertEquals(container_items(messages, "crafting").every((item: Msg) => item === null), true);
assertEquals(container_items(messages, "inventory")[8], { id: "bworld:oak_planks", count: 6 });
});
Deno.test("shift clicking the crafting grid puts its items back in the inventory", async () => {
@@ -112,6 +107,6 @@ Deno.test("shift clicking the crafting grid puts its items back in the inventory
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING + 4, button: 0 });
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING + 4, button: 0, shift: true });
const messages = t.take(1);
assertEquals(last_container(messages, "crafting").every((item: Msg) => item === null), true);
assertEquals(last_container(messages, "inventory")[9], { id: "bworld:oak_log", count: 3 });
assertEquals(container_items(messages, "crafting").every((item: Msg) => item === null), true);
assertEquals(container_items(messages, "inventory")[9], { id: "bworld:oak_log", count: 3 });
});