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 javax.annotation.Nullable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CraftingGridBehavior implements ICraftingGridBehavior {
|
public class CraftingGridBehavior implements ICraftingGridBehavior {
|
||||||
@@ -193,6 +195,11 @@ public class CraftingGridBehavior implements ICraftingGridBehavior {
|
|||||||
if (recipe[i] != null) {
|
if (recipe[i] != null) {
|
||||||
ItemStack[] possibilities = recipe[i];
|
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 we are a crafting grid
|
||||||
if (grid.getGridType() == GridType.CRAFTING) {
|
if (grid.getGridType() == GridType.CRAFTING) {
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
|
@@ -72,7 +72,7 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
|||||||
)
|
)
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
moveItems(container, recipeLayout);
|
moveItems(container, recipeLayout, tracker);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (tracker.hasMissing()) {
|
if (tracker.hasMissing()) {
|
||||||
@@ -87,7 +87,7 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
|||||||
IngredientTracker tracker = createTracker(container, recipeLayout, player);
|
IngredientTracker tracker = createTracker(container, recipeLayout, player);
|
||||||
|
|
||||||
if (doTransfer) {
|
if (doTransfer) {
|
||||||
moveItems(container, recipeLayout);
|
moveItems(container, recipeLayout, tracker);
|
||||||
} else {
|
} else {
|
||||||
if (tracker.isAutocraftingAvailable()) {
|
if (tracker.isAutocraftingAvailable()) {
|
||||||
return new RecipeTransferPatternGridError(tracker);
|
return new RecipeTransferPatternGridError(tracker);
|
||||||
@@ -140,11 +140,11 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
|||||||
return System.currentTimeMillis() - lastTransferTimeMs <= TRANSFER_SCROLLBAR_DELAY_MS;
|
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();
|
this.lastTransferTimeMs = System.currentTimeMillis();
|
||||||
|
|
||||||
if (gridContainer.getGrid().getGridType() == GridType.PATTERN && !recipeLayout.getRecipeCategory().getUid().equals(VanillaRecipeCategoryUid.CRAFTING)) {
|
if (gridContainer.getGrid().getGridType() == GridType.PATTERN && !recipeLayout.getRecipeCategory().getUid().equals(VanillaRecipeCategoryUid.CRAFTING)) {
|
||||||
moveForProcessing(recipeLayout);
|
moveForProcessing(recipeLayout, tracker);
|
||||||
} else {
|
} else {
|
||||||
move(gridContainer, recipeLayout);
|
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> inputs = new LinkedList<>();
|
||||||
List<ItemStack> outputs = new LinkedList<>();
|
List<ItemStack> outputs = new LinkedList<>();
|
||||||
|
|
||||||
@@ -165,7 +165,7 @@ public class GridRecipeTransferHandler implements IRecipeTransferHandler<GridCon
|
|||||||
List<FluidStack> fluidOutputs = new LinkedList<>();
|
List<FluidStack> fluidOutputs = new LinkedList<>();
|
||||||
|
|
||||||
for (IGuiIngredient<ItemStack> guiIngredient : recipeLayout.getItemStacks().getGuiIngredients().values()) {
|
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()) {
|
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) {
|
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()) {
|
if (guiIngredient.isInput()) {
|
||||||
inputs.add(ingredient);
|
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.IRecipeLayout;
|
||||||
import mezz.jei.api.gui.ingredient.IGuiIngredient;
|
import mezz.jei.api.gui.ingredient.IGuiIngredient;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class IngredientTracker {
|
public class IngredientTracker {
|
||||||
private final List<Ingredient> ingredients = new ArrayList<>();
|
private final List<Ingredient> ingredients = new ArrayList<>();
|
||||||
|
private final Map<ResourceLocation, Integer> storedItems = new HashMap<>();
|
||||||
|
|
||||||
public IngredientTracker(IRecipeLayout recipeLayout) {
|
public IngredientTracker(IRecipeLayout recipeLayout) {
|
||||||
for (IGuiIngredient<ItemStack> guiIngredient : recipeLayout.getItemStacks().getGuiIngredients().values()) {
|
for (IGuiIngredient<ItemStack> guiIngredient : recipeLayout.getItemStacks().getGuiIngredients().values()) {
|
||||||
@@ -27,6 +29,7 @@ public class IngredientTracker {
|
|||||||
|
|
||||||
public void addAvailableStack(ItemStack stack, @Nullable IGridStack gridStack) {
|
public void addAvailableStack(ItemStack stack, @Nullable IGridStack gridStack) {
|
||||||
int available = stack.getCount();
|
int available = stack.getCount();
|
||||||
|
storedItems.merge(stack.getItem().getRegistryName(), available, Integer::sum);
|
||||||
|
|
||||||
for (Ingredient ingredient : ingredients) {
|
for (Ingredient ingredient : ingredients) {
|
||||||
if (available == 0) {
|
if (available == 0) {
|
||||||
@@ -78,4 +81,19 @@ public class IngredientTracker {
|
|||||||
|
|
||||||
return toRequest;
|
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