Skip to content

Commit b34c92e

Browse files
committed
callbacks instead of duplicated rebuild and unload checks
1 parent f859ac0 commit b34c92e

3 files changed

Lines changed: 34 additions & 35 deletions

File tree

src/main/kotlin/com/lambda/graphics/mc/renderer/ChunkedRenderer.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import com.lambda.context.SafeContext
2222
import com.lambda.event.events.RenderEvent
2323
import com.lambda.event.events.TickEvent
2424
import com.lambda.event.events.WorldEvent
25-
import com.lambda.event.listener.SafeListener.Companion.listen
26-
import com.lambda.event.listener.SafeListener.Companion.listenConcurrently
2725
import com.lambda.event.listener.UnsafeListener.Companion.listenConcurrentlyUnsafe
2826
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
2927
import com.lambda.graphics.RenderMain
@@ -35,7 +33,7 @@ import com.lambda.util.world.FastVector
3533
import com.lambda.util.world.fastVectorOf
3634
import com.mojang.blaze3d.buffers.GpuBufferSlice
3735
import com.mojang.blaze3d.systems.RenderSystem
38-
import net.minecraft.client.world.ClientWorld
36+
import net.minecraft.util.math.ChunkPos
3937
import net.minecraft.util.math.Vec3d
4038
import net.minecraft.world.chunk.WorldChunk
4139
import org.joml.Vector3f
@@ -46,6 +44,8 @@ import java.util.concurrent.ConcurrentLinkedDeque
4644
class ChunkedRenderer(
4745
owner: Any,
4846
name: String,
47+
private val preChunkBuild: (ChunkPos) -> Unit = {},
48+
private val preChunkClear: (ChunkPos) -> Unit = {},
4949
depthTest: () -> Boolean,
5050
pauseUpdates: () -> Boolean,
5151
private val update: RenderBuilder.(FastVector) -> Unit
@@ -168,10 +168,12 @@ class ChunkedRenderer(
168168
fun rebuild(depthTest: Boolean) {
169169
val chunkOriginVec = Vec3d(originX, originY, originZ)
170170
val scope = RenderBuilder(chunkOriginVec, depthTest = depthTest)
171-
172-
for (x in chunk.pos.startX..chunk.pos.endX) {
173-
for (z in chunk.pos.startZ..chunk.pos.endZ) {
174-
for (y in chunk.bottomY..chunk.height) {
171+
172+
preChunkBuild(chunk.pos)
173+
174+
(chunk.pos.startX..chunk.pos.endX).forEach { x ->
175+
(chunk.pos.startZ..chunk.pos.endZ).forEach { z ->
176+
(chunk.bottomY..chunk.height).forEach { y ->
175177
update(scope, fastVectorOf(x, y, z))
176178
}
177179
}
@@ -180,16 +182,21 @@ class ChunkedRenderer(
180182
uploadQueue.add { renderer.upload(scope.collector) }
181183
}
182184

183-
fun clearData() = renderer.clearData()
185+
fun clearData() {
186+
preChunkClear(chunk.pos)
187+
renderer.clearData()
188+
}
184189
}
185190

186191
companion object {
187192
fun Any.chunkedRenderer(
188193
name: String,
194+
preChunkBuild: (ChunkPos) -> Unit = {},
195+
preChunkClear: (ChunkPos) -> Unit = {},
189196
depthTest: () -> Boolean = { false },
190197
pauseUpdates: () -> Boolean = { false },
191198
update: RenderBuilder.(FastVector) -> Unit
192-
) = ChunkedRenderer(this, name, depthTest, pauseUpdates, update).also { renderer ->
199+
) = ChunkedRenderer(this, name, preChunkBuild, preChunkClear, depthTest, pauseUpdates, update).also { renderer ->
193200
(this as? Module)?.let { module ->
194201
module.onEnable { renderer.rebuild() }
195202
module.onDisable { renderer.clear() }

src/main/kotlin/com/lambda/module/modules/render/LightLevels.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ object LightLevels : Module(
7575

7676
private val chunkedRenderer = chunkedRenderer(
7777
"LightLevels Chunked Renderer",
78-
{ depthTest },
79-
{ mode != Mode.Chunked }
78+
depthTest = { depthTest },
79+
pauseUpdates = { mode != Mode.Chunked }
8080
) { pos ->
8181
runSafe { buildRender(pos.toBlockPos(), worldLineConfig.getDashStyle()) }
8282
}

src/main/kotlin/com/lambda/module/modules/render/Search.kt

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717

1818
package com.lambda.module.modules.render
1919

20-
import com.lambda.Lambda.mc
2120
import com.lambda.config.applyEdits
2221
import com.lambda.config.groups.ScreenLineSettings
2322
import com.lambda.config.groups.WorldLineSettings
2423
import com.lambda.config.settings.collections.CollectionSetting.Companion.onDeselect
2524
import com.lambda.config.settings.collections.CollectionSetting.Companion.onSelect
2625
import com.lambda.context.SafeContext
27-
import com.lambda.event.events.WorldEvent
28-
import com.lambda.event.listener.SafeListener.Companion.listen
29-
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
3026
import com.lambda.graphics.mc.RenderBuilder
3127
import com.lambda.graphics.mc.renderer.ChunkedRenderer.Companion.chunkedRenderer
3228
import com.lambda.graphics.mc.renderer.ImmediateRenderer.Companion.immediateRenderer
@@ -35,9 +31,9 @@ import com.lambda.graphics.util.DirectionMask
3531
import com.lambda.graphics.util.DirectionMask.buildSideMesh
3632
import com.lambda.graphics.util.DynamicAABB.Companion.interpolatedBox
3733
import com.lambda.module.Module
38-
import com.lambda.module.modules.render.Search.chunkedRenderer
3934
import com.lambda.module.tag.ModuleTag
4035
import com.lambda.threading.runSafe
36+
import com.lambda.util.BlockUtils.blockState
4137
import com.lambda.util.EntityUtils.decorationEntityMap
4238
import com.lambda.util.EntityUtils.entityGroup
4339
import com.lambda.util.NamedEnum
@@ -46,14 +42,14 @@ import com.lambda.util.extension.entityColor
4642
import com.lambda.util.extension.getBlockState
4743
import com.lambda.util.math.setAlpha
4844
import com.lambda.util.world.toBlockPos
49-
import io.ktor.util.collections.ConcurrentMap
5045
import net.minecraft.block.BlockState
5146
import net.minecraft.block.Blocks
5247
import net.minecraft.entity.Entity
5348
import net.minecraft.util.math.BlockPos
5449
import net.minecraft.util.math.Box
5550
import net.minecraft.util.math.Vec3d
5651
import java.awt.Color
52+
import java.util.concurrent.ConcurrentHashMap
5753

5854
object Search : Module(
5955
name = "Search",
@@ -104,17 +100,17 @@ object Search : Module(
104100
}
105101
}
106102

107-
private val tracerBlockPositions = ConcurrentMap<BlockPos, Pair<Vec3d, Pair<Color, Color>>>()
108-
109-
val chunkedRenderer = chunkedRenderer("Search Chunked Renderer") { position ->
110-
val pos = position.toBlockPos()
111-
val state = mc.world?.getBlockState(pos)
112-
if (state == null || state.block !in blocks) {
113-
tracerBlockPositions.remove(pos)
114-
return@chunkedRenderer
115-
}
103+
private val tracerBlockPositions = ConcurrentHashMap<BlockPos, Pair<Vec3d, Pair<Color, Color>>>()
116104

105+
val chunkedRenderer = chunkedRenderer(
106+
"Search Chunked Renderer",
107+
{ chunkPos -> if (tracers) tracerBlockPositions.keys.removeIf { it in chunkPos } },
108+
{ chunkPos -> if (tracers) tracerBlockPositions.keys.removeIf { it in chunkPos } }
109+
) { position ->
117110
runSafe {
111+
val pos = position.toBlockPos()
112+
val state = blockState(pos)
113+
if (state.block !in blocks) return@chunkedRenderer
118114
val sides = if (mesh) {
119115
buildSideMesh(position) {
120116
world.getBlockState(it).block in blocks
@@ -128,12 +124,11 @@ object Search : Module(
128124
if (shape.isEmpty) listOf(Box(pos))
129125
else shape.boundingBoxes.map { it.offset(pos) }
130126
if (tracers) {
131-
val center =
132-
shape
133-
.boundingBoxes
134-
.reduce(Box::union)
135-
.offset(pos)
136-
.center
127+
val center = shape
128+
.boundingBoxes
129+
.reduce(Box::union)
130+
.offset(pos)
131+
.center
137132
tracerBlockPositions[pos] = Pair(center, getTracerColors(lineColor))
138133
}
139134
box(
@@ -163,9 +158,6 @@ object Search : Module(
163158
if (tracers) tracerBlockPositions.values.forEach { tracer(it) }
164159
}
165160
}
166-
167-
listenUnsafe<WorldEvent.ChunkEvent.Unload> { event -> if (tracers) tracerBlockPositions.keys.removeIf { it in event.chunk.pos } }
168-
listenUnsafe<WorldEvent.Leave> { if (tracers) tracerBlockPositions.clear() }
169161
}
170162

171163
private fun RenderBuilder.tracer(pair: Pair<Vec3d, Pair<Color, Color>>) {

0 commit comments

Comments
 (0)