JEI transfer picks the most common ingredient
This commit is contained in:
@@ -22,6 +22,8 @@ import net.minecraftforge.items.ItemHandlerHelper;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class CraftingGridBehavior implements ICraftingGridBehavior {
|
||||
@@ -193,6 +195,11 @@ public class CraftingGridBehavior implements ICraftingGridBehavior {
|
||||
if (recipe[i] != null) {
|
||||
ItemStack[] possibilities = recipe[i];
|
||||
|
||||
if (network != null && grid.isGridActive()) {
|
||||
// sort by the number of items in storage
|
||||
Arrays.sort(possibilities, Comparator.comparingInt((ItemStack a) -> network.extractItem(a, Integer.MAX_VALUE, IComparer.COMPARE_NBT, Action.SIMULATE).getCount()).reversed());
|
||||
}
|
||||
|
||||
// If we are a crafting grid
|
||||
if (grid.getGridType() == GridType.CRAFTING) {
|
||||
boolean found = false;
|
||||
|
@@ -72,7 +72,7 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
||||
)
|
||||
));
|
||||
} else {
|
||||
moveItems(container, recipeLayout);
|
||||
moveItems(container, recipeLayout, tracker);
|
||||
}
|
||||
} else {
|
||||
if (tracker.hasMissing()) {
|
||||
@@ -87,7 +87,7 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
||||
IngredientTracker tracker = createTracker(container, recipeLayout, player);
|
||||
|
||||
if (doTransfer) {
|
||||
moveItems(container, recipeLayout);
|
||||
moveItems(container, recipeLayout, tracker);
|
||||
} else {
|
||||
if (tracker.isAutocraftingAvailable()) {
|
||||
return new RecipeTransferPatternGridError(tracker);
|
||||
@@ -140,11 +140,11 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
||||
return System.currentTimeMillis() - lastTransferTimeMs <= TRANSFER_SCROLLBAR_DELAY_MS;
|
||||
}
|
||||
|
||||
private void moveItems(GridContainer gridContainer, IRecipeLayout recipeLayout) {
|
||||
private void moveItems(GridContainer gridContainer, IRecipeLayout recipeLayout, IngredientTracker tracker) {
|
||||
this.lastTransferTimeMs = System.currentTimeMillis();
|
||||
|
||||
if (gridContainer.getGrid().getGridType() == GridType.PATTERN && !recipeLayout.getRecipeCategory().getUid().equals(VanillaRecipeCategoryUid.CRAFTING)) {
|
||||
moveForProcessing(recipeLayout);
|
||||
moveForProcessing(recipeLayout, tracker);
|
||||
} else {
|
||||
move(gridContainer, recipeLayout);
|
||||
}
|
||||
@@ -157,7 +157,7 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
||||
));
|
||||
}
|
||||
|
||||
private void moveForProcessing(IRecipeLayout recipeLayout) {
|
||||
private void moveForProcessing(IRecipeLayout recipeLayout, IngredientTracker tracker) {
|
||||
List<ItemStack> inputs = new LinkedList<>();
|
||||
List<ItemStack> outputs = new LinkedList<>();
|
||||
|
||||
@@ -165,7 +165,7 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
||||
List<FluidStack> fluidOutputs = new LinkedList<>();
|
||||
|
||||
for (IGuiIngredient<ItemStack> guiIngredient : recipeLayout.getItemStacks().getGuiIngredients().values()) {
|
||||
handleItemIngredient(inputs, outputs, guiIngredient);
|
||||
handleItemIngredient(inputs, outputs, guiIngredient, tracker);
|
||||
}
|
||||
|
||||
for (IGuiIngredient<FluidStack> guiIngredient : recipeLayout.getFluidStacks().getGuiIngredients().values()) {
|
||||
@@ -187,9 +187,13 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
||||
}
|
||||
}
|
||||
|
||||
private void handleItemIngredient(List<ItemStack> inputs, List<ItemStack> outputs, IGuiIngredient<ItemStack> guiIngredient) {
|
||||
private void handleItemIngredient(List<ItemStack> inputs, List<ItemStack> outputs, IGuiIngredient<ItemStack> guiIngredient, IngredientTracker tracker) {
|
||||
if (guiIngredient != null && guiIngredient.getDisplayedIngredient() != null) {
|
||||
ItemStack ingredient = guiIngredient.getDisplayedIngredient().copy();
|
||||
ItemStack ingredient = tracker.findBestMatch(guiIngredient.getAllIngredients()).copy();
|
||||
|
||||
if (ingredient == ItemStack.EMPTY) {
|
||||
ingredient = guiIngredient.getDisplayedIngredient().copy();
|
||||
}
|
||||
|
||||
if (guiIngredient.isInput()) {
|
||||
inputs.add(ingredient);
|
||||
|
@@ -6,12 +6,14 @@ import com.refinedmods.refinedstorage.screen.grid.stack.IGridStack;
|
||||
import mezz.jei.api.gui.IRecipeLayout;
|
||||
import mezz.jei.api.gui.ingredient.IGuiIngredient;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
public class IngredientTracker {
|
||||
private final List<Ingredient> ingredients = new ArrayList<>();
|
||||
private final Map<ResourceLocation, Integer> storedItems = new HashMap<>();
|
||||
|
||||
public IngredientTracker(IRecipeLayout recipeLayout) {
|
||||
for (IGuiIngredient<ItemStack> guiIngredient : recipeLayout.getItemStacks().getGuiIngredients().values()) {
|
||||
@@ -27,6 +29,7 @@ public class IngredientTracker {
|
||||
|
||||
public void addAvailableStack(ItemStack stack, @Nullable IGridStack gridStack) {
|
||||
int available = stack.getCount();
|
||||
storedItems.merge(stack.getItem().getRegistryName(), available, Integer::sum);
|
||||
|
||||
for (Ingredient ingredient : ingredients) {
|
||||
if (available == 0) {
|
||||
@@ -78,4 +81,19 @@ public class IngredientTracker {
|
||||
|
||||
return toRequest;
|
||||
}
|
||||
|
||||
public ItemStack findBestMatch(List<ItemStack> list) {
|
||||
ItemStack stack = ItemStack.EMPTY;
|
||||
int count = 0;
|
||||
|
||||
for (ItemStack itemStack : list) {
|
||||
Integer stored = storedItems.get(itemStack.getItem().getRegistryName());
|
||||
if (stored != null && stored > count) {
|
||||
stack = itemStack;
|
||||
count = stored;
|
||||
}
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user