Files
bworld/tests/shift_click_test.ts
2026-11-04 21:51:26 -03:00

113 lines
5.6 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// 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 { container_items, menu_items, opened_menu, stone_floor, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
// 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");
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);
return t;
}
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 = 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 = 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);
});
Deno.test("shift clicking tops up stacks of the same item before using empty slots", async () => {
const t = await setup([["bworld:coal", 20]]);
// half of it to the main inventory by hand, then the other half shift clicked onto it
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 = container_items(t.take(1), "inventory");
assertEquals(inventory[0], null);
assertEquals(inventory[9], { id: "bworld:coal", count: 20 });
assertEquals(inventory[10], null);
});
Deno.test("with a chest open, shift clicking moves between it and the inventory", async () => {
const t = await setup([["bworld:coal", 10]]);
t.game.set_block(1, 100, 0, "bworld:chest");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const { id } = opened_menu(t.take(1));
// 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(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(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 () => {
const t = await setup([["bworld:iron_ore", 1], ["bworld:coal", 1], ["bworld:stick", 1]]);
t.game.set_block(1, 100, 0, "bworld:furnace");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const { id } = opened_menu(t.take(1));
// the ore goes in the input, the coal in the fuel slot, and sticks fit neither so they stay. the furnace's 3 slots
// come before the inventory's
for (const index of [0, 1, 2]) t.send(1, { type: "click", menu: id, slot: 3 + index, button: 0, shift: true });
let messages = t.take(1);
assertEquals(menu_items(messages).slice(0, 3), [
{ id: "bworld:iron_ore", count: 1 },
{ id: "bworld:coal", count: 1 },
null,
]);
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(container_items(messages, "inventory")[8], { id: "bworld:iron_ingot", count: 1 });
});
Deno.test("shift clicking the crafting result crafts as many as fit", async () => {
const t = await setup([["bworld:oak_log", 3]]);
// the logs into the grid, then the result shift clicked: 3 crafts of 2 planks
t.send(1, { type: "click", menu: 0, slot: 0, button: 0 });
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(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 () => {
const t = await setup([["bworld:oak_log", 3]]);
t.send(1, { type: "click", menu: 0, slot: 0, button: 0 });
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(container_items(messages, "crafting").every((item: Msg) => item === null), true);
assertEquals(container_items(messages, "inventory")[9], { id: "bworld:oak_log", count: 3 });
});