cache filled disks fixes #2771 (#2786)

* cache filled disks fixes #2771

* cache item count instead of fullness

* track itemcount instead of caching
This commit is contained in:
Darkere
2021-01-02 11:23:18 +01:00
committed by GitHub
parent 8e962178e0
commit 686190f232
2 changed files with 15 additions and 4 deletions

View File

@@ -35,6 +35,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
private final int capacity; private final int capacity;
private final Multimap<Item, ItemStack> stacks = ArrayListMultimap.create(); private final Multimap<Item, ItemStack> stacks = ArrayListMultimap.create();
private final UUID owner; private final UUID owner;
private int itemCount;
@Nullable @Nullable
private IStorageDiskListener listener; private IStorageDiskListener listener;
@@ -80,7 +81,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
@Override @Override
@Nonnull @Nonnull
public ItemStack insert(@Nonnull ItemStack stack, int size, Action action) { public ItemStack insert(@Nonnull ItemStack stack, int size, Action action) {
if (stack.isEmpty()) { if (stack.isEmpty() || itemCount == capacity) {
return stack; return stack;
} }
@@ -95,7 +96,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
if (action == Action.PERFORM) { if (action == Action.PERFORM) {
otherStack.grow(remainingSpace); otherStack.grow(remainingSpace);
itemCount += remainingSpace;
onChanged(); onChanged();
} }
@@ -103,6 +104,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
} else { } else {
if (action == Action.PERFORM) { if (action == Action.PERFORM) {
otherStack.grow(size); otherStack.grow(size);
itemCount += size;
onChanged(); onChanged();
} }
@@ -121,7 +123,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
if (action == Action.PERFORM) { if (action == Action.PERFORM) {
stacks.put(stack.getItem(), ItemHandlerHelper.copyStackWithSize(stack, remainingSpace)); stacks.put(stack.getItem(), ItemHandlerHelper.copyStackWithSize(stack, remainingSpace));
itemCount += remainingSpace;
onChanged(); onChanged();
} }
@@ -129,6 +131,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
} else { } else {
if (action == Action.PERFORM) { if (action == Action.PERFORM) {
stacks.put(stack.getItem(), ItemHandlerHelper.copyStackWithSize(stack, size)); stacks.put(stack.getItem(), ItemHandlerHelper.copyStackWithSize(stack, size));
itemCount += size;
onChanged(); onChanged();
} }
@@ -157,6 +160,8 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
otherStack.shrink(size); otherStack.shrink(size);
} }
itemCount -= size;
onChanged(); onChanged();
} }
@@ -169,7 +174,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
@Override @Override
public int getStored() { public int getStored() {
return stacks.values().stream().mapToInt(ItemStack::getCount).sum(); return itemCount;
} }
@Override @Override
@@ -221,4 +226,8 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
API.instance().getStorageDiskManager(world).markForSaving(); API.instance().getStorageDiskManager(world).markForSaving();
} }
} }
public void updateItemCount() {
itemCount = stacks.values().stream().mapToInt(ItemStack::getCount).sum();
}
} }

View File

@@ -39,6 +39,8 @@ public class ItemStorageDiskFactory implements IStorageDiskFactory<ItemStack> {
} }
} }
disk.updateItemCount();
return disk; return disk;
} }