changes to the clean up of StackLists, related to #838
This commit is contained in:
@@ -11,15 +11,14 @@ import org.apache.commons.lang3.tuple.Pair;
|
|||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class StackListItem implements IStackList<ItemStack> {
|
public class StackListItem implements IStackList<ItemStack> {
|
||||||
private ArrayListMultimap<Item, ItemStack> stacks = ArrayListMultimap.create();
|
private ArrayListMultimap<Item, ItemStack> stacks = ArrayListMultimap.create();
|
||||||
private List<ItemStack> removeTracker = new LinkedList<>();
|
private List<ItemStack> removeTracker = new LinkedList<>();
|
||||||
protected boolean needsCleanup = false;
|
protected boolean needsCleanup = false;
|
||||||
|
private Set<Item> touchedItems = new HashSet<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(@Nonnull ItemStack stack, int size) {
|
public void add(@Nonnull ItemStack stack, int size) {
|
||||||
@@ -47,6 +46,7 @@ public class StackListItem implements IStackList<ItemStack> {
|
|||||||
otherStack.shrink(size);
|
otherStack.shrink(size);
|
||||||
|
|
||||||
if (otherStack.isEmpty()) {
|
if (otherStack.isEmpty()) {
|
||||||
|
touchedItems.add(stack.getItem());
|
||||||
needsCleanup = true;
|
needsCleanup = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,6 +68,7 @@ public class StackListItem implements IStackList<ItemStack> {
|
|||||||
otherStack.shrink(size);
|
otherStack.shrink(size);
|
||||||
|
|
||||||
if (otherStack.isEmpty()) {
|
if (otherStack.isEmpty()) {
|
||||||
|
touchedItems.add(stack.getItem());
|
||||||
needsCleanup = true;
|
needsCleanup = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,13 +126,14 @@ public class StackListItem implements IStackList<ItemStack> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clean() {
|
public void clean() {
|
||||||
List<Pair<Item, ItemStack>> toRemove = stacks.asMap().entrySet().stream()
|
List<Pair<Item, ItemStack>> toRemove = touchedItems.stream()
|
||||||
.flatMap(entry -> entry.getValue().stream().map(value -> Pair.of(entry.getKey(), value)))
|
.flatMap(item -> stacks.get(item).stream().map(stack -> Pair.of(item, stack)))
|
||||||
.filter(pair -> pair.getValue().isEmpty())
|
.filter(pair -> pair.getValue().isEmpty())
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
toRemove.forEach(pair -> stacks.remove(pair.getLeft(), pair.getRight()));
|
toRemove.forEach(pair -> stacks.remove(pair.getLeft(), pair.getRight()));
|
||||||
|
|
||||||
|
touchedItems.clear();
|
||||||
needsCleanup = false;
|
needsCleanup = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,17 +5,17 @@ import com.raoulvdberge.refinedstorage.api.util.IComparer;
|
|||||||
import com.raoulvdberge.refinedstorage.api.util.IStackList;
|
import com.raoulvdberge.refinedstorage.api.util.IStackList;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraftforge.oredict.OreDictionary;
|
import net.minecraftforge.oredict.OreDictionary;
|
||||||
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class StackListItemOredicted implements IStackList<ItemStack> {
|
public class StackListItemOredicted implements IStackList<ItemStack> {
|
||||||
private StackListItem underlyingList;
|
private StackListItem underlyingList;
|
||||||
private ArrayListMultimap<Integer, ItemStack> stacks = ArrayListMultimap.create();
|
private ArrayListMultimap<Integer, ItemStack> stacks = ArrayListMultimap.create();
|
||||||
|
private Set<Integer> touchedIds = new HashSet<>();
|
||||||
|
|
||||||
private StackListItemOredicted() {
|
private StackListItemOredicted() {
|
||||||
}
|
}
|
||||||
@@ -50,6 +50,7 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
|
|||||||
public boolean remove(@Nonnull ItemStack stack, int size) {
|
public boolean remove(@Nonnull ItemStack stack, int size) {
|
||||||
boolean rvalue = underlyingList.remove(stack, size);
|
boolean rvalue = underlyingList.remove(stack, size);
|
||||||
if (underlyingList.needsCleanup) {
|
if (underlyingList.needsCleanup) {
|
||||||
|
touchedIds.addAll(Arrays.stream(OreDictionary.getOreIDs(stack)).boxed().collect(Collectors.toList()));
|
||||||
clean();
|
clean();
|
||||||
}
|
}
|
||||||
return rvalue;
|
return rvalue;
|
||||||
@@ -59,6 +60,7 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
|
|||||||
public boolean trackedRemove(@Nonnull ItemStack stack, int size) {
|
public boolean trackedRemove(@Nonnull ItemStack stack, int size) {
|
||||||
boolean rvalue = underlyingList.trackedRemove(stack, size);
|
boolean rvalue = underlyingList.trackedRemove(stack, size);
|
||||||
if (underlyingList.needsCleanup) {
|
if (underlyingList.needsCleanup) {
|
||||||
|
touchedIds.addAll(Arrays.stream(OreDictionary.getOreIDs(stack)).boxed().collect(Collectors.toList()));
|
||||||
clean();
|
clean();
|
||||||
}
|
}
|
||||||
return rvalue;
|
return rvalue;
|
||||||
@@ -117,11 +119,13 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void localClean() {
|
private void localClean() {
|
||||||
List<Map.Entry<Integer, ItemStack>> toRemove = stacks.entries().stream()
|
List<Map.Entry<Integer, ItemStack>> toRemove = touchedIds.stream()
|
||||||
|
.flatMap(id -> stacks.get(id).stream().map(stack -> Pair.of(id, stack)))
|
||||||
.filter(entry -> entry.getValue().isEmpty())
|
.filter(entry -> entry.getValue().isEmpty())
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
toRemove.forEach(entry -> stacks.remove(entry.getKey(), entry.getValue()));
|
toRemove.forEach(entry -> stacks.remove(entry.getKey(), entry.getValue()));
|
||||||
|
touchedIds.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user