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);
ItemStack take(ItemStack stack, int flags);
ItemStack take(ItemStack stack, int size, int flags);
boolean mayPush(ItemStack stack);

View File

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

View File

@@ -82,21 +82,16 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
}
@Override
public ItemStack take(ItemStack stack, int flags) {
int quantity = stack.stackSize;
public ItemStack take(ItemStack stack, int size, int flags) {
IDeepStorageUnit storageUnit = getStorageUnit();
if (storageUnit != null) {
if (storageUnit.getStoredItemType() != null && RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack)) {
if (quantity > storageUnit.getStoredItemType().stackSize) {
quantity = storageUnit.getStoredItemType().stackSize;
}
size = Math.min(size, storageUnit.getStoredItemType().stackSize);
ItemStack took = storageUnit.getStoredItemType().copy();
took.stackSize = quantity;
ItemStack took = ItemHandlerHelper.copyStackWithSize(storageUnit.getStoredItemType(), size);
storageUnit.setStoredItemCount(storageUnit.getStoredItemType().stackSize - quantity);
storageUnit.setStoredItemCount(storageUnit.getStoredItemType().stackSize - size);
return took;
}
@@ -108,13 +103,13 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
ItemStack slot = handler.getStackInSlot(i);
if (slot != null && RefinedStorageUtils.compareStack(slot, stack, flags)) {
if (quantity > slot.stackSize) {
quantity = slot.stackSize;
}
size = Math.min(size, 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;
for (IStorage storage : storages) {
ItemStack took = storage.take(stack, flags);
ItemStack took = storage.take(stack, requested, flags);
if (took != null) {
if (newStack == null) {