Skip to content

Commit

Permalink
cleanup and comments/javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
CursedFlames committed Mar 12, 2024
1 parent ae10850 commit 2f166d0
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.concurrent.Executor;
import java.util.function.Function;

import javax.annotation.Nullable;

import com.mojang.datafixers.util.Either;
import io.github.notstirred.dasm.api.annotations.redirect.redirects.ConstructorToFactoryRedirect;
import io.github.notstirred.dasm.api.annotations.redirect.redirects.FieldRedirect;
Expand Down Expand Up @@ -91,7 +93,13 @@ public abstract CompletableFuture<Either<CloAccess, ChunkHolder.ChunkLoadingFail
}

@TypeRedirect(from = @Ref(ChunkProgressListener.class), to = @Ref(CubicChunkProgressListener.class))
interface ChunkProgressListener_to_CubicChunkProgressListener_redirects { }
interface ChunkProgressListener_to_CubicChunkProgressListener_redirects {
@MethodRedirect(@MethodSig("updateSpawnPos(Lnet/minecraft/world/level/ChunkPos;)V"))
void cc_updateSpawnPos(CloPos pCenter);

@MethodRedirect(@MethodSig("onStatusChange(Lnet/minecraft/world/level/ChunkPos;Lnet/minecraft/world/level/chunk/ChunkStatus;)V"))
void cc_onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus);
}

