diff --git a/AGENTS.md b/AGENTS.md index 5d437dd..6eddfbc 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -40,7 +40,7 @@ Read [[🧠 Philosophy]] before changing the vault. The Bookshelf Principle, Pre ## Code -The first slice is in code and has been played (ADRs 012–018): [[▢️ Create Goal]], [[πŸ”Œ Recipe Provider]] via `RecipeManager`, overlay HUD, capture = tooltip + rebindable Add key, client-owned plan files. +The first slice is in code and has been played (ADRs 012–019): [[▢️ Create Goal]], [[πŸ”Œ Recipe Provider]] via `RecipeManager`, overlay HUD, capture = tooltip + rebindable Add key, client-owned plan files. Tag ingredients are any-of (ADR-019). Do not start EMI/JEI/REI. Recipe source is `RecipeManager` (ADR-016). The host does not need this mod (ADR-018). Project waits until settled. Do not rewrite the overlay as a `Screen` unless the HUD path fails. diff --git a/docs/02-Domain/βž• Material Aggregation.md b/docs/02-Domain/βž• Material Aggregation.md index a07607a..2af7004 100644 --- a/docs/02-Domain/βž• Material Aggregation.md +++ b/docs/02-Domain/βž• Material Aggregation.md @@ -14,6 +14,8 @@ Combines requirements from multiple [[🎯 Goal|goals]]. Same material from two goals is one entry. Goal A needs Diamond x4. Goal B needs Diamond x8. Aggregated: Diamond x12. +The same interchangeable set (every plank type) from two goals is one entry. + --- ## Rule diff --git a/docs/02-Domain/πŸ“ˆ Progress Calculation.md b/docs/02-Domain/πŸ“ˆ Progress Calculation.md index 64bf0af..78d75ec 100644 --- a/docs/02-Domain/πŸ“ˆ Progress Calculation.md +++ b/docs/02-Domain/πŸ“ˆ Progress Calculation.md @@ -31,6 +31,7 @@ aggregate requirements (same material β†’ one row) for each material: + available = sum of every interchangeable option (or that one item) missing = max(0, required βˆ’ available) ``` diff --git a/docs/02-Domain/πŸ“‰ Missing Material.md b/docs/02-Domain/πŸ“‰ Missing Material.md index 3227e66..e91a5dd 100644 --- a/docs/02-Domain/πŸ“‰ Missing Material.md +++ b/docs/02-Domain/πŸ“‰ Missing Material.md @@ -11,7 +11,7 @@ Missing = Required - Available ``` Required comes from goals (after resolution and aggregation). -Available comes from [[πŸŽ’ Inventory State]]. +Available comes from [[πŸŽ’ Inventory State]]. For interchangeable items (any plank), available is the **sum** of every option. --- @@ -19,7 +19,7 @@ Available comes from [[πŸŽ’ Inventory State]]. Missing materials are the **default** materials view. On the golden day they live in the **lower region** of the play overlay, under the goal list. See [[🚢 Day in the Life]] and [[πŸ–ΌοΈ Wireframes]]. -The player should see this first β€” one row per material, with how many are still needed: +The player should see this first β€” one row per material, with how many are still needed. A tag ingredient is still one row. If they hold spruce, the row says spruce. If they hold none, a generic label (Planks), not oak-by-default. - Diamond x12 - Redstone x82 diff --git a/docs/02-Domain/🧱 Material Requirement.md b/docs/02-Domain/🧱 Material Requirement.md index d3592c6..40243d6 100644 --- a/docs/02-Domain/🧱 Material Requirement.md +++ b/docs/02-Domain/🧱 Material Requirement.md @@ -2,7 +2,9 @@ A quantity of a material required to complete a [[🎯 Goal]]. -Identified by the same **ItemId** as Target Item (`namespace:path`). Quantity β‰₯ 1. +Identified by one **ItemId**, or by **any of several** ItemIds when the recipe accepts substitutes (planks for a chest). Quantity β‰₯ 1. + +Tags are not a domain type. The provider names the concrete items that all count. --- @@ -28,6 +30,8 @@ Aggregated requirement: Diamond x12 +Chest needs any plank Γ—8. Crafting table needs any plank Γ—4. Aggregated: any plank Γ—12. Spruce and oak both count. + Projects do not change this. See [[πŸ“ Project]] and [[βž• Material Aggregation]]. --- diff --git a/docs/03-Architecture/πŸ“ Decisions.md b/docs/03-Architecture/πŸ“ Decisions.md index e38a82c..84707fe 100644 --- a/docs/03-Architecture/πŸ“ Decisions.md +++ b/docs/03-Architecture/πŸ“ Decisions.md @@ -330,6 +330,24 @@ Remote identity is the **multiplayer list name plus MOTD**, not the IP. Hostname --- +### ADR-019 β€” Interchangeable ingredients are any-of, not the first tag member + +**Date:** 2026-09-08 + +**Decision:** When a recipe ingredient matches several items (a tag, a custom ingredient), [[🧱 Material Requirement]] is one row with **all** of those ItemIds. Available is the sum. The overlay shows the option the player holds most of; if they hold none, a generic path label (Planks), not oak-by-default. + +Not a tag type in the domain. Not biome-based wood. Not a recipe browser. + +**Reason:** + +A chest crafts from any `#minecraft:planks`. Picking `getItems()[0]` made spruce in a spruce biome invisible, and mixed stacks look short. The walk failed. + +**Status:** βœ… Accepted + +**Refines:** ADR-014 (concrete items; this is how several concretes count) + +--- + ## Related Documents [[πŸ›οΈ Architecture]] diff --git a/docs/03-Architecture/πŸ”Œ Recipe Provider.md b/docs/03-Architecture/πŸ”Œ Recipe Provider.md index 4278bfe..10e579f 100644 --- a/docs/03-Architecture/πŸ”Œ Recipe Provider.md +++ b/docs/03-Architecture/πŸ”Œ Recipe Provider.md @@ -17,7 +17,7 @@ RecipeProvider `ItemId` is `namespace:path` (e.g. `minecraft:furnace`). Not `net.minecraft.world.item.Item`. -`MaterialRequirement` is `(ItemId, quantity)`. +`MaterialRequirement` is `(options, quantity)`. `options` is one ItemId, or several that all count. --- @@ -29,7 +29,7 @@ RecipeProvider | Item has a recipe | **One crafting level** of ingredients, already scaled for `quantity` (including recipe output count) | | Item has no recipe | One requirement: `(target, quantity)` β€” a gather | | Several recipes exist | Provider picks **one**. Recipe choice is not in the slice | -| Ingredient is a tag | Provider returns **concrete** ItemIds. Tags are not a domain type yet | +| Ingredient is a tag | One requirement whose **options** are every concrete member. Tags are not a domain type. Available = sum of the members. | Not a recipe tree. Not a recipe browser. @@ -50,7 +50,7 @@ Catalysts (AE2 press not consumed) are not modelled yet. The adapter may list th ## Shipped implementation -`VanillaRecipeProvider` over `RecipeManager` (all types, by output). Crafting-table recipes preferred when several match; else first highest score. Empty `getIngredients()` β†’ gather. +`VanillaRecipeProvider` over `RecipeManager` (all types, by output). Crafting-table recipes preferred when several match; else first highest score. Empty `getIngredients()` β†’ gather. Several `getItems()` β†’ one interchangeable requirement (chest + spruce planks). `StubRecipeProvider` remains for unit tests. diff --git a/docs/04-Planning/πŸ—ΊοΈ Roadmap.md b/docs/04-Planning/πŸ—ΊοΈ Roadmap.md index c79adf7..aa6eb12 100644 --- a/docs/04-Planning/πŸ—ΊοΈ Roadmap.md +++ b/docs/04-Planning/πŸ—ΊοΈ Roadmap.md @@ -6,7 +6,7 @@ What exists, and what is next. Implementation detail will live in an Implementat ## βœ… Now (docs) -- Philosophy Rules #1–#10; ADRs **001–018** +- Philosophy Rules #1–#10; ADRs **001–019** - Vision, ubiquitous language, bounded context (one: Goal Planning) - Goal invariants: one per ItemId, quantity β‰₯ 1, create at 1 / +1, `[-]` at 1 removes - Packages named; [[πŸ”Œ Recipe Provider]] sketched; [[▢️ Create Goal]] written diff --git a/docs/04-Planning/🧭 Notes for the Wayfinder.md b/docs/04-Planning/🧭 Notes for the Wayfinder.md index fd6a8e5..1f2f8b8 100644 --- a/docs/04-Planning/🧭 Notes for the Wayfinder.md +++ b/docs/04-Planning/🧭 Notes for the Wayfinder.md @@ -425,6 +425,16 @@ The frozen first slice has been walked. Still listen. Still do not open Project --- +## πŸ“œ 2026-09-08 (any plank counts) + +Greetings, Wayfinder. + +The walk reported: chests take more than oak. Spruce in a spruce biome did not count. + +ADR-019: a tag ingredient is one requirement, every member counts. Overlay names the stack they have; empty β†’ β€œPlanks”, not oak. Not biome. Mixed spruce + birch covering a chest is Ready. + +--- + ## Related Documents [[🌟 Project Vision]] diff --git a/docs/05-Experience/πŸ–₯️ Experience.md b/docs/05-Experience/πŸ–₯️ Experience.md index ffef44d..0f8de63 100644 --- a/docs/05-Experience/πŸ–₯️ Experience.md +++ b/docs/05-Experience/πŸ–₯️ Experience.md @@ -62,7 +62,7 @@ The overlay answers both halves of the vision question: Walking has no cursor. Overlay `[+]` / `[-]` / Clear are HUD hit-tests (vanilla button sprites + click sound), not a `Screen`. Open chat (`T` or `/`) to click them. Capture does not use a clickable chip you leave the item to hit. -Same material from two goals is one row. Changing a goal quantity recalculates the lower list. As inventory fills, counts shrink. +Same material from two goals is one row. Any-plank recipes (chest, crafting table) are one row too: spruce counts, mixed stacks count. If they hold none, the row says Planks, not Oak. Changing a goal quantity recalculates the lower list. As inventory fills, counts shrink. Coming back is part of the prosthetic. Singleplayer: the plan belongs to that world. A server: the plan belongs to **the name you gave it in the multiplayer list**, plus that server’s description (MOTD). Not the IP. Two list entries with different names are two plans, even if they are the same host. diff --git a/src/main/java/com/sufficient/satistaskory/domain/Alternatives.java b/src/main/java/com/sufficient/satistaskory/domain/Alternatives.java new file mode 100644 index 0000000..1f11cd0 --- /dev/null +++ b/src/main/java/com/sufficient/satistaskory/domain/Alternatives.java @@ -0,0 +1,81 @@ +package com.sufficient.satistaskory.domain; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +/** + * Interchangeable items from one ingredient (a tag, a custom ingredient). Not a tag type. + */ +final class Alternatives { + private Alternatives() {} + + static List copy(List options) { + if (options == null || options.isEmpty()) { + throw new IllegalArgumentException("options must be non-empty"); + } + LinkedHashSet unique = new LinkedHashSet<>(); + for (ItemId option : options) { + if (option == null) { + throw new IllegalArgumentException("options must not contain null"); + } + unique.add(option); + } + return List.copyOf(unique); + } + + static Set members(List options) { + return Set.copyOf(options); + } + + static ItemId preferred(List options, InventoryState inventory) { + ItemId best = options.getFirst(); + int bestCount = -1; + for (ItemId option : options) { + int count = inventory.get(option); + if (count > bestCount) { + bestCount = count; + best = option; + } + } + return best; + } + + /** + * Shared trailing path tokens: oak_planks + spruce_planks β†’ {@code planks}. + * Empty when there is no shared suffix. + */ + static String commonPathLabel(List options) { + if (options.size() < 2) { + return ""; + } + List tokens = new ArrayList<>(options.size()); + int minLen = Integer.MAX_VALUE; + for (ItemId option : options) { + String[] parts = option.path().split("_"); + tokens.add(parts); + minLen = Math.min(minLen, parts.length); + } + List suffix = new ArrayList<>(); + for (int offset = 1; offset <= minLen; offset++) { + String[] first = tokens.getFirst(); + String token = first[first.length - offset]; + boolean all = true; + for (String[] parts : tokens) { + if (!parts[parts.length - offset].equals(token)) { + all = false; + break; + } + } + if (!all) { + break; + } + suffix.addFirst(token); + } + if (suffix.isEmpty()) { + return ""; + } + return String.join("_", suffix); + } +} diff --git a/src/main/java/com/sufficient/satistaskory/domain/InventoryState.java b/src/main/java/com/sufficient/satistaskory/domain/InventoryState.java index 0fb107d..8afdcbc 100644 --- a/src/main/java/com/sufficient/satistaskory/domain/InventoryState.java +++ b/src/main/java/com/sufficient/satistaskory/domain/InventoryState.java @@ -2,6 +2,7 @@ package com.sufficient.satistaskory.domain; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Objects; @@ -31,4 +32,12 @@ public final class InventoryState { public int get(ItemId item) { return available.getOrDefault(item, 0); } + + public int getAny(List items) { + int total = 0; + for (ItemId item : items) { + total += get(item); + } + return total; + } } diff --git a/src/main/java/com/sufficient/satistaskory/domain/MaterialAggregation.java b/src/main/java/com/sufficient/satistaskory/domain/MaterialAggregation.java index 00ea117..a7ce9bc 100644 --- a/src/main/java/com/sufficient/satistaskory/domain/MaterialAggregation.java +++ b/src/main/java/com/sufficient/satistaskory/domain/MaterialAggregation.java @@ -4,15 +4,19 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; public final class MaterialAggregation { public List aggregate(List requirements) { - Map sums = new LinkedHashMap<>(); + Map, Integer> sums = new LinkedHashMap<>(); + Map, List> order = new LinkedHashMap<>(); for (MaterialRequirement requirement : requirements) { - sums.merge(requirement.item(), requirement.quantity(), Integer::sum); + Set key = requirement.members(); + sums.merge(key, requirement.quantity(), Integer::sum); + order.putIfAbsent(key, requirement.options()); } List aggregated = new ArrayList<>(); - sums.forEach((item, quantity) -> aggregated.add(new MaterialRequirement(item, quantity))); + sums.forEach((key, quantity) -> aggregated.add(new MaterialRequirement(order.get(key), quantity))); return List.copyOf(aggregated); } } diff --git a/src/main/java/com/sufficient/satistaskory/domain/MaterialRequirement.java b/src/main/java/com/sufficient/satistaskory/domain/MaterialRequirement.java index 3402599..81cf291 100644 --- a/src/main/java/com/sufficient/satistaskory/domain/MaterialRequirement.java +++ b/src/main/java/com/sufficient/satistaskory/domain/MaterialRequirement.java @@ -1,27 +1,59 @@ package com.sufficient.satistaskory.domain; +import java.util.List; import java.util.Objects; +import java.util.Set; +/** + * A quantity of material. One item, or any of several (tag / custom ingredient). + */ public final class MaterialRequirement { - private final ItemId item; + private final List options; private final int quantity; public MaterialRequirement(ItemId item, int quantity) { - this.item = Objects.requireNonNull(item, "item"); + this(List.of(item), quantity); + } + + public MaterialRequirement(List options, int quantity) { + this.options = Alternatives.copy(options); if (quantity < 1) { throw new IllegalArgumentException("quantity must be >= 1"); } this.quantity = quantity; } + public List options() { + return options; + } + + /** + * First option. Display fallback, not the only item that counts. + */ public ItemId item() { - return item; + return options.getFirst(); } public int quantity() { return quantity; } + public boolean interchangeable() { + return options.size() > 1; + } + + public ItemId preferred(InventoryState inventory) { + return Alternatives.preferred(options, inventory); + } + + public String groupLabel() { + return Alternatives.commonPathLabel(options); + } + + Set members() { + return Alternatives.members(options); + } + @Override public boolean equals(Object o) { if (this == o) { @@ -30,11 +62,11 @@ public final class MaterialRequirement { if (!(o instanceof MaterialRequirement other)) { return false; } - return quantity == other.quantity && item.equals(other.item); + return quantity == other.quantity && members().equals(other.members()); } @Override public int hashCode() { - return Objects.hash(item, quantity); + return Objects.hash(quantity, members()); } } diff --git a/src/main/java/com/sufficient/satistaskory/domain/MissingMaterial.java b/src/main/java/com/sufficient/satistaskory/domain/MissingMaterial.java index c3562e5..f02a652 100644 --- a/src/main/java/com/sufficient/satistaskory/domain/MissingMaterial.java +++ b/src/main/java/com/sufficient/satistaskory/domain/MissingMaterial.java @@ -1,27 +1,56 @@ package com.sufficient.satistaskory.domain; +import java.util.List; import java.util.Objects; +import java.util.Set; public final class MissingMaterial { - private final ItemId item; + private final List options; private final int quantity; public MissingMaterial(ItemId item, int quantity) { - this.item = Objects.requireNonNull(item, "item"); + this(List.of(item), quantity); + } + + public MissingMaterial(List options, int quantity) { + this.options = Alternatives.copy(options); if (quantity < 1) { throw new IllegalArgumentException("quantity must be >= 1"); } this.quantity = quantity; } + public List options() { + return options; + } + + /** + * First option. Display fallback, not the only item that counts. + */ public ItemId item() { - return item; + return options.getFirst(); } public int quantity() { return quantity; } + public boolean interchangeable() { + return options.size() > 1; + } + + public ItemId preferred(InventoryState inventory) { + return Alternatives.preferred(options, inventory); + } + + public String groupLabel() { + return Alternatives.commonPathLabel(options); + } + + Set members() { + return Alternatives.members(options); + } + @Override public boolean equals(Object o) { if (this == o) { @@ -30,11 +59,11 @@ public final class MissingMaterial { if (!(o instanceof MissingMaterial other)) { return false; } - return quantity == other.quantity && item.equals(other.item); + return quantity == other.quantity && members().equals(other.members()); } @Override public int hashCode() { - return Objects.hash(item, quantity); + return Objects.hash(quantity, members()); } } diff --git a/src/main/java/com/sufficient/satistaskory/domain/ProgressCalculation.java b/src/main/java/com/sufficient/satistaskory/domain/ProgressCalculation.java index 16574d0..9619352 100644 --- a/src/main/java/com/sufficient/satistaskory/domain/ProgressCalculation.java +++ b/src/main/java/com/sufficient/satistaskory/domain/ProgressCalculation.java @@ -25,9 +25,9 @@ public final class ProgressCalculation { List missing = new ArrayList<>(); for (MaterialRequirement requirement : aggregation.aggregate(all)) { - int stillNeeded = Math.max(0, requirement.quantity() - inventory.get(requirement.item())); + int stillNeeded = Math.max(0, requirement.quantity() - inventory.getAny(requirement.options())); if (stillNeeded > 0) { - missing.add(new MissingMaterial(requirement.item(), stillNeeded)); + missing.add(new MissingMaterial(requirement.options(), stillNeeded)); } } return new ProgressSnapshot(goals, missing); diff --git a/src/main/java/com/sufficient/satistaskory/infrastructure/client/PlayOverlay.java b/src/main/java/com/sufficient/satistaskory/infrastructure/client/PlayOverlay.java index 76eea2d..d33c8e2 100644 --- a/src/main/java/com/sufficient/satistaskory/infrastructure/client/PlayOverlay.java +++ b/src/main/java/com/sufficient/satistaskory/infrastructure/client/PlayOverlay.java @@ -4,9 +4,11 @@ import java.util.ArrayList; import java.util.List; import com.sufficient.satistaskory.domain.Goal; +import com.sufficient.satistaskory.domain.InventoryState; import com.sufficient.satistaskory.domain.ItemId; import com.sufficient.satistaskory.domain.MissingMaterial; import com.sufficient.satistaskory.domain.ProgressSnapshot; +import com.sufficient.satistaskory.infrastructure.inventory.PlayerInventoryAdapter; import net.minecraft.client.DeltaTracker; import net.minecraft.client.Minecraft; @@ -84,8 +86,9 @@ public final class PlayOverlay { if (snapshot.missing().isEmpty()) { graphics.drawString(font, "Ready", x + PAD, cursor, 0xA0A0A0, false); } else { + InventoryState inventory = PlayerInventoryAdapter.from(minecraft.player.getInventory()); for (MissingMaterial missing : snapshot.missing()) { - String name = displayName(missing.item()); + String name = displayName(missing, inventory); String qty = Integer.toString(missing.quantity()); graphics.drawString(font, trim(font, name, WIDTH - PAD * 2 - 24), x + PAD, cursor, 0xFFFFFF, false); graphics.drawString(font, qty, x + WIDTH - PAD - font.width(qty), cursor, 0xFFFFFF, false); @@ -150,6 +153,16 @@ public final class PlayOverlay { HITS.add(new Hit(x, y, x + w, y + h, kind, item)); } + private static String displayName(MissingMaterial missing, InventoryState inventory) { + if (missing.interchangeable() && inventory.getAny(missing.options()) == 0) { + String group = missing.groupLabel(); + if (!group.isEmpty()) { + return titleCase(group); + } + } + return displayName(missing.preferred(inventory)); + } + private static String displayName(ItemId id) { ResourceLocation location = ResourceLocation.parse(id.toString()); Item item = BuiltInRegistries.ITEM.get(location); @@ -159,6 +172,23 @@ public final class PlayOverlay { return item.getDescription().getString(); } + private static String titleCase(String path) { + StringBuilder builder = new StringBuilder(); + for (String part : path.split("_")) { + if (part.isEmpty()) { + continue; + } + if (builder.length() > 0) { + builder.append(' '); + } + builder.append(Character.toUpperCase(part.charAt(0))); + if (part.length() > 1) { + builder.append(part.substring(1)); + } + } + return builder.toString(); + } + private static String trim(Font font, String text, int maxWidth) { if (font.width(text) <= maxWidth) { return text; diff --git a/src/main/java/com/sufficient/satistaskory/infrastructure/recipe/VanillaRecipeProvider.java b/src/main/java/com/sufficient/satistaskory/infrastructure/recipe/VanillaRecipeProvider.java index 4201e30..b8938f7 100644 --- a/src/main/java/com/sufficient/satistaskory/infrastructure/recipe/VanillaRecipeProvider.java +++ b/src/main/java/com/sufficient/satistaskory/infrastructure/recipe/VanillaRecipeProvider.java @@ -2,8 +2,11 @@ package com.sufficient.satistaskory.infrastructure.recipe; import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Set; import com.sufficient.satistaskory.domain.ItemId; import com.sufficient.satistaskory.domain.MaterialRequirement; @@ -23,7 +26,7 @@ import net.minecraft.world.item.crafting.RecipeType; /** * One crafting level from {@link RecipeManager}. All recipe types, by output. - * Crafting-table recipes win when several match. Tags become the first concrete item. + * Crafting-table recipes win when several match. Tags become one interchangeable requirement. */ public final class VanillaRecipeProvider implements RecipeProvider { @Override @@ -50,24 +53,27 @@ public final class VanillaRecipeProvider implements RecipeProvider { int outputCount = Math.max(1, result.getCount()); int crafts = (quantity + outputCount - 1) / outputCount; - Map counts = new LinkedHashMap<>(); + Map counts = new LinkedHashMap<>(); for (Ingredient ingredient : chosen.value().getIngredients()) { if (ingredient.isEmpty() || ingredient.hasNoItems()) { continue; } - ItemStack[] options = ingredient.getItems(); - if (options.length == 0) { + ItemStack[] stacks = ingredient.getItems(); + if (stacks.length == 0) { continue; } - ItemStack option = options[0]; - int perCraft = Math.max(1, option.getCount()); - counts.merge(itemId(option), perCraft * crafts, Integer::sum); + OptionGroup group = OptionGroup.of(stacks); + if (group.options.isEmpty()) { + continue; + } + int perCraft = Math.max(1, stacks[0].getCount()); + counts.merge(group, perCraft * crafts, Integer::sum); } if (counts.isEmpty()) { return gather(target, quantity); } List requirements = new ArrayList<>(); - counts.forEach((id, count) -> requirements.add(new MaterialRequirement(id, count))); + counts.forEach((group, count) -> requirements.add(new MaterialRequirement(group.options, count))); return List.copyOf(requirements); } @@ -101,4 +107,35 @@ public final class VanillaRecipeProvider implements RecipeProvider { private static List gather(ItemId target, int quantity) { return List.of(new MaterialRequirement(target, quantity)); } + + private static final class OptionGroup { + private final List options; + private final Set members; + + private OptionGroup(List options) { + this.options = List.copyOf(options); + this.members = Set.copyOf(options); + } + + static OptionGroup of(ItemStack[] stacks) { + LinkedHashSet ids = new LinkedHashSet<>(); + for (ItemStack stack : stacks) { + if (stack.isEmpty()) { + continue; + } + ids.add(itemId(stack)); + } + return new OptionGroup(List.copyOf(ids)); + } + + @Override + public boolean equals(Object o) { + return o instanceof OptionGroup other && members.equals(other.members); + } + + @Override + public int hashCode() { + return Objects.hash(members); + } + } } diff --git a/src/test/java/com/sufficient/satistaskory/application/PlannerTest.java b/src/test/java/com/sufficient/satistaskory/application/PlannerTest.java index 3e15ae3..4f5e57d 100644 --- a/src/test/java/com/sufficient/satistaskory/application/PlannerTest.java +++ b/src/test/java/com/sufficient/satistaskory/application/PlannerTest.java @@ -102,7 +102,7 @@ class PlannerTest { private static int quantity(ProgressSnapshot snapshot, ItemId item) { return snapshot.missing().stream() - .filter(missing -> missing.item().equals(item)) + .filter(missing -> missing.options().contains(item)) .findFirst() .orElseThrow() .quantity(); diff --git a/src/test/java/com/sufficient/satistaskory/domain/InterchangeableIngredientsTest.java b/src/test/java/com/sufficient/satistaskory/domain/InterchangeableIngredientsTest.java new file mode 100644 index 0000000..95b459e --- /dev/null +++ b/src/test/java/com/sufficient/satistaskory/domain/InterchangeableIngredientsTest.java @@ -0,0 +1,70 @@ +package com.sufficient.satistaskory.domain; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +class InterchangeableIngredientsTest { + private static final ItemId CHEST = ItemId.parse("minecraft:chest"); + private static final ItemId TABLE = ItemId.parse("minecraft:crafting_table"); + private static final ItemId OAK = ItemId.parse("minecraft:oak_planks"); + private static final ItemId SPRUCE = ItemId.parse("minecraft:spruce_planks"); + private static final ItemId BIRCH = ItemId.parse("minecraft:birch_planks"); + private static final List PLANKS = List.of(OAK, SPRUCE, BIRCH); + + private final ProgressCalculation progress = new ProgressCalculation( + new RecipeResolution((target, quantity) -> { + if (target.equals(CHEST)) { + return List.of(new MaterialRequirement(PLANKS, 8 * quantity)); + } + if (target.equals(TABLE)) { + return List.of(new MaterialRequirement(PLANKS, 4 * quantity)); + } + return List.of(new MaterialRequirement(target, quantity)); + }), + new MaterialAggregation()); + + @Test + void spruceCoversAChest() { + InventoryState inventory = new InventoryState(Map.of(SPRUCE, 8)); + ProgressSnapshot snapshot = progress.calculate(List.of(new Goal(CHEST, 1)), inventory); + assertTrue(snapshot.missing().isEmpty()); + } + + @Test + void mixedPlanksCoverAChest() { + InventoryState inventory = new InventoryState(Map.of(SPRUCE, 4, BIRCH, 4)); + ProgressSnapshot snapshot = progress.calculate(List.of(new Goal(CHEST, 1)), inventory); + assertTrue(snapshot.missing().isEmpty()); + } + + @Test + void partialSpruceShowsRemainder() { + InventoryState inventory = new InventoryState(Map.of(SPRUCE, 3)); + ProgressSnapshot snapshot = progress.calculate(List.of(new Goal(CHEST, 1)), inventory); + assertEquals(1, snapshot.missing().size()); + assertEquals(5, snapshot.missing().getFirst().quantity()); + assertEquals(SPRUCE, snapshot.missing().getFirst().preferred(inventory)); + } + + @Test + void chestAndTableShareOnePlankRow() { + ProgressSnapshot snapshot = progress.calculate( + List.of(new Goal(CHEST, 1), new Goal(TABLE, 1)), + InventoryState.empty()); + assertEquals(1, snapshot.missing().size()); + assertEquals(12, snapshot.missing().getFirst().quantity()); + assertEquals("planks", snapshot.missing().getFirst().groupLabel()); + } + + @Test + void emptyInventoryPrefersOakAsFallback() { + MissingMaterial missing = new MissingMaterial(PLANKS, 8); + assertEquals(OAK, missing.preferred(InventoryState.empty())); + assertEquals("planks", missing.groupLabel()); + } +}