Skip to content

Commit

Permalink
fix: Concurrent Modification Exception and detekt
Browse files Browse the repository at this point in the history
  • Loading branch information
commandblock2 committed Sep 22, 2024
1 parent dd17433 commit aa01fdd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.render.*
import net.ccbluex.liquidbounce.render.engine.Color4b
import net.ccbluex.liquidbounce.render.utils.interpolateHue
import net.ccbluex.liquidbounce.utils.block.getState
import net.ccbluex.liquidbounce.utils.block.toBlockPos
import net.ccbluex.liquidbounce.utils.entity.interpolateCurrentPosition
Expand Down Expand Up @@ -49,10 +50,11 @@ object ModuleProphuntESP :


override fun getColor(param: Any): Color4b =
if (param is TrackedBlock)
freshColor.interpolateHSV(expireColor, param.expirationProgress())
else
if (param is TrackedBlock) {
interpolateHue(freshColor, expireColor, param.expirationProgress())
} else {
freshColor
}

override val parent: ChoiceConfigurable<*>
get() = modes
Expand All @@ -74,13 +76,16 @@ object ModuleProphuntESP :
}

private val trackedBlocks = PriorityQueue<TrackedBlock>()
private val renderTicks by float("RenderTicks",60f, 0f..600f)
private val renderTicks by float("RenderTicks", 60f, 0f..600f)

init {
repeatable {
val currentTIme = mc.world?.time ?: 0
while (trackedBlocks.isNotEmpty() && trackedBlocks.peek().expirationTime <= currentTIme)
trackedBlocks.poll()
synchronized(trackedBlocks) {
while (trackedBlocks.isNotEmpty() && trackedBlocks.peek().expirationTime <= currentTIme) {
trackedBlocks.poll()
}
}

waitTicks(1)
}
Expand All @@ -94,31 +99,70 @@ object ModuleProphuntESP :

@Suppress("unused")
val renderHandler = handler<WorldRenderEvent> { event ->
drawBoxMode(event.matrixStack, this.outline, false)

drawBoxMode(event.matrixStack, event.partialTicks, this.outline, false)
renderEnvironmentForWorld(event.matrixStack) {
drawEntities(this, event.partialTicks, colorMode.activeChoice, true)
}
}


}

fun drawBoxMode(matrixStack: MatrixStack, partialTicks: Float, drawOutline: Boolean, fullAlpha: Boolean): Boolean {
fun drawBoxMode(matrixStack: MatrixStack, drawOutline: Boolean, fullAlpha: Boolean): Boolean {
val colorMode = colorMode.activeChoice

var dirty = false

renderEnvironmentForWorld(matrixStack) {
synchronized(trackedBlocks) {
dirty = drawInternal(this, partialTicks, trackedBlocks, colorMode, fullAlpha, drawOutline)
dirty = drawBlocks(this, trackedBlocks, colorMode, fullAlpha, drawOutline) || dirty
}

}

return dirty
}

private fun WorldRenderEnvironment.drawInternal(
private fun WorldRenderEnvironment.drawEntities(
env: WorldRenderEnvironment,
partialTicks: Float,
colorMode: GenericColorMode<Any>,
drawOutline: Boolean
): Boolean {
var dirty = false

if (renderFallingBlockEntity) {
BoxRenderer.drawWith(env) {
mc.world?.entities?.filterIsInstance<FallingBlockEntity>()?.map {
val dimension = it.getDimensions(it.pose)
val width = dimension.width.toDouble() / 2.0
it to Box(-width, 0.0, -width, width, dimension.height.toDouble(), width)
}?.forEach { (entity, box) ->
val pos = entity.interpolateCurrentPosition(partialTicks)
val color = colorMode.getColor(entity as Any) // which doesn't matter

val baseColor = color.alpha(50)
val outlineColor = color.alpha(100)

withPositionRelativeToCamera(pos) {
drawBox(
box,
baseColor,
outlineColor.takeIf { drawOutline }
)
}

dirty = true
}
}
}

return dirty
}


private fun WorldRenderEnvironment.drawBlocks(
env: WorldRenderEnvironment,
blocks: PriorityQueue<TrackedBlock>,
colorMode: GenericColorMode<Any>,
fullAlpha: Boolean,
Expand All @@ -128,7 +172,7 @@ object ModuleProphuntESP :

BoxRenderer.drawWith(env) {

if (renderBlockUpdates)
if (renderBlockUpdates) {
for (block in blocks) {
val pos = block.pos

Expand All @@ -146,8 +190,9 @@ object ModuleProphuntESP :

val color = colorMode.getColor(block)

if (fullAlpha)
if (fullAlpha) {
color.alpha(255)
}

withPositionRelativeToCamera(vec3d) {
drawBox(
Expand All @@ -159,31 +204,7 @@ object ModuleProphuntESP :

dirty = true
}


if (renderFallingBlockEntity) {
mc.world?.entities?.filterIsInstance<FallingBlockEntity>()?.map {
val dimension = it.getDimensions(it.pose)
val width = dimension.width.toDouble() / 2.0
it to Box(-width, 0.0, -width, width, dimension.height.toDouble(), width)
}?.forEach { (entity, box) ->
val pos = entity.interpolateCurrentPosition(partialTicks)
val color = colorMode.getColor(entity as Any) // which doesn't matter

val baseColor = color.alpha(50)
val outlineColor = color.alpha(100)

withPositionRelativeToCamera(pos) {
drawBox(
box,
baseColor,
outlineColor.takeIf { drawOutline }
)
}
}

}

}

return dirty
Expand All @@ -200,11 +221,15 @@ object ModuleProphuntESP :
return@handler
}

val dirty = drawBoxMode(event.matrixStack, event.partialTicks, drawOutline = false, fullAlpha = true)
var dirty = drawBoxMode(event.matrixStack, drawOutline = false, fullAlpha = true)

if (dirty)
event.markDirty()
renderEnvironmentForWorld(event.matrixStack) {
dirty = drawEntities(this, event.partialTicks, colorMode.activeChoice, true) || dirty
}

if (dirty) {
event.markDirty()
}
}
}

Expand All @@ -218,17 +243,24 @@ object ModuleProphuntESP :
return@handler
}

val dirty = drawBoxMode(event.matrixStack, event.partialTicks, drawOutline = false, fullAlpha = true)
var dirty = drawBoxMode(event.matrixStack, drawOutline = false, fullAlpha = true)

if (dirty)
event.markDirty()
renderEnvironmentForWorld(event.matrixStack) {
dirty = drawEntities(this, event.partialTicks, colorMode.activeChoice, true) || dirty
}

if (dirty) {
event.markDirty()
}
}
}


