Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importer and exporter gametest #532

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -96,6 +97,10 @@ public void readConfiguration(final CompoundTag tag) {
filter.load(tag);
}

protected void setFilters(final Set<ResourceKey> filters) {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
mainNode.setFilters(filters);
}

boolean isFuzzyMode() {
return filter.isFuzzyMode();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.refinedmods.refinedstorage2.platform.common.exporter;

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;
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;

import java.util.List;

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.*;
import static net.minecraft.world.level.material.Fluids.WATER;

@GameTestHolder(IdentifierUtil.MOD_ID)
@PrefixGameTestTemplate(false)
public final class ExporterTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about testing allowlist/blocklist here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grafik
I don't see a setting for the allowlist/blocklist for the exporter

private ExporterTest() {
}

@GameTest(template = "empty_15x15")
public static void shouldExportBlock(final GameTestHelper helper) {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
preparePlot(helper, Direction.EAST, (exporter, pos, sequence) -> {
// Arrange
helper.setBlock(pos.east(), Blocks.CHEST);
starforcraft marked this conversation as resolved.
Show resolved Hide resolved

sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Act
exporter.setFilters(List.of(asResource(DIRT)));

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

@GameTest(template = "empty_15x15")
public static void shouldExportBlockFuzzy(final GameTestHelper helper) {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
preparePlot(helper, Direction.EAST, (exporter, pos, sequence) -> {
// Arrange
helper.setBlock(pos.east(), Blocks.CHEST);

ItemStack damagedDiamondChestplate = DIAMOND_CHESTPLATE.getDefaultInstance();
damagedDiamondChestplate.setDamageValue(500);
starforcraft marked this conversation as resolved.
Show resolved Hide resolved

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);
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
}));

// 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) {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
preparePlot(helper, Direction.EAST, (exporter, pos, sequence) -> {
// Arrange
helper.setBlock(pos.east(), Blocks.CAULDRON);
starforcraft marked this conversation as resolved.
Show resolved Hide resolved

sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
insert(helper, network, WATER, Platform.INSTANCE.getBucketAmount() * 2);
}));

// Act
exporter.setFilters(List.of(asResource(WATER)));

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockProperty(pos.east(), LayeredCauldronBlock.LEVEL, 3))
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
.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,36 @@
package com.refinedmods.refinedstorage2.platform.common.exporter;

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 ExporterTestPlots {
private ExporterTestPlots() {
}

static void preparePlot(final GameTestHelper helper,
final Direction direction,
final TriConsumer<ExporterBlockEntity, 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 exporterPos = ZERO.above().above().above();
helper.setBlock(exporterPos, RSBLOCKS.getExporter().getDefault().rotated(direction));
consumer.accept(
requireBlockEntity(helper, exporterPos, ExporterBlockEntity.class),
exporterPos,
helper.startSequence()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
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;

Check failure on line 7 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.core.Direction' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.gametest.framework.GameTest;

Check failure on line 8 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.gametest.framework.GameTest' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.gametest.framework.GameTestHelper;

Check failure on line 9 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.gametest.framework.GameTestHelper' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.world.item.ItemStack;

Check failure on line 10 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.world.item.ItemStack' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.world.item.Items;

Check failure on line 11 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.world.item.Items' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.world.level.block.Blocks;

Check failure on line 12 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.world.level.block.Blocks' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
import net.minecraft.world.level.block.LayeredCauldronBlock;

Check failure on line 13 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTest.java

View workflow job for this annotation

GitHub Actions / build / build

Import statement for 'net.minecraft.world.level.block.LayeredCauldronBlock' is in the wrong order. Should be in the 'THIRD_PARTY_PACKAGE' group, expecting group 'STANDARD_JAVA_PACKAGE' on this line.
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.*;
import static net.minecraft.world.item.Items.*;
import static net.minecraft.world.level.material.Fluids.WATER;

@GameTestHolder(IdentifierUtil.MOD_ID)
@PrefixGameTestTemplate(false)
public final class ImporterTest {
private ImporterTest() {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
}

@GameTest(template = "empty_15x15")
public static void shouldImportBlock(final GameTestHelper helper) {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> {
// Arrange
prepareChest(helper, pos.east(), DIRT.getDefaultInstance(), COBBLESTONE.getDefaultInstance().copyWithCount(3));

sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

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

@GameTest(template = "empty_15x15")
public static void shouldImportBlockFuzzyBlocklist(final GameTestHelper helper) {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
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
raoulvdberge marked this conversation as resolved.
Show resolved Hide resolved
.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) {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
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) {
starforcraft marked this conversation as resolved.
Show resolved Hide resolved
preparePlot(helper, Direction.EAST, (importer, pos, sequence) -> {
// Arrange
helper.setBlock(pos.east(), Blocks.WATER_CAULDRON.defaultBlockState().setValue(LayeredCauldronBlock.LEVEL, 3));

sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
insert(helper, network, STONE, 15);
}));

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.CAULDRON, pos.east()))
raoulvdberge marked this conversation as resolved.
Show resolved Hide resolved
.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,50 @@
package com.refinedmods.refinedstorage2.platform.common.importer;

import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.ItemStorageType;
import net.minecraft.core.BlockPos;

Check failure on line 5 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTestPlots.java

View workflow job for this annotation

GitHub Actions / build / build

'net.minecraft.core.BlockPos' should be separated from previous import group by one line.
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.refinedstorage2.platform.forge.GameTestUtil.RSBLOCKS;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.requireBlockEntity;
import static net.minecraft.core.BlockPos.ZERO;

final class ImporterTestPlots {
private ImporterTestPlots() {
}

static void preparePlot(final GameTestHelper helper,
final Direction direction,
final TriConsumer<ImporterBlockEntity, 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 importerPos = ZERO.above().above().above();
helper.setBlock(importerPos, RSBLOCKS.getImporter().getDefault().rotated(direction));
consumer.accept(
requireBlockEntity(helper, importerPos, ImporterBlockEntity.class),
importerPos,
helper.startSequence()
);
}

static void prepareChest(final GameTestHelper helper,
final BlockPos pos,
final ItemStack... stacks) {
helper.setBlock(pos, Blocks.CHEST.defaultBlockState());
if (helper.getBlockEntity(pos) instanceof BaseContainerBlockEntity chest) {
for(int i = 0; i < stacks.length; i++) {

Check failure on line 45 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTestPlots.java

View workflow job for this annotation

GitHub Actions / build / build

'for' is not followed by whitespace.

Check failure on line 45 in refinedstorage2-platform-forge/src/test/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterTestPlots.java

View workflow job for this annotation

GitHub Actions / build / build

'for' is not followed by whitespace.
chest.setItem(i, stacks[i]);
}
}
}
}
Loading
Loading