Implement ore dictionary autocrafting.

This commit is contained in:
raoulvdberge
2018-06-08 21:53:20 +02:00
parent aa94d70009
commit cf7e2593e0
3 changed files with 47 additions and 10 deletions

View File

@@ -4,6 +4,7 @@ import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternContainer;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.registry.CraftingTaskFactory;
import com.raoulvdberge.refinedstorage.item.ItemPattern;
import com.raoulvdberge.refinedstorage.util.StackUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
@@ -34,15 +35,21 @@ public class CraftingPattern implements ICraftingPattern {
this.oredict = ItemPattern.isOredict(stack);
if (processing) {
this.valid = true;
for (int i = 0; i < 9; ++i) {
ItemStack input = ItemPattern.getInputSlot(stack, i);
inputs.add(input == null ? NonNullList.create() : NonNullList.from(ItemStack.EMPTY, input));
if (input == null) {
inputs.add(NonNullList.create());
} else if (processing) {
inputs.add(StackUtils.getEquivalentStacks(input));
} else {
inputs.add(NonNullList.from(ItemStack.EMPTY, input));
}
ItemStack output = ItemPattern.getOutputSlot(stack, i);
if (output != null) {
this.valid = true; // As soon as we have one output, we are valid.
outputs.add(output);
}
}

View File

@@ -80,7 +80,30 @@ public class CraftingTask implements ICraftingTask {
continue;
}
ItemStack possibleInput = possibleInputs.get(0); // TODO: Use first for now.
ItemStack possibleInput;
if (possibleInputs.size() == 1) {
possibleInput = possibleInputs.get(0);
} else {
NonNullList<ItemStack> sortedPossibleInputs = NonNullList.create();
sortedPossibleInputs.addAll(possibleInputs);
sortedPossibleInputs.sort((a, b) -> {
ItemStack ar = mutatedStorage.get(a);
ItemStack br = mutatedStorage.get(b);
return (br == null ? 0 : br.getCount()) - (ar == null ? 0 : ar.getCount());
});
sortedPossibleInputs.sort((a, b) -> {
ItemStack ar = results.get(a);
ItemStack br = results.get(b);
return (br == null ? 0 : br.getCount()) - (ar == null ? 0 : ar.getCount());
});
possibleInput = sortedPossibleInputs.get(0);
}
took.add(possibleInput);

View File

@@ -7,6 +7,7 @@ import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.api.util.IStackList;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import io.netty.buffer.ByteBuf;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.init.PotionTypes;
import net.minecraft.inventory.IInventory;
@@ -32,9 +33,7 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@@ -46,22 +45,30 @@ public final class StackUtils {
private static final String NBT_INVENTORY = "Inventory_%d";
private static final String NBT_SLOT = "Slot";
private static final Map<Integer, List<ItemStack>> OREDICT_CACHE = new HashMap<>();
private static final Map<Integer, NonNullList<ItemStack>> OREDICT_CACHE = new HashMap<>();
private static final Map<Integer, Boolean> OREDICT_EQUIVALENCY_CACHE = new HashMap<>();
private static final NonNullList<Object> EMPTY_NON_NULL_LIST = NonNullList.create();
public static List<ItemStack> getEquivalentStacks(ItemStack stack) {
public static NonNullList<ItemStack> getEquivalentStacks(ItemStack stack) {
int hash = API.instance().getItemStackHashCode(stack, false);
if (OREDICT_CACHE.containsKey(hash)) {
return OREDICT_CACHE.get(hash);
}
List<ItemStack> ores = new ArrayList<>();
NonNullList<ItemStack> ores = NonNullList.create();
for (int id : OreDictionary.getOreIDs(stack)) {
ores.addAll(OreDictionary.getOres(OreDictionary.getOreName(id)));
String name = OreDictionary.getOreName(id);
for (ItemStack ore : OreDictionary.getOres(name)) {
if (ore.getMetadata() == OreDictionary.WILDCARD_VALUE) {
ore.getItem().getSubItems(CreativeTabs.SEARCH, ores);
} else {
ores.add(ore);
}
}
}
OREDICT_CACHE.put(hash, ores);