Fix crafting task extras

This commit is contained in:
Raoul Van den Berge
2016-10-04 21:55:36 +02:00
parent 46d108e5e1
commit 73cb8c0cd1
3 changed files with 76 additions and 9 deletions

View File

@@ -64,6 +64,13 @@ public interface IGroupedItemStorage {
@Nullable @Nullable
ItemStack get(int hash); ItemStack get(int hash);
/**
* Copies a grouped item storage.
*
* @return the storage
*/
IGroupedItemStorage copy();
/** /**
* @return all items in this storage network * @return all items in this storage network
*/ */

View File

@@ -4,10 +4,12 @@ import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;
import refinedstorage.api.autocrafting.ICraftingPattern; import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.network.INetworkMaster; import refinedstorage.api.network.INetworkMaster;
import refinedstorage.api.network.NetworkUtils; import refinedstorage.api.network.NetworkUtils;
import refinedstorage.api.storage.CompareUtils; import refinedstorage.api.storage.CompareUtils;
import refinedstorage.api.storage.item.IGroupedItemStorage;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Deque; import java.util.Deque;
@@ -19,6 +21,7 @@ public class CraftingTask {
private Deque<ItemStack> toTake = new ArrayDeque<>(); private Deque<ItemStack> toTake = new ArrayDeque<>();
private Multimap<Item, ItemStack> toCraft = ArrayListMultimap.create(); private Multimap<Item, ItemStack> toCraft = ArrayListMultimap.create();
private Multimap<Item, ItemStack> missing = ArrayListMultimap.create(); private Multimap<Item, ItemStack> missing = ArrayListMultimap.create();
private Multimap<Item, ItemStack> extras = ArrayListMultimap.create();
public CraftingTask(INetworkMaster network, ICraftingPattern pattern, int quantity) { public CraftingTask(INetworkMaster network, ICraftingPattern pattern, int quantity) {
this.network = network; this.network = network;
@@ -31,30 +34,44 @@ public class CraftingTask {
} }
private void calculate(ICraftingPattern pattern, boolean basePattern) { private void calculate(ICraftingPattern pattern, boolean basePattern) {
IGroupedItemStorage itemStorage = network.getItemStorage().copy();
for (int i = 0; i < quantity; ++i) { for (int i = 0; i < quantity; ++i) {
for (ItemStack input : pattern.getInputs()) { for (ItemStack input : pattern.getInputs()) {
ItemStack inputInNetwork = network.getItemStorage().get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT); ItemStack inputInNetwork = itemStorage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
if (inputInNetwork == null || inputInNetwork.stackSize == 0) { if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input); if (getExtrasFor(input) != null) {
decrOrRemoveExtras(input);
if (inputPattern != null) {
addToCraft(input);
calculate(inputPattern, false);
} else { } else {
addMissing(input); ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input);
if (inputPattern != null) {
for (ItemStack output : inputPattern.getOutputs()) {
addToCraft(output);
}
calculate(inputPattern, false);
} else {
addMissing(input);
}
} }
} else { } else {
toTake.push(input); toTake.push(input);
itemStorage.remove(input);
} }
} }
if (!basePattern) {
pattern.getOutputs().stream().filter(o -> o.stackSize > 1).forEach(o -> addExtras(ItemHandlerHelper.copyStackWithSize(o, o.stackSize - 1)));
}
} }
} }
@Override @Override
public String toString() { public String toString() {
return "quantity=" + quantity + ",toTake=" + toTake.toString() + ",toCraft=" + toCraft.toString() + ",missing=" + missing.toString(); return "{quantity=" + quantity + ",toTake=" + toTake.toString() + ",toCraft=" + toCraft.toString() + ",missing=" + missing.toString() + "}";
} }
public boolean update() { public boolean update() {
@@ -95,6 +112,36 @@ public class CraftingTask {
missing.put(stack.getItem(), stack.copy()); missing.put(stack.getItem(), stack.copy());
} }
private void addExtras(ItemStack stack) {
ItemStack extras = getExtrasFor(stack);
if (extras != null) {
extras.stackSize += stack.stackSize;
} else {
this.extras.put(stack.getItem(), stack.copy());
}
}
private ItemStack getExtrasFor(ItemStack stack) {
for (ItemStack m : extras.get(stack.getItem())) {
if (CompareUtils.compareStackNoQuantity(m, stack)) {
return m;
}
}
return null;
}
private void decrOrRemoveExtras(ItemStack stack) {
ItemStack extras = getExtrasFor(stack);
extras.stackSize--;
if (extras.stackSize == 0) {
this.extras.remove(extras.getItem(), extras);
}
}
private void addToCraft(ItemStack stack) { private void addToCraft(ItemStack stack) {
for (ItemStack m : toCraft.get(stack.getItem())) { for (ItemStack m : toCraft.get(stack.getItem())) {
if (CompareUtils.compareStackNoQuantity(m, stack)) { if (CompareUtils.compareStackNoQuantity(m, stack)) {

View File

@@ -118,6 +118,19 @@ public class GroupedItemStorage implements IGroupedItemStorage {
return null; return null;
} }
@Override
public IGroupedItemStorage copy() {
GroupedItemStorage copy = new GroupedItemStorage(network);
copy.storages = storages;
for (ItemStack stack : stacks.values()) {
copy.stacks.put(stack.getItem(), stack.copy());
}
return copy;
}
@Override @Override
public Collection<ItemStack> getStacks() { public Collection<ItemStack> getStacks() {
return stacks.values(); return stacks.values();