Skip to content

Commit

Permalink
don't cache chunks and entities unless we're capturing
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Feb 5, 2024
1 parent b02574a commit 04f888b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions common/src/main/kotlin/org/waste/of/time/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ import java.awt.Color

object Events {
fun onChunkLoad(chunk: WorldChunk) {
if (!CaptureManager.capturing) return
RegionBasedChunk(chunk).cache()
}

fun onChunkUnload(chunk: WorldChunk) {
if (!CaptureManager.capturing) return
val regionBasedChunk = HotCache.chunks[chunk.pos] ?: RegionBasedChunk(chunk)
regionBasedChunk.apply {
cacheBlockEntities()
Expand All @@ -47,6 +49,7 @@ object Events {
}

fun onEntityLoad(entity: Entity) {
if (!CaptureManager.capturing) return
if (entity is PlayerEntity) {
PlayerStoreable(entity).cache()
} else {
Expand All @@ -55,6 +58,7 @@ object Events {
}

fun onEntityUnload(entity: Entity) {
if (!CaptureManager.capturing) return
if (entity !is PlayerEntity) return
PlayerStoreable(entity).apply {
emit()
Expand Down
20 changes: 19 additions & 1 deletion common/src/main/kotlin/org/waste/of/time/storage/StorageFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.util.path.SymlinkValidationException
import org.waste.of.time.WorldTools.LOG
import org.waste.of.time.WorldTools.mc
import org.waste.of.time.manager.MessageManager
import org.waste.of.time.manager.MessageManager.sendInfo
import org.waste.of.time.manager.StatisticManager
import org.waste.of.time.storage.cache.EntityCacheable
import org.waste.of.time.storage.serializable.EndFlow
import org.waste.of.time.storage.serializable.PlayerStoreable
import org.waste.of.time.storage.serializable.RegionBasedChunk
import java.io.IOException
import java.util.concurrent.CancellationException
import kotlin.time.Duration
Expand All @@ -38,6 +41,7 @@ object StorageFlow {

try {
LOG.info("Started caching")
syncCacheFromWorldState()
mc.levelStorage.createSession(levelName).use { openSession ->
sharedFlow.collect { storeable ->
if (!storeable.shouldStore()) {
Expand Down Expand Up @@ -71,5 +75,19 @@ object StorageFlow {
LOG.info("Finished caching")
}

private fun syncCacheFromWorldState() {
val diameter = mc.world?.chunkManager?.chunks?.diameter ?: 0
for (i in 0..<(diameter * diameter)) {
mc.world?.chunkManager?.chunks?.getChunk(i)?.let { chunk ->
RegionBasedChunk(chunk).cache()
}
}

mc.world?.entities?.forEach {
if (it is PlayerEntity) PlayerStoreable(it).cache()
else EntityCacheable(it).cache()
}
}

class StopCollectingException : Exception()
}
3 changes: 3 additions & 0 deletions common/src/main/resources/worldtools.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ accessWidener v2 named

accessible class net/minecraft/client/world/ClientChunkManager$ClientChunkMap
accessible method net/minecraft/client/world/ClientChunkManager$ClientChunkMap isInRadius (II)Z
accessible field net/minecraft/client/world/ClientChunkManager chunks Lnet/minecraft/client/world/ClientChunkManager$ClientChunkMap;
accessible method net/minecraft/client/world/ClientChunkManager$ClientChunkMap getChunk (I)Lnet/minecraft/world/chunk/WorldChunk;
accessible field net/minecraft/client/world/ClientChunkManager$ClientChunkMap diameter I

0 comments on commit 04f888b

Please sign in to comment.