Fixed External Storage cache not working properly on Compacting Drawers, fixes #591
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
| @@ -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<ItemStack> 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<ItemStack> 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; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Raoul Van den Berge
					Raoul Van den Berge