Avoid unnecessary ItemStack allocations #2

This commit is contained in:
Raoul Van den Berge
2016-06-03 23:22:37 +02:00
parent 3f0f990857
commit c62211654e
4 changed files with 12 additions and 19 deletions

View File

@@ -9,7 +9,7 @@ public interface IStorage {
void push(ItemStack stack); void push(ItemStack stack);
ItemStack take(ItemStack stack, int flags); ItemStack take(ItemStack stack, int size, int flags);
boolean mayPush(ItemStack stack); boolean mayPush(ItemStack stack);

View File

@@ -96,9 +96,7 @@ public abstract class NBTStorage implements IStorage {
} }
@Override @Override
public ItemStack take(ItemStack stack, int flags) { public ItemStack take(ItemStack stack, int size, int flags) {
int size = stack.stackSize;
for (ItemStack s : stacks) { for (ItemStack s : stacks) {
if (RefinedStorageUtils.compareStack(s, stack, flags)) { if (RefinedStorageUtils.compareStack(s, stack, flags)) {
if (size > s.stackSize) { if (size > s.stackSize) {

View File

@@ -82,21 +82,16 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
} }
@Override @Override
public ItemStack take(ItemStack stack, int flags) { public ItemStack take(ItemStack stack, int size, int flags) {
int quantity = stack.stackSize;
IDeepStorageUnit storageUnit = getStorageUnit(); IDeepStorageUnit storageUnit = getStorageUnit();
if (storageUnit != null) { if (storageUnit != null) {
if (storageUnit.getStoredItemType() != null && RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack)) { if (storageUnit.getStoredItemType() != null && RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack)) {
if (quantity > storageUnit.getStoredItemType().stackSize) { size = Math.min(size, storageUnit.getStoredItemType().stackSize);
quantity = storageUnit.getStoredItemType().stackSize;
}
ItemStack took = storageUnit.getStoredItemType().copy(); ItemStack took = ItemHandlerHelper.copyStackWithSize(storageUnit.getStoredItemType(), size);
took.stackSize = quantity;
storageUnit.setStoredItemCount(storageUnit.getStoredItemType().stackSize - quantity); storageUnit.setStoredItemCount(storageUnit.getStoredItemType().stackSize - size);
return took; return took;
} }
@@ -108,13 +103,13 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
ItemStack slot = handler.getStackInSlot(i); ItemStack slot = handler.getStackInSlot(i);
if (slot != null && RefinedStorageUtils.compareStack(slot, stack, flags)) { if (slot != null && RefinedStorageUtils.compareStack(slot, stack, flags)) {
if (quantity > slot.stackSize) { size = Math.min(size, slot.stackSize);
quantity = slot.stackSize;
}
handler.extractItem(i, quantity, false); ItemStack took = ItemHandlerHelper.copyStackWithSize(slot, size);
return ItemHandlerHelper.copyStackWithSize(slot, quantity); handler.extractItem(i, size, false);
return took;
} }
} }
} }

View File

@@ -396,7 +396,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
ItemStack newStack = null; ItemStack newStack = null;
for (IStorage storage : storages) { for (IStorage storage : storages) {
ItemStack took = storage.take(stack, flags); ItemStack took = storage.take(stack, requested, flags);
if (took != null) { if (took != null) {
if (newStack == null) { if (newStack == null) {