Skip to content

Commit

Permalink
feat: external storage gametest
Browse files Browse the repository at this point in the history
  • Loading branch information
starforcraft committed Aug 3, 2024
1 parent e05b665 commit 991d8b7
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -145,6 +148,19 @@ public void readConfiguration(final CompoundTag tag, final HolderLookup.Provider
configContainer.load(tag);
}

void setFilters(final Set<ResourceKey> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
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;

import net.minecraft.core.BlockPos;
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;
Expand Down Expand Up @@ -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]);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
});
}
}
Loading

0 comments on commit 991d8b7

Please sign in to comment.