Fixed storage cache counting up, #591

This commit is contained in:
Raoul Van den Berge
2016-11-09 17:58:42 +01:00
parent ec8438b6d2
commit 60d9d860ce
7 changed files with 46 additions and 46 deletions

View File

@@ -30,19 +30,21 @@ public interface IItemStorageCache {
* Will merge it with another item if it already exists. * Will merge it with another item if it already exists.
* *
* @param stack the stack to add, do NOT modify * @param stack the stack to add, do NOT modify
* @param size the size to add
* @param rebuilding true if this method is called while rebuilding, false otherwise * @param rebuilding true if this method is called while rebuilding, false otherwise
*/ */
void add(@Nonnull ItemStack stack, boolean rebuilding); void add(@Nonnull ItemStack stack, int size, boolean rebuilding);
/** /**
* Removes an item from the cache. * Removes an item from the cache.
* <p> * <p>
* Note that this doesn't modify any of the connected storages, but just modifies the cache. * Note that this doesn't modify any of the connected storages, but just modifies the cache.
* Use {@link INetworkMaster#extractItem(ItemStack, int, int)} to remove an item from an actual storage. * Use {@link INetworkMaster#extractItem(ItemStack, int, int, boolean)} to remove an item from an actual storage.
* *
* @param stack the item to remove, do NOT modify * @param stack the item to remove, do NOT modify
* @param size the size to remove
*/ */
void remove(@Nonnull ItemStack stack); void remove(@Nonnull ItemStack stack, int size);
/** /**
* @return the list behind this cache * @return the list behind this cache

View File

@@ -16,8 +16,18 @@ public interface IItemStackList {
* Adds a stack to the list, will merge it with another stack if it already exists in the list. * Adds a stack to the list, will merge it with another stack if it already exists in the list.
* *
* @param stack the stack * @param stack the stack
* @param size the size to add
*/ */
void add(ItemStack stack); void add(@Nonnull ItemStack stack, int size);
/**
* Adds a stack to the list, will merge it with another stack if it already exists in the list.
*
* @param stack the stack
*/
default void add(@Nonnull ItemStack stack) {
add(stack, stack.stackSize);
}
/** /**
* Decrements the count of that stack in the list. * Decrements the count of that stack in the list.

View File

@@ -40,7 +40,7 @@ public class ItemStorageCache implements IItemStorageCache {
for (ItemStack stack : storage.getStacks()) { for (ItemStack stack : storage.getStacks()) {
if (stack != null) { if (stack != null) {
add(stack, true); add(stack, stack.stackSize, true);
} }
} }
} }
@@ -49,7 +49,7 @@ public class ItemStorageCache implements IItemStorageCache {
for (ItemStack output : pattern.getOutputs()) { for (ItemStack output : pattern.getOutputs()) {
ItemStack patternStack = output.copy(); ItemStack patternStack = output.copy();
patternStack.stackSize = 0; patternStack.stackSize = 0;
add(patternStack, true); add(patternStack, patternStack.stackSize, true);
} }
} }
@@ -57,18 +57,18 @@ public class ItemStorageCache implements IItemStorageCache {
} }
@Override @Override
public synchronized void add(@Nonnull ItemStack stack, boolean rebuilding) { public synchronized void add(@Nonnull ItemStack stack, int size, boolean rebuilding) {
list.add(stack); list.add(stack, size);
if (!rebuilding) { if (!rebuilding) {
network.sendItemStorageDeltaToClient(stack, stack.stackSize); network.sendItemStorageDeltaToClient(stack, size);
} }
} }
@Override @Override
public synchronized void remove(@Nonnull ItemStack stack) { public synchronized void remove(@Nonnull ItemStack stack, int size) {
if (list.remove(stack, !network.hasPattern(stack))) { if (list.remove(stack, size, !network.hasPattern(stack))) {
network.sendItemStorageDeltaToClient(stack, -stack.stackSize); network.sendItemStorageDeltaToClient(stack, -size);
} }
} }

View File

@@ -20,20 +20,20 @@ public class ItemStackList implements IItemStackList {
private List<ItemStack> removeTracker = new LinkedList<>(); private List<ItemStack> removeTracker = new LinkedList<>();
@Override @Override
public void add(ItemStack stack) { public void add(@Nonnull ItemStack stack, int size) {
for (ItemStack otherStack : stacks.get(stack.getItem())) { for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (API.instance().getComparer().isEqualNoQuantity(otherStack, stack)) { if (API.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
if ((long) otherStack.stackSize + (long) stack.stackSize > Integer.MAX_VALUE) { if ((long) otherStack.stackSize + (long) size > Integer.MAX_VALUE) {
otherStack.stackSize = Integer.MAX_VALUE; otherStack.stackSize = Integer.MAX_VALUE;
} else { } else {
otherStack.stackSize += stack.stackSize; otherStack.stackSize += size;
} }
return; return;
} }
} }
stacks.put(stack.getItem(), stack.copy()); stacks.put(stack.getItem(), size == 0 ? stack.copy() : ItemHandlerHelper.copyStackWithSize(stack, size));
} }
@Override @Override

View File

@@ -34,8 +34,8 @@ public class ItemStackListOredicted implements IItemStackList {
} }
@Override @Override
public void add(ItemStack stack) { public void add(@Nonnull ItemStack stack, int size) {
underlyingList.add(stack); underlyingList.add(stack, size);
ItemStack internalStack = underlyingList.get(stack); ItemStack internalStack = underlyingList.get(stack);
if (internalStack != null && internalStack.stackSize == stack.stackSize) { if (internalStack != null && internalStack.stackSize == stack.stackSize) {
for (int id : OreDictionary.getOreIDs(internalStack)) { for (int id : OreDictionary.getOreIDs(internalStack)) {

View File

@@ -599,7 +599,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
} }
if (!simulate && inserted > 0 && accessType != AccessType.INSERT) { if (!simulate && inserted > 0 && accessType != AccessType.INSERT) {
itemStorage.add(ItemHandlerHelper.copyStackWithSize(stack, inserted), false); itemStorage.add(stack, inserted, false);
ItemStack checkSteps = ItemHandlerHelper.copyStackWithSize(stack, inserted); ItemStack checkSteps = ItemHandlerHelper.copyStackWithSize(stack, inserted);
for (ICraftingTask task : craftingTasks) { for (ICraftingTask task : craftingTasks) {
@@ -647,7 +647,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
} }
if (newStack != null && !simulate) { if (newStack != null && !simulate) {
itemStorage.remove(newStack); itemStorage.remove(newStack, newStack.stackSize);
} }
return newStack; return newStack;

View File

@@ -4,9 +4,7 @@ import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import com.raoulvdberge.refinedstorage.api.storage.item.IItemStorage; import com.raoulvdberge.refinedstorage.api.storage.item.IItemStorage;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.apiimpl.API;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;
import java.util.LinkedList;
import java.util.List; import java.util.List;
public abstract class ItemStorageExternal implements IItemStorage { public abstract class ItemStorageExternal implements IItemStorage {
@@ -18,8 +16,6 @@ public abstract class ItemStorageExternal implements IItemStorage {
if (cache == null) { if (cache == null) {
updateForced(); updateForced();
} else { } else {
LinkedList<ItemStack> changes = new LinkedList<>();
List<ItemStack> newStacks = getStacks(); List<ItemStack> newStacks = getStacks();
for (int i = 0; i < newStacks.size(); ++i) { for (int i = 0; i < newStacks.size(); ++i) {
@@ -28,7 +24,7 @@ public abstract class ItemStorageExternal implements IItemStorage {
// If we exceed the cache size, than that means this items is added // If we exceed the cache size, than that means this items is added
if (i >= cache.size()) { if (i >= cache.size()) {
if (actual != null) { if (actual != null) {
changes.add(ItemHandlerHelper.copyStackWithSize(actual, actual.stackSize)); network.getItemStorageCache().add(actual, actual.stackSize, false);
} }
continue; continue;
@@ -38,19 +34,25 @@ public abstract class ItemStorageExternal implements IItemStorage {
if (cached != null && actual == null) { if (cached != null && actual == null) {
// If the cached is not null but the actual is, we removed this item // If the cached is not null but the actual is, we removed this item
changes.add(ItemHandlerHelper.copyStackWithSize(cached, -cached.stackSize)); network.getItemStorageCache().remove(cached, cached.stackSize);
} else if (cached == null && actual != null) { } else if (cached == null && actual != null) {
// If the cached is null and the actual isn't, we added this item // If the cached is null and the actual isn't, we added this item
changes.add(ItemHandlerHelper.copyStackWithSize(actual, actual.stackSize)); network.getItemStorageCache().add(actual, actual.stackSize, false);
} else if (cached == null && actual == null) { } else if (cached == null && actual == null) {
// If they're both null, nothing happens // If they're both null, nothing happens
} else if (!API.instance().getComparer().isEqualNoQuantity(cached, actual)) { } else if (!API.instance().getComparer().isEqualNoQuantity(cached, actual)) {
// If both items mismatch, remove the old and add the new // If both items mismatch, remove the old and add the new
changes.add(ItemHandlerHelper.copyStackWithSize(cached, -cached.stackSize)); network.getItemStorageCache().remove(cached, cached.stackSize);
changes.add(ItemHandlerHelper.copyStackWithSize(actual, actual.stackSize)); network.getItemStorageCache().add(actual, actual.stackSize, false);
} else if (cached.stackSize != actual.stackSize) { } else if (cached.stackSize != actual.stackSize) {
// If both items mismatch on itemcount, apply the change // If both items mismatch on item count, apply the change
changes.add(ItemHandlerHelper.copyStackWithSize(cached, actual.stackSize - cached.stackSize)); int delta = actual.stackSize - cached.stackSize;
if (delta > 0) {
network.getItemStorageCache().add(cached, delta, false);
} else {
network.getItemStorageCache().remove(cached, delta);
}
} }
} }
@@ -59,26 +61,12 @@ public abstract class ItemStorageExternal implements IItemStorage {
if (cache.size() > newStacks.size()) { if (cache.size() > newStacks.size()) {
for (int i = newStacks.size(); i < cache.size(); ++i) { for (int i = newStacks.size(); i < cache.size(); ++i) {
if (cache.get(i) != null) { if (cache.get(i) != null) {
ItemStack change = ItemHandlerHelper.copyStackWithSize(cache.get(i), -cache.get(i).stackSize); network.getItemStorageCache().remove(cache.get(i), cache.get(i).stackSize);
if (change != null) {
changes.add(change);
}
} }
} }
} }
this.cache = newStacks; this.cache = newStacks;
for (ItemStack change : changes) {
if (change != null) {
if (change.stackSize > 0) {
network.getItemStorageCache().add(change, false);
} else {
network.getItemStorageCache().remove(change);
}
}
}
} }
} }