1919
2020package net.ccbluex.liquidbounce.utils.input
2121
22+ import net.ccbluex.liquidbounce.event.EventManager
2223import net.ccbluex.liquidbounce.event.Listenable
23- import net.ccbluex.liquidbounce.event.events.MouseButtonEvent
24+ import net.ccbluex.liquidbounce.event.events.*
2425import net.ccbluex.liquidbounce.event.handler
2526import net.ccbluex.liquidbounce.utils.client.mc
2627import net.minecraft.client.option.KeyBinding
2728import net.minecraft.client.util.InputUtil
29+ import net.minecraft.client.util.InputUtil.Type.KEYSYM
30+ import net.minecraft.client.util.InputUtil.Type.MOUSE
2831import org.lwjgl.glfw.GLFW
2932
3033/* *
@@ -34,8 +37,71 @@ import org.lwjgl.glfw.GLFW
3437 */
3538object InputTracker : Listenable {
3639
37- // Tracks the state of each mouse button (pressed or not).
38- private val mouseStates = mutableMapOf<Int , Boolean >()
40+ private val trackers = listOf (
41+ KeyBindingTracker (mc.options.forwardKey),
42+ KeyBindingTracker (mc.options.backKey),
43+ KeyBindingTracker (mc.options.leftKey),
44+ KeyBindingTracker (mc.options.rightKey),
45+ KeyBindingTracker (mc.options.jumpKey),
46+ KeyBindingTracker (mc.options.attackKey),
47+ KeyBindingTracker (mc.options.useKey),
48+ )
49+
50+ override fun children (): List <Listenable > = trackers
51+
52+ // Tracks CPS
53+ class KeyBindingTracker internal constructor(val keyBinding : KeyBinding ) : Listenable {
54+ // Records clicks in latest 20 ticks (1 sec)
55+ private val countByTick = IntArray (20 )
56+ private var tickIndex = 0
57+ private var currentCount = 0
58+
59+ // Sum of countByTick
60+ var cps = 0
61+ private set
62+
63+ var pressed = false
64+ private set(value) {
65+ if (value) {
66+ currentCount++
67+ }
68+ field = value
69+ }
70+
71+ @Suppress(" NOTHING_TO_INLINE" )
72+ private inline fun setPressed (action : Int ) {
73+ when (action) {
74+ GLFW .GLFW_RELEASE -> pressed = false
75+ GLFW .GLFW_PRESS -> pressed = true
76+ }
77+ }
78+
79+ val keyHandler = handler<KeyboardKeyEvent > {
80+ if (keyBinding.boundKey.category == KEYSYM && keyBinding.boundKey.code == it.key.code) {
81+ setPressed(it.action)
82+ EventManager .callEvent(KeyBindingEvent (keyBinding.boundKey, it.action, it.mods))
83+ }
84+ }
85+
86+ val mouseHandler = handler<MouseButtonEvent > {
87+ if (keyBinding.boundKey.category == MOUSE && keyBinding.boundKey.code == it.button) {
88+ setPressed(it.action)
89+ EventManager .callEvent(KeyBindingEvent (keyBinding.boundKey, it.action, it.mods))
90+ }
91+ }
92+
93+ val tickHandler = handler<PlayerTickEvent > {
94+ cps - = countByTick[tickIndex]
95+ countByTick[tickIndex] = currentCount
96+ cps + = currentCount
97+ currentCount = 0
98+ tickIndex = (tickIndex + 1 ) % countByTick.size
99+ EventManager .callEvent(KeyBindingCPSEvent (keyBinding.boundKey, cps))
100+ }
101+ }
102+
103+ // Tracks the state of each mouse button.
104+ private val mouseStates = IntArray (32 )
39105
40106 /* *
41107 * Extension property that checks if a key binding is pressed on either the keyboard or mouse.
@@ -68,7 +134,7 @@ object InputTracker : Listenable {
68134 */
69135 @Suppress(" unused" )
70136 private val handleMouseAction = handler<MouseButtonEvent > {
71- mouseStates[it.button] = it.action == GLFW . GLFW_PRESS
137+ mouseStates[it.button] = it.action
72138 }
73139
74140 /* *
@@ -77,5 +143,5 @@ object InputTracker : Listenable {
77143 * @param button The GLFW code of the mouse button.
78144 * @return True if the mouse button is pressed, false otherwise.
79145 */
80- fun isMouseButtonPressed (button : Int ): Boolean = mouseStates.getOrDefault( button, false )
146+ fun isMouseButtonPressed (button : Int ): Boolean = mouseStates[ button] == GLFW . GLFW_PRESS
81147}
0 commit comments