Don't rebuild the whole item cache on an external inventory change
This commit is contained in:
@@ -39,7 +39,9 @@ public class ItemStorageCache implements IItemStorageCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (ItemStack stack : storage.getStacks()) {
|
for (ItemStack stack : storage.getStacks()) {
|
||||||
add(stack, true);
|
if (stack != null) {
|
||||||
|
add(stack, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -530,13 +530,13 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
|||||||
|
|
||||||
if (remainder == null || remainder.stackSize < 0) {
|
if (remainder == null || remainder.stackSize < 0) {
|
||||||
if (storage instanceof ItemStorageExternal && !simulate) {
|
if (storage instanceof ItemStorageExternal && !simulate) {
|
||||||
((ItemStorageExternal) storage).updateCacheForcefully();
|
((ItemStorageExternal) storage).updateForced();
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if (size != remainder.stackSize && storage instanceof ItemStorageExternal && !simulate) {
|
if (size != remainder.stackSize && storage instanceof ItemStorageExternal && !simulate) {
|
||||||
((ItemStorageExternal) storage).updateCacheForcefully();
|
((ItemStorageExternal) storage).updateForced();
|
||||||
}
|
}
|
||||||
|
|
||||||
size = remainder.stackSize;
|
size = remainder.stackSize;
|
||||||
@@ -586,7 +586,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
|||||||
|
|
||||||
if (took != null) {
|
if (took != null) {
|
||||||
if (storage instanceof ItemStorageExternal) {
|
if (storage instanceof ItemStorageExternal) {
|
||||||
((ItemStorageExternal) storage).updateCacheForcefully();
|
((ItemStorageExternal) storage).updateForced();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newStack == null) {
|
if (newStack == null) {
|
||||||
|
@@ -1,9 +1,12 @@
|
|||||||
package com.raoulvdberge.refinedstorage.tile.externalstorage;
|
package com.raoulvdberge.refinedstorage.tile.externalstorage;
|
||||||
|
|
||||||
|
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 {
|
||||||
@@ -11,29 +14,67 @@ public abstract class ItemStorageExternal implements IItemStorage {
|
|||||||
|
|
||||||
public abstract int getCapacity();
|
public abstract int getCapacity();
|
||||||
|
|
||||||
public boolean updateCache() {
|
public void detectChanges(INetworkMaster network) {
|
||||||
List<ItemStack> newStacks = getStacks();
|
if (cache == null) {
|
||||||
|
updateForced();
|
||||||
if (this.cache == null) {
|
|
||||||
this.cache = newStacks;
|
|
||||||
} else if (newStacks.size() != cache.size()) {
|
|
||||||
this.cache = newStacks;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < newStacks.size(); ++i) {
|
LinkedList<ItemStack> changes = new LinkedList<>();
|
||||||
if (!API.instance().getComparer().isEqual(newStacks.get(i), cache.get(i))) {
|
|
||||||
this.cache = newStacks;
|
|
||||||
|
|
||||||
return true;
|
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) {
|
||||||
|
changes.add(ItemHandlerHelper.copyStackWithSize(actual, actual.stackSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack cached = cache.get(i);
|
||||||
|
|
||||||
|
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));
|
||||||
|
} 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));
|
||||||
|
} 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));
|
||||||
|
} 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 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) {
|
||||||
|
changes.add(ItemHandlerHelper.copyStackWithSize(cache.get(i), -cache.get(i).stackSize));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.cache = newStacks;
|
||||||
|
|
||||||
|
for (ItemStack change : changes) {
|
||||||
|
if (change.stackSize > 0) {
|
||||||
|
network.getItemStorageCache().add(change, false);
|
||||||
|
} else {
|
||||||
|
network.getItemStorageCache().remove(change);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateCacheForcefully() {
|
public void updateForced() {
|
||||||
this.cache = getStacks();
|
this.cache = getStacks();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -34,9 +34,7 @@ public class ItemStorageItemHandler extends ItemStorageExternal {
|
|||||||
List<ItemStack> items = new ArrayList<>();
|
List<ItemStack> items = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < handler.getSlots(); ++i) {
|
for (int i = 0; i < handler.getSlots(); ++i) {
|
||||||
if (handler.getStackInSlot(i) != null && handler.getStackInSlot(i).getItem() != null) {
|
items.add(handler.getStackInSlot(i) != null ? handler.getStackInSlot(i).copy() : null);
|
||||||
items.add(handler.getStackInSlot(i).copy());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
|
@@ -127,24 +127,18 @@ public class TileExternalStorage extends TileMultipartNode implements IItemStora
|
|||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
if (!worldObj.isRemote && network != null) {
|
if (!worldObj.isRemote && network != null) {
|
||||||
boolean itemChangeDetected = false, fluidChangeDetected = false;
|
|
||||||
|
|
||||||
for (ItemStorageExternal storage : itemStorages) {
|
for (ItemStorageExternal storage : itemStorages) {
|
||||||
if (storage.updateCache()) {
|
storage.detectChanges(network);
|
||||||
itemChangeDetected = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean fluidChangeDetected = false;
|
||||||
|
|
||||||
for (FluidStorageExternal storage : fluidStorages) {
|
for (FluidStorageExternal storage : fluidStorages) {
|
||||||
if (storage.updateCache()) {
|
if (storage.updateCache()) {
|
||||||
fluidChangeDetected = true;
|
fluidChangeDetected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (itemChangeDetected) {
|
|
||||||
network.getItemStorageCache().invalidate();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fluidChangeDetected) {
|
if (fluidChangeDetected) {
|
||||||
network.getFluidStorageCache().invalidate();
|
network.getFluidStorageCache().invalidate();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user