Skip to content

Commit bce4ce9

Browse files
committed
refactor: fit new bind system
- refactor: use `int[]` for `mouseStates`
1 parent dd71618 commit bce4ce9

File tree

6 files changed

+85
-22
lines changed

6 files changed

+85
-22
lines changed

src-theme/src/integration/events.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,13 @@ export interface KeyEvent {
4444
}
4545

4646
export interface KeyBindingEvent {
47-
key: {
48-
code: number;
49-
name: string;
50-
};
47+
key: string;
5148
action: number;
5249
mods: number;
5350
}
5451

5552
export interface KeyBindingCPSEvent {
56-
key: {
57-
code: number;
58-
name: string;
59-
};
53+
key: string;
6054
cps: number;
6155
}
6256

src-theme/src/routes/hud/elements/keystrokes/Key.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import {listen} from "../../../../integration/ws";
3-
import type {KeyEvent, KeyCPSEvent} from "../../../../integration/events";
3+
import type {KeyBindingEvent, KeyBindingCPSEvent} from "../../../../integration/events";
44
import type {MinecraftKeybind} from "../../../../integration/types";
55
66
export let flexBasis: string = '50px';
@@ -11,7 +11,7 @@
1111
1212
let cps = 0;
1313
14-
listen("key", (e: KeyEvent) => {
14+
listen("keybinding", (e: KeyBindingEvent) => {
1515
if (e.key !== key?.key.translationKey) {
1616
return;
1717
}
@@ -20,7 +20,7 @@
2020
});
2121
2222
if (showCPS) {
23-
listen("keyCPS", (e: KeyCPSEvent) => {
23+
listen("keybindingCPS", (e: KeyBindingCPSEvent) => {
2424
if (e.key !== key?.key.translationKey) {
2525
return;
2626
}

src/main/kotlin/net/ccbluex/liquidbounce/LiquidBounce.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ object LiquidBounce : Listenable {
133133

134134
ChunkScanner
135135
WorldChangeNotifier
136-
MouseStateTracker
137136
InputTracker
138137

139138
// Features

src/main/kotlin/net/ccbluex/liquidbounce/event/events/GameEvents.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ class GameTickEvent : Event()
3737
@WebSocketEvent
3838
class KeyEvent(val key: InputUtil.Key, val action: Int) : Event()
3939

40-
@Nameable("keyCPS")
41-
@WebSocketEvent
42-
class KeyCPSEvent(val key: InputUtil.Key, val cps: Int) : Event()
43-
4440
// Input events
4541
@Nameable("inputHandle")
4642
class InputHandleEvent : Event()
@@ -55,6 +51,14 @@ class MouseRotationEvent(var cursorDeltaX: Double, var cursorDeltaY: Double) : C
5551
@WebSocketEvent
5652
class KeybindChangeEvent : Event()
5753

54+
@Nameable("keybinding")
55+
@WebSocketEvent
56+
class KeyBindingEvent(val key: InputUtil.Key, val action: Int, val mods: Int) : Event()
57+
58+
@Nameable("keybindingCPS")
59+
@WebSocketEvent
60+
class KeyBindingCPSEvent(val key: InputUtil.Key, val cps: Int) : Event()
61+
5862
@Nameable("useCooldown")
5963
class UseCooldownEvent(var cooldown: Int) : Event()
6064

src/main/kotlin/net/ccbluex/liquidbounce/utils/input/InputTracker.kt

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919

2020
package net.ccbluex.liquidbounce.utils.input
2121

22+
import net.ccbluex.liquidbounce.event.EventManager
2223
import net.ccbluex.liquidbounce.event.Listenable
23-
import net.ccbluex.liquidbounce.event.events.MouseButtonEvent
24+
import net.ccbluex.liquidbounce.event.events.*
2425
import net.ccbluex.liquidbounce.event.handler
2526
import net.ccbluex.liquidbounce.utils.client.mc
2627
import net.minecraft.client.option.KeyBinding
2728
import net.minecraft.client.util.InputUtil
29+
import net.minecraft.client.util.InputUtil.Type.KEYSYM
30+
import net.minecraft.client.util.InputUtil.Type.MOUSE
2831
import org.lwjgl.glfw.GLFW
2932

3033
/**
@@ -34,8 +37,71 @@ import org.lwjgl.glfw.GLFW
3437
*/
3538
object 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
}

src/main/kotlin/net/ccbluex/liquidbounce/web/persistant/PersistentLocalStorage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import net.ccbluex.liquidbounce.config.Configurable
2424

2525
object PersistentLocalStorage : Configurable("storage") {
2626

27-
var map by value("map", mutableMapOf<String, String>())
27+
val map by value("map", mutableMapOf<String, String>())
2828

2929
init {
3030
ConfigSystem.root(this)

0 commit comments

Comments
 (0)