From f4109bf7e8328e3af0cb591f69ce5079f97915c6 Mon Sep 17 00:00:00 2001 From: Starforcraft Date: Sun, 26 May 2024 21:26:45 +0200 Subject: [PATCH] More exporter and importer tests --- .../common/importer/ImporterBlockEntity.java | 7 +- .../common/exporter/ExporterTest.java | 39 +++++++++- .../common/importer/ImporterTest.java | 71 +++++++++++++++++++ .../platform/forge/GameTestUtil.java | 27 +++++++ 4 files changed, 141 insertions(+), 3 deletions(-) diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterBlockEntity.java index 1945dfab1..e1528d8ab 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterBlockEntity.java @@ -20,6 +20,7 @@ import com.refinedmods.refinedstorage2.platform.common.upgrade.UpgradeDestinations; import java.util.List; +import java.util.Set; import java.util.function.LongSupplier; import javax.annotation.Nullable; @@ -57,7 +58,7 @@ public ImporterBlockEntity(final BlockPos pos, final BlockState state) { this.filter = FilterWithFuzzyMode.createAndListenForUniqueFilters( ResourceContainerImpl.createForFilter(), this::setChanged, - mainNode::setFilters + this::setFilters ); this.mainNode.setNormalizer(filter.createNormalizer()); } @@ -96,6 +97,10 @@ public void readConfiguration(final CompoundTag tag) { filter.load(tag); } + protected void setFilters(final Set filters) { + mainNode.setFilters(filters); + } + boolean isFuzzyMode() { return filter.isFuzzyMode(); } diff --git a/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/exporter/ExporterTest.java b/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/exporter/ExporterTest.java index 8783cbc88..ccff00355 100644 --- a/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/exporter/ExporterTest.java +++ b/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/exporter/ExporterTest.java @@ -2,10 +2,12 @@ import com.refinedmods.refinedstorage2.api.resource.ResourceAmount; import com.refinedmods.refinedstorage2.platform.common.Platform; +import com.refinedmods.refinedstorage2.platform.common.support.resource.ItemResource; import com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil; import net.minecraft.core.Direction; 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; @@ -15,8 +17,7 @@ import static com.refinedmods.refinedstorage2.platform.common.exporter.ExporterTestPlots.preparePlot; import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.*; -import static net.minecraft.world.item.Items.DIRT; -import static net.minecraft.world.item.Items.STONE; +import static net.minecraft.world.item.Items.*; import static net.minecraft.world.level.material.Fluids.WATER; @GameTestHolder(IdentifierUtil.MOD_ID) @@ -52,6 +53,40 @@ public static void shouldExportBlock(final GameTestHelper helper) { }); } + @GameTest(template = "empty_15x15") + public static void shouldExportBlockFuzzy(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (exporter, pos, sequence) -> { + // Arrange + helper.setBlock(pos.east(), Blocks.CHEST); + + ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); + damagedDiamondChestplate.setDamageValue(500); + + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + insert(helper, network, DIAMOND_CHESTPLATE, 1); + insert(helper, network, new ItemResource(damagedDiamondChestplate.getItem(), damagedDiamondChestplate.getTag()), 1); + })); + + // Act + exporter.setFuzzyMode(true); + exporter.setFilters(List.of(asResource(DIAMOND_CHESTPLATE))); + + // Assert + sequence + .thenWaitUntil(() -> assertContainerContains(helper, pos.east(), DIAMOND_CHESTPLATE.getDefaultInstance())) + .thenWaitUntil(() -> assertContainerContains(helper, pos.east(), damagedDiamondChestplate)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + @GameTest(template = "empty_15x15") public static void shouldExportWater(final GameTestHelper helper) { preparePlot(helper, Direction.EAST, (exporter, pos, sequence) -> { diff --git a/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java b/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java index 78674c9d6..59b491aac 100644 --- a/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java +++ b/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java @@ -1,16 +1,21 @@ package com.refinedmods.refinedstorage2.platform.common.importer; import com.refinedmods.refinedstorage2.api.resource.ResourceAmount; +import com.refinedmods.refinedstorage2.api.resource.filter.FilterMode; import com.refinedmods.refinedstorage2.platform.common.Platform; import com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil; import net.minecraft.core.Direction; import net.minecraft.gametest.framework.GameTest; import net.minecraft.gametest.framework.GameTestHelper; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; 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 java.util.Set; + import static com.refinedmods.refinedstorage2.platform.common.importer.ImporterTestPlots.prepareChest; import static com.refinedmods.refinedstorage2.platform.common.importer.ImporterTestPlots.preparePlot; import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.*; @@ -48,6 +53,71 @@ public static void shouldImportBlock(final GameTestHelper helper) { }); } + @GameTest(template = "empty_15x15") + public static void shouldImportBlockFuzzyBlocklist(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> { + // Arrange + ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); + damagedDiamondChestplate.setDamageValue(500); + prepareChest(helper, pos.east(), Items.DIRT.getDefaultInstance(), DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate); + + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + importer.setFuzzyMode(true); + importer.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()))); + importer.setFilterMode(FilterMode.BLOCK); + + // Assert + sequence + .thenWaitUntil(() -> assertContainerContains(helper, pos.east(), DIAMOND_CHESTPLATE.getDefaultInstance())) + .thenWaitUntil(() -> assertContainerContains(helper, pos.east(), damagedDiamondChestplate)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 11), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldImportBlockFuzzyAllowlist(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> { + // Arrange + ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance(); + damagedDiamondChestplate.setDamageValue(500); + prepareChest(helper, pos.east(), Items.DIRT.getDefaultInstance(), DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate); + + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + importer.setFuzzyMode(true); + importer.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()))); + importer.setFilterMode(FilterMode.ALLOW); + + // Assert + sequence + .thenWaitUntil(() -> helper.assertContainerContains(pos.east(), Items.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(); + }); + } + @GameTest(template = "empty_15x15") public static void shouldImportWater(final GameTestHelper helper) { preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> { @@ -61,6 +131,7 @@ public static void shouldImportWater(final GameTestHelper helper) { // Assert sequence + .thenWaitUntil(() -> helper.assertBlockPresent(Blocks.CAULDRON, pos.east())) .thenWaitUntil(storageContainsExactly( helper, pos, diff --git a/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/forge/GameTestUtil.java b/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/forge/GameTestUtil.java index 5485bc5f1..fa033c279 100644 --- a/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/forge/GameTestUtil.java +++ b/refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/forge/GameTestUtil.java @@ -14,12 +14,17 @@ import java.util.Arrays; import java.util.function.Consumer; +import java.util.stream.IntStream; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.gametest.framework.GameTestAssertException; import net.minecraft.gametest.framework.GameTestHelper; +import net.minecraft.nbt.CompoundTag; import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +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; @@ -94,6 +99,24 @@ public static T requireBlockEntity( return (T) blockEntity; } + public static void assertContainerContains(final GameTestHelper helper, + final BlockPos pos, + final ItemStack itemStack) { + BlockPos blockpos = helper.absolutePos(pos); + BlockEntity blockentity = helper.getLevel().getBlockEntity(blockpos); + if (!(blockentity instanceof BaseContainerBlockEntity containerBlockEntity)) { + throw new GameTestAssertException("Expected a container at " + pos + ", found " + BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(blockentity.getType())); + } else { + boolean success = IntStream.range(0, containerBlockEntity.getContainerSize()) + .mapToObj(containerBlockEntity::getItem) + .anyMatch(stack -> stack.getItem().equals(itemStack.getItem()) && stack.getTag().equals(itemStack.getTag())); + + if(!success) { + throw new GameTestAssertException("Container should contain: " + itemStack.getItem() + " with tag: " + itemStack.getTag()); + } + } + } + public static void assertFluidPresent(final GameTestHelper helper, final BlockPos pos, final Fluid fluid, @@ -131,6 +154,10 @@ public static ItemResource asResource(final Item item) { return new ItemResource(item, null); } + public static ItemResource asResource(final ItemStack itemStack) { + return new ItemResource(itemStack.getItem(), itemStack.getTag()); + } + public static FluidResource asResource(final Fluid fluid) { return new FluidResource(fluid, null); }