Use IItemStackList for toTake
This commit is contained in:
@@ -21,10 +21,22 @@ public interface IItemStackList {
|
|||||||
* Decrements the count of that stack in the list.
|
* Decrements the count of that stack in the list.
|
||||||
*
|
*
|
||||||
* @param stack the stack
|
* @param stack the stack
|
||||||
|
* @param size the size to remove
|
||||||
* @param removeIfReachedZero true to remove the stack if the count reaches 0, false otherwise
|
* @param removeIfReachedZero true to remove the stack if the count reaches 0, false otherwise
|
||||||
* @return whether the remove was successful
|
* @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.
|
* Returns a stack.
|
||||||
|
|||||||
@@ -13,18 +13,17 @@ import refinedstorage.api.util.IItemStackList;
|
|||||||
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementRoot;
|
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementRoot;
|
||||||
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementToTake;
|
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementToTake;
|
||||||
|
|
||||||
import java.util.ArrayDeque;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Deque;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class CraftingTaskNormal implements ICraftingTask {
|
public class CraftingTaskNormal implements ICraftingTask {
|
||||||
private INetworkMaster network;
|
private INetworkMaster network;
|
||||||
private ItemStack requested;
|
private ItemStack requested;
|
||||||
private ICraftingPattern pattern;
|
private ICraftingPattern pattern;
|
||||||
private int quantity;
|
private int quantity;
|
||||||
private Deque<ItemStack> toTake = new ArrayDeque<>();
|
|
||||||
private List<IProcessable> toProcess = new ArrayList<>();
|
private List<IProcessable> toProcess = new ArrayList<>();
|
||||||
|
private IItemStackList toTake = RSAPI.instance().createItemStackList();
|
||||||
private IItemStackList toCraft = RSAPI.instance().createItemStackList();
|
private IItemStackList toCraft = RSAPI.instance().createItemStackList();
|
||||||
private IItemStackList missing = RSAPI.instance().createItemStackList();
|
private IItemStackList missing = RSAPI.instance().createItemStackList();
|
||||||
private IItemStackList extras = RSAPI.instance().createItemStackList();
|
private IItemStackList extras = RSAPI.instance().createItemStackList();
|
||||||
@@ -74,7 +73,7 @@ public class CraftingTaskNormal implements ICraftingTask {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!pattern.isProcessing()) {
|
if (!pattern.isProcessing()) {
|
||||||
toTake.push(input);
|
toTake.add(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
list.remove(input, true);
|
list.remove(input, true);
|
||||||
@@ -113,12 +112,14 @@ public class CraftingTaskNormal implements ICraftingTask {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!toTake.isEmpty()) {
|
for (ItemStack toTakeStack : toTake.getStacks()) {
|
||||||
ItemStack took = network.extractItem(toTake.peek(), 1);
|
ItemStack took = network.extractItem(toTakeStack, 1);
|
||||||
|
|
||||||
if (took != null) {
|
if (took != null) {
|
||||||
toTake.pop();
|
toTake.remove(toTakeStack, 1, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (toTake.isEmpty() && missing.isEmpty() && toProcess.stream().allMatch(IProcessable::hasReceivedOutputs)) {
|
if (toTake.isEmpty() && missing.isEmpty() && toProcess.stream().allMatch(IProcessable::hasReceivedOutputs)) {
|
||||||
@@ -158,17 +159,10 @@ public class CraftingTaskNormal implements ICraftingTask {
|
|||||||
quantity
|
quantity
|
||||||
));
|
));
|
||||||
|
|
||||||
if (!toTake.isEmpty()) {
|
elements.addAll(toTake.getStacks().stream()
|
||||||
IItemStackList toTake = RSAPI.instance().createItemStackList();
|
.map(stack -> new CraftingMonitorElementToTake(stack, stack.stackSize))
|
||||||
|
.collect(Collectors.toList())
|
||||||
for (ItemStack stack : new ArrayList<>(this.toTake)) {
|
);
|
||||||
toTake.add(stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ItemStack stack : toTake.getStacks()) {
|
|
||||||
elements.add(new CraftingMonitorElementToTake(stack, stack.stackSize));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return elements;
|
return elements;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package refinedstorage.apiimpl.util;
|
package refinedstorage.apiimpl.util;
|
||||||
|
|
||||||
import com.google.common.collect.ArrayListMultimap;
|
import com.google.common.collect.ArrayListMultimap;
|
||||||
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 refinedstorage.api.RSAPI;
|
import refinedstorage.api.RSAPI;
|
||||||
@@ -12,8 +11,9 @@ import javax.annotation.Nullable;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
public class ItemStackList implements IItemStackList {
|
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) {
|
public void add(ItemStack stack) {
|
||||||
for (ItemStack otherStack : stacks.get(stack.getItem())) {
|
for (ItemStack otherStack : stacks.get(stack.getItem())) {
|
||||||
if (RSAPI.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
|
if (RSAPI.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
|
||||||
@@ -26,12 +26,13 @@ public class ItemStackList implements IItemStackList {
|
|||||||
stacks.put(stack.getItem(), stack.copy());
|
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())) {
|
for (ItemStack otherStack : stacks.get(stack.getItem())) {
|
||||||
if (RSAPI.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
|
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);
|
stacks.remove(otherStack.getItem(), otherStack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,6 +43,7 @@ public class ItemStackList implements IItemStackList {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public ItemStack get(@Nonnull ItemStack stack, int flags) {
|
public ItemStack get(@Nonnull ItemStack stack, int flags) {
|
||||||
for (ItemStack otherStack : stacks.get(stack.getItem())) {
|
for (ItemStack otherStack : stacks.get(stack.getItem())) {
|
||||||
@@ -53,6 +55,7 @@ public class ItemStackList implements IItemStackList {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public ItemStack get(int hash) {
|
public ItemStack get(int hash) {
|
||||||
for (ItemStack stack : this.stacks.values()) {
|
for (ItemStack stack : this.stacks.values()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user