Skip to content

Commit

Permalink
check playerinventory and patterns too
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkere committed Jun 9, 2021
1 parent e353efe commit 7d6bc10
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import com.refinedmods.refinedstorage.api.util.IStackList;
import com.refinedmods.refinedstorage.apiimpl.API;
import com.refinedmods.refinedstorage.apiimpl.network.node.GridNetworkNode;
import com.refinedmods.refinedstorage.item.PatternItem;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ICraftingRecipe;
import net.minecraft.util.NonNullList;
Expand All @@ -21,10 +23,9 @@
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;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class CraftingGridBehavior implements ICraftingGridBehavior {
@Override
Expand Down Expand Up @@ -190,17 +191,16 @@ public void onRecipeTransfer(INetworkAwareGrid grid, PlayerEntity player, ItemSt
}
}

AtomicReference<Map<Item, ItemStack>> playerItems = new AtomicReference<>();
// Now let's fill the matrix
for (int i = 0; i < grid.getCraftingMatrix().getSizeInventory(); ++i) {
if (recipe[i] != null) {
ItemStack[] possibilities = recipe[i];

if (network != null && grid.isGridActive() && network.getItemStorageCache() != null) {
// sort by the number of items in storage
Arrays.sort(possibilities, Comparator.comparingInt((ItemStack a) -> {
ItemStack stack = network.getItemStorageCache().getList().get(a);
return stack == null ? 0 : stack.getCount();
}).reversed());

// sort by the number of items in storage, craftables and inventory
Arrays.sort(possibilities, compareByItemStackCounts(player, network, playerItems));
}

// If we are a crafting grid
Expand Down Expand Up @@ -256,4 +256,52 @@ public void onRecipeTransfer(INetworkAwareGrid grid, PlayerEntity player, ItemSt
((GridNetworkNode) grid).markDirty();
}
}

private Comparator<ItemStack> compareByItemStackCounts(PlayerEntity player, INetwork network, AtomicReference<Map<Item, ItemStack>> playerItems) {
AtomicInteger s = new AtomicInteger();
return Comparator.comparingInt((ItemStack itemStack) -> {

ItemStack stack = network.getItemStorageCache().getList().get(itemStack);
if (stack != null) {
return stack.getCount();
}

if (network.getCraftingManager().getPattern(itemStack) != null) {
return 1;
}

if (playerItems.get() == null) {
playerItems.set(makePlayerInventoryMap(player, network));
} else {
System.out.println(s.incrementAndGet());
}

ItemStack onPlayer = playerItems.get().get(itemStack.getItem());
if (onPlayer != null) {
return onPlayer.getCount();
}
return 0;
}).reversed();
}

private Map<Item, ItemStack> makePlayerInventoryMap(PlayerEntity player, INetwork network) {
Map<Item, ItemStack> playerItems = new HashMap<>();
for (int j = 0; j < player.inventory.getSizeInventory(); j++) {
ItemStack inventoryStack = player.inventory.getStackInSlot(j);

if (inventoryStack.getItem() instanceof PatternItem) {
NonNullList<ItemStack> patternOutput = PatternItem.fromCache(network.getWorld(), inventoryStack).getOutputs();
for (ItemStack stack : patternOutput) {
if (!stack.isEmpty()) {
playerItems.put(stack.getItem(), stack);
}
}
} else {
playerItems.put(inventoryStack.getItem(), inventoryStack);
}
}

return playerItems;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.refinedmods.refinedstorage.api.util.IComparer;
import com.refinedmods.refinedstorage.apiimpl.API;
import com.refinedmods.refinedstorage.item.PatternItem;
import com.refinedmods.refinedstorage.screen.grid.stack.IGridStack;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.ingredient.IGuiIngredient;
Expand Down Expand Up @@ -29,7 +30,12 @@ public Collection<Ingredient> getIngredients() {

public void addAvailableStack(ItemStack stack, @Nullable IGridStack gridStack) {
int available = stack.getCount();
storedItems.merge(stack.getItem().getRegistryName(), available, Integer::sum);
if (stack.getItem() instanceof PatternItem) {
ItemStack outputStack = PatternItem.getOutputSlot(stack, 0);
storedItems.merge(outputStack.getItem().getRegistryName(), available, Integer::sum);
} else {
storedItems.merge(stack.getItem().getRegistryName(), available, Integer::sum);
}

for (Ingredient ingredient : ingredients) {
if (available == 0) {
Expand Down

0 comments on commit 7d6bc10

Please sign in to comment.