mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-14 12:10:30 +00:00
Fix OOB when item insertion wraps around (#1277)
Co-authored-by: Jonathan Coates <git@squiddev.cc>
This commit is contained in:
parent
a1d5c76d00
commit
7a83a403f0
@ -51,14 +51,13 @@ public final class InventoryUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static ItemStack storeItemsImpl(Container container, ItemStack stack, int offset, int slotCount) {
|
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());
|
var maxSize = Math.min(stack.getMaxStackSize(), container.getMaxStackSize());
|
||||||
if (maxSize <= 0) return stack;
|
if (maxSize <= 0) return stack;
|
||||||
|
|
||||||
for (var i = 0; i < slotCount; i++) {
|
for (var i = 0; i < slotCount; i++) {
|
||||||
var slot = i + offset;
|
var slot = i + offset;
|
||||||
if (slot > limit) slot -= limit;
|
if (slot >= limit) slot -= limit;
|
||||||
|
|
||||||
var currentStack = container.getItem(slot);
|
var currentStack = container.getItem(slot);
|
||||||
if (currentStack.isEmpty()) {
|
if (currentStack.isEmpty()) {
|
||||||
// If the current slot is empty and we can place them item then there's two cases:
|
// If the current slot is empty and we can place them item then there's two cases:
|
||||||
|
@ -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)));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user