Remove duplicate code
This commit is contained in:
+13
-18
@@ -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 });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user