@@ -4,45 +4,60 @@ import net.minecraft.client.MinecraftClient
44import net.minecraft.client.gui.DrawContext
55import net.minecraft.text.Text
66import java.awt.Color
7+ import java.util.concurrent.Executors
8+ import java.util.concurrent.TimeUnit
79
810object TextRender {
911 private var scale = 1.0f
1012 private val color = Color (255 , 255 , 0 , 255 ).rgb // White
11- private var cachedTitle: String = " No Music"
12- private var tickCounter = 0
13+
14+ @Volatile
15+ private var cachedTitle: String? = null
16+
17+ // Create a single background thread to handle the heavy querying
18+ private val executor = Executors .newSingleThreadScheduledExecutor { r ->
19+ val t = Thread (r, " NeteaseMusicQueryThread" )
20+ t.isDaemon = true // Ensure thread closes when the game closes
21+ t
22+ }
23+
24+ init {
25+ // Schedule the query to run every 1 second (initial delay 0)
26+ executor.scheduleAtFixedRate({
27+ try {
28+ // This heavy work now happens off the main thread
29+ val musicTitle = CloudMusicHelper .getCloudMusicTitle()
30+ cachedTitle = musicTitle
31+ } catch (e: Exception ) {
32+ e.printStackTrace()
33+ }
34+ }, 0 , 1 , TimeUnit .SECONDS )
35+ }
1336
1437 fun onRender (context : DrawContext ) {
15- if (CloudMusicHelper .getCloudMusicTitle() == null ) return
16- tickCounter++
17- if (tickCounter >= 20 ) { // Update every 1 second (20 ticks)
18- tickCounter = 0
19- // Run in background thread if you notice lag, though usually this is fast enough
20- val musicTitle = CloudMusicHelper .getCloudMusicTitle()
21- cachedTitle = musicTitle ? : " No Music Playing"
22- }
38+ // 1. Instant check: If title is null (e.g. not Windows or not playing), stop rendering.
39+ // We read the variable directly; no heavy calculation here.
40+ val titleToRender = cachedTitle ? : return
2341
2442 val client = MinecraftClient .getInstance()
2543 val textRenderer = client.textRenderer
2644
27- // 1 . Define your text first (needed to calculate width)
28- val text = Text .literal(" Playing: $cachedTitle " )
45+ // 2 . Define your text
46+ val text = Text .literal(" Playing: $titleToRender " )
2947
30- // 2. Calculate the Right Edge
31- // We divide scaledWidth by your 'scale' var to match the matrix scaling
48+ // 3. Calculate Dimensions (Standard Rendering Logic)
3249 val screenWidth = client.window.scaledWidth / scale
3350 val maxBoxWidth = 200
34- val padding = 10 // Space from the edge
51+ val padding = 10
3552 val fullTextWidth = textRenderer.getWidth(text)
3653 val currentBoxWidth = fullTextWidth.coerceAtMost(maxBoxWidth)
3754
38- // 3. Calculate Coordinates
39- // X = Far Right - Text Size - Padding
4055 val x = (screenWidth - currentBoxWidth - padding).toInt()
41- val y = 3 // Distance from top
56+ val y = 3
4257
4358 // 4. Render
4459 context.matrices.pushMatrix()
45- context.matrices.scale(scale, scale) // 2 args for 1.21.10
60+ context.matrices.scale(scale, scale)
4661
4762 RenderUtils .drawScrollingText(context, text, x, y, currentBoxWidth, color)
4863
0 commit comments