Use IItemStackList for toTake

This commit is contained in:
Raoul Van den Berge
2016-10-07 23:15:49 +02:00
parent fff627b2ec
commit b7f48357c1
3 changed files with 33 additions and 24 deletions

View File

@@ -21,10 +21,22 @@ public interface IItemStackList {
* Decrements the count of that stack in the list.
*
* @param stack the stack
* @param size the size to remove
* @param removeIfReachedZero true to remove the stack if the count reaches 0, false otherwise
* @return whether the remove was successful
*/
boolean remove(@Nonnull ItemStack stack, boolean removeIfReachedZero);
boolean remove(@Nonnull ItemStack stack, int size, boolean removeIfReachedZero);
/**
* Decrements the count of that stack in the list.
*
* @param stack the stack
* @param removeIfReachedZero true to remove the stack if the count reaches 0, false otherwise
* @return whether the remove was successful
*/
default boolean remove(@Nonnull ItemStack stack, boolean removeIfReachedZero) {
return remove(stack, stack.stackSize, removeIfReachedZero);
}
/**
* Returns a stack.

View File

@@ -13,18 +13,17 @@ import refinedstorage.api.util.IItemStackList;
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementRoot;
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementToTake;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.stream.Collectors;
public class CraftingTaskNormal implements ICraftingTask {
private INetworkMaster network;
private ItemStack requested;
private ICraftingPattern pattern;
private int quantity;
private Deque<ItemStack> toTake = new ArrayDeque<>();
private List<IProcessable> toProcess = new ArrayList<>();
private IItemStackList toTake = RSAPI.instance().createItemStackList();
private IItemStackList toCraft = RSAPI.instance().createItemStackList();
private IItemStackList missing = RSAPI.instance().createItemStackList();
private IItemStackList extras = RSAPI.instance().createItemStackList();
@@ -74,7 +73,7 @@ public class CraftingTaskNormal implements ICraftingTask {
}
} else {
if (!pattern.isProcessing()) {
toTake.push(input);
toTake.add(input);
}
list.remove(input, true);
@@ -113,12 +112,14 @@ public class CraftingTaskNormal implements ICraftingTask {
}
}
if (!toTake.isEmpty()) {
ItemStack took = network.extractItem(toTake.peek(), 1);
for (ItemStack toTakeStack : toTake.getStacks()) {
ItemStack took = network.extractItem(toTakeStack, 1);
if (took != null) {
toTake.pop();
toTake.remove(toTakeStack, 1, true);
}
break;
}
if (toTake.isEmpty() && missing.isEmpty() && toProcess.stream().allMatch(IProcessable::hasReceivedOutputs)) {
@@ -158,17 +159,10 @@ public class CraftingTaskNormal implements ICraftingTask {
quantity
));
if (!toTake.isEmpty()) {
IItemStackList toTake = RSAPI.instance().createItemStackList();
for (ItemStack stack : new ArrayList<>(this.toTake)) {
toTake.add(stack);
}
for (ItemStack stack : toTake.getStacks()) {
elements.add(new CraftingMonitorElementToTake(stack, stack.stackSize));
}
}
elements.addAll(toTake.getStacks().stream()
.map(stack -> new CraftingMonitorElementToTake(stack, stack.stackSize))
.collect(Collectors.toList())
);
return elements;
}

View File

@@ -1,7 +1,6 @@
package refinedstorage.apiimpl.util;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import refinedstorage.api.RSAPI;
@@ -12,8 +11,9 @@ import javax.annotation.Nullable;
import java.util.Collection;
public class ItemStackList implements IItemStackList {
private Multimap<Item, ItemStack> stacks = ArrayListMultimap.create();
private ArrayListMultimap<Item, ItemStack> stacks = ArrayListMultimap.create();
@Override
public void add(ItemStack stack) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (RSAPI.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
@@ -26,12 +26,13 @@ public class ItemStackList implements IItemStackList {
stacks.put(stack.getItem(), stack.copy());
}
public boolean remove(@Nonnull ItemStack stack, boolean removeIfReachedZero) {
@Override
public boolean remove(@Nonnull ItemStack stack, int size, boolean removeIfReachedZero) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (RSAPI.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
otherStack.stackSize -= stack.stackSize;
otherStack.stackSize -= size;
if (otherStack.stackSize == 0 && removeIfReachedZero) {
if (otherStack.stackSize <= 0 && removeIfReachedZero) {
stacks.remove(otherStack.getItem(), otherStack);
}
@@ -42,6 +43,7 @@ public class ItemStackList implements IItemStackList {
return false;
}
@Override
@Nullable
public ItemStack get(@Nonnull ItemStack stack, int flags) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
@@ -53,6 +55,7 @@ public class ItemStackList implements IItemStackList {
return null;
}
@Override
@Nullable
public ItemStack get(int hash) {
for (ItemStack stack : this.stacks.values()) {