diff --git a/src/main/java/appeng/me/cells/BasicCellInventory.java b/src/main/java/appeng/me/cells/BasicCellInventory.java index ad34b820953..43c3bf11578 100644 --- a/src/main/java/appeng/me/cells/BasicCellInventory.java +++ b/src/main/java/appeng/me/cells/BasicCellInventory.java @@ -348,7 +348,14 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc // Run regular insert logic and then apply void upgrade to the returned value. long inserted = innerInsert(what, amount, mode); - return this.hasVoidUpgrade ? amount : inserted; + + // In the event that a void card is being used on a (full) unformatted cell, ensure it doesn't void any items + // that the cell isn't even storing and cannot store to begin with + if (!isPreformatted() && hasVoidUpgrade && !canHoldNewItem()) { + return getCellItems().containsKey(what) ? amount : inserted; + } + + return hasVoidUpgrade ? amount : inserted; } // Inner insert for items that pass the filter. diff --git a/src/test/java/appeng/me/cells/BasicInventoryTest.java b/src/test/java/appeng/me/cells/BasicInventoryTest.java index 4937e9ef413..aabcfd06d78 100644 --- a/src/test/java/appeng/me/cells/BasicInventoryTest.java +++ b/src/test/java/appeng/me/cells/BasicInventoryTest.java @@ -29,7 +29,7 @@ public class BasicInventoryTest { /** * Check that we can extract more than MAX_INT fluid at once from a cell. Regression test for - * https://github.com/AppliedEnergistics/Applied-Energistics-2/issues/6794 + * #6794 */ @Test void testFluidExtract() { @@ -135,6 +135,47 @@ void testVoidUpgrade() { assertThat(cell.insert(rejected, Long.MAX_VALUE, Actionable.MODULATE, SRC)).isZero(); } + @Test + void testVoidUpgradeUnformatted() { + var item = AEItems.ITEM_CELL_1K.get(); + var stack = new ItemStack(item); + item.getUpgrades(stack).addItems(AEItems.VOID_CARD.stack()); + + var cell = StorageCells.getCellInventory(stack, null); + Objects.requireNonNull(cell); + + // Ensure that the first insert of a single type voids only excess. + var filler = AEItemKey.of(Items.DIAMOND); + assertThat(cell.insert(filler, Long.MAX_VALUE, Actionable.MODULATE, SRC)).isEqualTo(Long.MAX_VALUE); + assertThat(cell.getAvailableStacks().get(filler)).isNotZero(); + // Ensure that new item types that the cell cannot store don't get voided. + var rejected = AEItemKey.of(Items.STICK); + assertThat(cell.insert(rejected, Long.MAX_VALUE, Actionable.MODULATE, SRC)).isZero(); + + // Part two, fill cell with 63 different types this time. + cell.extract(filler, Long.MAX_VALUE, Actionable.MODULATE, SRC); + item.getUpgrades(stack).removeItems(1, AEItems.VOID_CARD.stack(), null); + item.getUpgrades(stack).addItems(AEItems.EQUAL_DISTRIBUTION_CARD.stack()); + cell = StorageCells.getCellInventory(stack, null); + Objects.requireNonNull(cell); + + var maxTypes = item.getTotalTypes(stack); + var keys = generateDifferentKeys(maxTypes); + + for (int i = 0; i < maxTypes; ++i) { + cell.insert(keys[i], Long.MAX_VALUE, Actionable.MODULATE, SRC); + } + + item.getUpgrades(stack).addItems(AEItems.VOID_CARD.stack()); + cell = StorageCells.getCellInventory(stack, null); + Objects.requireNonNull(cell); + + // Ensure that inserting an already-stored item voids. + assertThat(cell.insert(keys[0], Long.MAX_VALUE, Actionable.MODULATE, SRC)).isEqualTo(Long.MAX_VALUE); + // Ensure that items that aren't on the cell don't get voided. + assertThat(cell.insert(rejected, Long.MAX_VALUE, Actionable.MODULATE, SRC)).isZero(); + } + private static AEItemKey[] generateDifferentKeys(int count) { var out = new AEItemKey[count]; for (int i = 0; i < count; ++i) {