diff --git a/docs/modding/items/tools.md b/docs/modding/items/tools.md index 06c9cf2..86847ab 100644 --- a/docs/modding/items/tools.md +++ b/docs/modding/items/tools.md @@ -39,7 +39,9 @@ The base game's tiers: Tool types don't have to be the base game's. A mod can add a `"sickle"` type and blocks with `"tool": "sickle"`, and a server script can check a held item's type with `ctx.items.tool(id)`, like the base game's `bworld:hoeable` does for -hoes. +hoes. The base game's watering cans are tools of type `"watering_can"` that no block is mined with: they come in the +same five tiers with the same durability, and each holds more water than the last (16, 24, 32, 48 and 64), set by its +`bworld:watering_can` component. ## Durability diff --git a/mods/bworld/items/copper_watering_can.json b/mods/bworld/items/copper_watering_can.json new file mode 100644 index 0000000..ee97628 --- /dev/null +++ b/mods/bworld/items/copper_watering_can.json @@ -0,0 +1,18 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:copper_watering_can", + "texture": "bworld:copper_watering_can", + "tool": { + "type": "watering_can", + "tier": 1, + "speed": 3.5, + "durability": 5 + }, + "components": { + "bworld:watering_can": { + "max_water": 32 + } + } + } +} diff --git a/mods/bworld/items/gold_watering_can.json b/mods/bworld/items/gold_watering_can.json new file mode 100644 index 0000000..00816ad --- /dev/null +++ b/mods/bworld/items/gold_watering_can.json @@ -0,0 +1,18 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:gold_watering_can", + "texture": "bworld:gold_watering_can", + "tool": { + "type": "watering_can", + "tier": 0, + "speed": 6, + "durability": 9 + }, + "components": { + "bworld:watering_can": { + "max_water": 64 + } + } + } +} diff --git a/mods/bworld/items/iron_watering_can.json b/mods/bworld/items/iron_watering_can.json new file mode 100644 index 0000000..1a9d7fa --- /dev/null +++ b/mods/bworld/items/iron_watering_can.json @@ -0,0 +1,18 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:iron_watering_can", + "texture": "bworld:iron_watering_can", + "tool": { + "type": "watering_can", + "tier": 2, + "speed": 4, + "durability": 7 + }, + "components": { + "bworld:watering_can": { + "max_water": 48 + } + } + } +} diff --git a/mods/bworld/items/stone_watering_can.json b/mods/bworld/items/stone_watering_can.json new file mode 100644 index 0000000..45d0720 --- /dev/null +++ b/mods/bworld/items/stone_watering_can.json @@ -0,0 +1,18 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:stone_watering_can", + "texture": "bworld:stone_watering_can", + "tool": { + "type": "watering_can", + "tier": 1, + "speed": 3, + "durability": 3 + }, + "components": { + "bworld:watering_can": { + "max_water": 24 + } + } + } +} diff --git a/mods/bworld/items/watering_can.json b/mods/bworld/items/watering_can.json deleted file mode 100644 index a90e526..0000000 --- a/mods/bworld/items/watering_can.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "format_version": 1, - "item": { - "id": "bworld:watering_can", - "texture": "bworld:watering_can", - "components": { - "bworld:watering_can": { - "max_water": 32 - } - } - } -} diff --git a/mods/bworld/items/wood_watering_can.json b/mods/bworld/items/wood_watering_can.json new file mode 100644 index 0000000..7bc2f7a --- /dev/null +++ b/mods/bworld/items/wood_watering_can.json @@ -0,0 +1,18 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:wood_watering_can", + "texture": "bworld:wood_watering_can", + "tool": { + "type": "watering_can", + "tier": 0, + "speed": 2, + "durability": 2 + }, + "components": { + "bworld:watering_can": { + "max_water": 16 + } + } + } +} diff --git a/mods/bworld/recipes/crafting_wood_watering_can.json b/mods/bworld/recipes/crafting_wood_watering_can.json new file mode 100644 index 0000000..9ec34ac --- /dev/null +++ b/mods/bworld/recipes/crafting_wood_watering_can.json @@ -0,0 +1,27 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "S ", + " PP", + " PP" + ], + "key": { + "P": [ + "bworld:planks", + "bworld:spruce_planks", + "bworld:birch_planks", + "bworld:jungle_planks", + "bworld:acacia_planks", + "bworld:dark_oak_planks", + "bworld:cherry_planks" + ], + "S": "bworld:stick" + }, + "result": { + "id": "bworld:wood_watering_can", + "count": 1 + } + } +} diff --git a/mods/bworld/recipes/smithing_copper_watering_can.json b/mods/bworld/recipes/smithing_copper_watering_can.json new file mode 100644 index 0000000..cfa97e8 --- /dev/null +++ b/mods/bworld/recipes/smithing_copper_watering_can.json @@ -0,0 +1,12 @@ +{ + "format_version": 1, + "recipe": { + "type": "smithing", + "tool": "bworld:stone_watering_can", + "material": { + "id": "bworld:copper_ingot", + "count": 5 + }, + "result": "bworld:copper_watering_can" + } +} diff --git a/mods/bworld/recipes/smithing_gold_watering_can.json b/mods/bworld/recipes/smithing_gold_watering_can.json new file mode 100644 index 0000000..ecdf14d --- /dev/null +++ b/mods/bworld/recipes/smithing_gold_watering_can.json @@ -0,0 +1,12 @@ +{ + "format_version": 1, + "recipe": { + "type": "smithing", + "tool": "bworld:iron_watering_can", + "material": { + "id": "bworld:gold_ingot", + "count": 5 + }, + "result": "bworld:gold_watering_can" + } +} diff --git a/mods/bworld/recipes/smithing_iron_watering_can.json b/mods/bworld/recipes/smithing_iron_watering_can.json new file mode 100644 index 0000000..abc3e67 --- /dev/null +++ b/mods/bworld/recipes/smithing_iron_watering_can.json @@ -0,0 +1,12 @@ +{ + "format_version": 1, + "recipe": { + "type": "smithing", + "tool": "bworld:copper_watering_can", + "material": { + "id": "bworld:iron_ingot", + "count": 5 + }, + "result": "bworld:iron_watering_can" + } +} diff --git a/mods/bworld/recipes/smithing_stone_watering_can.json b/mods/bworld/recipes/smithing_stone_watering_can.json new file mode 100644 index 0000000..1120894 --- /dev/null +++ b/mods/bworld/recipes/smithing_stone_watering_can.json @@ -0,0 +1,12 @@ +{ + "format_version": 1, + "recipe": { + "type": "smithing", + "tool": "bworld:wood_watering_can", + "material": { + "id": "bworld:stone", + "count": 5 + }, + "result": "bworld:stone_watering_can" + } +} diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index e326ff8..b9d6725 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -284,7 +284,8 @@ function register_smithing_table(ctx: ServerContext) { // it's dug up const FARMLAND = "bworld:hoed_dirt"; -const WATERING_CAN = "bworld:watering_can"; +// every tier of watering can is a tool of this type, and shares the bworld:watering_can component +const WATERING_CAN = "watering_can"; interface WateringCanData { water: number; @@ -332,13 +333,18 @@ interface CropTopParams { } function register_farming(ctx: ServerContext) { + const is_can = (id: string | undefined) => id !== undefined && ctx.items.tool(id) === WATERING_CAN; + // how much water each tier holds, from its component. every stack goes through on_create, so every can's tier is + // in here before anything reads it + const capacity = new Map(); + const is_watered = (x: number, y: number, z: number) => ctx.world.get_block(x, y, z) === FARMLAND && ctx.world.get_state(x, y, z, "watered") === 1; // with the watering can the player holds. true when the click was about watering, so nothing gets placed const water = (x: number, y: number, z: number, player: Player) => { const can = player.held_item; - if (can?.id !== WATERING_CAN || ctx.world.get_block(x, y, z) !== FARMLAND) { + if (!can || !is_can(can.id) || ctx.world.get_block(x, y, z) !== FARMLAND) { return false; } const data = can.data as WateringCanData; @@ -438,7 +444,7 @@ function register_farming(ctx: ServerContext) { on_interact(block, params, player) { const held = player.held_item; // watering a crop waters the farmland under it - if (held?.id === WATERING_CAN) { + if (is_can(held?.id)) { block.data ??= { days: days_of(block, params) }; return water(block.x, block.y - 1, block.z, player); } @@ -490,20 +496,22 @@ function register_farming(ctx: ServerContext) { ctx.components.register_block("bworld:water_source", { on_interact(_block, _params, player) { const can = player.held_item; - if (can?.id !== WATERING_CAN) return false; - const data = can.data as WateringCanData; - can.data = { ...data, water: data.max_water }; + if (!can || !is_can(can.id)) return false; + // an upgraded can still has its old tier's max_water in its data, so this goes by its own + const max_water = capacity.get(can.id) ?? (can.data as WateringCanData).max_water; + can.data = { water: max_water, max_water } satisfies WateringCanData; return true; }, }); - ctx.components.register_item<{ max_water: number }>(WATERING_CAN, { + ctx.components.register_item<{ max_water: number }>("bworld:watering_can", { on_create(item, params) { + capacity.set(item.id, params.max_water); item.data = { water: 0, max_water: params.max_water } satisfies WateringCanData; }, - get_lore(item) { + get_lore(item, params) { const data = item.data as WateringCanData; - return `Water: ${data.water}/${data.max_water}`; + return `Water: ${data.water}/${params.max_water}`; }, }); } diff --git a/mods/bworld/textures/copper_watering_can.png b/mods/bworld/textures/copper_watering_can.png new file mode 100644 index 0000000..c451354 Binary files /dev/null and b/mods/bworld/textures/copper_watering_can.png differ diff --git a/mods/bworld/textures/gold_watering_can.png b/mods/bworld/textures/gold_watering_can.png new file mode 100644 index 0000000..1af8c1f Binary files /dev/null and b/mods/bworld/textures/gold_watering_can.png differ diff --git a/mods/bworld/textures/iron_watering_can.png b/mods/bworld/textures/iron_watering_can.png new file mode 100644 index 0000000..5139c42 Binary files /dev/null and b/mods/bworld/textures/iron_watering_can.png differ diff --git a/mods/bworld/textures/stone_watering_can.png b/mods/bworld/textures/stone_watering_can.png new file mode 100644 index 0000000..c43ea61 Binary files /dev/null and b/mods/bworld/textures/stone_watering_can.png differ diff --git a/mods/bworld/textures/watering_can.png b/mods/bworld/textures/watering_can.png deleted file mode 100644 index 4499912..0000000 Binary files a/mods/bworld/textures/watering_can.png and /dev/null differ diff --git a/mods/bworld/textures/wood_watering_can.png b/mods/bworld/textures/wood_watering_can.png new file mode 100644 index 0000000..25f171f Binary files /dev/null and b/mods/bworld/textures/wood_watering_can.png differ diff --git a/tests/base_game_test.ts b/tests/base_game_test.ts index 7aa96bb..e924f9f 100644 --- a/tests/base_game_test.ts +++ b/tests/base_game_test.ts @@ -159,18 +159,18 @@ Deno.test("the last bone meal is used up", async () => { }); Deno.test("a new watering can starts empty, and players are told so in its lore", async () => { - const t = await setup([["bworld:watering_can", 1]]); - const can = last_container(t.take(1), "inventory").find((i: Msg) => i?.id === "bworld:watering_can"); + 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"); assertEquals(can.data, { water: 0, max_water: 32 }); assertEquals(can.lore, "Water: 0/32"); // lore is worked out for players, it isn't saved const saved = JSON.parse(t.game.save().level); - const saved_can = saved.players.alice?.inventory?.find((i: Msg) => i?.id === "bworld:watering_can"); + const saved_can = saved.players.alice?.inventory?.find((i: Msg) => i?.id === "bworld:copper_watering_can"); assert(!saved_can || saved_can.lore === undefined); t.game.on_disconnect(1); const after = JSON.parse(t.game.save().level).players.alice.inventory.find((i: Msg) => - i?.id === "bworld:watering_can" + i?.id === "bworld:copper_watering_can" ); assertEquals(after.lore, undefined); }); diff --git a/tests/farming_test.ts b/tests/farming_test.ts index 3dea126..b99eb1f 100644 --- a/tests/farming_test.ts +++ b/tests/farming_test.ts @@ -41,7 +41,7 @@ async function setup(items: string[] = []) { } Deno.test("hoe, fill the can, plant, water, and the crop grows the next morning", async () => { - const t = await setup(["bworld:wood_hoe", "bworld:watering_can", "bworld:wheat_seeds 4"]); + const t = await setup(["bworld:wood_hoe", "bworld:copper_watering_can", "bworld:wheat_seeds 4"]); t.game.set_block(1, 100, 0, "bworld:grass"); t.game.set_block(0, 100, 1, "bworld:grass"); t.game.set_block(2, 100, 0, "bworld:water"); @@ -58,7 +58,7 @@ Deno.test("hoe, fill the can, plant, water, and the crop grows the next morning" assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:wheat"); // a new can is empty, water fills it, and watering the crop waters the farmland under it - t.hold("bworld:watering_can"); + t.hold("bworld:copper_watering_can"); t.take(1); t.use(1, 101, 0); assert(t.take(1).some((m) => m.type === "chat" && m.text.includes("empty"))); @@ -66,7 +66,7 @@ Deno.test("hoe, fill the can, plant, water, and the crop grows the next morning" t.use(2, 100, 0); t.use(1, 101, 0); assertEquals(t.state(1, 100, 0, "watered"), 1); - const can = t.inventory_now().find((i) => i?.id === "bworld:watering_can"); + const can = t.inventory_now().find((i) => i?.id === "bworld:copper_watering_can"); assertEquals(can?.data, { water: 31, max_water: 32 }); // nothing happens during the day diff --git a/tests/mod_loading_test.ts b/tests/mod_loading_test.ts index 9cdd23b..a2327fe 100644 --- a/tests/mod_loading_test.ts +++ b/tests/mod_loading_test.ts @@ -27,17 +27,17 @@ Deno.test("the base game loads from mods/bworld", async () => { const bworld = (entries: [string, unknown][]) => entries.filter(([id]) => id.startsWith("bworld:")).length; // 7 kinds of log, planks and leaves assertEquals(bworld(EverythingRegistry.entries("blocks")), 42); - // 38 items (20 of them tools) plus the item forms of the 37 blocks that have one - assertEquals(bworld(EverythingRegistry.entries("items")), 75); - // bworld's 16 (4 wood tools, 7 planks, the fishing rod), the cooking pot, stove, cutting board and wood knife are - // farmersdelight's - assertEquals(game.recipes.shaped.length, 20); - // every log and fish smelts, every log burns - assertEquals(game.recipes.furnace.size, 14); + // 42 items (25 of them tools) plus the item forms of the 37 blocks that have one + assertEquals(bworld(EverythingRegistry.entries("items")), 79); + // bworld's 18 (4 wood tools, the wood watering can, 7 planks, the fishing rod, the smithing table), the cooking pot, + // stove, cutting board and wood knife are farmersdelight's + assertEquals(game.recipes.shaped.length, 22); + // every log and fish smelts, every log burns, and so do sand into glass and cobblestone into stone + assertEquals(game.recipes.furnace.size, 16); assertEquals(game.recipes.fuel.size, 8); assertEquals(EverythingRegistry.get("blocks", "bworld:water")?.replaceable, true); // still has its code until it's a component - assert(EverythingRegistry.get("items", "bworld:watering_can")?.on_create); + assert(EverythingRegistry.get("items", "bworld:copper_watering_can")?.on_create); }); Deno.test("mods/bworld json survives going to the registry and back", async () => { @@ -242,6 +242,25 @@ Deno.test("every tier of pickaxe, axe, shovel and hoe exists and can be made", a } }); +Deno.test("every tier of watering can exists, can be made and holds more than the last", async () => { + const { game } = await test_game("mods"); + const made = new Set([ + ...game.recipes.shaped.map((recipe) => recipe.result.id), + ...game.recipes.smithing.map((recipe) => recipe.result), + ]); + let last = 0; + for (const tier of ["wood", "stone", "copper", "iron", "gold"]) { + const id = `bworld:${tier}_watering_can`; + const item = EverythingRegistry.get("items", id); + assertEquals(item?.tool?.type, "watering_can", id); + assertEquals(item?.texture_id, id); + assert(made.has(id), `nothing makes ${id}`); + const max_water = (item?.components?.["bworld:watering_can"] as { max_water: number }).max_water; + assert(max_water > last, id); + last = max_water; + } +}); + Deno.test("a lit furnace shows its lit front", async () => { await test_game("mods"); const furnace = EverythingRegistry.get("blocks", "bworld:furnace")!;