Skip to content

Commit

Permalink
Copy stack when component changes
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Mar 1, 2024
1 parent c3b738f commit e243c0a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.impl.transfer.item;

import java.util.Objects;

import net.minecraft.block.ChestBlock;
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.block.entity.BrewingStandBlockEntity;
Expand Down Expand Up @@ -153,9 +155,14 @@ protected void onFinalCommit() {
}

if (!original.isEmpty() && original.getItem() == currentStack.getItem()) {
// None is empty and the items match: just update the amount and NBT, and reuse the original stack.
// Components have changed, we need to copy the stack.
if (!Objects.equals(original.getComponentChanges(), currentStack.getComponentChanges())) {
setStack(currentStack.copy());
return;
}

// None is empty and the items and components match: just update the amount, and reuse the original stack.
original.setCount(currentStack.getCount());
original.copyComponentsFrom(currentStack.getComponents());
setStack(original);
} else {
// Otherwise assume everything was taken from original so empty it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public void testStackReference() {

if (stack != inv.getStack(0)) throw new AssertionError("Stack should have stayed the same.");

// Also edit the stack when the item matches, even when the NBT and the count change.
// Also edit the stack when the item matches, even when the components and the count change.
// We expect the stack to change
ItemVariant oldVariant = ItemVariant.of(Items.DIAMOND);
ComponentChanges components = ComponentChanges.builder().add(ENERGY, 42).build();
ItemVariant newVariant = ItemVariant.of(Items.DIAMOND, components);
Expand All @@ -95,7 +96,8 @@ public void testStackReference() {
tx.commit();
}

if (stack != inv.getStack(0)) throw new AssertionError("Stack should have stayed the same.");
if (stack == inv.getStack(0)) throw new AssertionError("Stack should have changed");
stack = inv.getStack(0);
if (!stackEquals(stack, newVariant, 5)) throw new AssertionError("Failed to update stack NBT or count.");
}

Expand Down

0 comments on commit e243c0a

Please sign in to comment.