@TypeRedirect(from = @Ref(ChunkTrackingView.class), to = @Ref(CloTrackingView.class))
interface ChunkTrackingView_to_CloTrackingView_redirects { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import io.github.opencubicchunks.cubicchunks.world.level.chunklike.CloPos;
import net.minecraft.core.SectionPos;

/**
* Replaces {@link SectionPos}.chunk() in dasm-copied code in cases where a chunk position should still be returned, not a cube position.
*/
@RedirectSet
public interface SectionPosToChunkSet {
@IntraOwnerContainer(owner = @Ref(SectionPos.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import io.github.opencubicchunks.cubicchunks.world.level.chunklike.CloPos;
import net.minecraft.core.SectionPos;

/**
* Replaces {@link SectionPos}.chunk() in dasm-copied code in cases where a cube position should be returned, rather than a chunk position.
*/
@RedirectSet
public interface SectionPosToCubeSet {
@IntraOwnerContainer(owner = @Ref(SectionPos.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@
public abstract class MixinSectionPos {
@Shadow public abstract ChunkPos chunk();

/**
* Replaces {@link SectionPos}.chunk() in dasm-copied code in cases where a cube position should be returned, rather than a chunk position.
* @return The position of the cube containing this section, as a {@link CloPos}
*/
public CloPos cc_cube() {
return CloPos.section((SectionPos) (Object) this);
}

/**
* Replaces {@link SectionPos}.chunk() in dasm-copied code in cases where a chunk position should still be returned, not a cube position.
* @return The position of the chunk containing this section, as a {@link CloPos}
*/
public CloPos cc_chunk() {
return CloPos.chunk(this.chunk());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package io.github.opencubicchunks.cubicchunks.mixin.core.common.server.level;

import io.github.notstirred.dasm.api.annotations.redirect.redirects.AddFieldToSets;
import io.github.notstirred.dasm.api.annotations.selector.FieldSig;
import io.github.notstirred.dasm.api.annotations.selector.Ref;
import io.github.opencubicchunks.cubicchunks.mixin.ForgeSet;
import io.github.opencubicchunks.cubicchunks.world.level.chunklike.LevelClo;
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.world.level.chunk.LevelChunk;
import org.spongepowered.asm.mixin.Mixin;

// FIXME should be in forge sourceset once tests run against forge
@Mixin(ChunkHolder.class)
public class MixinChunkHolder_Forge {
// Field added by Forge
@AddFieldToSets(sets = ForgeSet.class, owner = @Ref(ChunkHolder.class), field = @FieldSig(name = "currentlyLoading", type = @Ref(LevelChunk.class)))
LevelClo currentlyLoading;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public abstract class MixinChunkMap extends MixinChunkStorage implements CubicCh
@AddFieldToSets(sets = GeneralSet.class, owner = @Ref(ChunkMap.class), field = @FieldSig(type = @Ref(ChunkProgressListener.class), name = "progressListener"))
private CubicChunkProgressListener cc_progressListener;

// TODO once we can target non-return locations in constructors, do this when the vanilla field is set
@Inject(method = "<init>", at = @At("RETURN"))
private void onInit(ServerLevel pLevel, LevelStorageSource.LevelStorageAccess pLevelStorageAccess, DataFixer pFixerUpper, StructureTemplateManager pStructureManager,
Executor pDispatcher, BlockableEventLoop pMainThreadExecutor, LightChunkGetter pLightChunk, ChunkGenerator pGenerator, ChunkProgressListener pProgressListener,
Expand Down Expand Up @@ -204,7 +205,8 @@ public String toString() {
}
}

// Vanilla expects that the center chunk is in the middle of the list; this is not the case for cubes, so we manually swap the center cube to the middle.
// Vanilla expects that the center chunk is in the middle of the list; this is not the case for cubes, so we manually swap the center cube to the middle
// - this is a temporary approach, until we make our own cube+chunk list wrapper
swap(cloHolders, middleCubeIndex, cloHolders.size() / 2);
swap(dependencyFutures, middleCubeIndex, cloHolders.size() / 2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
@Mixin(LoggerChunkProgressListener.class)
public abstract class MixinLoggerChunkProgressListener implements CubicChunkProgressListener {
@AddTransformToSets(GeneralSet.class) @TransformFromMethod(@MethodSig("updateSpawnPos(Lnet/minecraft/world/level/ChunkPos;)V"))
@Override public native void updateSpawnPos(CloPos pCenter);
@Override public native void cc_updateSpawnPos(CloPos pCenter);

@AddTransformToSets(GeneralSet.class) @TransformFromMethod(@MethodSig("onStatusChange(Lnet/minecraft/world/level/ChunkPos;Lnet/minecraft/world/level/chunk/ChunkStatus;)V"))
@Override public native void onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus);
@Override public native void cc_onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus);

@Override @Shadow public abstract void start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public abstract class MixinProcessorChunkProgressListener implements CubicChunkP
// TODO these two could be dasm?
@AddMethodToSets(owner = @Ref(ProcessorChunkProgressListener.class), method = @MethodSig("updateSpawnPos(Lnet/minecraft/world/level/ChunkPos;)V"), sets = GeneralSet.class)
@Override
public void updateSpawnPos(CloPos pCenter) {
this.mailbox.tell(() -> ((CubicChunkProgressListener) this.delegate).updateSpawnPos(pCenter));
public void cc_updateSpawnPos(CloPos pCenter) {
this.mailbox.tell(() -> ((CubicChunkProgressListener) this.delegate).cc_updateSpawnPos(pCenter));
}

@AddMethodToSets(owner = @Ref(ProcessorChunkProgressListener.class), method = @MethodSig("onStatusChange(Lnet/minecraft/world/level/ChunkPos;Lnet/minecraft/world/level/chunk/ChunkStatus;)V"), sets = GeneralSet.class)
@Override
public void onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus) {
this.mailbox.tell(() -> ((CubicChunkProgressListener) this.delegate).onStatusChange(pChunkPosition, pNewStatus));
public void cc_onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus) {
this.mailbox.tell(() -> ((CubicChunkProgressListener) this.delegate).cc_onStatusChange(pChunkPosition, pNewStatus));
}

@Override @Shadow public abstract void start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ public abstract class MixinStoringChunkProgressListener implements CubicChunkPro
// TODO these two could be dasm?
@AddMethodToSets(owner = @Ref(StoringChunkProgressListener.class), method = @MethodSig("updateSpawnPos(Lnet/minecraft/world/level/ChunkPos;)V"), sets = GeneralSet.class)
@Override
public void updateSpawnPos(CloPos pCenter) {
public void cc_updateSpawnPos(CloPos pCenter) {
if (this.started) {
((CubicChunkProgressListener) this.delegate).updateSpawnPos(pCenter);
((CubicChunkProgressListener) this.delegate).cc_updateSpawnPos(pCenter);
// FIXME we should have a CloPos spawnPos
this.spawnPos = pCenter.correspondingChunkPos();
}
}

@AddMethodToSets(owner = @Ref(StoringChunkProgressListener.class), method = @MethodSig("onStatusChange(Lnet/minecraft/world/level/ChunkPos;Lnet/minecraft/world/level/chunk/ChunkStatus;)V"), sets = GeneralSet.class)
@Override
public void onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus) {
public void cc_onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus) {
if (this.started) {
((CubicChunkProgressListener) this.delegate).onStatusChange(pChunkPosition, pNewStatus);
((CubicChunkProgressListener) this.delegate).cc_onStatusChange(pChunkPosition, pNewStatus);
if (pNewStatus == null) {
this.statuses.remove(pChunkPosition.toLong());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.world.level.chunk.ChunkAccess;
import net.neoforged.neoforge.event.level.ChunkEvent;

// In DASM-copied code we redirect forge event construction to factory methods on this class, allowing for easier control (e.g. not firing events for cubic equivalents of vanilla things)
public class EventConstructorDelegates {
public static ChunkEvent.Load create_ChunkEvent$Load(CloAccess cloAccess, boolean newChunk) {
if (cloAccess instanceof ChunkAccess chunk) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class CubicTicketType {
public static final TicketType<CloPos> FORCED = create("forced", Comparator.comparingLong(CloPos::asLong));
public static final TicketType<CloPos> LIGHT = create("light", Comparator.comparingLong(CloPos::asLong));
public static final TicketType<CloPos> UNKNOWN = create("unknown", Comparator.comparingLong(CloPos::asLong), 1);
// TODO we don't actually need this; remove it and use TicketType.START
public static final TicketType<Unit> START = create("start", (a, b) -> 0);

public static <T> TicketType<T> create(String nameIn, Comparator<T> comparator) {
return TicketTypeAccess.createNew(nameIn, comparator, 0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import net.minecraft.world.level.chunk.ChunkStatus;

public interface CubicChunkProgressListener {
void updateSpawnPos(CloPos pCenter);
void cc_updateSpawnPos(CloPos pCenter);

void onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus);
void cc_onStatusChange(CloPos pChunkPosition, @Nullable ChunkStatus pNewStatus);

void start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.github.opencubicchunks.cubicchunks.MarkableAsCubic;
import io.github.opencubicchunks.cubicchunks.mixin.test.common.server.level.CubicDistanceManagerTestAccess;
import io.github.opencubicchunks.cubicchunks.server.level.CubicDistanceManager;
import io.github.opencubicchunks.cubicchunks.server.level.CubicTicketType;
import io.github.opencubicchunks.cubicchunks.testutils.BaseTest;
import io.github.opencubicchunks.cubicchunks.world.level.chunklike.CloPos;
import net.minecraft.Util;
Expand Down Expand Up @@ -230,7 +229,7 @@ private void testAddAndRemove(DistanceManager distanceManager, int iterations, i
@Test public void testRemoveTicketsOnClosing() {
var distanceManager = setupDistanceManager();
((MarkableAsCubic) distanceManager).cc_setCubic();
((CubicDistanceManager)distanceManager).addTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
((CubicDistanceManager)distanceManager).addTicket(TicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
assertTrue(distanceManager.hasTickets());
distanceManager.removeTicketsOnClosing();
assertFalse(distanceManager.hasTickets());
Expand All @@ -251,13 +250,13 @@ private void testAddAndRemove(DistanceManager distanceManager, int iterations, i
@Test public void testAddRemoveTickets() {
var distanceManager = setupDistanceManager();
((MarkableAsCubic) distanceManager).cc_setCubic();
((CubicDistanceManager)distanceManager).addTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
((CubicDistanceManager)distanceManager).addTicket(TicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
assertTrue(distanceManager.hasTickets());
((CubicDistanceManager)distanceManager).removeTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
((CubicDistanceManager)distanceManager).removeTicket(TicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
assertFalse(distanceManager.hasTickets());
((CubicDistanceManager)distanceManager).addRegionTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
((CubicDistanceManager)distanceManager).addRegionTicket(TicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
assertTrue(distanceManager.hasTickets());
((CubicDistanceManager)distanceManager).removeRegionTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
((CubicDistanceManager)distanceManager).removeRegionTicket(TicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE);
assertFalse(distanceManager.hasTickets());
}

Expand All @@ -272,9 +271,9 @@ private void testAddAndRemove(DistanceManager distanceManager, int iterations, i
@Test public void testShouldForceTicks() {
var distanceManager = setupDistanceManager();
((MarkableAsCubic) distanceManager).cc_setCubic();
((CubicDistanceManager)distanceManager).addRegionTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE, true);
((CubicDistanceManager)distanceManager).addRegionTicket(TicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE, true);
assertTrue(distanceManager.shouldForceTicks(CloPos.asLong(0, 0, 0)));
((CubicDistanceManager)distanceManager).removeRegionTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE, true);
((CubicDistanceManager)distanceManager).removeRegionTicket(TicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE, true);
assertFalse(distanceManager.shouldForceTicks(CloPos.asLong(0, 0, 0)));
}

Expand Down

0 comments on commit 2f166d0

Please sign in to comment.