Skip to content

Commit

Permalink
refactor: have stricter method visibility again by placing test in th…
Browse files Browse the repository at this point in the history
…e same package
  • Loading branch information
raoulvdberge committed May 26, 2024
1 parent 6e43ee1 commit f144bcf
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ConstructorBlockEntity(final BlockPos pos, final BlockState state) {
}

@Override
public void setFilters(final List<ResourceKey> filters) {
protected void setFilters(final List<ResourceKey> filters) {
this.tasks.clear();
this.tasks.addAll(filters.stream().map(TaskImpl::new).toList());
}
Expand Down Expand Up @@ -117,7 +117,7 @@ boolean isDropItems() {
return dropItems;
}

public void setDropItems(final boolean dropItems) {
void setDropItems(final boolean dropItems) {
this.dropItems = dropItems;
setChanged();
if (level instanceof ServerLevel serverLevel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected void setTaskExecutor(final TaskExecutor<ExporterNetworkNode.TaskContex
}

@Override
public void setFilters(final List<ResourceKey> filters) {
protected void setFilters(final List<ResourceKey> filters) {
mainNode.setFilters(filters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByte

protected abstract void setTaskExecutor(TaskExecutor<C> taskExecutor);

public abstract void setFilters(List<ResourceKey> filters);
protected abstract void setFilters(List<ResourceKey> filters);
}
Original file line number Diff line number Diff line change
@@ -1,92 +1,65 @@
package com.refinedmods.refinedstorage2.platform.forge;
package com.refinedmods.refinedstorage2.platform.common.constructordestructor;

import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.constructordestructor.ConstructorBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.ItemStorageType;
import com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil;

import java.util.List;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.gametest.framework.GameTestSequence;
import net.minecraft.world.item.Items;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.gametest.GameTestHolder;
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;
import org.apache.commons.lang3.function.TriConsumer;

import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.DIRT;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.RSBLOCKS;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.STONE;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.WATER;
import static com.refinedmods.refinedstorage2.platform.common.constructordestructor.ConstructorTestPlots.preparePlot;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.asResource;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.assertFluidPresent;
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.requireBlockEntity;
import static com.refinedmods.refinedstorage2.platform.forge.GameTestUtil.storageContainsExactly;
import static net.minecraft.core.BlockPos.ZERO;
import static net.minecraft.world.item.Items.DIRT;
import static net.minecraft.world.item.Items.FIREWORK_ROCKET;
import static net.minecraft.world.item.Items.STONE;
import static net.minecraft.world.level.material.Fluids.WATER;

@GameTestHolder(IdentifierUtil.MOD_ID)
@PrefixGameTestTemplate(false)
public final class ConstructorTest {

private ConstructorTest() {
}

private static void prepareConstructorPlot(final GameTestHelper helper,
final Direction direction,
final TriConsumer<ConstructorBlockEntity, 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 constructorPos = ZERO.above().above().above();
helper.setBlock(constructorPos, RSBLOCKS.getConstructor().getDefault().rotated(direction));
consumer.accept(
requireBlockEntity(helper, constructorPos, ConstructorBlockEntity.class),
constructorPos,
helper.startSequence()
);
}

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

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

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

@GameTest(template = "empty_15x15")
public static void shouldPlaceWater(final GameTestHelper helper) {
prepareConstructorPlot(helper, Direction.EAST, (constructor, pos, sequence) -> {
preparePlot(helper, Direction.EAST, (constructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
Expand All @@ -95,25 +68,25 @@ public static void shouldPlaceWater(final GameTestHelper helper) {
}));

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

// Assert
sequence
.thenWaitUntil(() -> assertFluidPresent(helper, pos.east(), Fluids.WATER, FluidState.AMOUNT_FULL))
.thenWaitUntil(() -> assertFluidPresent(helper, pos.east(), WATER, FluidState.AMOUNT_FULL))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(DIRT, 10),
new ResourceAmount(STONE, 15),
new ResourceAmount(WATER, Platform.INSTANCE.getBucketAmount())
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 shouldDropItem(final GameTestHelper helper) {
prepareConstructorPlot(helper, Direction.EAST, (constructor, pos, sequence) -> {
preparePlot(helper, Direction.EAST, (constructor, pos, sequence) -> {
// Arrange
sequence.thenWaitUntil(networkIsAvailable(helper, pos, network -> {
insert(helper, network, DIRT, 10);
Expand All @@ -122,17 +95,43 @@ public static void shouldDropItem(final GameTestHelper helper) {

// Act
constructor.setDropItems(true);
constructor.setFilters(List.of(DIRT));
constructor.setFilters(List.of(asResource(DIRT)));

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

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

// Act
constructor.setFilters(List.of(asResource(FIREWORK_ROCKET)));

// Assert
sequence
.thenWaitUntil(() -> helper.assertBlockPresent(Blocks.AIR, pos.east()))
.thenWaitUntil(() -> helper.assertEntityPresent(EntityType.FIREWORK_ROCKET, pos.east()))
.thenWaitUntil(storageContainsExactly(
helper,
pos,
new ResourceAmount(DIRT, 9),
new ResourceAmount(STONE, 15)
new ResourceAmount(asResource(DIRT), 10),
new ResourceAmount(asResource(FIREWORK_ROCKET), 14)
))
.thenSucceed();
});
Expand Down
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 ConstructorTestPlots {
private ConstructorTestPlots() {
}

static void preparePlot(final GameTestHelper helper,
final Direction direction,
final TriConsumer<ConstructorBlockEntity, 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 constructorPos = ZERO.above().above().above();
helper.setBlock(constructorPos, RSBLOCKS.getConstructor().getDefault().rotated(direction));
consumer.accept(
requireBlockEntity(helper, constructorPos, ConstructorBlockEntity.class),
constructorPos,
helper.startSequence()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@
import net.minecraft.core.BlockPos;
import net.minecraft.gametest.framework.GameTestAssertException;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;

public final class GameTestUtil {
public static final ItemResource DIRT = ItemResource.ofItemStack(new ItemStack(Items.DIRT));
public static final ItemResource STONE = ItemResource.ofItemStack(new ItemStack(Items.STONE));
public static final FluidResource WATER = new FluidResource(Fluids.WATER, null);
public static final Blocks RSBLOCKS = Blocks.INSTANCE;

private GameTestUtil() {
Expand Down Expand Up @@ -58,6 +53,20 @@ public static Runnable networkIsAvailable(final GameTestHelper helper,
};
}

public static void insert(final GameTestHelper helper,
final Network network,
final Item resource,
final long amount) {
insert(helper, network, new ItemResource(resource, null), amount);
}

public static void insert(final GameTestHelper helper,
final Network network,
final Fluid resource,
final long amount) {
insert(helper, network, new FluidResource(resource, null), amount);
}

public static void insert(final GameTestHelper helper,
final Network network,
final ResourceKey resource,
Expand Down Expand Up @@ -117,4 +126,12 @@ public static Runnable storageContainsExactly(final GameTestHelper helper,
}
});
}

public static ItemResource asResource(final Item item) {
return new ItemResource(item, null);
}

public static FluidResource asResource(final Fluid fluid) {
return new FluidResource(fluid, null);
}
}

0 comments on commit f144bcf

Please sign in to comment.