1414import net .minecraft .world .level .block .entity .BlockEntity ;
1515import net .neoforged .neoforge .items .IItemHandler ;
1616import net .neoforged .testframework .gametest .ExtendedGameTestHelper ;
17- import org .apache .commons .lang3 .mutable .MutableInt ;
1817import org .jetbrains .annotations .Nullable ;
1918
2019public class GameTestUtils {
@@ -24,22 +23,26 @@ public class GameTestUtils {
2423
2524 static final boolean DEBUG_CHUNK_LOADING = false ;
2625
27- public static void succeedIfAfterUnload (ExtendedGameTestHelper helper , ChunkPos relativePos , Runnable criteria ) {
28- helper .startSequence ()
29- .thenWaitUntil (() -> unloadChunk (helper , relativePos ))
30- //Wait 5 ticks in case anything needs more time to process after the chunk unloads
31- .thenIdle (5 )
32- .thenWaitUntil (0 , criteria )
33- .thenSucceed ();
34- }
35-
3626 public static void succeedIfAfterReload (ExtendedGameTestHelper helper , ChunkPos relativePos , Runnable afterReload ) {
37- MutableInt lastLevel = new MutableInt (UNLOAD_LEVEL );
3827 helper .startSequence ()
39- .thenWaitUntil (() -> unloadChunk (helper , relativePos , lastLevel ))
28+ .thenMap (() -> unloadChunk (helper , relativePos ))
29+ .thenWaitUntil (() -> {
30+ ChunkPos absolutePos = absolutePos (helper , relativePos );
31+ long absPos = absolutePos .toLong ();
32+ if (GameTestEventListeners .watchedChunks .containsKey (absPos )) {
33+ //Watched chunk
34+ if (GameTestEventListeners .watchedChunks .get (absPos )) {
35+ //Is loaded, but we are watching, throw exception so that we keep waiting for it to unload
36+ fail (helper , "Chunk has not been marked as unloaded yet" , absolutePos , relativePos );
37+ } else {
38+ //Remove the watch on the chunk now that it has been unloaded
39+ GameTestEventListeners .watchedChunks .remove (absPos );
40+ }
41+ }
42+ })
4043 //Wait 5 ticks in case anything needs more time to process after the chunk unloads
4144 .thenIdle (5 )
42- .thenWaitUntil (() -> loadChunk (helper , relativePos , lastLevel ))
45+ .thenWaitUntil (level -> loadChunk (helper , relativePos , level ))
4346 //Wait 5 ticks in case anything needs more time to process after the chunk loads
4447 .thenIdle (5 )
4548 .thenWaitUntil (0 , afterReload )
@@ -48,23 +51,11 @@ public static void succeedIfAfterReload(ExtendedGameTestHelper helper, ChunkPos
4851
4952 //TODO - GameTest: Can we make unloads not cause the game to crash if a player tries to run a test that uses them instead of using game test server?
5053 // Most likely the answer is no
51- private static void unloadChunk (ExtendedGameTestHelper helper , ChunkPos relativePos ) {
52- unloadChunk (helper , relativePos , new MutableInt ());
53- }
54-
55- private static void unloadChunk (ExtendedGameTestHelper helper , ChunkPos relativePos , MutableInt levelMemory ) {
54+ private static int unloadChunk (ExtendedGameTestHelper helper , ChunkPos relativePos ) {
55+ int level = UNLOAD_LEVEL ;
5656 ChunkPos absolutePos = absolutePos (helper , relativePos );
5757 long absPos = absolutePos .toLong ();
58- if (GameTestEventListeners .watchedChunks .containsKey (absPos )) {
59- //Watched chunk
60- if (GameTestEventListeners .watchedChunks .get (absPos )) {
61- //Is loaded, but we are watching, throw exception so that we keep waiting for it to unload
62- fail (helper , "Chunk has not been marked as unloaded yet" , absolutePos , relativePos );
63- } else {
64- //Remove the watch on the chunk now that it has been unloaded
65- GameTestEventListeners .watchedChunks .remove (absPos );
66- }
67- } else if (WorldUtils .isChunkLoaded (helper .getLevel (), absolutePos )) {
58+ if (WorldUtils .isChunkLoaded (helper .getLevel (), absolutePos )) {
6859 //If the chunk isn't watched and is loaded we want to try and unload it
6960 ChunkMap chunkMap = helper .getLevel ().getChunkSource ().chunkMap ;
7061 DistanceManager distanceManager = chunkMap .getDistanceManager ();
@@ -77,25 +68,25 @@ private static void unloadChunk(ExtendedGameTestHelper helper, ChunkPos relative
7768 Mekanism .logger .info ("Trying to unload chunk at: {}, {}" , absolutePos .x , absolutePos .z );
7869 }
7970 //If it is currently loaded, queue it for unload
80- levelMemory . setValue ( holder .getTicketLevel () );
81- distanceManager .updateChunkScheduling (absPos , UNLOAD_LEVEL , holder , holder . getTicketLevel () );
71+ level = holder .getTicketLevel ();
72+ distanceManager .updateChunkScheduling (absPos , UNLOAD_LEVEL , holder , level );
8273 //And then unload it
8374 chunkMap .processUnloads (ConstantPredicates .ALWAYS_TRUE );
84- fail (helper , "Chunk queued for unloading" , absolutePos , relativePos );
8575 } else if (DEBUG_CHUNK_LOADING ) {
8676 //Note: Even with debug logging enabled odds are this case isn't even possible due to the earlier check to skip if unloaded
8777 Mekanism .logger .info ("Trying to unload already unloaded chunk at: {}, {}" , absolutePos .x , absolutePos .z );
8878 }
8979 } else if (DEBUG_CHUNK_LOADING ) {
9080 Mekanism .logger .info ("Chunk at: {}, {} is already unloaded" , absolutePos .x , absolutePos .z );
9181 }
82+ return level ;
9283 }
9384
9485 private static void loadChunk (ExtendedGameTestHelper helper , ChunkPos relativePos ) {
95- loadChunk (helper , relativePos , new MutableInt ( ChunkMap .FORCED_TICKET_LEVEL ) );
86+ loadChunk (helper , relativePos , ChunkMap .FORCED_TICKET_LEVEL );
9687 }
9788
98- private static void loadChunk (ExtendedGameTestHelper helper , ChunkPos relativePos , MutableInt levelMemory ) {
89+ private static void loadChunk (ExtendedGameTestHelper helper , ChunkPos relativePos , int level ) {
9990 ChunkPos absolutePos = absolutePos (helper , relativePos );
10091 long absPos = absolutePos .toLong ();
10192 if (GameTestEventListeners .watchedChunks .containsKey (absPos )) {
@@ -119,7 +110,7 @@ private static void loadChunk(ExtendedGameTestHelper helper, ChunkPos relativePo
119110 Mekanism .logger .info ("Trying to load chunk at: {}, {}" , absolutePos .x , absolutePos .z );
120111 }
121112 //Load the chunk to the level it was unloaded at
122- holder = distanceManager .updateChunkScheduling (absPos , levelMemory . getValue () , null , UNLOAD_LEVEL );
113+ holder = distanceManager .updateChunkScheduling (absPos , level , null , UNLOAD_LEVEL );
123114 if (holder == null ) {//Should never happen unless start value was unloaded
124115 fail (helper , "Error loading chunk" , absolutePos , relativePos );
125116 } else {
0 commit comments