diff --git a/projects/common/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java b/projects/common/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java index 63c369865..241a1bc01 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java @@ -51,14 +51,13 @@ private static ItemStack storeItems(Container container, ItemStack stack, int of } private static ItemStack storeItemsImpl(Container container, ItemStack stack, int offset, int slotCount) { - var limit = container.getMaxStackSize(); + var limit = container.getContainerSize(); var maxSize = Math.min(stack.getMaxStackSize(), container.getMaxStackSize()); if (maxSize <= 0) return stack; for (var i = 0; i < slotCount; i++) { var slot = i + offset; - if (slot > limit) slot -= limit; - + if (slot >= limit) slot -= limit; var currentStack = container.getItem(slot); if (currentStack.isEmpty()) { // If the current slot is empty and we can place them item then there's two cases: diff --git a/projects/common/src/test/java/dan200/computercraft/shared/util/InventoryUtilTest.java b/projects/common/src/test/java/dan200/computercraft/shared/util/InventoryUtilTest.java new file mode 100644 index 000000000..c03f64abb --- /dev/null +++ b/projects/common/src/test/java/dan200/computercraft/shared/util/InventoryUtilTest.java @@ -0,0 +1,38 @@ +/* + * This file is part of ComputerCraft - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission. + * Send enquiries to dratcliffe@gmail.com + */ +package dan200.computercraft.shared.util; + +import dan200.computercraft.test.shared.WithMinecraft; +import net.minecraft.world.SimpleContainer; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import org.junit.jupiter.api.Test; + +import static dan200.computercraft.test.shared.ItemStackMatcher.isStack; +import static org.hamcrest.MatcherAssert.assertThat; + +@WithMinecraft +public class InventoryUtilTest { + @Test + public void testStoreOffset() { + var container = new SimpleContainer(9); + + var remainder = InventoryUtil.storeItemsFromOffset(container, new ItemStack(Items.COBBLESTONE, 32), 4); + assertThat("Remainder is empty", remainder, isStack(ItemStack.EMPTY)); + assertThat("Was inserted into slot", container.getItem(4), isStack(new ItemStack(Items.COBBLESTONE, 32))); + } + + @Test + public void testStoreOffsetWraps() { + var container = new SimpleContainer(9); + container.setItem(0, new ItemStack(Items.DIRT)); + for (var slot = 4; slot < 9; slot++) container.setItem(slot, new ItemStack(Items.DIRT)); + + var remainder = InventoryUtil.storeItemsFromOffset(container, new ItemStack(Items.COBBLESTONE, 32), 4); + assertThat("Remainder is empty", remainder, isStack(ItemStack.EMPTY)); + assertThat("Was inserted into slot", container.getItem(1), isStack(new ItemStack(Items.COBBLESTONE, 32))); + } +}