Skip to content

Commit dd222eb

Browse files
committed
Continue adding more transporter related game tests, and fix accidentally breaking stuff in previous commits
1 parent 33e72ba commit dd222eb

File tree

3 files changed

+98
-18
lines changed

3 files changed

+98
-18
lines changed

src/gameTest/java/mekanism/common/tests/network/InventoryNetworkTest.java

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mekanism.common.tests.network;
22

3+
import static mekanism.common.tests.network.TransmitterTestUtils.useConfigurator;
34
import static mekanism.common.tests.util.TransporterTestUtils.colored;
45
import static mekanism.common.tests.util.TransporterTestUtils.configured;
56
import static mekanism.common.tests.util.TransporterTestUtils.containing;
@@ -16,7 +17,6 @@
1617
import net.minecraft.core.BlockPos;
1718
import net.minecraft.core.Direction;
1819
import net.minecraft.gametest.framework.GameTest;
19-
import net.minecraft.world.entity.player.Player;
2020
import net.minecraft.world.item.ItemStack;
2121
import net.minecraft.world.item.Items;
2222
import net.minecraft.world.level.block.Blocks;
@@ -52,6 +52,18 @@ public class InventoryNetworkTest {
5252
.fill(0, 0, 2, 9, 0, 2, MekanismBlocks.BASIC_LOGISTICAL_TRANSPORTER.defaultState())
5353
.fill(9, 0, 0, 9, 0, 1, MekanismBlocks.BASIC_LOGISTICAL_TRANSPORTER.defaultState())
5454
);
55+
private static final String SIMPLE_PATH = MekanismTests.MODID + ":simple_path";
56+
//Note: Our template is lazy so that we ensure the transporters are registered
57+
@RegisterStructureTemplate(SIMPLE_PATH)
58+
public static final Supplier<StructureTemplate> SIMPLE_PATH_TEMPLATE = StructureTemplateBuilder.lazy(1, 1, 6, builder -> builder
59+
//Start barrel
60+
.set(0, 0, 0, Blocks.BARREL.defaultBlockState(), containing(Items.STONE))
61+
//End barrel
62+
.set(0, 0, 5, Blocks.BARREL.defaultBlockState())
63+
64+
.set(0, 0, 1, MekanismBlocks.BASIC_LOGISTICAL_TRANSPORTER.defaultState(), configured(Direction.NORTH))
65+
.fill(0, 0, 2, 0, 0, 4, MekanismBlocks.BASIC_LOGISTICAL_TRANSPORTER.defaultState())
66+
);
5567

