Fluid containers
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// items that hold one fluid, full or empty: how many stack, what they look like, and turning the held one into another
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { item_tooltip } from "$/client/gui/tooltip.ts";
|
||||
import { item_layers } from "$/client/rendering/item_renderer.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
const JAR = "testjars:jar";
|
||||
const FULL = { fluid: { id: "testjars:syrup", data: { tree: "maple" } } };
|
||||
|
||||
// mods/bworld, and testjars: syrup, and a jar of 250 mB that stacks to 16 empty and 4 full, drawn with the syrup in it
|
||||
// and a red label
|
||||
async function setup() {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_jars_" });
|
||||
copy_dir("mods/bworld", `${dir}/bworld`);
|
||||
const write = (path: string, json: unknown) => {
|
||||
Deno.mkdirSync(`${dir}/testjars/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
|
||||
Deno.writeTextFileSync(`${dir}/testjars/${path}`, JSON.stringify(json));
|
||||
};
|
||||
write("manifest.json", { format_version: 1, id: "testjars", name: "Test Jars", version: "1.0.0" });
|
||||
write("fluids/syrup.json", { format_version: 1, fluid: { id: "testjars:syrup", color: "#a0522d" } });
|
||||
write("items/jar.json", {
|
||||
format_version: 1,
|
||||
item: {
|
||||
id: JAR,
|
||||
texture: "engine:missing",
|
||||
max_stack: 16,
|
||||
fluid_container: { capacity: 250, full_max_stack: 4 },
|
||||
layers: [{ texture: "engine:break_0", tint: "fluid" }, { texture: "engine:break_1", tint: "#ff0000" }],
|
||||
},
|
||||
});
|
||||
const t = await test_game(dir);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
t.join(1, "alice");
|
||||
const player = t.game.players()[0];
|
||||
return { ...t, player, api: t.game.mods.player(player) };
|
||||
}
|
||||
|
||||
const slots = (t: Awaited<ReturnType<typeof setup>>) =>
|
||||
t.player.inventory.to_data().flatMap((item, slot) => item ? [[slot, item.count, item.data !== undefined]] : []);
|
||||
|
||||
Deno.test("a full fluid container stacks to its full_max_stack, and an empty one to the item's max_stack", async () => {
|
||||
const t = await setup();
|
||||
t.api.give_item(JAR, 20);
|
||||
t.api.give_item(JAR, 6, FULL);
|
||||
assertEquals(slots(t), [[0, 16, false], [1, 4, false], [2, 4, true], [3, 2, true]]);
|
||||
// the fluid's name, from its id since it has none, and how much there is
|
||||
const tooltip = (slot: number) => item_tooltip(t.player.inventory.get_item(slot)!).map((line) => line.text);
|
||||
assertEquals(tooltip(2).slice(0, 2), ["Jar", "Syrup, 250 mB"]);
|
||||
assertEquals(tooltip(0), ["Jar"]);
|
||||
});
|
||||
|
||||
Deno.test("a layer tinted fluid is drawn in the fluid's color while there's one, and others always", async () => {
|
||||
await setup();
|
||||
const jar = (data?: unknown) => {
|
||||
const stack = new ItemStack(JAR);
|
||||
stack.data = data;
|
||||
return item_layers(stack);
|
||||
};
|
||||
const red = { texture: "engine:break_1", color: [1, 0, 0] };
|
||||
assertEquals(jar(), [red]);
|
||||
assertEquals(jar(FULL), [{ texture: "engine:break_0", color: [0xa0 / 255, 0x52 / 255, 0x2d / 255] }, red]);
|
||||
});
|
||||
|
||||
Deno.test("exchange_held puts the result in the hand from a stack of one, and in the inventory from more", async () => {
|
||||
const t = await setup();
|
||||
t.api.give_item(JAR, 3);
|
||||
t.api.exchange_held({ id: JAR, count: 1, data: FULL });
|
||||
assertEquals(slots(t), [[0, 2, false], [1, 1, true]]);
|
||||
|
||||
t.player.inventory.set_item(0, undefined);
|
||||
t.api.give_item(JAR, 1);
|
||||
t.api.exchange_held({ id: JAR, count: 1, data: FULL });
|
||||
// the filled one in the hand, the one filled before still beside it
|
||||
assertEquals(slots(t), [[0, 1, true], [1, 1, true]]);
|
||||
});
|
||||
@@ -8,7 +8,8 @@ import type { ModListing } from "$/common/mod_loader.ts";
|
||||
import type { FluidApi } from "$/common/mod_api/shared.ts";
|
||||
import { read_bmod } from "$/common/bmod.ts";
|
||||
import { register_mod_data } from "$/common/mod_loader.ts";
|
||||
import { fluid_rules, load_client_mods } from "$/client/mods.ts";
|
||||
import { load_client_mods } from "$/client/mods.ts";
|
||||
import { fluid_rules } from "$/client/fluid_rules.ts";
|
||||
import { load_and_check } from "$/tools/check_mods.ts";
|
||||
import { pack_mod } from "$/tools/pack_mod.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
+10
-2
@@ -372,7 +372,7 @@ Deno.test("cubes stay solid, and models that can't be drawn are placeholders", (
|
||||
assertStringIncludes(report.warnings[0].warning, "placeholder");
|
||||
});
|
||||
|
||||
Deno.test("items that aren't a block's get their sprite as their texture, or their model when java draws them in 3D", () => {
|
||||
Deno.test("items that aren't a block's get their sprite as their texture and layers, or their model when java draws them in 3D", () => {
|
||||
const output = Deno.makeTempDirSync();
|
||||
const report = import_items(
|
||||
assets({
|
||||
@@ -380,6 +380,10 @@ Deno.test("items that aren't a block's get their sprite as their texture, or the
|
||||
"models/block/gear": GEAR,
|
||||
"models/item/gear": { parent: "gears:block/gear" },
|
||||
"models/item/sheet": { parent: "item/generated", textures: { layer0: "gears:item/sheet" } },
|
||||
"models/item/trimmed_sheet": {
|
||||
parent: "item/generated",
|
||||
textures: { layer0: "gears:item/sheet", layer1: "gears:item/trim" },
|
||||
},
|
||||
"models/item/wrench": { parent: "gears:item/wrench/item" },
|
||||
"models/item/wrench/item": { textures: { handle: "gears:item/wrench" }, elements: GEAR.elements },
|
||||
}),
|
||||
@@ -387,10 +391,14 @@ Deno.test("items that aren't a block's get their sprite as their texture, or the
|
||||
);
|
||||
assertEquals(report.errors, []);
|
||||
// the gear's item comes with its block, and the wrench's model parts aren't items
|
||||
assertEquals(report.written.map(({ id }) => id).sort(), ["gears:sheet", "gears:wrench"]);
|
||||
assertEquals(report.written.map(({ id }) => id).sort(), ["gears:sheet", "gears:trimmed_sheet", "gears:wrench"]);
|
||||
const sheet = JSON.parse(Deno.readTextFileSync(`${output}/sheet.json`)).item;
|
||||
assertEquals(sheet, { id: "gears:sheet", texture: "gears:item_sheet" });
|
||||
assertEquals(validate_item(sheet), []);
|
||||
// java's other layers are drawn over the first
|
||||
const trimmed = JSON.parse(Deno.readTextFileSync(`${output}/trimmed_sheet.json`)).item;
|
||||
assertEquals(trimmed.layers, [{ texture: "gears:item_trim" }]);
|
||||
assertEquals(validate_item(trimmed), []);
|
||||
// the model kind writes its model, and its texture is its model's
|
||||
const wrench = JSON.parse(Deno.readTextFileSync(`${output}/wrench.json`)).item;
|
||||
assertEquals(wrench, { id: "gears:wrench", texture: "gears:item_wrench", model: "gears:item_wrench" });
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// stacks of one item only merge when their data is the same, whatever order its keys are in
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { click_slot, Container, type Cursor, ItemStack, LEFT_CLICK } from "$/common/inventory.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
const coal = (count: number, data?: unknown) => {
|
||||
const stack = new ItemStack("bworld:coal", count);
|
||||
stack.data = data;
|
||||
return stack;
|
||||
};
|
||||
|
||||
Deno.test("stacks with different data don't merge in an inventory, and ones whose data only differs in key order do", async () => {
|
||||
await test_game("mods");
|
||||
const container = new Container(2);
|
||||
container.add_item(coal(3, { mine: "north", grade: 1 }));
|
||||
container.add_item(coal(2, { mine: "south" }));
|
||||
assertEquals(container.to_data().map((item) => item?.count), [3, 2]);
|
||||
assertEquals(container.add_item(coal(4, { mine: "east" })), 4, "there's no slot left for a third kind");
|
||||
container.add_item(coal(1, { grade: 1, mine: "north" }));
|
||||
assertEquals(container.get_item(0)?.amount, 4);
|
||||
|
||||
// clicking a stack onto one with other data swaps them instead
|
||||
const cursor: Cursor = { item: coal(5, { mine: "east" }) };
|
||||
click_slot(container, 1, cursor, LEFT_CLICK);
|
||||
assertEquals([container.get_item(1)?.amount, cursor.item?.amount], [5, 2]);
|
||||
cursor.item = coal(5, { mine: "east" });
|
||||
click_slot(container, 1, cursor, LEFT_CLICK);
|
||||
assertEquals([container.get_item(1)?.amount, cursor.item], [10, undefined]);
|
||||
|
||||
// no data is the same as {}
|
||||
const plain = new Container(1);
|
||||
plain.add_item(coal(1));
|
||||
plain.add_item(coal(1, {}));
|
||||
assertEquals(plain.get_item(0)?.amount, 2);
|
||||
});
|
||||
|
||||
Deno.test("stacks with different data don't merge on the ground, and ones whose data only differs in key order do", async () => {
|
||||
const { game, join } = await test_game("mods");
|
||||
// items only move near a player
|
||||
join(1, "alice");
|
||||
for (let x = -2; x <= 2; x++) {
|
||||
for (let z = -2; z <= 2; z++) game.set_block(x, 99, z, "bworld:stone");
|
||||
}
|
||||
game.spawn_item(0.5, 100, 0.5, coal(1, { mine: "north", grade: 1 }));
|
||||
game.spawn_item(0.5, 100, 0.5, coal(1, { mine: "south" }));
|
||||
game.spawn_item(0.5, 100, 0.5, coal(1, { grade: 1, mine: "north" }));
|
||||
for (let i = 0; i < 4; i++) game.tick();
|
||||
const counts = game.item_entities().map((
|
||||
entity,
|
||||
) => [(entity.item.data as { mine: string }).mine, entity.item.amount]);
|
||||
assertEquals(counts.sort(), [["north", 2], ["south", 1]]);
|
||||
});
|
||||
Reference in New Issue
Block a user