Skip to content

Commit 5212a3b

Browse files
committed
Update scheduler
1 parent 974d9cd commit 5212a3b

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

common/src/main/kotlin/com/lambda/graphics/renderer/esp/ChunkedESP.kt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ import com.lambda.event.listener.SafeListener.Companion.concurrentListener
77
import com.lambda.event.listener.SafeListener.Companion.listener
88
import com.lambda.module.modules.client.RenderSettings
99
import com.lambda.threading.runGameBlocking
10-
import net.minecraft.util.math.BlockPos
1110
import net.minecraft.util.math.ChunkPos
1211
import net.minecraft.world.BlockView
1312
import net.minecraft.world.chunk.WorldChunk
14-
import java.awt.Color
1513
import java.util.concurrent.ConcurrentHashMap
1614
import java.util.concurrent.ConcurrentLinkedDeque
1715

1816
class ChunkedESP private constructor(
1917
owner: Any,
20-
private val update: EspRenderer.(BlockView, BlockPos) -> Boolean
18+
private val update: EspRenderer.(BlockView, Int, Int, Int) -> Unit
2119
) {
2220
private val rendererMap = ConcurrentHashMap<ChunkPos, EspChunk>()
2321
private val WorldChunk.renderer get() = rendererMap.getOrPut(pos) {
@@ -52,19 +50,23 @@ class ChunkedESP private constructor(
5250

5351
owner.concurrentListener<TickEvent.Pre> {
5452
if (++ticks % RenderSettings.updateFrequency == 0) {
55-
rendererMap.values
56-
.filter { it.neighborsLoaded }
57-
.forEach { it.rebuild() }
53+
val polls = minOf(RenderSettings.rebuildsPerTick, rebuildPool.size)
54+
55+
repeat(polls) {
56+
rebuildPool.poll()?.rebuild()
57+
}
5858
ticks = 0
5959
}
6060
}
6161

6262
owner.listener<TickEvent.Pre> {
6363
if (uploadPool.isEmpty()) return@listener
6464

65-
uploadPool
66-
.take(RenderSettings.uploadsPerTick)
67-
.forEach { it() }
65+
val polls = minOf(RenderSettings.uploadsPerTick, uploadPool.size)
66+
67+
repeat(polls) {
68+
uploadPool.poll()?.invoke()
69+
}
6870
}
6971

7072
owner.listener<RenderEvent.World> {
@@ -76,14 +78,12 @@ class ChunkedESP private constructor(
7678

7779
companion object {
7880
fun Any.newChunkedESP(
79-
filter: (BlockView, BlockPos) -> Boolean,
80-
painter: (BlockView, BlockPos) -> Pair<Color, Color>
81-
) = ChunkedESP(this, filter, painter)
81+
update: EspRenderer.(BlockView, Int, Int, Int) -> Unit
82+
) = ChunkedESP(this, update)
8283
}
8384

8485
private class EspChunk(val chunk: WorldChunk, val owner: ChunkedESP) {
8586
var renderer: EspRenderer? = null
86-
var outdated = true
8787

8888
private val chunkOffsets = listOf(1 to 0, 0 to 1, -1 to 0, 0 to -1)
8989
val neighbors = chunkOffsets.map {
@@ -104,14 +104,12 @@ class ChunkedESP private constructor(
104104
}
105105

106106
suspend fun rebuild() {
107-
outdated = false
108-
109107
val newRenderer = runGameBlocking {
110108
EspRenderer()
111109
}
112110

113111
iterateChunk { x, y, z ->
114-
draw(newRenderer, BlockPos(x, y, z))
112+
owner.update(newRenderer, chunk.world, x, y, z)
115113
}
116114

117115
val upload = {
@@ -131,10 +129,6 @@ class ChunkedESP private constructor(
131129
}
132130
}
133131

134-
private fun draw(renderer: EspRenderer, x: Int, y: Int, z: Int) {
135-
if (!owner.update(chunk, blockPos)) return false
136-
}
137-
138132
private fun iterateChunk(block: (Int, Int, Int) -> Unit) = chunk.apply {
139133
for (x in pos.startX..pos.endX) {
140134
for (z in pos.startZ..pos.endZ) {

common/src/main/kotlin/com/lambda/module/modules/client/RenderSettings.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ object RenderSettings : Module(
2121
// ESP
2222
val uploadScheduler by setting("Upload Scheduler", UploadScheduler.Instant) { page == Page.ESP }
2323
val uploadsPerTick by setting("Uploads", 8, 1..32, 1, unit = " chunk/tick") { page == Page.ESP && uploadScheduler == UploadScheduler.Delayed }
24+
val rebuildsPerTick by setting("Rebuilds", 8, 1..32, 1, unit = " chunk/tick") { page == Page.ESP }
2425
val vertexMapping by setting("Vertex Mapping", true) { page == Page.ESP }
2526
val updateFrequency by setting("Update Frequency", 2, 1..10, 1, "Frequency of block updates", unit = " ticks") { page == Page.ESP }
2627
val outlineWidth by setting("Outline Width", 1.0, 0.1..5.0, 0.1, "Width of block outlines", unit = "px") { page == Page.ESP }

common/src/main/kotlin/com/lambda/module/modules/render/BlockESP.kt

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package com.lambda.module.modules.render
22

33
import com.lambda.Lambda.mc
4+
import com.lambda.context.SafeContext
45
import com.lambda.graphics.renderer.esp.ChunkedESP.Companion.newChunkedESP
6+
import com.lambda.graphics.renderer.esp.DirectionMask
7+
import com.lambda.graphics.renderer.esp.DirectionMask.exclude
8+
import com.lambda.graphics.renderer.esp.DirectionMask.mask
59
import com.lambda.module.Module
610
import com.lambda.module.tag.ModuleTag
11+
import com.lambda.util.BlockUtils.blockState
12+
import net.minecraft.block.Block
713
import net.minecraft.block.Blocks
814
import net.minecraft.client.render.model.BakedModel
15+
import net.minecraft.util.math.BlockPos
16+
import net.minecraft.util.math.Direction
917
import java.awt.Color
1018

1119
object BlockESP : Module(
@@ -37,6 +45,7 @@ object BlockESP : Module(
3745
}
3846
}
3947
}
48+
private val blocks by setting("Blocks", setOf(Blocks.BEDROCK), "Render blocks")
4049

4150
@JvmStatic
4251
val barrier by setting("Solid Barrier Block", true, "Render barrier blocks")
@@ -48,10 +57,33 @@ object BlockESP : Module(
4857
@JvmStatic
4958
val model: BakedModel get() = mc.bakedModelManager.missingModel
5059

51-
private val esp = newChunkedESP(
52-
{ view, pos -> view.getBlockState(pos).block == Blocks.BEDROCK },
53-
{ _, _ -> faceColor to outlineColor }
54-
)
60+
private val esp = newChunkedESP { view, x, y, z ->
61+
val blockPos = BlockPos(x, y, z)
62+
val state = view.getBlockState(blockPos)
63+
if (state.isAir) return@newChunkedESP
64+
if (state.block !in blocks) return@newChunkedESP
65+
66+
val shape = state.getOutlineShape(view, blockPos)
67+
if (shape.isEmpty) return@newChunkedESP
68+
69+
var sides = DirectionMask.ALL
70+
71+
Direction.entries
72+
.filter { blockPos.offset(it).blockState(view).block in blocks }
73+
.forEach { sides = sides.exclude(it.mask) }
74+
75+
shape.boundingBoxes.forEach { box ->
76+
val offsetBox = box.offset(blockPos)
77+
if (drawFaces) build(offsetBox, faceColor, outlineColor, sides, DirectionMask.OutlineMode.AND)
78+
if (drawOutlines) buildOutline(offsetBox, outlineColor, sides, DirectionMask.OutlineMode.AND)
79+
}
80+
}
81+
82+
fun SafeContext.getBlock(x: Int, y: Int, z: Int): Block {
83+
val chunk = world.getChunk(x shr 4, z shr 4)
84+
val section = chunk.getSection(y shr 4)
85+
return section.getBlockState(x and 15, y and 15, z and 15).block
86+
}
5587

5688
init {
5789
onToggle {

common/src/main/kotlin/com/lambda/util/BlockUtils.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import net.minecraft.fluid.FluidState
1111
import net.minecraft.fluid.Fluids
1212
import net.minecraft.item.Item
1313
import net.minecraft.util.math.*
14+
import net.minecraft.world.BlockView
1415
import kotlin.math.floor
1516

1617
object BlockUtils {
@@ -97,9 +98,9 @@ object BlockUtils {
9798

9899
val allSigns = signs + wallSigns + hangingSigns + hangingWallSigns
99100

100-
fun BlockPos.blockState(world: ClientWorld): BlockState = world.getBlockState(this)
101-
fun BlockPos.fluidState(world: ClientWorld): FluidState = world.getFluidState(this)
102-
fun BlockPos.blockEntity(world: ClientWorld) = world.getBlockEntity(this)
101+
fun BlockPos.blockState(world: BlockView): BlockState = world.getBlockState(this)
102+
fun BlockPos.fluidState(world: BlockView): FluidState = world.getFluidState(this)
103+
fun BlockPos.blockEntity(world: BlockView) = world.getBlockEntity(this)
103104
fun SafeContext.instantBreakable(blockState: BlockState, blockPos: BlockPos): Boolean {
104105
val ticksNeeded = 1 / blockState.calcBlockBreakingDelta(player, world, blockPos)
105106
return (ticksNeeded <= 1 && ticksNeeded != 0f) || player.isCreative

0 commit comments

Comments
 (0)