aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/inventory/SaveCursorPosition.kt
blob: c47867b311fafb6417bec2d662ea656b8ee187c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package moe.nea.firmament.features.inventory

import kotlin.math.absoluteValue
import kotlin.time.Duration.Companion.milliseconds
import net.minecraft.client.util.InputUtil
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.TimeMark
import moe.nea.firmament.util.assertNotNullOr

object SaveCursorPosition : FirmamentFeature {
    override val identifier: String
        get() = "save-cursor-position"

    object TConfig : ManagedConfig(identifier, Category.INVENTORY) {
        val enable by toggle("enable") { true }
        val tolerance by duration("tolerance", 10.milliseconds, 5000.milliseconds) { 500.milliseconds }
    }

    override val config: TConfig
        get() = TConfig

    var savedPositionedP1: Pair<Double, Double>? = null
    var savedPosition: SavedPosition? = null

    data class SavedPosition(
        val middle: Pair<Double, Double>,
        val cursor: Pair<Double, Double>,
        val savedAt: TimeMark = TimeMark.now()
    )

    @JvmStatic
    fun saveCursorOriginal(positionedX: Double, positionedY: Double) {
        savedPositionedP1 = Pair(positionedX, positionedY)
    }

    @JvmStatic
    fun loadCursor(middleX: Double, middleY: Double): Pair<Double, Double>? {
        if (!TConfig.enable) return null
        val lastPosition = savedPosition?.takeIf { it.savedAt.passedTime() < TConfig.tolerance }
        savedPosition = null
        if (lastPosition != null &&
            (lastPosition.middle.first - middleX).absoluteValue < 1 &&
            (lastPosition.middle.second - middleY).absoluteValue < 1
        ) {
            InputUtil.setCursorParameters(
                MC.window.handle,
                InputUtil.GLFW_CURSOR_NORMAL,
                lastPosition.cursor.first,
                lastPosition.cursor.second
            )
            return lastPosition.cursor
        }
        return null
    }

    @JvmStatic
    fun saveCursorMiddle(middleX: Double, middleY: Double) {
        if (!TConfig.enable) return
        val cursorPos = assertNotNullOr(savedPositionedP1) { return }
        savedPosition = SavedPosition(Pair(middleX, middleY), cursorPos)
    }
}