From d961e5fabafa526bfe71bcbb339b141b7a00992e Mon Sep 17 00:00:00 2001 From: Starforcraft Date: Fri, 12 Jul 2024 09:30:43 +0200 Subject: [PATCH] feat: stack upgrade gametest setup --- .../platform/common/GameTestUtil.java | 98 +++++- .../ConstructorTest.java | 49 ++- .../constructordestructor/DestructorTest.java | 136 ++++----- .../common/exporter/ExporterTest.java | 152 +++++++--- .../common/exporter/ExporterTestPlots.java | 6 +- .../common/importer/ImporterTest.java | 278 +++++++++++++----- .../common/importer/ImporterTestPlots.java | 15 + 7 files changed, 547 insertions(+), 187 deletions(-) diff --git a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/GameTestUtil.java b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/GameTestUtil.java index c2d0b5fea..249734897 100644 --- a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/GameTestUtil.java +++ b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/GameTestUtil.java @@ -8,11 +8,15 @@ import com.refinedmods.refinedstorage.api.resource.ResourceKey; import com.refinedmods.refinedstorage.api.storage.EmptyActor; import com.refinedmods.refinedstorage.platform.api.support.network.AbstractNetworkNodeContainerBlockEntity; +import com.refinedmods.refinedstorage.platform.api.support.resource.ResourceContainer; import com.refinedmods.refinedstorage.platform.common.content.Blocks; +import com.refinedmods.refinedstorage.platform.common.content.Items; +import com.refinedmods.refinedstorage.platform.common.iface.InterfaceBlockEntity; import com.refinedmods.refinedstorage.platform.common.support.resource.FluidResource; import com.refinedmods.refinedstorage.platform.common.support.resource.ItemResource; import java.util.Arrays; +import java.util.Iterator; import java.util.function.Consumer; import java.util.stream.IntStream; import javax.annotation.Nullable; @@ -20,16 +24,21 @@ import net.minecraft.core.BlockPos; import net.minecraft.gametest.framework.GameTestAssertException; import net.minecraft.gametest.framework.GameTestHelper; +import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; import net.minecraft.world.level.block.entity.BaseContainerBlockEntity; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; +import net.minecraft.world.phys.AABB; public final class GameTestUtil { public static final Blocks RSBLOCKS = Blocks.INSTANCE; + public static final Items RSITEMS = Items.INSTANCE; private GameTestUtil() { } @@ -106,29 +115,110 @@ public static void assertFluidPresent(final GameTestHelper helper, ); } + public static void assertItemEntityPresentExactly(final GameTestHelper helper, + final ItemStack itemStack, + final BlockPos pos, + final double expansionAmount) { + final BlockPos blockpos = helper.absolutePos(pos); + final Iterator entityIterator = helper.getLevel().getEntities(EntityType.ITEM, + (new AABB(blockpos)).inflate(expansionAmount), Entity::isAlive).iterator(); + + ItemEntity itemEntity; + do { + if (!entityIterator.hasNext()) { + throw new GameTestAssertException("Expected " + itemStack.getItem().getDescription().getString() + + " item at: " + blockpos + " with count: " + itemStack.getCount()); + } + + itemEntity = entityIterator.next(); + } while (!itemEntity.getItem().getItem().equals(itemStack.getItem()) + || itemEntity.getItem().getCount() != itemStack.getCount()); + } + + public static Runnable assertInterfaceEmpty(final GameTestHelper helper, + final BlockPos pos) { + final var interfaceBlockEntity = requireBlockEntity(helper, pos, InterfaceBlockEntity.class); + + return assertResourceContainerEmpty(interfaceBlockEntity.getDisplayName(), + interfaceBlockEntity.getExportedResources()); + } + + private static Runnable assertResourceContainerEmpty(final Component displayName, + final ResourceContainer container) { + return () -> { + for (int i = 0; i < container.size(); i++) { + if (!container.isEmpty(i)) { + throw new GameTestAssertException(displayName.getString() + " should be empty"); + } + } + }; + } + + public static Runnable interfaceContainsExactly(final GameTestHelper helper, + final BlockPos pos, + final ResourceAmount... expected) { + final var interfaceBlockEntity = requireBlockEntity(helper, pos, InterfaceBlockEntity.class); + + return resourceContainerContainsExactly(helper, interfaceBlockEntity.getDisplayName(), + interfaceBlockEntity.getExportedResources(), expected); + } + + private static Runnable resourceContainerContainsExactly(final GameTestHelper helper, + final Component displayName, + final ResourceContainer container, + final ResourceAmount... expected) { + return () -> { + // TODO: This does not take duplicate ResourceAmount into account + for (final ResourceAmount expectedStack : expected) { + final boolean contains = IntStream.range(0, container.size()) + .mapToObj(container::get) + .anyMatch(resource -> resource != null + && resource.getResource().equals(expectedStack.getResource()) + && resource.getAmount() == expectedStack.getAmount()); + + helper.assertTrue(contains, "Expected resource is missing from " + displayName.getString() + ": " + + expectedStack + " with count: " + expectedStack.getAmount()); + } + + for (int i = 0; i < container.size(); i++) { + final ResourceAmount resource = container.get(i); + if (resource != null) { + final boolean wasExpected = Arrays.stream(expected).anyMatch( + expectedResource -> expectedResource.getResource().equals(resource.getResource()) + && expectedResource.getAmount() == resource.getAmount() + ); + + helper.assertTrue(wasExpected, "Unexpected resource found in " + displayName.getString() + ": " + + resource.getResource() + " with count: " + resource.getAmount()); + } + } + }; + } + public static Runnable containerContainsExactly(final GameTestHelper helper, final BlockPos pos, final ResourceAmount... expected) { final var containerBlockEntity = requireBlockEntity(helper, pos, BaseContainerBlockEntity.class); return () -> { + // TODO: This does not take duplicate ResourceAmount into account for (final ResourceAmount expectedStack : expected) { final boolean contains = IntStream.range(0, containerBlockEntity.getContainerSize()) .mapToObj(containerBlockEntity::getItem) .anyMatch(inContainer -> asResource(inContainer).equals(expectedStack.getResource()) && inContainer.getCount() == expectedStack.getAmount()); - helper.assertTrue(contains, "Expected resource is missing from storage: " + helper.assertTrue(contains, "Expected resource is missing from container: " + expectedStack + " with count: " + expectedStack.getAmount()); } for (int i = 0; i < containerBlockEntity.getContainerSize(); i++) { final ItemStack inContainer = containerBlockEntity.getItem(i); - if (inContainer.getItem() != Items.AIR) { + if (!inContainer.isEmpty()) { final boolean wasExpected = Arrays.stream(expected).anyMatch( expectedStack -> expectedStack.getResource().equals(asResource(inContainer)) && expectedStack.getAmount() == inContainer.getCount() ); - helper.assertTrue(wasExpected, "Unexpected resource found in storage: " + helper.assertTrue(wasExpected, "Unexpected resource found in container: " + inContainer.getDescriptionId() + " with count: " + inContainer.getCount()); } } diff --git a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/constructordestructor/ConstructorTest.java b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/constructordestructor/ConstructorTest.java index 505551875..9221aa1c5 100644 --- a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/constructordestructor/ConstructorTest.java +++ b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/constructordestructor/ConstructorTest.java @@ -15,8 +15,10 @@ import net.neoforged.neoforge.gametest.GameTestHolder; import net.neoforged.neoforge.gametest.PrefixGameTestTemplate; +import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.RSITEMS; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.asResource; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.assertFluidPresent; +import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.assertItemEntityPresentExactly; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.insert; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.networkIsAvailable; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.storageContainsExactly; @@ -100,7 +102,12 @@ public static void shouldDropItem(final GameTestHelper helper) { // Assert sequence .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east())) - .thenWaitUntil(() -> helper.assertItemEntityPresent(DIRT, pos.east(), 1)) + .thenWaitUntil(() -> assertItemEntityPresentExactly( + helper, + DIRT.getDefaultInstance().copyWithCount(1), + pos.east(), + 1 + )) .thenWaitUntil(storageContainsExactly( helper, pos, @@ -111,6 +118,46 @@ public static void shouldDropItem(final GameTestHelper helper) { }); } + @GameTest(template = "empty_15x15") + public static void shouldDropItemWithStackUpgrade(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (constructor, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 65); + insert(helper, network, STONE, 15); + })); + + // Act + constructor.setDropItems(true); + constructor.setFilters(List.of(asResource(DIRT))); + constructor.addUpgradeItem(RSITEMS.getStackUpgrade()); + + // Assert + sequence + .thenIdle(9) + .thenExecute(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east())) + .thenExecute(() -> assertItemEntityPresentExactly( + helper, + DIRT.getDefaultInstance().copyWithCount(64), + pos.east(), + 1 + )) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 1), + new ResourceAmount(asResource(STONE), 15) + )) + .thenIdle(9) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + @GameTest(template = "empty_15x15") public static void shouldPlaceFireworks(final GameTestHelper helper) { preparePlot(helper, Direction.EAST, (constructor, pos, sequence) -> { diff --git a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/constructordestructor/DestructorTest.java b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/constructordestructor/DestructorTest.java index c6207d66e..f6ae954c5 100644 --- a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/constructordestructor/DestructorTest.java +++ b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/constructordestructor/DestructorTest.java @@ -72,17 +72,17 @@ public static void shouldBreakBlockAllowlist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east())) - .thenExecute(() -> helper.setBlock(pos.east(), Blocks.STONE)) - .thenIdle(20) - .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.STONE, pos.east())) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 11), - new ResourceAmount(asResource(STONE), 15) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east())) + .thenExecute(() -> helper.setBlock(pos.east(), Blocks.STONE)) + .thenIdle(20) + .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.STONE, pos.east())) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); }); } @@ -103,17 +103,17 @@ public static void shouldBreakBlockBlocklist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east())) - .thenExecute(() -> helper.setBlock(pos.east(), Blocks.STONE)) - .thenIdle(20) - .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.STONE, pos.east())) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 11), - new ResourceAmount(asResource(STONE), 15) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east())) + .thenExecute(() -> helper.setBlock(pos.east(), Blocks.STONE)) + .thenIdle(20) + .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.STONE, pos.east())) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); }); } @@ -135,17 +135,17 @@ public static void shouldPickupItemAllowlist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertItemEntityNotPresent(DIRT, pos.east(), 1)) - .thenExecute(() -> helper.spawnItem(STONE, pos.east())) - .thenIdle(20) - .thenWaitUntil(() -> helper.assertItemEntityPresent(STONE, pos.east(), 1)) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 11), - new ResourceAmount(asResource(STONE), 15) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertItemEntityNotPresent(DIRT, pos.east(), 1)) + .thenExecute(() -> helper.spawnItem(STONE, pos.east())) + .thenIdle(20) + .thenWaitUntil(() -> helper.assertItemEntityPresent(STONE, pos.east(), 1)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); }); } @@ -167,17 +167,17 @@ public static void shouldPickupItemBlocklist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertItemEntityNotPresent(DIRT, pos.east(), 1)) - .thenExecute(() -> helper.spawnItem(STONE, pos.east())) - .thenIdle(20) - .thenWaitUntil(() -> helper.assertItemEntityPresent(STONE, pos.east(), 1)) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 11), - new ResourceAmount(asResource(STONE), 15) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertItemEntityNotPresent(DIRT, pos.east(), 1)) + .thenExecute(() -> helper.spawnItem(STONE, pos.east())) + .thenIdle(20) + .thenWaitUntil(() -> helper.assertItemEntityPresent(STONE, pos.east(), 1)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); }); } @@ -198,18 +198,18 @@ public static void shouldDrainFluidAllowlist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.WATER, pos.east())) - .thenExecute(() -> helper.setBlock(pos.east(), Blocks.DIRT)) - .thenIdle(20) - .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.DIRT, pos.east())) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 10), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount()) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.WATER, pos.east())) + .thenExecute(() -> helper.setBlock(pos.east(), Blocks.DIRT)) + .thenIdle(20) + .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.DIRT, pos.east())) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount()) + )) + .thenSucceed(); }); } @@ -230,18 +230,18 @@ public static void shouldDrainFluidBlocklist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.WATER, pos.east())) - .thenExecute(() -> helper.setBlock(pos.east(), Blocks.LAVA)) - .thenIdle(20) - .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.LAVA, pos.east())) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 10), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount()) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.WATER, pos.east())) + .thenExecute(() -> helper.setBlock(pos.east(), Blocks.LAVA)) + .thenIdle(20) + .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.LAVA, pos.east())) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount()) + )) + .thenSucceed(); }); } } diff --git a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/exporter/ExporterTest.java b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/exporter/ExporterTest.java index dd4e4809f..4bcc08ee4 100644 --- a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/exporter/ExporterTest.java +++ b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/exporter/ExporterTest.java @@ -10,13 +10,17 @@ import net.minecraft.gametest.framework.GameTest; import net.minecraft.gametest.framework.GameTestHelper; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.LayeredCauldronBlock; import net.neoforged.neoforge.gametest.GameTestHolder; import net.neoforged.neoforge.gametest.PrefixGameTestTemplate; +import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.RSBLOCKS; +import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.RSITEMS; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.asResource; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.containerContainsExactly; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.insert; +import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.interfaceContainsExactly; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.networkIsAvailable; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.storageContainsExactly; import static com.refinedmods.refinedstorage.platform.common.exporter.ExporterTestPlots.preparePlot; @@ -33,7 +37,7 @@ private ExporterTest() { @GameTest(template = "empty_15x15") public static void shouldExportItem(final GameTestHelper helper) { - preparePlot(helper, true, Direction.EAST, (exporter, pos, sequence) -> { + preparePlot(helper, Blocks.CHEST, Direction.EAST, (exporter, pos, sequence) -> { // Arrange final ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); damagedDiamondChestplate.setDamageValue(500); @@ -50,15 +54,60 @@ public static void shouldExportItem(final GameTestHelper helper) { // Assert sequence .thenWaitUntil(containerContainsExactly( - helper, - pos.east(), - new ResourceAmount(asResource(DIRT), 1))) + helper, + pos.east(), + new ResourceAmount(asResource(DIRT), 1) + )) .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 9), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(damagedDiamondChestplate), 1) + helper, + pos, + new ResourceAmount(asResource(DIRT), 9), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldExportItemWithStackUpgrade(final GameTestHelper helper) { + preparePlot(helper, Blocks.CHEST, Direction.EAST, (exporter, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 65); + insert(helper, network, STONE, 1); + })); + + // Act + exporter.setFilters(List.of(asResource(DIRT))); + exporter.addUpgradeItem(RSITEMS.getStackUpgrade()); + + // Assert + sequence + .thenIdle(9) + .thenExecute(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIRT), 64) + )) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 1), + new ResourceAmount(asResource(STONE), 1) + )) + .thenExecute(() -> exporter.setFilters(List.of(asResource(STONE)))) + .thenIdle(9) + .thenExecute(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIRT), 64), + new ResourceAmount(asResource(STONE), 1) + )) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 1) )) .thenSucceed(); }); @@ -66,7 +115,7 @@ public static void shouldExportItem(final GameTestHelper helper) { @GameTest(template = "empty_15x15") public static void shouldExportItemFuzzy(final GameTestHelper helper) { - preparePlot(helper, true, Direction.EAST, (exporter, pos, sequence) -> { + preparePlot(helper, Blocks.CHEST, Direction.EAST, (exporter, pos, sequence) -> { // Arrange final ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); damagedDiamondChestplate.setDamageValue(500); @@ -84,24 +133,25 @@ public static void shouldExportItemFuzzy(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(containerContainsExactly( - helper, - pos.east(), - new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1), - new ResourceAmount(asResource(damagedDiamondChestplate), 1))) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 10), - new ResourceAmount(asResource(STONE), 15) - )) - .thenSucceed(); + .thenWaitUntil(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); }); } @GameTest(template = "empty_15x15") public static void shouldExportFluid(final GameTestHelper helper) { - preparePlot(helper, false, Direction.EAST, (exporter, pos, sequence) -> { + preparePlot(helper, Blocks.CAULDRON, Direction.EAST, (exporter, pos, sequence) -> { // Arrange sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { insert(helper, network, DIRT, 10); @@ -114,16 +164,52 @@ public static void shouldExportFluid(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertBlockProperty(pos.east(), LayeredCauldronBlock.LEVEL, - LayeredCauldronBlock.MAX_FILL_LEVEL)) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 10), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount()) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertBlockProperty(pos.east(), LayeredCauldronBlock.LEVEL, + LayeredCauldronBlock.MAX_FILL_LEVEL)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount()) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldExportFluidWithStackUpgrade(final GameTestHelper helper) { + preparePlot(helper, RSBLOCKS.getInterface(), Direction.EAST, (exporter, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + insert(helper, network, WATER, Platform.INSTANCE.getBucketAmount() * 64); + })); + + // Act + exporter.addUpgradeItem(RSITEMS.getStackUpgrade()); + exporter.setFilters(List.of(asResource(WATER))); + + // Assert + sequence + .thenIdle(9) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenExecute(interfaceContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16) + )) + + .thenSucceed(); }); } } diff --git a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/exporter/ExporterTestPlots.java b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/exporter/ExporterTestPlots.java index ae62e4ce8..9eefb3773 100644 --- a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/exporter/ExporterTestPlots.java +++ b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/exporter/ExporterTestPlots.java @@ -7,7 +7,7 @@ import net.minecraft.core.Direction; import net.minecraft.gametest.framework.GameTestHelper; import net.minecraft.gametest.framework.GameTestSequence; -import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.Block; import org.apache.commons.lang3.function.TriConsumer; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.RSBLOCKS; @@ -19,7 +19,7 @@ private ExporterTestPlots() { } static void preparePlot(final GameTestHelper helper, - final boolean itemTest, + final Block block, final Direction direction, final TriConsumer consumer) { helper.setBlock(ZERO.above(), RSBLOCKS.getCreativeController().getDefault()); @@ -30,7 +30,7 @@ static void preparePlot(final GameTestHelper helper, ); final BlockPos exporterPos = ZERO.above().above().above(); helper.setBlock(exporterPos, RSBLOCKS.getExporter().getDefault().rotated(direction)); - helper.setBlock(exporterPos.east(), itemTest ? Blocks.CHEST : Blocks.CAULDRON); + helper.setBlock(exporterPos.east(), block); consumer.accept( requireBlockEntity(helper, exporterPos, ExporterBlockEntity.class), exporterPos, diff --git a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/importer/ImporterTest.java b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/importer/ImporterTest.java index f2650ec82..1cfe5ebd8 100644 --- a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/importer/ImporterTest.java +++ b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/importer/ImporterTest.java @@ -16,17 +16,22 @@ import net.neoforged.neoforge.gametest.GameTestHolder; import net.neoforged.neoforge.gametest.PrefixGameTestTemplate; +import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.RSITEMS; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.asResource; +import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.assertInterfaceEmpty; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.containerContainsExactly; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.insert; +import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.interfaceContainsExactly; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.networkIsAvailable; import static com.refinedmods.refinedstorage.platform.common.GameTestUtil.storageContainsExactly; import static com.refinedmods.refinedstorage.platform.common.importer.ImporterTestPlots.prepareChest; +import static com.refinedmods.refinedstorage.platform.common.importer.ImporterTestPlots.prepareInterface; import static com.refinedmods.refinedstorage.platform.common.importer.ImporterTestPlots.preparePlot; import static net.minecraft.world.item.Items.COBBLESTONE; import static net.minecraft.world.item.Items.DIAMOND_CHESTPLATE; import static net.minecraft.world.item.Items.DIRT; import static net.minecraft.world.item.Items.STONE; +import static net.minecraft.world.level.material.Fluids.LAVA; import static net.minecraft.world.level.material.Fluids.WATER; @GameTestHolder(IdentifierUtil.MOD_ID) @@ -45,20 +50,69 @@ public static void shouldImportItem(final GameTestHelper helper) { })); // Act - prepareChest(helper, pos.east(), DIRT.getDefaultInstance(), - COBBLESTONE.getDefaultInstance().copyWithCount(3)); + prepareChest( + helper, + pos.east(), + DIRT.getDefaultInstance(), + COBBLESTONE.getDefaultInstance().copyWithCount(3) + ); // Assert sequence - .thenWaitUntil(() -> helper.assertContainerEmpty(pos.east())) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 11), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(COBBLESTONE), 3) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertContainerEmpty(pos.east())) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(COBBLESTONE), 3) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldImportItemWithStackUpgrade(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + prepareChest( + helper, + pos.east(), + COBBLESTONE.getDefaultInstance().copyWithCount(64), + DIRT.getDefaultInstance() + ); + importer.addUpgradeItem(RSITEMS.getStackUpgrade()); + + // Assert + sequence + .thenExecute(() -> containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIRT), 1) + )) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(COBBLESTONE), 64) + )) + .thenIdle(9) + .thenExecute(() -> helper.assertContainerEmpty(pos.east())) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(COBBLESTONE), 64) + )) + .thenSucceed(); }); } @@ -74,8 +128,12 @@ public static void shouldImportItemBlocklist(final GameTestHelper helper) { // Act final ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); damagedDiamondChestplate.setDamageValue(500); - prepareChest(helper, pos.east(), DIRT.getDefaultInstance(), - DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate); + prepareChest( + helper, + pos.east(), + DIRT.getDefaultInstance(), + DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate + ); importer.setFuzzyMode(false); importer.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()))); @@ -83,18 +141,19 @@ public static void shouldImportItemBlocklist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(containerContainsExactly( - helper, - pos.east(), - new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1))) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 11), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(damagedDiamondChestplate), 1) - )) - .thenSucceed(); + .thenWaitUntil(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenSucceed(); }); } @@ -110,8 +169,12 @@ public static void shouldImportItemFuzzyBlocklist(final GameTestHelper helper) { // Act final ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); damagedDiamondChestplate.setDamageValue(500); - prepareChest(helper, pos.east(), DIRT.getDefaultInstance(), - DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate); + prepareChest( + helper, + pos.east(), + DIRT.getDefaultInstance(), + DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate + ); importer.setFuzzyMode(true); importer.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()))); @@ -119,19 +182,20 @@ public static void shouldImportItemFuzzyBlocklist(final GameTestHelper helper) { // Assert sequence - .thenIdle(10) - .thenWaitUntil(containerContainsExactly( - helper, - pos.east(), - new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1), - new ResourceAmount(asResource(damagedDiamondChestplate), 1))) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 11), - new ResourceAmount(asResource(STONE), 15) - )) - .thenSucceed(); + .thenIdle(9) + .thenWaitUntil(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); }); } @@ -147,8 +211,12 @@ public static void shouldImportItemAllowlist(final GameTestHelper helper) { // Act final ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); damagedDiamondChestplate.setDamageValue(500); - prepareChest(helper, pos.east(), DIRT.getDefaultInstance(), - DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate); + prepareChest( + helper, + pos.east(), + DIRT.getDefaultInstance(), + DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate + ); importer.setFuzzyMode(false); importer.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()))); @@ -156,20 +224,21 @@ public static void shouldImportItemAllowlist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertContainerContains(pos.east(), DIRT)) - .thenWaitUntil(containerContainsExactly( - helper, - pos.east(), - new ResourceAmount(asResource(DIRT), 1), - new ResourceAmount(asResource(damagedDiamondChestplate), 1))) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 10), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertContainerContains(pos.east(), DIRT)) + .thenWaitUntil(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIRT), 1), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1) + )) + .thenSucceed(); }); } @@ -185,8 +254,12 @@ public static void shouldImportItemFuzzyAllowlist(final GameTestHelper helper) { // Act final ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); damagedDiamondChestplate.setDamageValue(500); - prepareChest(helper, pos.east(), DIRT.getDefaultInstance(), - DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate); + prepareChest( + helper, + pos.east(), + DIRT.getDefaultInstance(), + DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate + ); importer.setFuzzyMode(true); importer.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()))); @@ -194,16 +267,16 @@ public static void shouldImportItemFuzzyAllowlist(final GameTestHelper helper) { // Assert sequence - .thenWaitUntil(() -> helper.assertContainerContains(pos.east(), DIRT)) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 10), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1), - new ResourceAmount(asResource(damagedDiamondChestplate), 1) - )) - .thenSucceed(); + .thenWaitUntil(() -> helper.assertContainerContains(pos.east(), DIRT)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()), 1), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenSucceed(); }); } @@ -218,19 +291,68 @@ public static void shouldImportFluid(final GameTestHelper helper) { // Act helper.setBlock(pos.east(), Blocks.WATER_CAULDRON.defaultBlockState() - .setValue(LayeredCauldronBlock.LEVEL, LayeredCauldronBlock.MAX_FILL_LEVEL)); + .setValue(LayeredCauldronBlock.LEVEL, LayeredCauldronBlock.MAX_FILL_LEVEL)); + + // Assert + sequence + .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.CAULDRON, pos.east())) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount()) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldImportFluidWithStackUpgrade(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + prepareInterface( + helper, + pos.east(), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 15), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + ); + importer.addUpgradeItem(RSITEMS.getStackUpgrade()); // Assert sequence - .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.CAULDRON, pos.east())) - .thenWaitUntil(storageContainsExactly( - helper, - pos, - new ResourceAmount(asResource(DIRT), 10), - new ResourceAmount(asResource(STONE), 15), - new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount()) - )) - .thenSucceed(); + .thenExecute(interfaceContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + )) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 63) + )) + .thenIdle(9) + .thenExecute(assertInterfaceEmpty(helper, pos.east())) + .thenExecute(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 63), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + )) + .thenSucceed(); }); } } diff --git a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/importer/ImporterTestPlots.java b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/importer/ImporterTestPlots.java index fa19894f5..54d6dda48 100644 --- a/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/importer/ImporterTestPlots.java +++ b/refinedstorage-platform-neoforge/src/test/java/com/refinedmods/refinedstorage/platform/common/importer/ImporterTestPlots.java @@ -1,5 +1,8 @@ package com.refinedmods.refinedstorage.platform.common.importer; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; +import com.refinedmods.refinedstorage.platform.common.iface.ExportedResourcesContainer; +import com.refinedmods.refinedstorage.platform.common.iface.InterfaceBlockEntity; import com.refinedmods.refinedstorage.platform.common.storage.FluidStorageVariant; import com.refinedmods.refinedstorage.platform.common.storage.ItemStorageVariant; @@ -47,4 +50,16 @@ static void prepareChest(final GameTestHelper helper, chestBlockEntity.setItem(i, stacks[i]); } } + + static void prepareInterface(final GameTestHelper helper, + final BlockPos pos, + final ResourceAmount... resource) { + helper.setBlock(pos, RSBLOCKS.getInterface()); + final var interfaceBlockEntity = requireBlockEntity(helper, pos, InterfaceBlockEntity.class); + final ExportedResourcesContainer exportedResources = interfaceBlockEntity.getExportedResources(); + + for (int i = 0; i < resource.length; i++) { + exportedResources.set(i, resource[i]); + } + } }