package net.minecraft.world.entity.player; import java.util.Comparator; import java.util.List; import java.util.stream.Stream; import net.minecraft.core.Holder; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.PlacementInfo; import net.minecraft.world.item.crafting.Recipe; import org.jetbrains.annotations.Nullable; public class StackedItemContents { private final StackedContents> raw = new StackedContents<>(); public void accountSimpleStack(ItemStack stack) { if (Inventory.isUsableForCrafting(stack)) { this.accountStack(stack); } } public void accountStack(ItemStack stack) { this.accountStack(stack, stack.getMaxStackSize()); } public void accountStack(ItemStack stack, int maxStackSize) { if (!stack.isEmpty()) { int i = Math.min(maxStackSize, stack.getCount()); this.raw.account(stack.getItemHolder(), i); } } public static StackedContents.IngredientInfo> convertIngredientContents(Stream> items) { List> list = items.sorted(Comparator.comparingInt(holder -> BuiltInRegistries.ITEM.getId((Item)holder.value()))).toList(); return new StackedContents.IngredientInfo<>(list); } public boolean canCraft(Recipe recipe, @Nullable StackedContents.Output> output) { return this.canCraft(recipe, 1, output); } public boolean canCraft(Recipe recipe, int maxCount, @Nullable StackedContents.Output> output) { PlacementInfo placementInfo = recipe.placementInfo(); return placementInfo.isImpossibleToPlace() ? false : this.canCraft(placementInfo.unpackedIngredients(), maxCount, output); } public boolean canCraft(List>> ingredients, @Nullable StackedContents.Output> output) { return this.canCraft(ingredients, 1, output); } private boolean canCraft(List>> ingredients, int maxCount, @Nullable StackedContents.Output> output) { return this.raw.tryPick(ingredients, maxCount, output); } public int getBiggestCraftableStack(Recipe recipe, @Nullable StackedContents.Output> output) { return this.getBiggestCraftableStack(recipe, Integer.MAX_VALUE, output); } public int getBiggestCraftableStack(Recipe recipe, int maxCount, @Nullable StackedContents.Output> output) { return this.raw.tryPickAll(recipe.placementInfo().unpackedIngredients(), maxCount, output); } public void clear() { this.raw.clear(); } }