From 29985c0bae3114145c245d36600dcf800a3e7d6c Mon Sep 17 00:00:00 2001 From: Starforcraft Date: Tue, 10 Sep 2024 21:11:43 +0200 Subject: [PATCH] feat: interface gametest --- .../common/iface/InterfaceBlockEntity.java | 12 + .../common/iface/InterfaceTest.java | 279 ++++++++++++++++++ .../common/iface/InterfaceTestPlots.java | 35 +++ 3 files changed, 326 insertions(+) create mode 100644 refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/iface/InterfaceTest.java create mode 100644 refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/iface/InterfaceTestPlots.java diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/iface/InterfaceBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/iface/InterfaceBlockEntity.java index 3817a7da2..8d80a2eec 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/iface/InterfaceBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/iface/InterfaceBlockEntity.java @@ -3,6 +3,7 @@ import com.refinedmods.refinedstorage.api.network.impl.node.iface.InterfaceNetworkNode; import com.refinedmods.refinedstorage.api.network.impl.node.iface.externalstorage.InterfaceExternalStorageProvider; import com.refinedmods.refinedstorage.api.network.impl.node.iface.externalstorage.InterfaceExternalStorageProviderImpl; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.api.resource.ResourceKey; import com.refinedmods.refinedstorage.common.Platform; import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; @@ -17,6 +18,7 @@ import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerData; import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerImpl; +import java.util.List; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; @@ -137,6 +139,16 @@ void setFuzzyMode(final boolean fuzzyMode) { filter.setFuzzyMode(fuzzyMode); } + void clearFilters() { + filter.getFilterContainer().clear(); + } + + void setFilters(final List filters) { + for (int i = 0; i < filters.size(); i++) { + filter.getFilterContainer().set(i, filters.get(i)); + } + } + public ExportedResourcesContainer getExportedResources() { return exportedResources; } diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/iface/InterfaceTest.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/iface/InterfaceTest.java new file mode 100644 index 000000000..5a02aef4c --- /dev/null +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/iface/InterfaceTest.java @@ -0,0 +1,279 @@ +package com.refinedmods.refinedstorage.common.iface; + +import com.refinedmods.refinedstorage.api.core.Action; +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; +import com.refinedmods.refinedstorage.common.Platform; +import com.refinedmods.refinedstorage.common.util.IdentifierUtil; + +import java.util.List; + +import net.minecraft.gametest.framework.GameTest; +import net.minecraft.gametest.framework.GameTestHelper; +import net.minecraft.world.item.ItemStack; +import net.neoforged.neoforge.gametest.GameTestHolder; +import net.neoforged.neoforge.gametest.PrefixGameTestTemplate; + +import static com.refinedmods.refinedstorage.common.GameTestUtil.asResource; +import static com.refinedmods.refinedstorage.common.GameTestUtil.assertInterfaceEmpty; +import static com.refinedmods.refinedstorage.common.GameTestUtil.getItemAsDamaged; +import static com.refinedmods.refinedstorage.common.GameTestUtil.insert; +import static com.refinedmods.refinedstorage.common.GameTestUtil.interfaceContainsExactly; +import static com.refinedmods.refinedstorage.common.GameTestUtil.networkIsAvailable; +import static com.refinedmods.refinedstorage.common.GameTestUtil.storageContainsExactly; +import static com.refinedmods.refinedstorage.common.iface.InterfaceTestPlots.preparePlot; +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.WATER; + +@GameTestHolder(IdentifierUtil.MOD_ID) +@PrefixGameTestTemplate(false) +public final class InterfaceTest { + private InterfaceTest() { + } + + @GameTest(template = "empty_15x15") + public static void shouldExportItem(final GameTestHelper helper) { + preparePlot(helper, (iface, pos, sequence) -> { + // Arrange + final ItemStack damagedDiamondChestplate = getItemAsDamaged(DIAMOND_CHESTPLATE.getDefaultInstance(), 500); + + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + insert(helper, network, asResource(damagedDiamondChestplate), 1); + })); + + // Act + iface.setFuzzyMode(false); + iface.setFilters(List.of( + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE), 1) + )); + + // Assert + sequence + .thenWaitUntil(interfaceContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenExecute(() -> { + iface.clearFilters(); + iface.setFilters(List.of( + new ResourceAmount(asResource(DIRT), 5) + )); + }) + .thenWaitUntil(interfaceContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 5) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 5), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenExecute(() -> iface.setFilters(List.of( + new ResourceAmount(asResource(DIRT), 7) + ))) + .thenWaitUntil(interfaceContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 7) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 3), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldExportItemFuzzy(final GameTestHelper helper) { + preparePlot(helper, (iface, pos, sequence) -> { + // Arrange + final ItemStack damagedDiamondChestplate = getItemAsDamaged(DIAMOND_CHESTPLATE.getDefaultInstance(), 500); + + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + insert(helper, network, asResource(damagedDiamondChestplate), 1); + insert(helper, network, DIAMOND_CHESTPLATE, 1); + })); + + // Act + iface.setFuzzyMode(true); + iface.setFilters(List.of( + new ResourceAmount(asResource(DIRT), 5), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE), 1), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE), 1) + )); + + // Assert + sequence + .thenWaitUntil(interfaceContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 5), + new ResourceAmount(asResource(damagedDiamondChestplate), 1), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE), 1) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 5), + new ResourceAmount(asResource(STONE), 15) + )) + .thenExecute(() -> { + iface.clearFilters(); + iface.setFilters(List.of( + new ResourceAmount(asResource(DIRT), 10)) + ); + }) + .thenWaitUntil(interfaceContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(damagedDiamondChestplate), 1), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE), 1) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldExportFluid(final GameTestHelper helper) { + preparePlot(helper, (iface, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, WATER, Platform.INSTANCE.getBucketAmount() * 10); + insert(helper, network, STONE, 15); + })); + + // Act + iface.setFilters(List.of(new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 10))); + + // Assert + sequence + .thenWaitUntil(interfaceContainsExactly( + helper, + pos, + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 10) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 15) + )) + .thenExecute(() -> iface.setFilters(List.of( + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 5)) + )) + .thenWaitUntil(interfaceContainsExactly( + helper, + pos, + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 5) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 5), + new ResourceAmount(asResource(STONE), 15) + )) + .thenExecute(() -> iface.setFilters(List.of( + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 7)) + )) + .thenWaitUntil(interfaceContainsExactly( + helper, + pos, + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 7) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 3), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldImportItem(final GameTestHelper helper) { + preparePlot(helper, (iface, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 5); + insert(helper, network, STONE, 15); + })); + + // Assert + sequence + .thenWaitUntil(assertInterfaceEmpty(helper, pos)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 5), + new ResourceAmount(asResource(STONE), 15) + )) + .thenExecute(() -> iface.getExportedResources().insert(asResource(DIRT), 5, Action.EXECUTE)) + .thenWaitUntil(assertInterfaceEmpty(helper, pos)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldImportFluid(final GameTestHelper helper) { + preparePlot(helper, (iface, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, WATER, Platform.INSTANCE.getBucketAmount() * 5); + insert(helper, network, STONE, 15); + })); + + // Assert + sequence + .thenWaitUntil(assertInterfaceEmpty(helper, pos)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 5), + new ResourceAmount(asResource(STONE), 15) + )) + .thenExecute(() -> iface.getExportedResources().insert( + asResource(WATER), Platform.INSTANCE.getBucketAmount() * 5, Action.EXECUTE + )) + .thenWaitUntil(assertInterfaceEmpty(helper, pos)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } +} diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/iface/InterfaceTestPlots.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/iface/InterfaceTestPlots.java new file mode 100644 index 000000000..0f63c94e4 --- /dev/null +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/iface/InterfaceTestPlots.java @@ -0,0 +1,35 @@ +package com.refinedmods.refinedstorage.common.iface; + +import com.refinedmods.refinedstorage.common.storage.FluidStorageVariant; +import com.refinedmods.refinedstorage.common.storage.ItemStorageVariant; + +import net.minecraft.core.BlockPos; +import net.minecraft.gametest.framework.GameTestHelper; +import net.minecraft.gametest.framework.GameTestSequence; +import org.apache.commons.lang3.function.TriConsumer; + +import static com.refinedmods.refinedstorage.common.GameTestUtil.RSBLOCKS; +import static com.refinedmods.refinedstorage.common.GameTestUtil.requireBlockEntity; +import static net.minecraft.core.BlockPos.ZERO; + +final class InterfaceTestPlots { + private InterfaceTestPlots() { + } + + static void preparePlot(final GameTestHelper helper, + final TriConsumer consumer) { + helper.setBlock(ZERO.above(), RSBLOCKS.getCreativeController().getDefault()); + helper.setBlock(ZERO.above().above(), RSBLOCKS.getItemStorageBlock(ItemStorageVariant.ONE_K)); + helper.setBlock( + ZERO.above().above().north(), + RSBLOCKS.getFluidStorageBlock(FluidStorageVariant.SIXTY_FOUR_B) + ); + final BlockPos interfacePos = ZERO.above().above().above(); + helper.setBlock(interfacePos, RSBLOCKS.getInterface()); + consumer.accept( + requireBlockEntity(helper, interfacePos, InterfaceBlockEntity.class), + interfacePos, + helper.startSequence() + ); + } +}