VanillaRecipeProvider scans every RecipeType by output, prefers crafting-table recipes, and falls back to gather. Stub stays in unit tests. Overlay still HUD; persistence still RAM.
This commit is contained in:
@@ -7,7 +7,7 @@ import com.sufficient.satistaskory.domain.MaterialAggregation;
|
||||
import com.sufficient.satistaskory.domain.ProgressCalculation;
|
||||
import com.sufficient.satistaskory.domain.RecipeResolution;
|
||||
import com.sufficient.satistaskory.infrastructure.inventory.PlayerInventoryAdapter;
|
||||
import com.sufficient.satistaskory.infrastructure.recipe.StubRecipeProvider;
|
||||
import com.sufficient.satistaskory.infrastructure.recipe.VanillaRecipeProvider;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
@@ -24,7 +24,7 @@ public final class PlannerSession {
|
||||
if (planner != null) {
|
||||
return;
|
||||
}
|
||||
RecipeResolution resolution = new RecipeResolution(new StubRecipeProvider());
|
||||
RecipeResolution resolution = new RecipeResolution(new VanillaRecipeProvider());
|
||||
ProgressCalculation progress = new ProgressCalculation(resolution, new MaterialAggregation());
|
||||
planner = new Planner(new InMemoryGoalStore(), progress, PlannerSession::currentInventory);
|
||||
}
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ import com.sufficient.satistaskory.domain.MaterialRequirement;
|
||||
import com.sufficient.satistaskory.domain.RecipeProvider;
|
||||
|
||||
/**
|
||||
* First implementation. A few shaped recipes plus identity fallback. Not EMI. Not vanilla-complete.
|
||||
* Unit-test recipes. Furnace, crafting table, chest, plus identity fallback. Not the play adapter.
|
||||
*/
|
||||
public final class StubRecipeProvider implements RecipeProvider {
|
||||
private static final ItemId FURNACE = ItemId.parse("minecraft:furnace");
|
||||
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package com.sufficient.satistaskory.infrastructure.recipe;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sufficient.satistaskory.domain.ItemId;
|
||||
import com.sufficient.satistaskory.domain.MaterialRequirement;
|
||||
import com.sufficient.satistaskory.domain.RecipeProvider;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.item.crafting.RecipeHolder;
|
||||
import net.minecraft.world.item.crafting.RecipeManager;
|
||||
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.
|
||||
*/
|
||||
public final class VanillaRecipeProvider implements RecipeProvider {
|
||||
@Override
|
||||
public List<MaterialRequirement> resolve(ItemId target, int quantity) {
|
||||
if (quantity < 1) {
|
||||
return List.of();
|
||||
}
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
if (minecraft.level == null) {
|
||||
return gather(target, quantity);
|
||||
}
|
||||
Item item = BuiltInRegistries.ITEM.get(ResourceLocation.parse(target.toString()));
|
||||
if (item == Items.AIR && !target.toString().equals("minecraft:air")) {
|
||||
return gather(target, quantity);
|
||||
}
|
||||
|
||||
HolderLookup.Provider registries = minecraft.level.registryAccess();
|
||||
RecipeHolder<?> chosen = pick(minecraft.level.getRecipeManager(), registries, item);
|
||||
if (chosen == null) {
|
||||
return gather(target, quantity);
|
||||
}
|
||||
|
||||
ItemStack result = chosen.value().getResultItem(registries);
|
||||
int outputCount = Math.max(1, result.getCount());
|
||||
int crafts = (quantity + outputCount - 1) / outputCount;
|
||||
|
||||
Map<ItemId, Integer> counts = new LinkedHashMap<>();
|
||||
for (Ingredient ingredient : chosen.value().getIngredients()) {
|
||||
if (ingredient.isEmpty() || ingredient.hasNoItems()) {
|
||||
continue;
|
||||
}
|
||||
ItemStack[] options = ingredient.getItems();
|
||||
if (options.length == 0) {
|
||||
continue;
|
||||
}
|
||||
ItemStack option = options[0];
|
||||
int perCraft = Math.max(1, option.getCount());
|
||||
counts.merge(itemId(option), 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)));
|
||||
return List.copyOf(requirements);
|
||||
}
|
||||
|
||||
private static RecipeHolder<?> pick(RecipeManager manager, HolderLookup.Provider registries, Item target) {
|
||||
RecipeHolder<?> best = null;
|
||||
int bestScore = Integer.MIN_VALUE;
|
||||
for (RecipeHolder<?> holder : manager.getRecipes()) {
|
||||
ItemStack result = holder.value().getResultItem(registries);
|
||||
if (result.isEmpty() || result.getItem() != target) {
|
||||
continue;
|
||||
}
|
||||
int score = 0;
|
||||
if (holder.value().getType() == RecipeType.CRAFTING) {
|
||||
score += 2;
|
||||
}
|
||||
if (ItemStack.isSameItemSameComponents(result, new ItemStack(target))) {
|
||||
score += 1;
|
||||
}
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
best = holder;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
private static ItemId itemId(ItemStack stack) {
|
||||
return ItemId.parse(BuiltInRegistries.ITEM.getKey(stack.getItem()).toString());
|
||||
}
|
||||
|
||||
private static List<MaterialRequirement> gather(ItemId target, int quantity) {
|
||||
return List.of(new MaterialRequirement(target, quantity));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user