205 lines
9.1 KiB
TypeScript
205 lines
9.1 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// curios: accessory slots for items in the curios tags, next to the player's inventory, see
|
|
// mods/bworld/scripts/curios.ts
|
|
import { assert, assertEquals } from "@std/assert";
|
|
import { PLAYER_MENU_EXTRA, PROTOCOL_VERSION } from "@/common/protocol.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>>;
|
|
|
|
// 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_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const write = (path: string, json: unknown) => {
|
|
Deno.mkdirSync(`${dir}/jewelry/${path}`.replace(/\/[^/]+$/, ""), { recursive: true });
|
|
Deno.writeTextFileSync(`${dir}/jewelry/${path}`, JSON.stringify(json));
|
|
};
|
|
write("manifest.json", {
|
|
format_version: 1,
|
|
id: "jewelry",
|
|
name: "Jewelry",
|
|
version: "1.0.0",
|
|
dependencies: [{ id: "bworld", version: "*" }],
|
|
});
|
|
// the ring shows on the player's right arm, the amulet doesn't show
|
|
const worn = { model: "jewelry:ring_worn", texture: "bworld:coal" };
|
|
write("items/ring.json", { format_version: 1, item: { id: "jewelry:ring", texture: "bworld:coal", worn } });
|
|
write("models/ring_worn.geo.json", {
|
|
format_version: "1.12.0",
|
|
"minecraft:geometry": [{
|
|
description: { identifier: "geometry.ring", texture_width: 16, texture_height: 16 },
|
|
bones: [{
|
|
name: "right_arm",
|
|
pivot: [-5, 22, 0],
|
|
cubes: [{ origin: [-8, 12, -2], size: [4, 1, 4], uv: [0, 0] }],
|
|
}],
|
|
}],
|
|
});
|
|
write("items/amulet.json", { format_version: 1, item: { id: "jewelry:amulet", texture: "bworld:coal" } });
|
|
write("tags/items/bworld/curios/ring.json", { format_version: 1, tag: { values: ["jewelry:ring"] } });
|
|
write("tags/items/bworld/curios/curio.json", { format_version: 1, tag: { values: ["jewelry:amulet"] } });
|
|
|
|
const t = await test_game(dir, save);
|
|
t.join(1, "alice");
|
|
for (const id of ["jewelry:ring", "jewelry:amulet", "bworld:coal"]) {
|
|
t.send(1, { type: "chat", text: `/give ${id}` });
|
|
}
|
|
t.take(1);
|
|
return t;
|
|
}
|
|
|
|
// 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[]) => 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) {
|
|
const index = t.game.players()[0].inventory_menu.extra.findIndex((s) => s.icon === `bworld:empty_${type}_slot`);
|
|
assert(index >= 0, `no ${type} slot`);
|
|
return index;
|
|
}
|
|
|
|
Deno.test("the slot types some item goes in are in 2 columns left of the inventory, with their icons", async () => {
|
|
const t = await setup();
|
|
t.game.on_disconnect(1);
|
|
const join = t.join(1, "alice");
|
|
const icons = join.player_menu.map((slot: Msg) => slot.icon);
|
|
assert(icons.indexOf("bworld:empty_curio_slot") < icons.indexOf("bworld:empty_ring_slot"), "in curios' order");
|
|
assert(!icons.includes("bworld:empty_charm_slot"), "nothing goes in it");
|
|
join.player_menu.forEach((slot: Msg, i: number) => {
|
|
assertEquals([slot.x, slot.y], [-45 + (i % 2) * 18, 7 + Math.floor(i / 2) * 18]);
|
|
});
|
|
});
|
|
|
|
Deno.test("items only go in the slots they fit, and fall out when the player dies", async () => {
|
|
const t = await setup();
|
|
|
|
// coal fits nowhere so it moves to the rest of the inventory, the ring only goes in the ring slot, and
|
|
// #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(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 });
|
|
|
|
// and they come back out with a shift click, into the inventory
|
|
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_EXTRA + ring, button: 0, shift: true });
|
|
assertEquals(curios_items(t.take(1))[ring], null);
|
|
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
|
|
|
|
const player = t.game.players()[0];
|
|
t.game.damage_player(player, 100, "fall");
|
|
const dropped = t.game.item_entities().map((e) => e.item.type_id).sort();
|
|
assertEquals(dropped, ["bworld:coal", "jewelry:amulet", "jewelry:ring"]);
|
|
t.game.tick();
|
|
assert(curios_items(t.take(1)).every((item: Msg) => item === null));
|
|
});
|
|
|
|
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 = 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
|
|
t.game.tick();
|
|
const body = curio_slot(t, "body");
|
|
assertEquals(curios_items(t.take(1))[body], {
|
|
id: "bworld:giant_chestplate",
|
|
count: 1,
|
|
lore: "When worn:\n+20 Max Health\nAfter 10s unhurt: Regeneration III",
|
|
});
|
|
assertEquals(alice.max_health, 40);
|
|
|
|
// like minecraft, more max health doesn't heal, regenerating fills it
|
|
assertEquals(alice.health, 20);
|
|
for (let i = 0; i < 20 * 20; i++) t.game.tick();
|
|
assert(alice.health > 20, `regenerated to ${alice.health}`);
|
|
const vitals = t.take(1).findLast((m: Msg) => m.type === "vitals");
|
|
assertEquals(vitals.max_health, 40);
|
|
|
|
// it's kept over rejoining, the chestplate's health comes back before the health is checked against it
|
|
const health = alice.health;
|
|
t.game.on_disconnect(1);
|
|
t.join(1, "alice");
|
|
assertEquals(t.game.players()[0].health, health);
|
|
|
|
// taking it off takes the health over 20 away
|
|
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_EXTRA + body, button: 0, shift: true });
|
|
t.game.tick();
|
|
assertEquals(t.game.players()[0].max_health, 20);
|
|
assertEquals(t.game.players()[0].health, 20);
|
|
});
|
|
|
|
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 = 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");
|
|
const regeneration = () => alice.get_effect("bworld:regeneration");
|
|
|
|
for (let i = 0; i < 199; i++) t.game.tick();
|
|
assertEquals(regeneration(), undefined);
|
|
t.game.tick();
|
|
assertEquals(regeneration()?.amplifier, 2);
|
|
// it keeps being topped up while they stay unhurt
|
|
for (let i = 0; i < 200; i++) t.game.tick();
|
|
assertEquals(regeneration()?.amplifier, 2);
|
|
|
|
// being hurt starts the 10 seconds again, what they were given runs out
|
|
t.game.damage_player(alice, 2, "test");
|
|
for (let i = 0; i < 199; i++) t.game.tick();
|
|
assertEquals(regeneration(), undefined);
|
|
t.game.tick();
|
|
assertEquals(regeneration()?.amplifier, 2);
|
|
});
|
|
|
|
Deno.test("everyone sees what a player wears that has a worn model, and players joining later too", async () => {
|
|
const t = await setup();
|
|
const alice = t.game.players()[0].id;
|
|
t.join(2, "bob");
|
|
const worn_messages = (conn: number) => t.take(conn).filter((m: Msg) => m.type === "player_worn");
|
|
|
|
// the ring (given first) and the amulet, only the ring has a worn model
|
|
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
|
|
t.send(1, { type: "click", menu: 0, slot: 1, button: 0, shift: true });
|
|
t.game.tick();
|
|
const ring = [{ id: alice, worn: [{ id: "jewelry:ring", count: 1 }] }];
|
|
for (const conn of [1, 2]) {
|
|
assertEquals(worn_messages(conn).map(({ id, worn }: Msg) => ({ id, worn })), ring);
|
|
}
|
|
t.game.tick();
|
|
assertEquals(worn_messages(2), [], "only sent when it changes");
|
|
|
|
// someone joining later gets it with the players already there
|
|
const carol = t.join(3, "carol");
|
|
assertEquals(carol.players.find((p: Msg) => p.id === alice).worn, ring[0].worn);
|
|
|
|
// rejoining wears it again: alice is told about herself, the others with player_join
|
|
t.game.on_disconnect(1);
|
|
t.take(2);
|
|
t.game.on_connect(1);
|
|
t.send(1, { type: "hello", name: "alice", protocol: PROTOCOL_VERSION });
|
|
t.send(1, { type: "ready" });
|
|
const messages = t.take(1);
|
|
const rejoined = messages.find((m: Msg) => m.type === "join");
|
|
assertEquals(messages.filter((m: Msg) => m.type === "player_worn").map((m: Msg) => m.worn), [ring[0].worn]);
|
|
const joined = t.take(2).find((m: Msg) => m.type === "player_join");
|
|
assertEquals(joined.player.worn, ring[0].worn);
|
|
|
|
// and taking it off
|
|
const ring_slot = PLAYER_MENU_EXTRA + curio_slot(t, "ring");
|
|
t.send(1, { type: "click", menu: 0, slot: ring_slot, button: 0, shift: true });
|
|
t.game.tick();
|
|
assertEquals(worn_messages(2).map((m: Msg) => [m.id, m.worn]), [[rejoined.id, []]]);
|
|
});
|