Skip to content

Commit c01634c

Browse files
CursedFlamesNotStirred
authored andcommitted
Remaining changes for P1
1 parent 33771b7 commit c01634c

File tree

9 files changed

+111
-32
lines changed

9 files changed

+111
-32
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package io.github.opencubicchunks.cubicchunks.mixin.core.common.server;
22

3+
import java.util.function.Predicate;
4+
import java.util.stream.Stream;
5+
36
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
47
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
58
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
69
import com.llamalad7.mixinextras.sugar.Share;
710
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
811
import io.github.opencubicchunks.cc_core.api.CubePos;
9-
import io.github.opencubicchunks.cc_core.api.CubicConstants;
1012
import io.github.opencubicchunks.cc_core.utils.Coords;
1113
import io.github.opencubicchunks.cc_core.world.SpawnPlaceFinder;
1214
import io.github.opencubicchunks.cc_core.world.level.CloPos;
1315
import io.github.opencubicchunks.cubicchunks.CanBeCubic;
1416
import io.github.opencubicchunks.cubicchunks.server.level.ServerCubeCache;
15-
import net.minecraft.Util;
1617
import net.minecraft.core.BlockPos;
1718
import net.minecraft.server.MinecraftServer;
1819
import net.minecraft.server.level.ServerChunkCache;
@@ -28,6 +29,7 @@
2829
import org.spongepowered.asm.mixin.injection.Constant;
2930
import org.spongepowered.asm.mixin.injection.Inject;
3031
import org.spongepowered.asm.mixin.injection.ModifyConstant;
32+
import org.spongepowered.asm.mixin.injection.Redirect;
3133
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
3234

