Skip to content

Commit

Permalink
fix: full lithium 0.14.5+ compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Jan 16, 2025
1 parent 09194fe commit 4ee9be9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.ishland.c2me.rewrites.chunksystem.common.compat.lithium;

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;

public class LithiumChunkStatusTrackerInvoker {

private static final Logger LOGGER = LoggerFactory.getLogger(LithiumChunkStatusTrackerInvoker.class);
private static final MethodHandle MH_onChunkInaccessible;

static {
MethodHandle mh_onChunkInaccessible = null;

Class<?> clazzChunkStatusTracker;
try {
clazzChunkStatusTracker = Class.forName("net.caffeinemc.mods.lithium.common.world.chunk.ChunkStatusTracker");
} catch (ClassNotFoundException e) {
if (FabricLoader.getInstance().isModLoaded("lithium")) {
LOGGER.warn("Couldn't find net.caffeinemc.mods.lithium.common.world.chunk.ChunkStatusTracker, yet fabric claims lithium is there. Curious");
}
clazzChunkStatusTracker = null;
}
if (clazzChunkStatusTracker != null) {
Method methodOnChunkInaccessible;
try {
methodOnChunkInaccessible = clazzChunkStatusTracker.getMethod("onChunkInaccessible", ServerWorld.class, ChunkPos.class);
} catch (NoSuchMethodException e) {
LOGGER.warn("Couldn't find net.caffeinemc.mods.lithium.common.world.chunk.ChunkStatusTracker#onChunkInaccessible(ServerWorld, ChunkPos), yet fabric claims lithium is there. Curious");
methodOnChunkInaccessible = null;
}
if (methodOnChunkInaccessible != null) {
try {
mh_onChunkInaccessible = MethodHandles.lookup().unreflect(methodOnChunkInaccessible);
} catch (IllegalAccessException e) {
LOGGER.warn("Couldn't access net.caffeinemc.mods.lithium.common.world.chunk.ChunkStatusTracker#onChunkInaccessible(ServerWorld, ChunkPos), yet fabric claims lithium is there. Curious", e);
}
}
}
MH_onChunkInaccessible = mh_onChunkInaccessible;
}

public static void invokeOnChunkInaccessible(ServerWorld world, ChunkPos pos) {
if (MH_onChunkInaccessible != null) {
try {
MH_onChunkInaccessible.invokeExact(world, pos);
} catch (Throwable e) {
LOGGER.error("net.caffeinemc.mods.lithium.common.world.chunk.ChunkStatusTracker#onChunkInaccessible(ServerWorld, ChunkPos) failed", e);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.ishland.c2me.rewrites.chunksystem.common.compat;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.ishland.c2me.rewrites.chunksystem.common.ChunkState;
import com.ishland.c2me.rewrites.chunksystem.common.Config;
import com.ishland.c2me.rewrites.chunksystem.common.NewChunkStatus;
import com.ishland.c2me.rewrites.chunksystem.common.compat.lithium.LithiumChunkStatusTrackerInvoker;
import com.ishland.c2me.rewrites.chunksystem.common.fapi.LifecycleEventInvoker;
import com.ishland.c2me.rewrites.chunksystem.common.threadstate.ChunkTaskWork;
import com.ishland.flowsched.scheduler.Cancellable;
Expand Down Expand Up @@ -123,6 +124,7 @@ public CompletionStage<Void> downgradeFromThis(ChunkLoadingContext context, Canc
worldChunk.setLevelTypeProvider(null);
worldChunk.setUnsavedListener(pos -> {
});
LithiumChunkStatusTrackerInvoker.invokeOnChunkInaccessible(((IThreadedAnvilChunkStorage) context.tacs()).getWorld(), context.holder().getKey());
context.holder().getItem().set(new ChunkState(state.protoChunk(), state.protoChunk(), ChunkStatus.FULL));
}, ((IThreadedAnvilChunkStorage) context.tacs()).getMainThreadExecutor());
}
Expand Down

0 comments on commit 4ee9be9

Please sign in to comment.