Count every plank a chest will take, not only oak.
Build / build (push) Failing after 32s

Tag ingredients are one interchangeable requirement. Available is the sum. Overlay names the stack the player holds; empty shows Planks, not oak-by-default.
This commit is contained in:
Stephen Fasciani
2026-09-08 06:28:04 -04:00
parent 5d49f1482f
commit bac020f335
20 changed files with 361 additions and 34 deletions
@@ -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<ItemId> copy(List<ItemId> options) {
if (options == null || options.isEmpty()) {
throw new IllegalArgumentException("options must be non-empty");
}
LinkedHashSet<ItemId> 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<ItemId> members(List<ItemId> options) {
return Set.copyOf(options);
}
static ItemId preferred(List<ItemId> 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<ItemId> options) {
if (options.size() < 2) {
return "";
}
List<String[]> 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<String> 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);
}
}
@@ -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<ItemId> items) {
int total = 0;
for (ItemId item : items) {
total += get(item);
}
return total;
}
}
@@ -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<MaterialRequirement> aggregate(List<MaterialRequirement> requirements) {
Map<ItemId, Integer> sums = new LinkedHashMap<>();
Map<Set<ItemId>, Integer> sums = new LinkedHashMap<>();
Map<Set<ItemId>, List<ItemId>> order = new LinkedHashMap<>();
for (MaterialRequirement requirement : requirements) {
sums.merge(requirement.item(), requirement.quantity(), Integer::sum);
Set<ItemId> key = requirement.members();
sums.merge(key, requirement.quantity(), Integer::sum);
order.putIfAbsent(key, requirement.options());
}
List<MaterialRequirement> 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);
}
}
@@ -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<ItemId> 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<ItemId> options, int quantity) {
this.options = Alternatives.copy(options);
if (quantity < 1) {
throw new IllegalArgumentException("quantity must be >= 1");
}
this.quantity = quantity;
}
public List<ItemId> 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<ItemId> 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());
}
}
@@ -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<ItemId> 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<ItemId> options, int quantity) {
this.options = Alternatives.copy(options);
if (quantity < 1) {
throw new IllegalArgumentException("quantity must be >= 1");
}
this.quantity = quantity;
}
public List<ItemId> 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<ItemId> 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());
}
}
@@ -25,9 +25,9 @@ public final class ProgressCalculation {
List<MissingMaterial> 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);
@@ -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;
@@ -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<ItemId, Integer> counts = new LinkedHashMap<>();
Map<OptionGroup, Integer> 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<MaterialRequirement> 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<MaterialRequirement> gather(ItemId target, int quantity) {
return List.of(new MaterialRequirement(target, quantity));
}
private static final class OptionGroup {
private final List<ItemId> options;
private final Set<ItemId> members;
private OptionGroup(List<ItemId> options) {
this.options = List.copyOf(options);
this.members = Set.copyOf(options);
}
static OptionGroup of(ItemStack[] stacks) {
LinkedHashSet<ItemId> 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);
}
}
}