5668
//TODO: Do we want to somehow test the case of when we add a shorter path to the same destination, as the newly pulled items go via the new shorter path
5769
// but any already en-route ones continue the original way
@@ -61,39 +73,77 @@ public class InventoryNetworkTest {
6173
// - Cutting off an existing path -> recalculating pathfinding of currently travelling stacks, for example when setting a side to none or breaking a transporter
6274
// - Color changing on an existing path??
6375
// - Diversion transporters?? Might basically be just the same as the cutting off existing path
64-
// - destination becomes inaccessible due to side changes
65-
// - base path becomes disabled but there is a different path to the same destination
66-
// - Remove destination and source and let it idle for a bit in the transporter then add a new spot and validate it all enters it??
6776
// - Make destination no longer able to accept item, validate input doesn't contain stack, and then validate it made its way back to the input
6877
// - Make destination no longer able to accept item, fill input fully, wait a little, and then allow for room in the destination, validate it makes it there
6978
// - Single transporter with no destination stays in the transporter, it fails and pops out if there is only a single connection point
7079
// Note: We may want to change that behavior so even with a single connection point it just stays idling and only pops out if there are zero connection points
7180

81+
@GameTest(template = SIMPLE_PATH, setupTicks = SETUP_TICKS, timeoutTicks = TIMEOUT_TICKS)
82+
@TestHolder(description = "Tests that items will properly be sent back and inserted into their home if the destination is removed while the stacks are en-route.")
83+
public static void sendsBackToHome(final ExtendedGameTestHelper helper) {
84+
helper.startSequence()
85+
//Wait a second for it to pull the item out, and remove the destination
86+
.thenExecuteAfter(SharedConstants.TICKS_PER_SECOND, () -> helper.setBlock(0, 1, 5, Blocks.AIR))
87+
//Make sure the start container is empty
88+
.thenExecute(() -> helper.assertContainerEmpty(0, 1, 0))
89+
//And then after a few seconds that the item has transferred back into the destination it was pulled from
90+
.thenExecuteAfter(6 * SharedConstants.TICKS_PER_SECOND, () -> helper.assertContainerContains(0, 1, 0, Items.STONE))
91+
.thenSucceed();
92+
}
93+
94+
@GameTest(template = SIMPLE_PATH, setupTicks = SETUP_TICKS, timeoutTicks = TIMEOUT_TICKS)
95+
@TestHolder(description = "Tests that items will properly be sent back and inserted into their home if the destination becomes inaccessible "
96+
+ "due to side changes while the stacks are en-route.")
97+
public static void sendsBackToHomeDisabled(final ExtendedGameTestHelper helper) {
98+
helper.startSequence()
99+
//Wait a second for it to pull the item out, and disable the path to the destination
100+
.thenExecuteAfter(SharedConstants.TICKS_PER_SECOND, () -> useConfigurator(helper, 0, 1, 4, Direction.SOUTH, 3))
101+
//Make sure the start container is empty
102+
.thenExecute(() -> helper.assertContainerEmpty(0, 1, 0))
103+
//And then after a few seconds that the item has transferred back into the destination it was pulled from
104+
.thenExecuteAfter(6 * SharedConstants.TICKS_PER_SECOND, () -> helper.assertContainerContains(0, 1, 0, Items.STONE))
105+
.thenSucceed();
106+
}
107+
72108
@GameTest(setupTicks = SETUP_TICKS, timeoutTicks = TIMEOUT_TICKS)
73-
@TestHolder(description = "Tests that newly pulled items will go to the new destination that has a shorter path, "
74-
+ "but any items that were already en-route will continue to the destination they had already calculated.")
75-
public static void sendsBackToHome(final DynamicTest test) {
76-
test.registerGameTestTemplate(() -> StructureTemplateBuilder.withSize(1, 1, 6)
109+
@TestHolder(description = "Tests that items will properly get to their destination, even if one of the paths to them is disabled, but another exists.")
110+
public static void pathDisabledButStillHasPath(final DynamicTest test) {
111+
test.registerGameTestTemplate(() -> StructureTemplateBuilder.withSize(3, 1, 6)
77112
//Start barrel
78113
.set(0, 0, 0, Blocks.BARREL.defaultBlockState(), containing(Items.STONE))
79-
//End barrel
114+
//End barrels
80115
.set(0, 0, 5, Blocks.BARREL.defaultBlockState())
81116

82117
.set(0, 0, 1, MekanismBlocks.BASIC_LOGISTICAL_TRANSPORTER.defaultState(), configured(Direction.NORTH))
83118
.fill(0, 0, 2, 0, 0, 4, MekanismBlocks.BASIC_LOGISTICAL_TRANSPORTER.defaultState())
119+
.fill(1, 0, 4, 1, 0, 5, MekanismBlocks.BASIC_LOGISTICAL_TRANSPORTER.defaultState())
84120
);
85121

86122
test.onGameTest(helper -> helper.startSequence()
87-
//Wait a second for it to pull the item out, and remove the destination
88-
.thenExecuteAfter(SharedConstants.TICKS_PER_SECOND, () -> helper.setBlock(0, 1, 5, Blocks.AIR))
123+
//Wait a second for it to pull the item out, and disable the base path to the destination
124+
.thenExecuteAfter(SharedConstants.TICKS_PER_SECOND, () -> useConfigurator(helper, 0, 1, 4, Direction.SOUTH, 3))
89125
//Make sure the start container is empty
90126
.thenExecute(() -> helper.assertContainerEmpty(0, 1, 0))
91127
//And then after a few seconds that the item has transferred back into the destination it was pulled from
92-
.thenExecuteAfter(6 * SharedConstants.TICKS_PER_SECOND, () -> helper.assertContainerContains(0, 1, 0, Items.STONE))
128+
.thenExecuteAfter(6 * SharedConstants.TICKS_PER_SECOND, () -> helper.assertContainerContains(0, 1, 5, Items.STONE))
93129
.thenSucceed()
94130
);
95131
}
96132

133+
@GameTest(template = SIMPLE_PATH, setupTicks = SETUP_TICKS, timeoutTicks = TIMEOUT_TICKS)
134+
@TestHolder(description = "Tests that idling items will properly find a destination if both the destination and source are removed "
135+
+ "and then later a new destination is added.")
136+
public static void findPathFromIdle(final ExtendedGameTestHelper helper) {
137+
helper.startSequence()
138+
//Wait a second for it to pull the item out, and remove the destination
139+
.thenExecuteAfter(SharedConstants.TICKS_PER_SECOND, () -> helper.setBlock(0, 1, 5, Blocks.AIR))
140+
.thenExecute(() -> helper.setBlock(0, 1, 0, Blocks.AIR))
141+
.thenExecuteAfter(10 * SharedConstants.TICKS_PER_SECOND, () -> helper.setBlock(0, 1, 5, Blocks.BARREL))
142+
//And then after a few seconds that the item has transferred back into the destination it was pulled from
143+
.thenExecuteAfter(5 * SharedConstants.TICKS_PER_SECOND, () -> helper.assertContainerContains(0, 1, 5, Items.STONE))
144+
.thenSucceed();
145+
}
146+
97147
@GameTest(setupTicks = SETUP_TICKS, timeoutTicks = TIMEOUT_TICKS)
98148
@TestHolder(description = "Tests that newly pulled items will go to the new destination that has a shorter path, "
99149
+ "but any items that were already en-route will continue to the destination they had already calculated.")
@@ -143,11 +193,8 @@ public static void shorterEnabledPath(final DynamicTest test) {
143193

144194
test.onGameTest(helper -> helper.startSequence()
145195
//Wait a few seconds for it to pull some items out, re-enable a disabled path to make there be a shorter destination
146-
.thenExecuteAfter(4 * SharedConstants.TICKS_PER_SECOND, () -> {
147-
Player player = helper.makeMockPlayer();
148-
player.setShiftKeyDown(true);
149-
helper.useOn(new BlockPos(1, 1, 2), MekanismItems.CONFIGURATOR.getItemStack(), player, Direction.WEST);
150-
}).thenExecuteAfter(10 * SharedConstants.TICKS_PER_SECOND, () -> {
196+
.thenExecuteAfter(4 * SharedConstants.TICKS_PER_SECOND, () -> useConfigurator(helper, 1, 1, 2, Direction.WEST))
197+
.thenExecuteAfter(10 * SharedConstants.TICKS_PER_SECOND, () -> {
151198
//Validate original destination has expected count
152199
GameTestUtils.validateContainerHas(helper, new BlockPos(0, 1, 5), 0, new ItemStack(Items.STONE, 8));
153200
//Validate new destination has expected count
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mekanism.common.tests.network;
2+
3+
import mekanism.common.registries.MekanismItems;
4+
import net.minecraft.core.BlockPos;
5+
import net.minecraft.core.Direction;
6+
import net.minecraft.world.entity.player.Player;
7+
import net.neoforged.testframework.gametest.ExtendedGameTestHelper;
8+
9+
public class TransmitterTestUtils {
10+
11+
private TransmitterTestUtils() {
12+
}
13+
14+
public static void useConfigurator(ExtendedGameTestHelper helper, int x, int y, int z, Direction side) {
15+
useConfigurator(helper, x, y, z, side, 1);
16+
}
17+
18+
public static void useConfigurator(ExtendedGameTestHelper helper, int x, int y, int z, Direction side, int times) {
19+
Player player = helper.makeMockPlayer();
20+
player.setShiftKeyDown(true);
21+
BlockPos pos = new BlockPos(x, y, z);
22+
//Set the player's look and position as we need accurate information for configurator usage to be applied properly to transmitters
23+
Direction direction = side.getOpposite();
24+
player.setXRot(direction == Direction.DOWN ? 90 : direction == Direction.UP ? -90 : 0);
25+
float yRot = direction.toYRot();
26+
player.setYRot(yRot);
27+
player.setYHeadRot(yRot);
28+
player.setPos(helper.absolutePos(pos).getCenter().subtract(0.45 * direction.getStepX(), 0.45 * direction.getStepY() + player.getEyeHeight(), 0.45 * direction.getStepZ()));
29+
for (int i = 0; i < times; i++) {
30+
helper.useOn(pos, MekanismItems.CONFIGURATOR.getItemStack(), player, side);
31+
}
32+
}
33+
}

src/main/java/mekanism/common/content/transporter/TransporterManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void remove(Level world, TransporterStack stack) {
3535
if (stack.hasPath() && stack.getPathType().hasTarget()) {
3636
GlobalPos pos = GlobalPos.of(world.dimension(), stack.getDest());
3737
Set<TransporterStack> transporterStacks = flowingStacks.get(pos);
38-
if (transporterStacks.remove(stack) && transporterStacks.isEmpty()) {
38+
if (transporterStacks != null && transporterStacks.remove(stack) && transporterStacks.isEmpty()) {
3939
flowingStacks.remove(pos);
4040
}
4141
}

0 commit comments

Comments
 (0)