Make the new crafting task use the IItemStackList

This commit is contained in:
Raoul Van den Berge
2016-10-07 02:18:58 +02:00
parent 3ee04b6480
commit ca47253ecd
4 changed files with 22 additions and 61 deletions

View File

@@ -30,7 +30,7 @@ public interface IAPI {
ICraftingMonitorElementRegistry getCraftingMonitorElementRegistry(); ICraftingMonitorElementRegistry getCraftingMonitorElementRegistry();
/** /**
* @return a empty fast item stack list * @return an empty item stack list
*/ */
@Nonnull @Nonnull
IItemStackList createItemStackList(); IItemStackList createItemStackList();

View File

@@ -19,6 +19,8 @@ public interface IItemStackList {
void clear(); void clear();
boolean isEmpty();
@Nonnull @Nonnull
Collection<ItemStack> getStacks(); Collection<ItemStack> getStacks();

View File

@@ -1,11 +1,9 @@
package refinedstorage.apiimpl.autocrafting.task; package refinedstorage.apiimpl.autocrafting.task;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import refinedstorage.api.RefinedStorageAPI;
import refinedstorage.api.autocrafting.ICraftingPattern; import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElement; import refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElement;
import refinedstorage.api.autocrafting.task.ICraftingTask; import refinedstorage.api.autocrafting.task.ICraftingTask;
@@ -29,9 +27,9 @@ public class CraftingTaskNormal implements ICraftingTask {
private int quantity; private int quantity;
private Deque<ItemStack> toTake = new ArrayDeque<>(); private Deque<ItemStack> toTake = new ArrayDeque<>();
private List<IProcessable> toProcess = new ArrayList<>(); private List<IProcessable> toProcess = new ArrayList<>();
private Multimap<Item, ItemStack> toCraft = ArrayListMultimap.create(); private IItemStackList toCraft = RefinedStorageAPI.instance().createItemStackList();
private Multimap<Item, ItemStack> missing = ArrayListMultimap.create(); private IItemStackList missing = RefinedStorageAPI.instance().createItemStackList();
private Multimap<Item, ItemStack> extras = ArrayListMultimap.create(); private IItemStackList extras = RefinedStorageAPI.instance().createItemStackList();
public CraftingTaskNormal(INetworkMaster network, ItemStack requested, ICraftingPattern pattern, int quantity) { public CraftingTaskNormal(INetworkMaster network, ItemStack requested, ICraftingPattern pattern, int quantity) {
this.network = network; this.network = network;
@@ -61,19 +59,19 @@ public class CraftingTaskNormal implements ICraftingTask {
ItemStack inputInNetwork = list.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT); ItemStack inputInNetwork = list.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
if (inputInNetwork == null || inputInNetwork.stackSize == 0) { if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
if (getExtrasFor(input) != null) { if (extras.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT) != null) {
decrOrRemoveExtras(input); decrOrRemoveExtras(input);
} else { } else {
ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input); ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input);
if (inputPattern != null) { if (inputPattern != null) {
for (ItemStack output : inputPattern.getOutputs()) { for (ItemStack output : inputPattern.getOutputs()) {
addToCraft(output); toCraft.add(output);
} }
calculate(list, inputPattern, false); calculate(list, inputPattern, false);
} else { } else {
addMissing(input); missing.add(input);
} }
} }
} else { } else {
@@ -163,13 +161,13 @@ public class CraftingTaskNormal implements ICraftingTask {
)); ));
if (!toTake.isEmpty()) { if (!toTake.isEmpty()) {
Multimap<Item, ItemStack> toTake = ArrayListMultimap.create(); IItemStackList toTake = RefinedStorageAPI.instance().createItemStackList();
for (ItemStack stack : new ArrayList<>(this.toTake)) { for (ItemStack stack : new ArrayList<>(this.toTake)) {
add(toTake, stack); toTake.add(stack);
} }
for (ItemStack stack : toTake.values()) { for (ItemStack stack : toTake.getStacks()) {
elements.add(new CraftingMonitorElementToTake(stack, stack.stackSize)); elements.add(new CraftingMonitorElementToTake(stack, stack.stackSize));
} }
} }
@@ -188,57 +186,13 @@ public class CraftingTaskNormal implements ICraftingTask {
return toProcess; return toProcess;
} }
private void addMissing(ItemStack stack) {
add(missing, stack);
}
private void addToCraft(ItemStack stack) {
add(toCraft, stack);
}
private void add(Multimap<Item, ItemStack> map, ItemStack stack) {
for (ItemStack m : map.get(stack.getItem())) {
if (CompareUtils.compareStackNoQuantity(m, stack)) {
m.stackSize += stack.stackSize;
return;
}
}
map.put(stack.getItem(), stack.copy());
}
private void addExtras(ICraftingPattern pattern) { private void addExtras(ICraftingPattern pattern) {
pattern.getOutputs().stream().filter(o -> o.stackSize > 1).forEach(o -> addExtras(ItemHandlerHelper.copyStackWithSize(o, o.stackSize - 1))); pattern.getOutputs().stream()
} .filter(o -> o.stackSize > 1)
.forEach(o -> extras.add(ItemHandlerHelper.copyStackWithSize(o, o.stackSize - 1)));
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) { private void decrOrRemoveExtras(ItemStack stack) {
ItemStack extras = getExtrasFor(stack); extras.remove(ItemHandlerHelper.copyStackWithSize(stack, 1), true);
extras.stackSize--;
if (extras.stackSize == 0) {
this.extras.remove(extras.getItem(), extras);
}
} }
} }

View File

@@ -70,6 +70,11 @@ public class ItemStackList implements IItemStackList {
stacks.clear(); stacks.clear();
} }
@Override
public boolean isEmpty() {
return stacks.isEmpty();
}
@Nonnull @Nonnull
@Override @Override
public Collection<ItemStack> getStacks() { public Collection<ItemStack> getStacks() {