val networkHandler = handler<PacketEvent> { event ->
if (event.packet is BlockUpdateS2CPacket)
trackedBlocks.offer(TrackedBlock(event.packet.pos, (mc.world?.time ?: 0L) + renderTicks.toLong()))
if (event.packet is BlockUpdateS2CPacket) {
synchronized(trackedBlocks) {
trackedBlocks.offer(TrackedBlock(event.packet.pos, (mc.world?.time ?: 0L) + renderTicks.toLong()))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,4 @@ data class Color4b(val r: Int, val g: Int, val b: Int, val a: Int) {
} else {
alpha((a * fade).toInt())
}

fun interpolateHSV(otherColor: Color4b, percentageOther: Float): Color4b {
val hsv1 = FloatArray(3)
val hsv2 = FloatArray(3)
Color.RGBtoHSB(r, g, b, hsv1)
Color.RGBtoHSB(otherColor.r, otherColor.g, otherColor.b, hsv2)

val h = hsv1[0] + (hsv2[0] - hsv1[0]) * percentageOther
val s = hsv1[1] + (hsv2[1] - hsv1[1]) * percentageOther
val v = hsv1[2] + (hsv2[2] - hsv1[2]) * percentageOther
val alpha = this.a + (otherColor.a - this.a) * percentageOther

val rgb = Color.HSBtoRGB(h, s, v)
return Color4b(
(rgb shr 16) and 0xFF,
(rgb shr 8) and 0xFF,
rgb and 0xFF,
alpha.toInt()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,23 @@ fun shiftHue(color4b: Color4b, shift: Int): Color4b {

return Color4b(shiftedColor).alpha(color4b.a)
}

fun interpolateHue(primaryColor: Color4b, otherColor: Color4b, percentageOther: Float): Color4b {
val hsb1 = FloatArray(3)
val hsb2 = FloatArray(3)
Color.RGBtoHSB(primaryColor.r,primaryColor.g, primaryColor.b, hsb1)
Color.RGBtoHSB(otherColor.r, otherColor.g, otherColor.b, hsb2)

val h = hsb1[0] + (hsb2[0] - hsb1[0]) * percentageOther
val s = hsb1[1] + (hsb2[1] - hsb1[1]) * percentageOther
val v = hsb1[2] + (hsb2[2] - hsb1[2]) * percentageOther
val alpha = primaryColor.a + (otherColor.a - primaryColor.a) * percentageOther

val rgb = Color.HSBtoRGB(h, s, v)
return Color4b(
(rgb shr 16) and 0xFF,
(rgb shr 8) and 0xFF,
rgb and 0xFF,
alpha.toInt()
)
}

0 comments on commit aa01fdd

Please sign in to comment.