diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageBlockEntity.java index 580c8ea61..73408a1c7 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageBlockEntity.java @@ -1,6 +1,8 @@ package com.refinedmods.refinedstorage.common.storage.externalstorage; import com.refinedmods.refinedstorage.api.network.impl.node.externalstorage.ExternalStorageNetworkNode; +import com.refinedmods.refinedstorage.api.resource.ResourceKey; +import com.refinedmods.refinedstorage.api.resource.filter.FilterMode; import com.refinedmods.refinedstorage.common.Platform; import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; import com.refinedmods.refinedstorage.common.content.BlockEntities; @@ -13,6 +15,7 @@ import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerData; import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerImpl; +import java.util.Set; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; @@ -145,6 +148,19 @@ public void readConfiguration(final CompoundTag tag, final HolderLookup.Provider configContainer.load(tag); } + void setFilters(final Set filters) { + mainNetworkNode.getStorageConfiguration().setFilters(filters); + } + + void setFilterMode(final FilterMode mode) { + mainNetworkNode.getStorageConfiguration().setFilterMode(mode); + setChanged(); + } + + void setPriority(final int priority) { + mainNetworkNode.getStorageConfiguration().setPriority(priority); + } + @Override public ResourceContainerData getMenuData() { return ResourceContainerData.of(filter.getFilterContainer()); diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java index 2c68b9160..96e4ae6a1 100644 --- a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/GameTestUtil.java @@ -11,6 +11,7 @@ import com.refinedmods.refinedstorage.common.api.support.resource.ResourceContainer; import com.refinedmods.refinedstorage.common.content.Blocks; import com.refinedmods.refinedstorage.common.content.Items; +import com.refinedmods.refinedstorage.common.iface.ExportedResourcesContainer; import com.refinedmods.refinedstorage.common.iface.InterfaceBlockEntity; import com.refinedmods.refinedstorage.common.support.resource.FluidResource; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; @@ -248,6 +249,28 @@ public static Runnable storageContainsExactly(final GameTestHelper helper, }); } + public static void prepareChest(final GameTestHelper helper, + final BlockPos pos, + final ItemStack... stacks) { + helper.setBlock(pos, net.minecraft.world.level.block.Blocks.CHEST.defaultBlockState()); + final var chestBlockEntity = requireBlockEntity(helper, pos, BaseContainerBlockEntity.class); + for (int i = 0; i < stacks.length; i++) { + chestBlockEntity.setItem(i, stacks[i]); + } + } + + public 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]); + } + } + public static ItemResource asResource(final Item item) { return new ItemResource(item); } diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/importer/ImporterTest.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/importer/ImporterTest.java index 409126da3..36e61a98a 100644 --- a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/importer/ImporterTest.java +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/importer/ImporterTest.java @@ -23,9 +23,9 @@ 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.prepareChest; +import static com.refinedmods.refinedstorage.common.GameTestUtil.prepareInterface; import static com.refinedmods.refinedstorage.common.GameTestUtil.storageContainsExactly; -import static com.refinedmods.refinedstorage.common.importer.ImporterTestPlots.prepareChest; -import static com.refinedmods.refinedstorage.common.importer.ImporterTestPlots.prepareInterface; import static com.refinedmods.refinedstorage.common.importer.ImporterTestPlots.preparePlot; import static net.minecraft.world.item.Items.COBBLESTONE; import static net.minecraft.world.item.Items.DIAMOND_CHESTPLATE; diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/importer/ImporterTestPlots.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/importer/ImporterTestPlots.java index 9c07f625c..957a0c74a 100644 --- a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/importer/ImporterTestPlots.java +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/importer/ImporterTestPlots.java @@ -1,8 +1,5 @@ package com.refinedmods.refinedstorage.common.importer; -import com.refinedmods.refinedstorage.api.resource.ResourceAmount; -import com.refinedmods.refinedstorage.common.iface.ExportedResourcesContainer; -import com.refinedmods.refinedstorage.common.iface.InterfaceBlockEntity; import com.refinedmods.refinedstorage.common.storage.FluidStorageVariant; import com.refinedmods.refinedstorage.common.storage.ItemStorageVariant; @@ -10,9 +7,6 @@ import net.minecraft.core.Direction; import net.minecraft.gametest.framework.GameTestHelper; import net.minecraft.gametest.framework.GameTestSequence; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.entity.BaseContainerBlockEntity; import org.apache.commons.lang3.function.TriConsumer; import static com.refinedmods.refinedstorage.common.GameTestUtil.RSBLOCKS; @@ -40,26 +34,4 @@ static void preparePlot(final GameTestHelper helper, helper.startSequence() ); } - - static void prepareChest(final GameTestHelper helper, - final BlockPos pos, - final ItemStack... stacks) { - helper.setBlock(pos, Blocks.CHEST.defaultBlockState()); - final var chestBlockEntity = requireBlockEntity(helper, pos, BaseContainerBlockEntity.class); - for (int i = 0; i < stacks.length; i++) { - 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]); - } - } } diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageTest.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageTest.java new file mode 100644 index 000000000..956872bb7 --- /dev/null +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageTest.java @@ -0,0 +1,234 @@ +package com.refinedmods.refinedstorage.common.storage.externalstorage; + +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; +import com.refinedmods.refinedstorage.api.resource.filter.FilterMode; +import com.refinedmods.refinedstorage.common.Platform; +import com.refinedmods.refinedstorage.common.util.IdentifierUtil; + +import java.util.Set; + +import net.minecraft.core.Direction; +import net.minecraft.gametest.framework.GameTest; +import net.minecraft.gametest.framework.GameTestHelper; +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.containerContainsExactly; +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.prepareChest; +import static com.refinedmods.refinedstorage.common.GameTestUtil.prepareInterface; +import static com.refinedmods.refinedstorage.common.GameTestUtil.storageContainsExactly; +import static com.refinedmods.refinedstorage.common.storage.externalstorage.ExternalStorageTestPlots.preparePlot; +import static net.minecraft.world.item.Items.COBBLESTONE; +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) +@PrefixGameTestTemplate(false) +public final class ExternalStorageTest { + private ExternalStorageTest() { + } + + @GameTest(template = "empty_15x15") + public static void shouldExposeItem(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (externalStorage, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, STONE, 2); + })); + + // Act + prepareChest( + helper, + pos.east(), + DIRT.getDefaultInstance().copyWithCount(10), + COBBLESTONE.getDefaultInstance().copyWithCount(3) + ); + + // Assert + sequence + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 2), + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(COBBLESTONE), 3) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldStoreItemAllowlist(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (externalStorage, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + })); + + // Act + prepareChest( + helper, + pos.east(), + DIRT.getDefaultInstance().copyWithCount(10), + COBBLESTONE.getDefaultInstance().copyWithCount(3) + ); + + externalStorage.setFilterMode(FilterMode.ALLOW); + externalStorage.setFilters(Set.of(asResource(STONE))); + externalStorage.setPriority(1); + + // Assert + sequence + .thenWaitUntil(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(COBBLESTONE), 3) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(COBBLESTONE), 3) + )) + .thenWaitUntil(networkIsAvailable(helper, pos, network -> insert(helper, network, STONE, 2))) + .thenWaitUntil(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(COBBLESTONE), 3), + new ResourceAmount(asResource(STONE), 2))) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(COBBLESTONE), 3), + new ResourceAmount(asResource(STONE), 2) + )) + .thenWaitUntil(networkIsAvailable(helper, pos, network -> insert(helper, network, DIRT, 2))) + .thenWaitUntil(containerContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(COBBLESTONE), 3), + new ResourceAmount(asResource(STONE), 2) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 12), + new ResourceAmount(asResource(COBBLESTONE), 3), + new ResourceAmount(asResource(STONE), 2) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldExposeFluid(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (externalStorage, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, STONE, 2); + })); + + // Act + prepareInterface( + helper, + pos.east(), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + ); + + // Assert + sequence + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 2), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 32), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldStoreFluidAllowlist(final GameTestHelper helper) { + preparePlot(helper, Direction.EAST, (externalStorage, pos, sequence) -> { + // Arrange + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, STONE, 2); + })); + + // Act + prepareInterface( + helper, + pos.east(), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 14), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + ); + + externalStorage.setFilterMode(FilterMode.ALLOW); + externalStorage.setFilters(Set.of(asResource(WATER))); + externalStorage.setPriority(1); + + // Assert + sequence + .thenWaitUntil(interfaceContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 14), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 2), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 30), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + )) + .thenWaitUntil(networkIsAvailable(helper, pos, network -> + insert(helper, network, WATER, Platform.INSTANCE.getBucketAmount() * 2))) + .thenWaitUntil(interfaceContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 2), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 32), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + )) + .thenWaitUntil(networkIsAvailable(helper, pos, network -> + insert(helper, network, LAVA, Platform.INSTANCE.getBucketAmount()))) + .thenWaitUntil(interfaceContainsExactly( + helper, + pos.east(), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount()) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 2), + new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 32), + new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount() * 2) + )) + .thenSucceed(); + }); + } +} diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageTestPlots.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageTestPlots.java new file mode 100644 index 000000000..a650af7bb --- /dev/null +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageTestPlots.java @@ -0,0 +1,37 @@ +package com.refinedmods.refinedstorage.common.storage.externalstorage; + +import com.refinedmods.refinedstorage.common.storage.FluidStorageVariant; +import com.refinedmods.refinedstorage.common.storage.ItemStorageVariant; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +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 ExternalStorageTestPlots { + private ExternalStorageTestPlots() { + } + + static void preparePlot(final GameTestHelper helper, + final Direction direction, + 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 externalStoragePos = ZERO.above().above().above(); + helper.setBlock(externalStoragePos, RSBLOCKS.getExternalStorage().getDefault().rotated(direction)); + consumer.accept( + requireBlockEntity(helper, externalStoragePos, ExternalStorageBlockEntity.class), + externalStoragePos, + helper.startSequence() + ); + } +}