Skip to content

Commit

Permalink
JEI transfer picks the most common ingredient
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkere committed May 13, 2021
1 parent c45991e commit 6d4f794
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

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 {
Expand Down Expand Up @@ -193,6 +195,11 @@ public void onRecipeTransfer(INetworkAwareGrid grid, PlayerEntity player, ItemSt
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private RecipeTransferCraftingGridError transferRecipeForCraftingGrid(GridContai
)
));
} else {
moveItems(container, recipeLayout);
moveItems(container, recipeLayout, tracker);
}
} else {
if (tracker.hasMissing()) {
Expand All @@ -87,7 +87,7 @@ private IRecipeTransferError transferRecipeForPatternGrid(GridContainer containe
IngredientTracker tracker = createTracker(container, recipeLayout, player);

if (doTransfer) {
moveItems(container, recipeLayout);
moveItems(container, recipeLayout, tracker);
} else {
if (tracker.isAutocraftingAvailable()) {
return new RecipeTransferPatternGridError(tracker);
Expand Down Expand Up @@ -140,11 +140,11 @@ public boolean hasTransferredRecently() {
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);
}
Expand All @@ -157,15 +157,15 @@ private void move(GridContainer gridContainer, IRecipeLayout recipeLayout) {
));
}

private void moveForProcessing(IRecipeLayout recipeLayout) {
private void moveForProcessing(IRecipeLayout recipeLayout, IngredientTracker tracker) {
List<ItemStack> inputs = new LinkedList<>();
List<ItemStack> outputs = new LinkedList<>();

List<FluidStack> fluidInputs = new LinkedList<>();
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()) {
Expand All @@ -187,9 +187,13 @@ private void handleFluidIngredient(List<FluidStack> fluidInputs, List<FluidStack
}
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
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()) {
Expand All @@ -27,6 +29,7 @@ public Collection<Ingredient> getIngredients() {

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) {
Expand Down Expand Up @@ -78,4 +81,19 @@ public Map<UUID, Integer> createCraftingRequests() {

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;
}
}

0 comments on commit 6d4f794

Please sign in to comment.