Skip to content

Commit

Permalink
feat: relay gametest
Browse files Browse the repository at this point in the history
  • Loading branch information
starforcraft committed Dec 11, 2024
1 parent 102ae60 commit 26b7632
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -98,7 +98,7 @@ void setFuzzyMode(final boolean fuzzyMode) {
mainNetworkNode.setActive(wasActive);
}

private void filterContainerChanged(final Set<ResourceKey> filters) {
void setFilters(final Set<ResourceKey> filters) {
mainNetworkNode.setFilters(filters);
setChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
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.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.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();
});
}
}
Original file line number Diff line number Diff line change
@@ -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<RelayBlockEntity, BlockPos, GameTestSequence> 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()
);
}
}

0 comments on commit 26b7632

Please sign in to comment.