Skip to content

Commit

Permalink
Fix ChunkSchedulingPatch
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Dec 26, 2024
1 parent bb9e267 commit 5e7784d
Showing 1 changed file with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package xyz.xenondevs.nova.patch.impl.chunk

import ca.spottedleaf.moonrise.common.util.ChunkSystem
import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.NewChunkHolder
import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.task.GenericDataLoadTask
import kotlinx.coroutines.runBlocking
import net.minecraft.nbt.CompoundTag
import net.minecraft.server.level.ServerLevel
import net.minecraft.world.level.chunk.LevelChunk
import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.MethodInsnNode
import xyz.xenondevs.bytebase.asm.buildInsnList
import xyz.xenondevs.bytebase.jvm.VirtualClassPath
import xyz.xenondevs.bytebase.util.insertBeforeEvery
import xyz.xenondevs.nova.patch.MultiTransformer
import xyz.xenondevs.nova.util.reflection.ReflectionUtils
import xyz.xenondevs.nova.world.ChunkPos
import xyz.xenondevs.nova.world.format.WorldDataManager
import kotlin.reflect.KFunction


private val CHUNK_DATA_LOAD_TASK = Class.forName("ca.spottedleaf.moonrise.patches.chunk_system.scheduling.task.ChunkLoadTask\$ChunkDataLoadTask")
private val CHUNK_DATA_LOAD_TASK_RUN_OFF_MAIN = ReflectionUtils.getMethod(
CHUNK_DATA_LOAD_TASK,
Expand All @@ -26,7 +28,7 @@ private val GENERIC_DATA_LOAD_TASK_WORLD = ReflectionUtils.getField(GenericDataL
private val GENERIC_DATA_LOAD_TASK_CHUNK_X = ReflectionUtils.getField(GenericDataLoadTask::class, "chunkX")
private val GENERIC_DATA_LOAD_TASK_CHUNK_Z = ReflectionUtils.getField(GenericDataLoadTask::class, "chunkZ")

internal object ChunkSchedulingPatch : MultiTransformer(CHUNK_DATA_LOAD_TASK.kotlin, ChunkSystem::class) {
internal object ChunkSchedulingPatch : MultiTransformer(CHUNK_DATA_LOAD_TASK.kotlin, NewChunkHolder::class) {

override fun transform() {
VirtualClassPath[CHUNK_DATA_LOAD_TASK_RUN_OFF_MAIN].instructions.insert(buildInsnList {
Expand All @@ -40,24 +42,18 @@ internal object ChunkSchedulingPatch : MultiTransformer(CHUNK_DATA_LOAD_TASK.kot
invokeStatic(::loadChunkBlocking)
})

fun insertEnableTicking(fn: KFunction<*>) =
VirtualClassPath[fn].instructions.insert(buildInsnList {
addLabel()
aLoad(0)
invokeStatic(::enableChunkTicking)
})

fun insertDisableTicking(fn: KFunction<*>) =
VirtualClassPath[fn].instructions.insert(buildInsnList {
addLabel()
aLoad(0)
invokeStatic(::disableChunkTicking)
})
fun insertBeforePlatformHooks(platformHooksFn: String, fn: KFunction<*>) =
VirtualClassPath[NewChunkHolder::handleFullStatusChange].insertBeforeEvery(buildInsnList {
swap() // ... chunk, vanillaChunkHolder -> ... vanillaChunkHolder, chunk
dup() // ... vanillaChunkHolder, chunk -> ... vanillaChunkHolder, chunk, chunk
invokeStatic(fn) // ... vanillaChunkHolder, chunk, chunk -> ... vanillaChunkHolder, chunk
swap() // ... vanillaChunkHolder, chunk -> ... chunk, vanillaChunkHolder
}) { it.opcode == Opcodes.INVOKEINTERFACE && (it as MethodInsnNode).name == platformHooksFn }

insertEnableTicking(ChunkSystem::onChunkEntityTicking)
insertEnableTicking(ChunkSystem::onChunkTicking)
insertDisableTicking(ChunkSystem::onChunkNotTicking)
insertDisableTicking(ChunkSystem::onChunkNotEntityTicking)
insertBeforePlatformHooks("onChunkTicking", ::enableChunkTicking)
insertBeforePlatformHooks("onChunkEntityTicking", ::enableChunkTicking)
insertBeforePlatformHooks("onChunkNotTicking", ::disableChunkTicking)
insertBeforePlatformHooks("onChunkNotEntityTicking", ::disableChunkTicking)
}

@JvmStatic
Expand Down

0 comments on commit 5e7784d

Please sign in to comment.