From 60d9d860ce53736639d74201db12a2ff8f389086 Mon Sep 17 00:00:00 2001 From: Raoul Van den Berge Date: Wed, 9 Nov 2016 17:58:42 +0100 Subject: [PATCH] Fixed storage cache counting up, #591 --- .../api/storage/item/IItemStorageCache.java | 8 ++-- .../api/util/IItemStackList.java | 12 +++++- .../storage/item/ItemStorageCache.java | 16 ++++---- .../apiimpl/util/ItemStackList.java | 8 ++-- .../apiimpl/util/ItemStackListOredicted.java | 4 +- .../refinedstorage/tile/TileController.java | 4 +- .../externalstorage/ItemStorageExternal.java | 40 +++++++------------ 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/raoulvdberge/refinedstorage/api/storage/item/IItemStorageCache.java b/src/main/java/com/raoulvdberge/refinedstorage/api/storage/item/IItemStorageCache.java index 90eaf23ec..bc3cb9194 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/api/storage/item/IItemStorageCache.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/api/storage/item/IItemStorageCache.java @@ -30,19 +30,21 @@ public interface IItemStorageCache { * Will merge it with another item if it already exists. * * @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 */ - void add(@Nonnull ItemStack stack, boolean rebuilding); + void add(@Nonnull ItemStack stack, int size, boolean rebuilding); /** * Removes an item from 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 size the size to remove */ - void remove(@Nonnull ItemStack stack); + void remove(@Nonnull ItemStack stack, int size); /** * @return the list behind this cache diff --git a/src/main/java/com/raoulvdberge/refinedstorage/api/util/IItemStackList.java b/src/main/java/com/raoulvdberge/refinedstorage/api/util/IItemStackList.java index a9da85c19..044bbf76c 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/api/util/IItemStackList.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/api/util/IItemStackList.java @@ -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. * * @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. diff --git a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/storage/item/ItemStorageCache.java b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/storage/item/ItemStorageCache.java index 0aeeab786..d4804c378 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/storage/item/ItemStorageCache.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/storage/item/ItemStorageCache.java @@ -40,7 +40,7 @@ public class ItemStorageCache implements IItemStorageCache { for (ItemStack stack : storage.getStacks()) { 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()) { ItemStack patternStack = output.copy(); patternStack.stackSize = 0; - add(patternStack, true); + add(patternStack, patternStack.stackSize, true); } } @@ -57,18 +57,18 @@ public class ItemStorageCache implements IItemStorageCache { } @Override - public synchronized void add(@Nonnull ItemStack stack, boolean rebuilding) { - list.add(stack); + public synchronized void add(@Nonnull ItemStack stack, int size, boolean rebuilding) { + list.add(stack, size); if (!rebuilding) { - network.sendItemStorageDeltaToClient(stack, stack.stackSize); + network.sendItemStorageDeltaToClient(stack, size); } } @Override - public synchronized void remove(@Nonnull ItemStack stack) { - if (list.remove(stack, !network.hasPattern(stack))) { - network.sendItemStorageDeltaToClient(stack, -stack.stackSize); + public synchronized void remove(@Nonnull ItemStack stack, int size) { + if (list.remove(stack, size, !network.hasPattern(stack))) { + network.sendItemStorageDeltaToClient(stack, -size); } } diff --git a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/util/ItemStackList.java b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/util/ItemStackList.java index 17bf928d9..d93d4e67d 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/util/ItemStackList.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/util/ItemStackList.java @@ -20,20 +20,20 @@ public class ItemStackList implements IItemStackList { private List removeTracker = new LinkedList<>(); @Override - public void add(ItemStack stack) { + public void add(@Nonnull ItemStack stack, int size) { for (ItemStack otherStack : stacks.get(stack.getItem())) { 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; } else { - otherStack.stackSize += stack.stackSize; + otherStack.stackSize += size; } return; } } - stacks.put(stack.getItem(), stack.copy()); + stacks.put(stack.getItem(), size == 0 ? stack.copy() : ItemHandlerHelper.copyStackWithSize(stack, size)); } @Override diff --git a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/util/ItemStackListOredicted.java b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/util/ItemStackListOredicted.java index 2a6aecc06..e6c6b7b2d 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/util/ItemStackListOredicted.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/util/ItemStackListOredicted.java @@ -34,8 +34,8 @@ public class ItemStackListOredicted implements IItemStackList { } @Override - public void add(ItemStack stack) { - underlyingList.add(stack); + public void add(@Nonnull ItemStack stack, int size) { + underlyingList.add(stack, size); ItemStack internalStack = underlyingList.get(stack); if (internalStack != null && internalStack.stackSize == stack.stackSize) { for (int id : OreDictionary.getOreIDs(internalStack)) { diff --git a/src/main/java/com/raoulvdberge/refinedstorage/tile/TileController.java b/src/main/java/com/raoulvdberge/refinedstorage/tile/TileController.java index 22ad57894..208a11723 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/tile/TileController.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/tile/TileController.java @@ -599,7 +599,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR } 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); for (ICraftingTask task : craftingTasks) { @@ -647,7 +647,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR } if (newStack != null && !simulate) { - itemStorage.remove(newStack); + itemStorage.remove(newStack, newStack.stackSize); } return newStack; diff --git a/src/main/java/com/raoulvdberge/refinedstorage/tile/externalstorage/ItemStorageExternal.java b/src/main/java/com/raoulvdberge/refinedstorage/tile/externalstorage/ItemStorageExternal.java index 48f5316e7..ee9fd5d92 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/tile/externalstorage/ItemStorageExternal.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/tile/externalstorage/ItemStorageExternal.java @@ -4,9 +4,7 @@ import com.raoulvdberge.refinedstorage.api.network.INetworkMaster; import com.raoulvdberge.refinedstorage.api.storage.item.IItemStorage; import com.raoulvdberge.refinedstorage.apiimpl.API; import net.minecraft.item.ItemStack; -import net.minecraftforge.items.ItemHandlerHelper; -import java.util.LinkedList; import java.util.List; public abstract class ItemStorageExternal implements IItemStorage { @@ -18,8 +16,6 @@ public abstract class ItemStorageExternal implements IItemStorage { if (cache == null) { updateForced(); } else { - LinkedList changes = new LinkedList<>(); - List newStacks = getStacks(); 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 (i >= cache.size()) { if (actual != null) { - changes.add(ItemHandlerHelper.copyStackWithSize(actual, actual.stackSize)); + network.getItemStorageCache().add(actual, actual.stackSize, false); } continue; @@ -38,19 +34,25 @@ public abstract class ItemStorageExternal implements IItemStorage { if (cached != null && actual == null) { // 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) { // 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) { // If they're both null, nothing happens } else if (!API.instance().getComparer().isEqualNoQuantity(cached, actual)) { // If both items mismatch, remove the old and add the new - changes.add(ItemHandlerHelper.copyStackWithSize(cached, -cached.stackSize)); - changes.add(ItemHandlerHelper.copyStackWithSize(actual, actual.stackSize)); + network.getItemStorageCache().remove(cached, cached.stackSize); + network.getItemStorageCache().add(actual, actual.stackSize, false); } else if (cached.stackSize != actual.stackSize) { - // If both items mismatch on itemcount, apply the change - changes.add(ItemHandlerHelper.copyStackWithSize(cached, actual.stackSize - cached.stackSize)); + // If both items mismatch on item count, apply the change + 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()) { for (int i = newStacks.size(); i < cache.size(); ++i) { if (cache.get(i) != null) { - ItemStack change = ItemHandlerHelper.copyStackWithSize(cache.get(i), -cache.get(i).stackSize); - - if (change != null) { - changes.add(change); - } + network.getItemStorageCache().remove(cache.get(i), cache.get(i).stackSize); } } } this.cache = newStacks; - - for (ItemStack change : changes) { - if (change != null) { - if (change.stackSize > 0) { - network.getItemStorageCache().add(change, false); - } else { - network.getItemStorageCache().remove(change); - } - } - } } }