Skip to content

Commit

Permalink
fix AContainer can't find recipes with similar items
Browse files Browse the repository at this point in the history
  • Loading branch information
JWJUN233233 committed Apr 27, 2024
1 parent 45601c8 commit 004cbc7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.thebusybiscuit.slimefun4.utils;

import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Set;

public class ItemUtils {
public static int getAllItemAmount(@Nonnull ItemStack... itemStacks) {
int amount = 0;
for (ItemStack itemStack : itemStacks) {
if (itemStack == null || itemStack.getType().isAir()) continue;
amount += itemStack.getAmount();
}

return amount;
}

public static int getAllItemTypeAmount(@Nonnull ItemStack... itemStacks) {
Set<SlimefunItem> sfitems = new HashSet<>();
Set<Material> materials = new HashSet<>();

for (ItemStack itemStack : itemStacks) {
if (itemStack == null || itemStack.getType().isAir()) continue;
SlimefunItem sfitem = SlimefunItem.getByItem(itemStack);
if (sfitem != null) sfitems.add(sfitem);
else materials.add(itemStack.getType());
}

return sfitems.size() + materials.size();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Stream;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.bakedlibs.dough.collections.Pair;
import io.github.thebusybiscuit.slimefun4.utils.ItemUtils;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -416,33 +416,38 @@ protected MachineRecipe findNextRecipe(BlockMenu inv) {
}
}

Map<Integer, Integer> found = new HashMap<>();
Map<MachineRecipe, Map<Integer, Integer>> matched = new HashMap<>();

for (MachineRecipe recipe : recipes) {
Map<Integer, Integer> found = new HashMap<>();

for (ItemStack input : recipe.getInput()) {
for (int slot : getInputSlots()) {
if (SlimefunUtils.isItemSimilar(inventory.get(slot), input, true)) {
found.put(slot, input.getAmount());
break;
}
}
}

if (found.size() == recipe.getInput().length) {
if (!InvUtils.fitAll(inv.toInventory(), recipe.getOutput(), getOutputSlots())) {
return null;
}

for (Map.Entry<Integer, Integer> entry : found.entrySet()) {
inv.consumeItem(entry.getKey(), entry.getValue());
if (found.size() == recipe.getInput().length) {
matched.put(recipe, found);
}

return recipe;
} else {
found.clear();
}
}
if (matched.isEmpty()) return null;
Map.Entry<MachineRecipe, Map<Integer, Integer>> recipe = matched.entrySet().stream().max((x, y) -> {
int sizex = ItemUtils.getAllItemTypeAmount(x.getKey().getInput()) * 1000 + ItemUtils.getAllItemAmount(x.getKey().getInput());
int sizey = ItemUtils.getAllItemTypeAmount(y.getKey().getInput()) * 1000 + ItemUtils.getAllItemAmount(y.getKey().getInput());
return Integer.compare(sizex, sizey);
}).get();

if (!InvUtils.fitAll(inv.toInventory(), recipe.getKey().getOutput(), getOutputSlots())) {
return null;
}

for (Map.Entry<Integer, Integer> entry : recipe.getValue().entrySet()) {
inv.consumeItem(entry.getKey(), entry.getValue());
}

return null;
return recipe.getKey();
}
}

0 comments on commit 004cbc7

Please sign in to comment.