Skip to content

Commit

Permalink
Changed item merging from full inventory copy to partial stacks copy
Browse files Browse the repository at this point in the history
avoids copying itemstacks that are at max size, and cant be merged into anyway
  • Loading branch information
PrototypeTrousers committed May 25, 2021
1 parent aada24d commit f05c193
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import gregtech.api.render.Textures;
import gregtech.api.util.GTFluidUtils;
import gregtech.api.util.GTUtility;
import gregtech.api.util.InventoryUtils;
import net.minecraft.block.Block;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
Expand Down Expand Up @@ -56,8 +57,6 @@
import java.util.List;
import java.util.function.Consumer;

import static gregtech.api.util.InventoryUtils.simulateItemStackMerge;

public abstract class MetaTileEntity implements ICoverable {

public static final int DEFAULT_PAINTING_COLOR = 0xFFFFFF;
Expand Down Expand Up @@ -973,8 +972,14 @@ public static boolean addItemsToItemHandler(final IItemHandler handler,
final boolean simulate,
final List<ItemStack> items) {
// determine if there is sufficient room to insert all items into the target inventory
final boolean canMerge = simulateItemStackMerge(items, handler);

boolean canMerge;
if (items.size() == 1) {
canMerge = ItemHandlerHelper.insertItemStacked(handler, items.get(0), true).isEmpty();
} else {
// merging 2 or more stacks may change the slots available for merging,
// needing full simulation of the state of the inventory after every merge
canMerge = InventoryUtils.simulateItemStackMerge(items, handler);
}
// if we're not simulating and the merge should succeed, perform the merge.
if (!simulate && canMerge)
items.forEach(stack -> {
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/gregtech/api/util/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public static List<ItemStack> deepCopy(final IItemHandler inventory,
.collect(Collectors.toList());
}

/**
* Creates a copy of the partial stacks in target inventory
*
* @param inventory the target inventory
* @return a copy of the partial stacks of the inventory.
*/
public static List<ItemStack> copyPartialStacks(final IItemHandler inventory) {

return streamFrom(inventory)
.filter(not(ItemStack::isEmpty))
.filter(not(itemStack -> itemStack.getCount() == itemStack.getMaxStackSize()))
.map(ItemStack::copy)
.collect(Collectors.toList());
}

/**
* Determines whether all specified items will fit into a target inventory by
* simulating merging like items into existing stacks, then checking if there
Expand Down Expand Up @@ -75,7 +90,7 @@ public static boolean simulateItemStackMerge(List<ItemStack> inputItems,
itemStacks.sort(Comparator.comparingInt(ItemStack::getCount));

// Deep copy the contents of the target inventory, skipping empty stacks
final List<ItemStack> inventoryStacks = deepCopy(inventory, false);
final List<ItemStack> inventoryStacks = copyPartialStacks(inventory);

// Perform a merge of the ItemStacks
mergeItemStacks(itemStacks, inventoryStacks);
Expand Down

0 comments on commit f05c193

Please sign in to comment.