From 070ff514ef0e260134bae06faf170a83325f7277 Mon Sep 17 00:00:00 2001 From: Starforcraft Date: Wed, 11 Dec 2024 16:47:51 +0100 Subject: [PATCH] feat: relay gametest --- .../common/networking/RelayBlockEntity.java | 4 +- ...ctBaseNetworkNodeContainerBlockEntity.java | 2 +- .../refinedstorage/common/GameTestUtil.java | 14 + .../common/networking/RelayTest.java | 457 ++++++++++++++++++ .../common/networking/RelayTestPlots.java | 37 ++ 5 files changed, 511 insertions(+), 3 deletions(-) create mode 100644 refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/networking/RelayTest.java create mode 100644 refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/networking/RelayTestPlots.java diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/networking/RelayBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/networking/RelayBlockEntity.java index 2898b7ab3..768fa7b96 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/networking/RelayBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/networking/RelayBlockEntity.java @@ -71,7 +71,7 @@ public RelayBlockEntity(final BlockPos pos, final BlockState state) { this.filter = FilterWithFuzzyMode.createAndListenForUniqueFilters( ResourceContainerImpl.createForFilter(), this::setChanged, - this::filterContainerChanged + this::setFilters ); this.mainNetworkNode.setFilterNormalizer(filter.createNormalizer()); this.containers.addContainer( @@ -98,7 +98,7 @@ void setFuzzyMode(final boolean fuzzyMode) { mainNetworkNode.setActive(wasActive); } - private void filterContainerChanged(final Set filters) { + void setFilters(final Set filters) { mainNetworkNode.setFilters(filters); setChanged(); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/network/AbstractBaseNetworkNodeContainerBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/network/AbstractBaseNetworkNodeContainerBlockEntity.java index 5f2a3dd74..c1466c893 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/network/AbstractBaseNetworkNodeContainerBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/network/AbstractBaseNetworkNodeContainerBlockEntity.java @@ -71,7 +71,7 @@ protected InWorldNetworkNodeContainer createMainContainer(final T networkNode) { .build(); } - protected boolean calculateActive() { + public boolean calculateActive() { final long energyUsage = mainNetworkNode.getEnergyUsage(); final boolean hasLevel = level != null && level.isLoaded(worldPosition); final boolean redstoneModeActive = !hasRedstoneMode() 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 ea15f2a27..41087dc5b 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 @@ -16,6 +16,7 @@ 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.network.AbstractBaseNetworkNodeContainerBlockEntity; import com.refinedmods.refinedstorage.common.support.resource.FluidResource; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; @@ -70,6 +71,19 @@ public static Runnable networkIsAvailable(final GameTestHelper helper, }; } + public static void checkBlockEntityActiveness(final GameTestHelper helper, + final BlockPos pos, + final boolean expectedActive) { + final var blockEntity = requireBlockEntity( + helper, + pos, + AbstractBaseNetworkNodeContainerBlockEntity.class + ); + final boolean actualActive = blockEntity.calculateActive(); + helper.assertTrue(actualActive == expectedActive, "Activeness of Block Entity should be " + expectedActive + + " but is " + actualActive); + } + public static void insert(final GameTestHelper helper, final Network network, final Item resource, diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/networking/RelayTest.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/networking/RelayTest.java new file mode 100644 index 000000000..b0180ce75 --- /dev/null +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/networking/RelayTest.java @@ -0,0 +1,457 @@ +package com.refinedmods.refinedstorage.common.networking; + +import com.refinedmods.refinedstorage.api.resource.ResourceAmount; +import com.refinedmods.refinedstorage.api.resource.filter.FilterMode; +import com.refinedmods.refinedstorage.api.storage.AccessMode; +import com.refinedmods.refinedstorage.common.util.IdentifierUtil; + +import java.util.Set; + +import net.minecraft.core.BlockPos; +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.checkBlockEntityActiveness; +import static com.refinedmods.refinedstorage.common.GameTestUtil.extract; +import static com.refinedmods.refinedstorage.common.GameTestUtil.getItemAsDamaged; +import static com.refinedmods.refinedstorage.common.GameTestUtil.insert; +import static com.refinedmods.refinedstorage.common.GameTestUtil.networkIsAvailable; +import static com.refinedmods.refinedstorage.common.GameTestUtil.storageContainsExactly; +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; + +@GameTestHolder(IdentifierUtil.MOD_ID) +@PrefixGameTestTemplate(false) +public final class RelayTest { + private RelayTest() { + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThrough(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(true); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldNotPassThrough(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(false); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, false)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughEnergy(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(storageContainsExactly(helper, pos2)) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughStorage(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + relay.setPassStorage(true); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughStorageBlocklist(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + relay.setPassStorage(true); + relay.setFilters(Set.of(asResource(DIRT))); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughStorageFuzzyBlocklist(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + 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, DIAMOND_CHESTPLATE, 1); + insert(helper, network, asResource(damagedDiamondChestplate), 1); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + relay.setPassStorage(true); + relay.setFuzzyMode(true); + relay.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()))); + relay.setFilterMode(FilterMode.BLOCK); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15), + new ResourceAmount(asResource(DIAMOND_CHESTPLATE), 1), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughStorageAllowlist(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + relay.setPassStorage(true); + relay.setFilters(Set.of(asResource(DIRT))); + relay.setFilterMode(FilterMode.ALLOW); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 10) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughStorageFuzzyAllowlist(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + 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, DIAMOND_CHESTPLATE, 1); + insert(helper, network, asResource(damagedDiamondChestplate), 1); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + relay.setPassStorage(true); + relay.setFuzzyMode(true); + relay.setFilters(Set.of(asResource(DIAMOND_CHESTPLATE.getDefaultInstance()))); + relay.setFilterMode(FilterMode.ALLOW); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIAMOND_CHESTPLATE), 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), 1), + new ResourceAmount(asResource(damagedDiamondChestplate), 1) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughStorageAndInsertExtract(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + relay.setPassStorage(true); + relay.setAccessMode(AccessMode.INSERT_EXTRACT); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(networkIsAvailable(helper, pos2, network -> + insert(helper, network, DIRT, 10))) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 20), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 20), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(networkIsAvailable(helper, pos2, network -> + extract(helper, network, DIRT, 10))) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughStorageAndInsert(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + relay.setPassStorage(true); + relay.setAccessMode(AccessMode.INSERT); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(networkIsAvailable(helper, pos2, network -> + insert(helper, network, DIRT, 10))) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 20), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 20), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(networkIsAvailable(helper, pos2, network -> + extract(helper, network, DIRT, 10, false))) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 20), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 20), + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } + + @GameTest(template = "empty_15x15") + public static void shouldPassThroughStorageAndExtract(final GameTestHelper helper) { + RelayTestPlots.preparePlot(helper, (relay, pos, sequence) -> { + // Arrange + final BlockPos pos2 = pos.above(); + sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> { + insert(helper, network, DIRT, 10); + insert(helper, network, STONE, 15); + })); + + // Act + relay.setPassThrough(false); + relay.setPassEnergy(true); + relay.setPassStorage(true); + relay.setAccessMode(AccessMode.EXTRACT); + + // Assert + sequence + .thenWaitUntil(() -> checkBlockEntityActiveness(helper, pos2, true)) + .thenWaitUntil(networkIsAvailable(helper, pos2, network -> + insert(helper, network, DIRT, 10, false))) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(DIRT), 10), + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(networkIsAvailable(helper, pos2, network -> + extract(helper, network, DIRT, 10))) + .thenWaitUntil(storageContainsExactly( + helper, + pos2, + new ResourceAmount(asResource(STONE), 15) + )) + .thenWaitUntil(storageContainsExactly( + helper, + pos, + new ResourceAmount(asResource(STONE), 15) + )) + .thenSucceed(); + }); + } +} diff --git a/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/networking/RelayTestPlots.java b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/networking/RelayTestPlots.java new file mode 100644 index 000000000..0a906137d --- /dev/null +++ b/refinedstorage-neoforge/src/test/java/com/refinedmods/refinedstorage/common/networking/RelayTestPlots.java @@ -0,0 +1,37 @@ +package com.refinedmods.refinedstorage.common.networking; + +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 RelayTestPlots { + private RelayTestPlots() { + } + + 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 relayPos = ZERO.above().above().above(); + helper.setBlock(relayPos, RSBLOCKS.getRelay().getDefault().rotated(Direction.UP)); + helper.setBlock(relayPos.above(), RSBLOCKS.getGrid().getDefault()); + consumer.accept( + requireBlockEntity(helper, relayPos, RelayBlockEntity.class), + relayPos, + helper.startSequence() + ); + } +}