Skip to content

Commit

Permalink
The way Item stacks get merged can cause an incredibly long runtime i…
Browse files Browse the repository at this point in the history
…f there is a large amount of times. this can take hours or days if the ItemStack array is large enough. This doesn’t happen during regular gameplay. But it can be caused by crafting continuously with a mod like Itemscroller. Like this the main Consumer while loop gets stuck in the merge. (as it causes 100k+ stacks to be processed) This causes an indefinite (depending on how long you crafted) halt of all database pushes. This can not only render these mods basically unusable, but it also generates a memory leek (as the que is stacked up) and also prevents logging till the merge is done. which, not only prevents proper restarts, but can also lose data when forcefully restarted.
  • Loading branch information
asdanjer committed Jun 18, 2023
1 parent 814ae4d commit 78dd534
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/main/java/net/coreprotect/utility/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,20 @@ public static void mergeItems(Material material, ItemStack[] items) {
try {
int c1 = 0;
for (ItemStack o1 : items) {
int c2 = 0;
for (ItemStack o2 : items) {
if (o1 != null && o2 != null && c2 > c1 && o1.isSimilar(o2) && !Util.isAir(o1.getType())) { // Ignores amount
int namount = o1.getAmount() + o2.getAmount();
o1.setAmount(namount);
o2.setAmount(0);
if (o1 != null) {
if (o1.getAmount() != 0) {
int c2 = 0;
for (ItemStack o2 : items) {
if (o2 != null && c2 > c1 && o1.isSimilar(o2) && !Util.isAir(o1.getType())) { // Ignores amount
int namount = o1.getAmount() + o2.getAmount();
o1.setAmount(namount);
o2.setAmount(0);
}
c2++;
}
}
c2++;
}
c1++;
c1++;
}
}
catch (Exception e) {
Expand Down

2 comments on commit 78dd534

@asdanjer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: the stack ends up in the first occurrence not in the last

@asdanjer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: Because of "c2 > c1" this issue is false. It ends up in the first occurrence in either cases.

Please sign in to comment.