77import com .llamalad7 .mixinextras .sugar .ref .LocalRef ;
88import io .github .opencubicchunks .cc_core .api .CubePos ;
99import io .github .opencubicchunks .cc_core .api .CubicConstants ;
10+ import io .github .opencubicchunks .cc_core .utils .Coords ;
1011import io .github .opencubicchunks .cc_core .world .SpawnPlaceFinder ;
12+ import io .github .opencubicchunks .cc_core .world .level .CloPos ;
1113import io .github .opencubicchunks .cubicchunks .CanBeCubic ;
14+ import io .github .opencubicchunks .cubicchunks .server .level .ServerCubeCache ;
15+ import net .minecraft .Util ;
1216import net .minecraft .core .BlockPos ;
1317import net .minecraft .server .MinecraftServer ;
1418import net .minecraft .server .level .ServerChunkCache ;
2125import org .spongepowered .asm .mixin .Mixin ;
2226import org .spongepowered .asm .mixin .Shadow ;
2327import org .spongepowered .asm .mixin .injection .At ;
28+ import org .spongepowered .asm .mixin .injection .Constant ;
2429import org .spongepowered .asm .mixin .injection .Inject ;
30+ import org .spongepowered .asm .mixin .injection .ModifyConstant ;
2531import org .spongepowered .asm .mixin .injection .callback .CallbackInfo ;
2632
2733@ Mixin (MinecraftServer .class )
@@ -34,6 +40,10 @@ public abstract class MixinMinecraftServer {
3440
3541 @ Shadow protected abstract void waitUntilNextTick ();
3642
43+
44+ // TODO P2 :: This value is dynamic in 1.21, we will need to revisit this
45+ private static final int VANILLA_DEFAULT_SPAWN_CHUNK_RADIUS = 11 ;
46+
3747 // setInitialSpawn
3848 // We replace the ChunkPos spawn position with a CubePos spawn position and reuse it later to get the world position.
3949 @ Inject (method = "setInitialSpawn" , at = @ At (value = "INVOKE" , target = "Lnet/minecraft/world/level/ChunkPos;<init>(Lnet/minecraft/core/BlockPos;)V" ))
@@ -81,26 +91,31 @@ private static int cc_replaceGetHeightWithSpawnPlaceFinder(ServerLevel serverLev
8191 private <T > boolean cc_replaceAddRegionTicketInPrepareLevels (ServerChunkCache serverChunkCache , TicketType <T > ticketType , ChunkPos chunkPos , int originalSpawnRadius , T unit ,
8292 @ Share ("spawnRadius" ) LocalRef <Integer > spawnRadiusRef ) {
8393 if (((CanBeCubic ) serverChunkCache ).cc_isCubic ()) {
84- int spawnRadius = ( int ) Math . ceil ( 10 * ( 16 / ( float ) CubicConstants . DIAMETER_IN_BLOCKS )); //vanilla is 10, 32: 5, 64: 3
94+ int spawnRadius = Coords . sectionToCube ( VANILLA_DEFAULT_SPAWN_CHUNK_RADIUS );
8595 spawnRadiusRef .set (spawnRadius );
86- // TODO: Fix this when ServerChunkCache exists
87- //(ServerCubeCache)serverChunkCache.addRegionTicket(CubicTicketType.START, CloPos.cube(overworld().getSharedSpawnPos()), spawnRadius + 1, unit);
96+ ((ServerCubeCache )serverChunkCache ).cc_addRegionTicket (ticketType , CloPos .cube (overworld ().getSharedSpawnPos ()), spawnRadius , unit );
8897 return false ;
8998 }
99+
90100 return true ;
91101 }
92102
93- @ Inject (method = "prepareLevels" , at = @ At (value = "FIELD" , target = "Lnet/minecraft/server/MinecraftServer;nextTickTimeNanos:J" ))
94- private void cc_waitUntilCubicGenerationComplete (CallbackInfo ci , @ Share ("spawnRadius" ) LocalRef <Integer > spawnRadiusRef ) {
95- if (((CanBeCubic ) overworld ().getChunkSource ()).cc_isCubic ()) {
96- int d = spawnRadiusRef .get () * 2 + 1 ;
97- // TODO: Fix this when ServerChunkCache exists
98- //while (this.isRunning() && ((ServerCubeCache) overworld().getChunkSource()).getTickingGeneratedCubes() < d * d * d) {
99- // this.nextTickTimeNanos = Util.getMillis() + 10L;
100- // this.waitUntilNextTick();
101- //}
103+ @ ModifyConstant (method = "prepareLevels" , constant = @ Constant (intValue = 441 ), require = 1 )
104+ private int cc_modifyExpectedNumberOfTickingGenerated (int constant , @ Share ("spawnRadius" ) LocalRef <Integer > spawnRadiusRef ) {
105+ if (((CanBeCubic ) overworld ()).cc_isCubic ()) {
106+ // We need to calculate the number of cubes + chunks in the expected radius
107+ int spawnRadius = spawnRadiusRef .get ();
108+ int spawnDiameterCubes = spawnRadius * 2 + 1 ;
109+ int cubesInRadius = spawnDiameterCubes * spawnDiameterCubes * spawnDiameterCubes ;
110+
111+ int spawnDiameterChunks = Coords .cubeToSection (spawnDiameterCubes , 0 );
112+ int chunksInRadius = spawnDiameterChunks * spawnDiameterChunks ;
113+
114+ return cubesInRadius + chunksInRadius ;
102115 }
116+
117+ return constant ;
103118 }
104119
105- // TODO: forced cubes will need to be implemented for prepareLevels as well in the same way as above
120+ // TODO P2 :: Forced cubes will need to be implemented here as well; but this includes saving logic so P2
106121}
0 commit comments