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