From b0b402e125cccbb94833afbed181f3527dfd53a1 Mon Sep 17 00:00:00 2001 From: Raoul Van den Berge Date: Thu, 10 Nov 2016 21:46:34 +0100 Subject: [PATCH] Fixed External Storage cache not working properly on Compacting Drawers, fixes #591 --- CHANGELOG.md | 2 + .../refinedstorage/tile/TileController.java | 36 ++++-- .../externalstorage/ItemStorageExternal.java | 116 +++++++++--------- 3 files changed, 86 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dbfd3dcd..abdc83b4e 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - Fixed Disk Drive stored quantity GUI text hovering over other text (raoulvdberge) - Fixed External Storage being in item and fluid mode at the same time (raoulvdberge) - Fixed Wrench working when player is not sneaking (raoulvdberge) +- Fixed External Storage cache counting items up when extracting (raoulvdberge) +- Fixed External Storage cache not working properly on Compacting Drawers (raoulvdberge) ### 1.2.3 - Fixed fluid cache updating wrongly (raoulvdberge) diff --git a/src/main/java/com/raoulvdberge/refinedstorage/tile/TileController.java b/src/main/java/com/raoulvdberge/refinedstorage/tile/TileController.java index 35cb8153e..30c7a80d6 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/tile/TileController.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/tile/TileController.java @@ -563,6 +563,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR int orginalSize = size; AccessType accessType = AccessType.INSERT_EXTRACT; ItemStack remainder = stack; + int externalStorageInserted = 0; for (IItemStorage storage : this.itemStorage.getStorages()) { accessType = storage.getAccessType(); @@ -573,13 +574,17 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR if (remainder == null || remainder.stackSize <= 0) { if (storage instanceof ItemStorageExternal && !simulate) { - ((ItemStorageExternal) storage).updateForced(); + ((ItemStorageExternal) storage).detectChanges(this); + // the external storage will send the change, we don't need to anymore + externalStorageInserted += size; } break; } else { if (size != remainder.stackSize && storage instanceof ItemStorageExternal && !simulate) { - ((ItemStorageExternal) storage).updateForced(); + ((ItemStorageExternal) storage).detectChanges(this); + // the external storage will send the change, we don't need to anymore + externalStorageInserted += size - remainder.stackSize; } size = remainder.stackSize; @@ -598,15 +603,19 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR inserted = orginalSize - remainder.stackSize; } - if (!simulate && inserted > 0 && accessType != AccessType.INSERT) { - itemStorage.add(stack, inserted, false); + if (!simulate && accessType != AccessType.INSERT) { + if (inserted - externalStorageInserted > 0) { + itemStorage.add(stack, inserted - externalStorageInserted, false); + } - ItemStack checkSteps = ItemHandlerHelper.copyStackWithSize(stack, inserted); + if (inserted > 0) { + ItemStack checkSteps = ItemHandlerHelper.copyStackWithSize(stack, inserted); - for (ICraftingTask task : craftingTasks) { - for (ICraftingStep processable : task.getSteps()) { - if (processable.onReceiveOutput(checkSteps)) { - return remainder; // All done + for (ICraftingTask task : craftingTasks) { + for (ICraftingStep processable : task.getSteps()) { + if (processable.onReceiveOutput(checkSteps)) { + return remainder; // All done + } } } } @@ -619,6 +628,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR public ItemStack extractItem(@Nonnull ItemStack stack, int size, int flags, boolean simulate) { int requested = size; int received = 0; + int externalStorageExtracted = 0; ItemStack newStack = null; for (IItemStorage storage : this.itemStorage.getStorages()) { @@ -630,7 +640,9 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR if (took != null) { if (storage instanceof ItemStorageExternal && !simulate) { - ((ItemStorageExternal) storage).updateForced(); + ((ItemStorageExternal) storage).detectChanges(this); + // the external storage will send the change, we don't need to anymore + externalStorageExtracted += took.stackSize; } if (newStack == null) { @@ -647,8 +659,8 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR } } - if (newStack != null && !simulate) { - itemStorage.remove(newStack, newStack.stackSize); + if (newStack != null && newStack.stackSize - externalStorageExtracted > 0 && !simulate) { + itemStorage.remove(newStack, newStack.stackSize - externalStorageExtracted); } 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 39e53b3db..bd029bb09 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/tile/externalstorage/ItemStorageExternal.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/tile/externalstorage/ItemStorageExternal.java @@ -1,6 +1,7 @@ package com.raoulvdberge.refinedstorage.tile.externalstorage; import com.raoulvdberge.refinedstorage.api.network.INetworkMaster; +import com.raoulvdberge.refinedstorage.api.storage.AccessType; import com.raoulvdberge.refinedstorage.api.storage.item.IItemStorage; import com.raoulvdberge.refinedstorage.apiimpl.API; import net.minecraft.item.ItemStack; @@ -13,63 +14,66 @@ public abstract class ItemStorageExternal implements IItemStorage { public abstract int getCapacity(); public void detectChanges(INetworkMaster network) { - if (cache == null) { - updateForced(); - } else { - List newStacks = getStacks(); - - for (int i = 0; i < newStacks.size(); ++i) { - ItemStack actual = newStacks.get(i); - - // If we exceed the cache size, than that means this items is added - if (i >= cache.size()) { - if (actual != null) { - network.getItemStorageCache().add(actual, actual.stackSize, false); - } - - continue; - } - - ItemStack cached = cache.get(i); - - if (cached != null && actual == null) { - // If the cached is not null but the actual is, we remove this item - 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 - 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 - network.getItemStorageCache().remove(cached, cached.stackSize); - network.getItemStorageCache().add(actual, actual.stackSize, false); - } else if (cached.stackSize != actual.stackSize) { - int delta = actual.stackSize - cached.stackSize; - - if (delta > 0) { - network.getItemStorageCache().add(actual, delta, false); - } else { - network.getItemStorageCache().remove(actual, Math.abs(delta)); - } - } - } - - // If the cache size is somehow bigger than the actual stacks, that means the inventory shrunk - // In that case, we remove the items that have been removed due to the shrinkage - if (cache.size() > newStacks.size()) { - for (int i = newStacks.size(); i < cache.size(); ++i) { - if (cache.get(i) != null) { - network.getItemStorageCache().remove(cache.get(i), cache.get(i).stackSize); - } - } - } - - this.cache = newStacks; + // if we are insert-only, we don't care about sending changes + if (getAccessType() == AccessType.INSERT) { + return; } - } - public void updateForced() { - this.cache = getStacks(); + if (cache == null) { + cache = getStacks(); + + return; + } + + List newStacks = getStacks(); + + for (int i = 0; i < newStacks.size(); ++i) { + ItemStack actual = newStacks.get(i); + + // If we exceed the cache size, than that means this items is added + if (i >= cache.size()) { + if (actual != null) { + network.getItemStorageCache().add(actual, actual.stackSize, false); + } + + continue; + } + + ItemStack cached = cache.get(i); + + if (cached != null && actual == null) { + // If the cached is not null but the actual is, we remove this item + 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 + 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 + network.getItemStorageCache().remove(cached, cached.stackSize); + network.getItemStorageCache().add(actual, actual.stackSize, false); + } else if (cached.stackSize != actual.stackSize) { + int delta = actual.stackSize - cached.stackSize; + + if (delta > 0) { + network.getItemStorageCache().add(actual, delta, false); + } else { + network.getItemStorageCache().remove(actual, Math.abs(delta)); + } + } + } + + // If the cache size is somehow bigger than the actual stacks, that means the inventory shrunk + // In that case, we remove the items that have been removed due to the shrinkage + if (cache.size() > newStacks.size()) { + for (int i = newStacks.size(); i < cache.size(); ++i) { + if (cache.get(i) != null) { + network.getItemStorageCache().remove(cache.get(i), cache.get(i).stackSize); + } + } + } + + this.cache = newStacks; } }