3335
@Mixin(MinecraftServer.class)
@@ -86,6 +88,7 @@ private <T> boolean cc_replaceAddRegionTicketInPrepareLevels(ServerChunkCache se
8688
if (((CanBeCubic) serverChunkCache).cc_isCubic()) {
8789
int spawnRadius = Coords.sectionToCube(VANILLA_DEFAULT_SPAWN_CHUNK_RADIUS);
8890
spawnRadiusRef.set(spawnRadius);
91+
spawnRadius++; // TODO there's an off-by-one error somewhere that means that we need to add one here to generate the actual correct spawn radius
8992
((ServerCubeCache)serverChunkCache).cc_addRegionTicket(ticketType, CloPos.cube(overworld().getSharedSpawnPos()), spawnRadius, unit);
9093
return false;
9194
}
@@ -110,5 +113,12 @@ private int cc_modifyExpectedNumberOfTickingGenerated(int constant, @Share("spaw
110113
return constant;
111114
}
112115

116+
// Temporary hack to let us unload a world without saving
117+
// TODO (P2): saving
118+
@Redirect(method = "stopServer", at = @At(value = "INVOKE", target = "Ljava/util/stream/Stream;anyMatch(Ljava/util/function/Predicate;)Z"))
119+
private boolean cc_onStopServer_chunkMapHasWork(Stream instance, Predicate<?> predicate) {
120+
return false;
121+
}
122+
113123
// TODO P2 :: Forced cubes will need to be implemented here as well; but this includes saving logic so P2
114124
}

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.concurrent.CompletableFuture;
77
import java.util.concurrent.Executor;
88
import java.util.function.BiConsumer;
9+
import java.util.function.BooleanSupplier;
910
import java.util.function.IntFunction;
1011
import java.util.function.Supplier;
1112

@@ -32,6 +33,7 @@
3233
import io.github.opencubicchunks.cubicchunks.MarkableAsCubic;
3334
import io.github.opencubicchunks.cubicchunks.mixin.core.common.world.level.chunk.storage.MixinChunkStorage;
3435
import io.github.opencubicchunks.cubicchunks.mixin.dasmsets.ChunkToCloSet;
36+
import io.github.opencubicchunks.cubicchunks.mixin.dasmsets.GlobalSet;
3537
import io.github.opencubicchunks.cubicchunks.mixin.dasmsets.SectionPosToCubeSet;
3638
import io.github.opencubicchunks.cubicchunks.network.CCClientboundSetCubeCacheCenterPacket;
3739
import io.github.opencubicchunks.cubicchunks.server.level.CloCollectorFuture;
@@ -298,6 +300,14 @@ private void cc_onUpdateChunkScheduling(long chunkPos, int newLevel, ChunkHolder
298300
@AddTransformToSets(ChunkToCloSet.class) @TransformFromMethod(@MethodSig("saveAllChunks(Z)V"))
299301
public native void cc_saveAllChunks(boolean flush);
300302

303+
@Inject(method = "saveAllChunks", at = @At("HEAD"), cancellable = true)
304+
private void cc_onSaveAllChunks(boolean flush, CallbackInfo ci) {
305+
if (((CanBeCubic) level).cc_isCubic()) {
306+
cc_saveAllChunks(flush);
307+
ci.cancel();
308+
}
309+
}
310+
301311
// P4: scheduleUnload lambda we'll want to mirror the forge API for cubes
302312
@AddTransformToSets(ChunkToCloSet.class) @TransformFromMethod(@MethodSig("scheduleUnload(JLnet/minecraft/server/level/ChunkHolder;)V"))
303313
private native void cc_scheduleUnload(long chunkPos, ChunkHolder chunkHolder);
@@ -435,19 +445,25 @@ public String toString() {
435445
@AddTransformToSets(ChunkToCloSet.class) @TransformFromMethod(@MethodSig("saveChunkIfNeeded(Lnet/minecraft/server/level/ChunkHolder;)Z"))
436446
private native boolean cc_saveChunkIfNeeded(ChunkHolder holder);
437447

438-
// dasm + mixin
439-
@AddTransformToSets(ChunkToCloSet.class) @TransformFromMethod(@MethodSig("save(Lnet/minecraft/world/level/chunk/ChunkAccess;)Z"))
440-
private native boolean cc_save(CloAccess cloAccess);
441-
442-
/**
443-
* Redirect error logging to log with CloPos
444-
*/
445-
@Dynamic @Inject(method = "cc_dasm$cc_save", at = @At(value = "INVOKE", target = "Lio/github/opencubicchunks/cc_core/world/level/CloPos;getX()I"), cancellable = true)
446-
private void cc_onSave_errorLog(CloAccess cloAccess, CallbackInfoReturnable<Boolean> cir, @Local Exception exception) {
447-
LOGGER.error("Failed to save chunk or cube {}", cloAccess.cc_getCloPos().toString(), exception);
448-
cir.setReturnValue(false);
448+
// TODO (P2): for now we just don't save (requires more things to be CC-ified to not crash)
449+
@AddMethodToSets(sets = ChunkToCloSet.class, owner = @Ref(ChunkMap.class), method = @MethodSig("save(Lnet/minecraft/world/level/chunk/ChunkAccess;)Z"))
450+
private boolean cc_save(CloAccess cloAccess) {
451+
return false;
449452
}
450453

454+
// // dasm + mixin
455+
// @AddTransformToSets(ChunkToCloSet.class) @TransformFromMethod(@MethodSig("save(Lnet/minecraft/world/level/chunk/ChunkAccess;)Z"))
456+
// private native boolean cc_save(CloAccess cloAccess);
457+
//
458+
// /**
459+
// * Redirect error logging to log with CloPos
460+
// */
461+
// @Dynamic @Inject(method = "cc_dasm$cc_save", at = @At(value = "INVOKE", target = "Lio/github/opencubicchunks/cc_core/world/level/CloPos;getX()I"), cancellable = true)
462+
// private void cc_onSave_errorLog(CloAccess cloAccess, CallbackInfoReturnable<Boolean> cir, @Local Exception exception) {
463+
// LOGGER.error("Failed to save chunk or cube {}", cloAccess.cc_getCloPos().toString(), exception);
464+
// cir.setReturnValue(false);
465+
// }
466+
451467
// This calls ChunkSerializer.getChunkTypeFromTag, which could be an issue?
452468
@AddTransformToSets(ChunkToCloSet.class) @TransformFromMethod(@MethodSig("isExistingChunkFull(Lnet/minecraft/world/level/ChunkPos;)Z"))
453469
private native boolean cc_isExistingChunkFull(CloPos cloPos);
@@ -521,9 +537,15 @@ private boolean cc_getPlayers_isChunkTracked(ChunkMap instance, ServerPlayer pla
521537
}
522538

523539
// Replace `SectionPos.chunk()` with `SectionPos.cc_cube()` unconditionally here
524-
@AddTransformToSets(ChunkToCloSet.class) @TransformFromMethod(value = @MethodSig("tick()V"), useRedirectSets = { ChunkToCloSet.class, SectionPosToCubeSet.class })
540+
@AddTransformToSets(GlobalSet.class) @TransformFromMethod(value = @MethodSig("tick(Ljava/util/function/BooleanSupplier;)V"), useRedirectSets = { ChunkToCloSet.class, SectionPosToCubeSet.class })
541+
protected native void cc_tick(BooleanSupplier hasMoreTime);
542+
543+
@AddTransformToSets(GlobalSet.class) @TransformFromMethod(value = @MethodSig("tick()V"), useRedirectSets = { ChunkToCloSet.class, SectionPosToCubeSet.class })
525544
public native void cc_tick();
526545

546+
@AddTransformToSets(GlobalSet.class) @TransformFromMethod(value = @MethodSig("processUnloads(Ljava/util/function/BooleanSupplier;)V"))
547+
private native void processUnloads(BooleanSupplier hasMoreTime);
548+
527549
// TODO resendBiomesForChunks - only used for FillBiomeCommand
528550

529551
@AddTransformToSets(ChunkToCloSet.class) @TransformFromMethod(@MethodSig("onFullChunkStatusChange(Lnet/minecraft/world/level/ChunkPos;Lnet/minecraft/server/level/FullChunkStatus;)V"))

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,40 +111,41 @@ public void cc_setCubic() {
111111
public abstract void cc_updateCubeForced(CloPos pos, boolean add);
112112

113113
// CubePos equivalents that delegate to their corresponding CloPos method
114+
// For ticket types that hold a CloPos, we additionally must convert the ticket value.
114115
@UsedFromASM
115116
@AddMethodToSets(sets = ChunkToCubeSet.class, owner = @Ref(DistanceManager.class), method = @MethodSig("addTicket(Lnet/minecraft/server/level/TicketType;Lnet/minecraft/world/level/ChunkPos;ILjava/lang/Object;)V"))
116117
public <T> void cc_addTicket(TicketType<T> type, CubePos pos, int level, T value) {
117-
cc_addTicket(type, CloPos.cube(pos), level, value);
118+
cc_addTicket(type, CloPos.cube(pos), level, value instanceof CubePos cube ? (T) CloPos.cube(cube) : value);
118119
}
119120

120121
@UsedFromASM
121122
@AddMethodToSets(sets = ChunkToCubeSet.class, owner = @Ref(DistanceManager.class), method = @MethodSig("removeTicket(Lnet/minecraft/server/level/TicketType;Lnet/minecraft/world/level/ChunkPos;ILjava/lang/Object;)V"))
122123
public <T> void cc_removeTicket(TicketType<T> type, CubePos pos, int level, T value) {
123-
cc_removeTicket(type, CloPos.cube(pos), level, value);
124+
cc_removeTicket(type, CloPos.cube(pos), level, value instanceof CubePos cube ? (T) CloPos.cube(cube) : value);
124125
}
125126

126127
@UsedFromASM
127128
@AddMethodToSets(sets = ChunkToCubeSet.class, owner = @Ref(DistanceManager.class), method = @MethodSig("addRegionTicket(Lnet/minecraft/server/level/TicketType;Lnet/minecraft/world/level/ChunkPos;ILjava/lang/Object;)V"))
128129
public <T> void cc_addRegionTicket(TicketType<T> type, CubePos pos, int distance, T value) {
129-
cc_addRegionTicket(type, CloPos.cube(pos), distance, value);
130+
cc_addRegionTicket(type, CloPos.cube(pos), distance, value instanceof CubePos cube ? (T) CloPos.cube(cube) : value);
130131
}
131132

132133
@UsedFromASM
133134
@AddMethodToSets(sets = ChunkToCubeSet.class, owner = @Ref(DistanceManager.class), method = @MethodSig("addRegionTicket(Lnet/minecraft/server/level/TicketType;Lnet/minecraft/world/level/ChunkPos;ILjava/lang/Object;Z)V"))
134135
public <T> void cc_addRegionTicket(TicketType<T> type, CubePos pos, int distance, T value, boolean forceTicks) {
135-
cc_addRegionTicket(type, CloPos.cube(pos), distance, value, forceTicks);
136+
cc_addRegionTicket(type, CloPos.cube(pos), distance, value instanceof CubePos cube ? (T) CloPos.cube(cube) : value, forceTicks);
136137
}
137138

138139
@UsedFromASM
139140
@AddMethodToSets(sets = ChunkToCubeSet.class, owner = @Ref(DistanceManager.class), method = @MethodSig("removeRegionTicket(Lnet/minecraft/server/level/TicketType;Lnet/minecraft/world/level/ChunkPos;ILjava/lang/Object;)V"))
140141
public <T> void cc_removeRegionTicket(TicketType<T> type, CubePos pos, int distance, T value) {
141-
cc_removeRegionTicket(type, CloPos.cube(pos), distance, value);
142+
cc_removeRegionTicket(type, CloPos.cube(pos), distance, value instanceof CubePos cube ? (T) CloPos.cube(cube) : value);
142143
}
143144

144145
@UsedFromASM
145146
@AddMethodToSets(sets = ChunkToCubeSet.class, owner = @Ref(DistanceManager.class), method = @MethodSig("removeRegionTicket(Lnet/minecraft/server/level/TicketType;Lnet/minecraft/world/level/ChunkPos;ILjava/lang/Object;Z)V"))
146147
public <T> void cc_removeRegionTicket(TicketType<T> type, CubePos pos, int distance, T value, boolean forceTicks) {
147-
cc_removeRegionTicket(type, CloPos.cube(pos), distance, value, forceTicks);
148+
cc_removeRegionTicket(type, CloPos.cube(pos), distance, value instanceof CubePos cube ? (T) CloPos.cube(cube) : value, forceTicks);
148149
}
149150

150151
@UsedFromASM

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.concurrent.CompletableFuture;
55
import java.util.concurrent.Executor;
6+
import java.util.function.BooleanSupplier;
67
import java.util.function.Consumer;
78
import java.util.function.Supplier;
89

@@ -224,19 +225,19 @@ private boolean cc_onRunDistanceManagerUpdates(DistanceManager instance, ChunkMa
224225
return ((CubicDistanceManager) instance).cc_runAllUpdates(chunkMap);
225226
}
226227

227-
/**
228-
* In cubic levels, redirect to the cubic tickChunks method
229-
*/
230-
@WrapWithCondition(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerChunkCache;tickChunks()V"))
231-
private boolean cc_onTickChunks(ServerChunkCache instance) {
232-
if (((CanBeCubic) level).cc_isCubic()) {
233-
cc_tickChunks();
234-
return false;
228+
@Inject(method = "tick", at = @At("HEAD"), cancellable = true)
229+
private void cc_onTick(BooleanSupplier hasTimeLeft, boolean tickChunks, CallbackInfo ci) {
230+
if (this.cc_isCubic) {
231+
ci.cancel();
232+
cc_tick(hasTimeLeft, tickChunks);
235233
}
236-
return true;
237234
}
238235

236+
@AddTransformToSets(GlobalSet.class) @TransformFromMethod(@MethodSig("tick(Ljava/util/function/BooleanSupplier;Z)V"))
237+
public native void cc_tick(BooleanSupplier hasTimeLeft, boolean tickChunks);
238+
239239
// This could maybe be DASM, but the mixins into the copied method would likely end up being quite complex
240+
@AddMethodToSets(sets = GlobalSet.class, owner = @Ref(ServerChunkCache.class), method = @MethodSig("tickChunks()V"))
240241
private void cc_tickChunks() {
241242
long i = this.level.getGameTime();
242243
long j = i - this.lastInhabitedUpdate;
@@ -310,6 +311,14 @@ public void cc_blockChanged(BlockPos pos) {
310311
}
311312
}
312313

314+
@Inject(method = "onLightUpdate", at = @At("HEAD"), cancellable = true)
315+
private void cc_onVanillaOnLightUpdate(LightLayer type, SectionPos pos, CallbackInfo ci) {
316+
if (this.cc_isCubic) {
317+
ci.cancel();
318+
cc_onLightUpdate(type, pos);
319+
}
320+
}
321+
313322
@Override
314323
@AddMethodToSets(sets = GlobalSet.class, owner = @Ref(ServerChunkCache.class), method = @MethodSig("onLightUpdate(Lnet/minecraft/world/level/LightLayer;Lnet/minecraft/core/SectionPos;)V"))
315324
public void cc_onLightUpdate(LightLayer pType, SectionPos pPos) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.concurrent.Executor;
66

7+
import io.github.notstirred.dasm.api.annotations.Dasm;
78
import io.github.notstirred.dasm.api.annotations.redirect.redirects.AddTransformToSets;
89
import io.github.notstirred.dasm.api.annotations.selector.MethodSig;
910
import io.github.notstirred.dasm.api.annotations.transform.TransformFromMethod;
@@ -28,6 +29,7 @@
2829
import org.spongepowered.asm.mixin.injection.Inject;
2930
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
3031

32+
@Dasm(ChunkToCloSet.class)
3133
@Mixin(ServerLevel.class)
3234
public abstract class MixinServerLevel extends MixinLevel implements CubicServerLevel {
3335
@Shadow @Final private ServerChunkCache chunkSource;

src/main/java/io/github/opencubicchunks/cubicchunks/mixin/core/common/world/level/MixinLevel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private boolean cc_replaceLevelChunkInMarkAndNotifyBlock(Level level, BlockPos b
124124
return true;
125125
}
126126

127-
@AddTransformToSets(ChunkToCubeSet.class) @TransformFromMethod(@MethodSig("markAndNotifyBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/chunk/LevelChunk;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/state/BlockState;II)V"))
127+
@AddTransformToSets(ChunkToCubeSet.class) @TransformFromMethod(useRedirectSets = { ChunkToCubeSet.class }, value = @MethodSig("markAndNotifyBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/chunk/LevelChunk;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/state/BlockState;II)V"))
128128
public native void cc_markAndNotifyBlock(BlockPos blockPos, @Nullable LevelCube levelCube, BlockState blockStatePrev, BlockState blockStateNew, int flags, int p_46608_);
129129

130130
// getBlockState
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.opencubicchunks.cubicchunks.mixin.core.common.world.level.lighting;
2+
3+
import io.github.opencubicchunks.cubicchunks.CanBeCubic;
4+
import net.minecraft.core.BlockPos;
5+
import net.minecraft.core.SectionPos;
6+
import net.minecraft.world.level.LevelHeightAccessor;
7+
import net.minecraft.world.level.lighting.LevelLightEngine;
8+
import org.spongepowered.asm.mixin.Final;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Shadow;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
import org.spongepowered.asm.mixin.injection.Inject;
13+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
14+
15+
@Mixin(LevelLightEngine.class)
16+
public class MixinLevelLightEngine {
17+
@Shadow @Final protected LevelHeightAccessor levelHeightAccessor;
18+
19+
// TODO (P2) lighting
20+
21+
@Inject(method = "lightOnInSection", at = @At("HEAD"), cancellable = true)
22+
private void cc_onLightOnInSection(SectionPos sectionPos, CallbackInfoReturnable<Boolean> cir) {
23+
if (((CanBeCubic) this.levelHeightAccessor).cc_isCubic()) cir.setReturnValue(true);
24+
}
25+
26+
@Inject(method = "getRawBrightness", at = @At("HEAD"), cancellable = true)
27+
private void cc_onGetRawBrightness(BlockPos blockPos, int amount, CallbackInfoReturnable<Integer> cir) {
28+
if (((CanBeCubic) this.levelHeightAccessor).cc_isCubic()) cir.setReturnValue(15);
29+
}
30+
}
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.core.common.world.level.lighting;
4+
5+
import javax.annotation.ParametersAreNonnullByDefault;
6+
7+
import io.github.opencubicchunks.cc_core.annotation.MethodsReturnNonnullByDefault;

src/main/java/io/github/opencubicchunks/cubicchunks/mixin/dasmsets/ChunkToCloSet.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
* <br/><br/>
4444
* Clo-related field and type redirects, and method redirects containing Clo-related types in the signature or return type should be added to this set.
4545
* <br/>
46-
* TODO: CloPos is currently an exception to this; it and things that use it are in GlobalSet instead. This should be refactored eventually.
47-
* <br/>
4846
* Other redirects may also be added to this set if they should only be applied in contexts working with both Chunks and Cubes.
4947
* Redirects applicable in all contexts should be added to {@link GlobalSet}.
5048
*/

0 commit comments

Comments
 (0)