Skip to content

Commit 8d1fa70

Browse files
seelderrCursedFlames
authored andcommitted
MinecraftServer and ServerPlayer
1 parent 6d911db commit 8d1fa70

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

src/main/java/io/github/opencubicchunks/cubicchunks/mixin/core/common/server/MixinMinecraftServer.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
88
import io.github.opencubicchunks.cc_core.api.CubePos;
99
import io.github.opencubicchunks.cc_core.api.CubicConstants;
10+
import io.github.opencubicchunks.cc_core.utils.Coords;
1011
import io.github.opencubicchunks.cc_core.world.SpawnPlaceFinder;
12+
import io.github.opencubicchunks.cc_core.world.level.CloPos;
1113
import io.github.opencubicchunks.cubicchunks.CanBeCubic;
14+
import io.github.opencubicchunks.cubicchunks.server.level.ServerCubeCache;
15+
import net.minecraft.Util;
1216
import net.minecraft.core.BlockPos;
1317
import net.minecraft.server.MinecraftServer;
1418
import net.minecraft.server.level.ServerChunkCache;
@@ -21,7 +25,9 @@
2125
import org.spongepowered.asm.mixin.Mixin;
2226
import org.spongepowered.asm.mixin.Shadow;
2327
import org.spongepowered.asm.mixin.injection.At;
28+
import org.spongepowered.asm.mixin.injection.Constant;
2429
import org.spongepowered.asm.mixin.injection.Inject;
30+
import org.spongepowered.asm.mixin.injection.ModifyConstant;
2531
import 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
}

src/main/java/io/github/opencubicchunks/cubicchunks/mixin/core/common/server/level/MixinServerPlayer.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
44
import com.llamalad7.mixinextras.sugar.Local;
5+
import io.github.opencubicchunks.cc_core.world.level.CloPos;
56
import io.github.opencubicchunks.cubicchunks.MarkableAsCubic;
7+
import io.github.opencubicchunks.cubicchunks.server.level.ServerCubeCache;
8+
import net.minecraft.core.BlockPos;
69
import net.minecraft.server.level.ServerChunkCache;
710
import net.minecraft.server.level.ServerPlayer;
811
import net.minecraft.server.level.TicketType;
@@ -40,12 +43,12 @@ public <T> boolean cc_wrapAddRegionTicket(ServerChunkCache instance, TicketType<
4043
if (!cc_isCubic) {
4144
return true;
4245
}
43-
// TODO: Add once Cubic ServerChunkCache exists
44-
//instance.addRegionTicket(type, CloPos.cube(BlockPos.containing(x, y, z)), 1, this.getId());
46+
47+
((ServerCubeCache)instance).cc_addRegionTicket(type, CloPos.cube(BlockPos.containing(x, y, z)), distance, value);
4548
return false;
4649
}
4750

48-
// TODO: phase 3 - findDimensionEntryPoint
51+
// TODO P3 :: findDimensionEntryPoint
4952

50-
// TODO: phase 3 - changeDimension
53+
// TODO P3 :: changeDimension
5154
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@ParametersAreNonnullByDefault
2+
@MethodsReturnNonnullByDefault
3+
package io.github.opencubicchunks.cubicchunks.mixin.test.client.multiplayer;
4+
5+
import javax.annotation.ParametersAreNonnullByDefault;
6+
7+
import io.github.opencubicchunks.cc_core.annotation.MethodsReturnNonnullByDefault;

src/test/java/io/github/opencubicchunks/cubicchunks/test/server/TestMinecraftServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.mockito.MockedConstruction;
2424
import org.mockito.Mockito;
2525

26+
27+
// TODO :: These tests don't work, because mocking the minecraft server is extremely hard. If anyone can figure it out, please re-enable these tests.
2628
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2729
public class TestMinecraftServer extends BaseTest {
2830
private CloseableReference<IntegratedServer> setupServer() {
@@ -35,7 +37,6 @@ private CloseableReference<IntegratedServer> setupServer() {
3537
);
3638
}
3739

38-
//TODO: This test hangs for some reason
3940
@Test @Disabled public void testSetInitialSpawnVanilla() throws Exception {
4041
try (CloseableReference<ServerLevel> serverLevelReference = setupServerLevel()) {
4142
((MarkableAsCubic)serverLevelReference.value()).cc_setCubic();
@@ -45,8 +46,7 @@ private CloseableReference<IntegratedServer> setupServer() {
4546
}
4647
}
4748

48-
// TODO: Need to mock overworld() in some way
49-
@Test @Disabled public void testPrepareLevelsVanilla() throws Exception {
49+
@Test @Disabled void testPrepareLevelsVanilla() throws Exception {
5050
try (CloseableReference<IntegratedServer> server = setupServer()) {
5151
((MarkableAsCubic)server.value().overworld()).cc_setCubic();
5252
((MinecraftServerTestAccess)server.value()).invoke_prepareLevels(mock(RETURNS_DEEP_STUBS));

0 commit comments

Comments
 (0)