[cleanup] Cleaned up WaypointRender code a bit

This commit is contained in:
Xiaro 2020-11-29 22:52:46 -05:00
parent 07657c2446
commit 7093f392c8
No known key found for this signature in database
GPG Key ID: 996D265D6E155377
2 changed files with 30 additions and 28 deletions

View File

@ -10,6 +10,7 @@ import me.zeroeightsix.kami.manager.Manager
import me.zeroeightsix.kami.util.ConfigUtils
import me.zeroeightsix.kami.util.Wrapper
import me.zeroeightsix.kami.util.math.CoordinateConverter
import me.zeroeightsix.kami.util.math.CoordinateConverter.asString
import me.zeroeightsix.kami.util.math.VectorUtils.toBlockPos
import net.minecraft.util.math.BlockPos
import java.io.*
@ -181,7 +182,7 @@ object WaypointManager : Manager {
private fun genID(): Int = waypoints.lastOrNull()?.id?.plus(1) ?: 0
override fun toString() = currentPos().let { "${it.x}, ${it.y}, ${it.z}" }
override fun toString() = currentPos().asString()
override fun equals(other: Any?): Boolean {
if (this === other) return true

View File

@ -23,11 +23,12 @@ import kotlin.math.roundToInt
import kotlin.math.sqrt
@Module.Info(
name = "WaypointRender",
description = "Render saved waypoints",
category = Module.Category.RENDER
name = "WaypointRender",
description = "Render saved waypoints",
category = Module.Category.RENDER
)
object WaypointRender : Module() {
private val page = register(Settings.e<Page>("Page", Page.INFO_BOX))
/* Page one */
@ -62,9 +63,9 @@ object WaypointRender : Module() {
}
// This has to be sorted so the further ones doesn't overlaps the closer ones
private val waypointMap = TreeMap<BlockPos, TextComponent>(compareByDescending {
it.distanceSq(mc.player?.position
?: BlockPos(0, -69420, 0))
private val waypointMap = TreeMap<Waypoint, TextComponent>(compareByDescending {
it.pos.distanceSq(mc.player?.position
?: BlockPos(0, -69420, 0))
})
private var currentServer: String? = null
private var timer = TimerUtils.TickTimer(TimerUtils.TimeUnit.SECONDS)
@ -81,11 +82,11 @@ object WaypointRender : Module() {
renderer.aTracer = if (tracer.value) aTracer.value else 0
renderer.thickness = thickness.value
GlStateUtils.depth(false)
for (pos in waypointMap.keys) {
val distance = sqrt(mc.player.getDistanceSq(pos))
for (waypoint in waypointMap.keys) {
val distance = sqrt(mc.player.getDistanceSq(waypoint.pos))
if (espRangeLimit.value && distance > espRange.value) continue
renderer.add(AxisAlignedBB(pos), color) /* Adds pos to ESPRenderer list */
drawVerticalLines(pos, color, aOutline.value) /* Draw lines from y 0 to y 256 */
renderer.add(AxisAlignedBB(waypoint.pos), color) /* Adds pos to ESPRenderer list */
drawVerticalLines(waypoint.pos, color, aOutline.value) /* Draw lines from y 0 to y 256 */
}
GlStateUtils.depth(true)
renderer.render(true)
@ -94,7 +95,7 @@ object WaypointRender : Module() {
private fun drawVerticalLines(pos: BlockPos, color: ColorHolder, a: Int) {
val box = AxisAlignedBB(pos.x.toDouble(), 0.0, pos.z.toDouble(),
pos.x + 1.0, 256.0, pos.z + 1.0)
pos.x + 1.0, 256.0, pos.z + 1.0)
KamiTessellator.begin(GL_LINES)
KamiTessellator.drawOutline(box, color, a, GeometryMasks.Quad.ALL, thickness.value)
KamiTessellator.render()
@ -104,10 +105,10 @@ object WaypointRender : Module() {
listener<RenderOverlayEvent> {
if (waypointMap.isEmpty() || !showCoords.value && !showName.value && !showDate.value && !showDist.value) return@listener
GlStateUtils.rescaleActual()
for ((pos, textComponent) in waypointMap) {
val distance = sqrt(mc.player.getDistanceSqToCenter(pos))
for ((waypoint, textComponent) in waypointMap) {
val distance = sqrt(mc.player.getDistanceSqToCenter(waypoint.pos))
if (distance > infoBoxRange.value) continue
drawText(pos, textComponent, distance.roundToInt())
drawText(waypoint.pos, textComponent, distance.roundToInt())
}
GlStateUtils.rescaleMc()
}
@ -145,7 +146,6 @@ object WaypointRender : Module() {
init {
listener<SafeTickEvent> {
if (WaypointManager.genDimension() != prevDimension || timer.tick(10L, false)) {
if (WaypointManager.genDimension() != prevDimension) waypointMap.clear()
updateList()
}
}
@ -154,12 +154,10 @@ object WaypointRender : Module() {
// This could be called from another thread so we have to synchronize the map
synchronized(lockObject) {
when (it.type) {
WaypointUpdateEvent.Type.ADD -> it.waypoint?.let { updateTextComponent(it) }
WaypointUpdateEvent.Type.REMOVE -> waypointMap.remove(it.waypoint?.pos)
WaypointUpdateEvent.Type.ADD -> updateTextComponent(it.waypoint)
WaypointUpdateEvent.Type.REMOVE -> waypointMap.remove(it.waypoint)
WaypointUpdateEvent.Type.CLEAR -> waypointMap.clear()
WaypointUpdateEvent.Type.RELOAD -> {
waypointMap.clear(); updateList()
}
WaypointUpdateEvent.Type.RELOAD -> updateList()
else -> {
}
}
@ -175,20 +173,23 @@ object WaypointRender : Module() {
timer.reset()
prevDimension = WaypointManager.genDimension()
if (currentServer == null) {
waypointMap.clear()
currentServer = WaypointManager.genServer()
}
val cacheList = WaypointManager.waypoints.filter { (it.server == null || it.server == currentServer) && (dimension.value == Dimension.ANY || it.dimension == prevDimension) }
waypointMap.keys.removeIf { pos -> cacheList.firstOrNull { it.pos == pos } != null }
val cacheList = WaypointManager.waypoints.filter {
(it.server == null || it.server == currentServer)
&& (dimension.value == Dimension.ANY || it.dimension == prevDimension)
}
waypointMap.clear()
for (waypoint in cacheList) updateTextComponent(waypoint)
}
private fun updateTextComponent(waypoint: Waypoint) {
private fun updateTextComponent(waypoint: Waypoint?) {
if (waypoint == null) return
// Don't wanna update this continuously
waypointMap.computeIfAbsent(waypoint.pos) {
waypointMap.computeIfAbsent(waypoint) {
TextComponent().apply {
if (showName.value) addLine(waypoint.name)
if (showDate.value) addLine(waypoint.date)
@ -199,7 +200,7 @@ object WaypointRender : Module() {
init {
with(Setting.SettingListeners {
synchronized(lockObject) { waypointMap.clear(); updateList() } // This could be called from another thread so we have to synchronize the map
synchronized(lockObject) { updateList() } // This could be called from another thread so we have to synchronize the map
}) {
dimension.settingListener = this
showName.settingListener = this