Skip to content

Commit

Permalink
Merge pull request #534 from starforcraft/feat/GH-248/dtortests
Browse files Browse the repository at this point in the history
Destructor gametests
  • Loading branch information
raoulvdberge authored May 28, 2024
2 parents 51519e3 + 4556982 commit ce7ee72
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.refinedmods.refinedstorage2.api.network.impl.node.SimpleNetworkNode;
import com.refinedmods.refinedstorage2.api.network.node.NetworkNodeActor;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.resource.filter.Filter;
import com.refinedmods.refinedstorage2.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage2.api.storage.Actor;
Expand All @@ -19,6 +20,7 @@
import com.refinedmods.refinedstorage2.platform.common.upgrade.UpgradeDestinations;

import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -57,7 +59,7 @@ public DestructorBlockEntity(final BlockPos pos, final BlockState state) {
this.filterWithFuzzyMode = FilterWithFuzzyMode.createAndListenForUniqueFilters(
ResourceContainerImpl.createForFilter(),
this::setChanged,
filter::setFilters
this::setFilters
);
}

Expand All @@ -73,6 +75,10 @@ public void setPickupItems(final boolean pickupItems) {
}
}

void setFilters(final Set<ResourceKey> filters) {
filter.setFilters(filters);
}

public FilterMode getFilterMode() {
return filter.getMode();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package com.refinedmods.refinedstorage2.platform.common.constructordestructor;

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 java.util.Set;

import net.minecraft.core.Direction;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.world.level.block.Blocks;
import net.neoforged.neoforge.gametest.GameTestHolder;
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;

import static com.refinedmods.refinedstorage2.platform.common.constructordestructor.DestructorTestPlots.preparePlot;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.asResource;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.insert;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.networkIsAvailable;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.storageContainsExactly;
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 DestructorTest {
private DestructorTest() {
}

@GameTest(template = "empty_15x15")
public static void shouldBreakBlock(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (destructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
helper.setBlock(pos.east(), Blocks.DIRT);

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 11),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldBreakBlockAllowlist(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (destructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
destructor.setFilterMode(FilterMode.ALLOW);
destructor.setFilters(Set.of(asResource(DIRT)));

helper.setBlock(pos.east(), Blocks.DIRT);

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east()))
.thenExecute(() -> helper.setBlock(pos.east(), Blocks.STONE))
.thenIdle(20)
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.STONE, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 11),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldBreakBlockBlocklist(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (destructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
destructor.setFilterMode(FilterMode.BLOCK);
destructor.setFilters(Set.of(asResource(STONE)));

helper.setBlock(pos.east(), Blocks.DIRT);

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.DIRT, pos.east()))
.thenExecute(() -> helper.setBlock(pos.east(), Blocks.STONE))
.thenIdle(20)
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.STONE, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 11),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldPickupItemAllowlist(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (destructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
destructor.setFilterMode(FilterMode.ALLOW);
destructor.setPickupItems(true);
destructor.setFilters(Set.of(asResource(DIRT)));

helper.spawnItem(DIRT, pos.east());

// Assert
sequence
.thenWaitUntil(() -> helper.assertItemEntityNotPresent(DIRT, pos.east(), 1))
.thenExecute(() -> helper.spawnItem(STONE, pos.east()))
.thenIdle(20)
.thenWaitUntil(() -> helper.assertItemEntityPresent(STONE, pos.east(), 1))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 11),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldPickupItemBlocklist(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (destructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
destructor.setFilterMode(FilterMode.BLOCK);
destructor.setPickupItems(true);
destructor.setFilters(Set.of(asResource(STONE)));

helper.spawnItem(DIRT, pos.east());

// Assert
sequence
.thenWaitUntil(() -> helper.assertItemEntityNotPresent(DIRT, pos.east(), 1))
.thenExecute(() -> helper.spawnItem(STONE, pos.east()))
.thenIdle(20)
.thenWaitUntil(() -> helper.assertItemEntityPresent(STONE, pos.east(), 1))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 11),
new ResourceAmount(asResource(STONE), 15)
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldDrainFluidAllowlist(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (destructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
destructor.setFilterMode(FilterMode.ALLOW);
destructor.setFilters(Set.of(asResource(WATER)));

helper.setBlock(pos.east(), Blocks.WATER);

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.WATER, pos.east()))
.thenExecute(() -> helper.setBlock(pos.east(), Blocks.DIRT))
.thenIdle(20)
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.DIRT, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 10),
new ResourceAmount(asResource(STONE), 15),
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount())
))
.thenSucceed();
});
}

@GameTest(template = "empty_15x15")
public static void shouldDrainFluidBlocklist(final GameTestHelper helper) {
preparePlot(helper, Direction.EAST, (destructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
destructor.setFilterMode(FilterMode.BLOCK);
destructor.setFilters(Set.of(asResource(LAVA)));

helper.setBlock(pos.east(), Blocks.WATER);

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockNotPresent(Blocks.WATER, pos.east()))
.thenExecute(() -> helper.setBlock(pos.east(), Blocks.LAVA))
.thenIdle(20)
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.LAVA, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 10),
new ResourceAmount(asResource(STONE), 15),
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount())
))
.thenSucceed();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.refinedmods.refinedstorage2.platform.common.constructordestructor;

import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.ItemStorageType;

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.refinedstorage2.platform.forge.GameTestUtil.RSBLOCKS;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.requireBlockEntity;
import static net.minecraft.core.BlockPos.ZERO;

final class DestructorTestPlots {
private DestructorTestPlots() {
}

static void preparePlot(final GameTestHelper helper,
final Direction direction,
final TriConsumer<DestructorBlockEntity, BlockPos, GameTestSequence> consumer) {
helper.setBlock(ZERO.above(), RSBLOCKS.getCreativeController().getDefault());
helper.setBlock(ZERO.above().above(), RSBLOCKS.getItemStorageBlock(ItemStorageType.Variant.ONE_K));
helper.setBlock(
ZERO.above().above().north(),
RSBLOCKS.getFluidStorageBlock(FluidStorageType.Variant.SIXTY_FOUR_B)
);
final BlockPos destructorPos = ZERO.above().above().above();
helper.setBlock(destructorPos, RSBLOCKS.getDestructor().getDefault().rotated(direction));
consumer.accept(
requireBlockEntity(helper, destructorPos, DestructorBlockEntity.class),
destructorPos,
helper.startSequence()
);
}
}

0 comments on commit ce7ee72

Please sign in to comment.