Skip to content

Commit 695cd92

Browse files
committed
Bug Fixing
1 parent e00d75b commit 695cd92

File tree

256 files changed

+596
-421
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+596
-421
lines changed

documentation/docs/libraries/lia.keybind.md

Lines changed: 123 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
# Keybind Library
22

3-
This page describes functions for registering and managing custom keybinds.
3+
This page describes functions for registering and managing custom keybinds in the Lilia framework.
44

55
---
66

77
## Overview
88

9-
The keybind library runs **client-side** and stores user-defined key bindings in `lia.keybind.stored`. Bindings are saved to `data/lilia/keybinds/<gamemode>/<server-ip>.json` (using the server IP with dots replaced by underscores) and loaded automatically on start. If a legacy `.txt` file exists it is migrated to the new JSON format. Press and release callbacks are dispatched through `PlayerButtonDown` and `PlayerButtonUp`, and a “Keybinds” page is added to the configuration menu via `PopulateConfigurationButtons`. Editing in this menu can be disabled with the `AllowKeybindEditing` configuration option.
9+
The keybind library runs **client-side** and stores user-defined key bindings in `lia.keybind.stored`. Bindings are saved to `data/lilia/keybinds/<gamemode>/<server-ip>.json` (using the server IP with dots replaced by underscores) and loaded automatically on start. If a legacy `.txt` file exists it is migrated to the new JSON format.
10+
11+
Keybinds are triggered through `PlayerButtonDown` and `PlayerButtonUp` hooks, and a "Keybinds" page is added to the configuration menu via `PopulateConfigurationButtons`. Editing in this menu can be disabled with the `AllowKeybindEditing` configuration option.
1012

1113
Each entry in `lia.keybind.stored` contains:
1214

13-
* `default` (*number*) – key code assigned on registration.
14-
* `value` (*number*) – current key code (initially the default).
15-
* `callback` (*function | nil*) – invoked when the key is pressed. The player is passed as the sole argument.
16-
* `release` (*function | nil*) – invoked when the key is released. The player is passed as the sole argument.
15+
* `default` (*number*) – key code assigned on registration
16+
* `value` (*number*) – current key code (initially the default)
17+
* `callback` (*function | nil*) – invoked when the key is pressed
18+
* `release` (*function | nil*) – invoked when the key is released
19+
* `shouldRun` (*function | nil*) – optional validation function that must return true for the keybind to execute
20+
* `serverOnly` (*boolean | nil*) – if true, the callback runs server-side via networking
1721

18-
Numeric key codes are also mapped back to their action identifiers for reverse lookup. The library registers some actions by default (e.g. `openInventory` and `adminMode`) but leaves them unbound.
22+
The library also maintains reverse mappings from key codes to action identifiers for efficient lookup.
1923

2024
---
2125

26+
## Functions
27+
2228
### lia.keybind.add
2329

2430
**Purpose**
2531

26-
Register a keybind action and optional callbacks. The default key is stored and a reverse mapping from key code to action is created. Existing user selections are preserved.
32+
Register a keybind action with callbacks and optional validation. The default key is stored and a reverse mapping from key code to action is created.
2733

2834
**Parameters**
2935

3036
* `k` (*string | number*): Key identifier. A string is matched case-insensitively against the internal key map. Invalid keys abort registration.
31-
* `d` (*string*): Action identifier.
32-
* `cb` (*function | nil*): Called when the key is pressed. Receives the player as its only argument. Optional.
33-
* `rcb` (*function | nil*): Called when the key is released. Receives the player as its only argument. Optional.
37+
* `d` (*string*): Action identifier (will be localized using `L()` function).
38+
* `cb` (*table*): Callback table containing:
39+
* `onPress` (*function*): Called when the key is pressed. Receives the player as its only argument.
40+
* `onRelease` (*function | nil*): Called when the key is released. Receives the player as its only argument. Optional.
41+
* `shouldRun` (*function | nil*): Validation function that must return true for the keybind to execute. Optional.
42+
* `serverOnly` (*boolean | nil*): If true, the callback runs server-side via networking. Optional.
3443

3544
**Realm**
3645

@@ -45,19 +54,43 @@ Register a keybind action and optional callbacks. The default key is stored and
4554
```lua
4655
-- Bind F1 to open the inventory while held
4756
local inv
48-
lia.keybind.add(
49-
KEY_F1,
50-
"Open Inventory",
51-
function(client)
57+
lia.keybind.add(KEY_F1, "Open Inventory", {
58+
shouldRun = function(client)
59+
-- Check if player can perform this action
60+
return client:IsValid() and not client:IsDead()
61+
end,
62+
63+
onPress = function(client)
5264
inv = vgui.Create("liaMenu")
53-
inv:setActiveTab("Inventory")
65+
inv:setActiveTab("inv")
5466
end,
55-
function(client)
67+
68+
onRelease = function(client)
5669
if IsValid(inv) then
5770
inv:Close()
5871
end
5972
end
60-
)
73+
})
74+
75+
-- Simple keybind with only onPress
76+
lia.keybind.add(KEY_F2, "Simple Action", {
77+
onPress = function(client)
78+
client:ChatPrint("F2 pressed!")
79+
end
80+
})
81+
82+
-- Server-side keybind
83+
lia.keybind.add(KEY_F3, "Server Action", {
84+
shouldRun = function(client)
85+
return client:IsValid() and client:hasPermission("admin")
86+
end,
87+
88+
onPress = function(client)
89+
client:addMoney(100)
90+
end,
91+
92+
serverOnly = true
93+
})
6194
```
6295

6396
---
@@ -84,7 +117,7 @@ Fetch the key code for a keybind action. It checks the current value, then the r
84117
**Example Usage**
85118

86119
```lua
87-
local invKey = lia.keybind.get("Open Inventory", KEY_I)
120+
local invKey = lia.keybind.get("openInventory", KEY_I)
88121
print("Inventory key:", input.GetKeyName(invKey or KEY_NONE))
89122
```
90123

@@ -145,3 +178,74 @@ end)
145178
-- Reload keybinds
146179
lia.keybind.load()
147180
```
181+
182+
---
183+
184+
## Key Mapping
185+
186+
The library provides a comprehensive mapping of key names to key codes, including:
187+
188+
* **Alphanumeric keys**: `a`, `b`, `c`, etc.
189+
* **Function keys**: `f1`, `f2`, `f3`, etc.
190+
* **Special keys**: `space`, `enter`, `tab`, `escape`, etc.
191+
* **Arrow keys**: `up`, `down`, `left`, `right`
192+
* **Modifier keys**: `lshift`, `rshift`, `lctrl`, `rctrl`, `lalt`, `ralt`
193+
* **Numpad keys**: `kp_0`, `kp_1`, `kp_plus`, `kp_minus`, etc.
194+
195+
Keys can be specified either as strings (e.g., `"f1"`) or as constants (e.g., `KEY_F1`).
196+
197+
---
198+
199+
## Server-Side Execution
200+
201+
When `serverOnly` is set to true in a keybind callback, the action is executed server-side via networking:
202+
203+
1. The client sends a `liaKeybindServer` net message with the action name and player entity
204+
2. The server receives the message and executes the callback function
205+
3. The same process occurs for release actions with `_release` suffix
206+
207+
This allows server-side validation and execution while maintaining the responsive feel of client-side keybinds.
208+
209+
---
210+
211+
## Configuration Menu
212+
213+
The keybind system automatically adds a "Keybinds" page to the configuration menu (`PopulateConfigurationButtons` hook). This page provides:
214+
215+
* A searchable list of all registered keybinds
216+
* Dropdown menus to change key assignments
217+
* Unbind buttons to remove key assignments
218+
* A reset button to restore all default keybinds
219+
* Automatic conflict detection (prevents multiple actions on the same key)
220+
221+
The editing functionality can be disabled by setting `lia.config.get("AllowKeybindEditing", false)`.
222+
223+
---
224+
225+
## Default Keybinds
226+
227+
The library registers several default keybinds:
228+
229+
* `openInventory` - Opens the F1 menu with inventory tab active
230+
* `adminMode` - Switches to/from staff character mode (server-side only)
231+
232+
These default keybinds are initially unbound (`KEY_NONE`) and must be assigned by the user.
233+
234+
---
235+
236+
## Hooks
237+
238+
* `InitializedKeybinds` - Fired after keybinds are loaded and initialized
239+
240+
---
241+
242+
## File Storage
243+
244+
Keybinds are stored in JSON format at:
245+
```
246+
data/lilia/keybinds/<gamemode>/<server-ip>.json
247+
```
248+
249+
Where `<server-ip>` has dots replaced with underscores (e.g., `192_168_1_1.json`).
250+
251+
Legacy `.txt` files are automatically migrated to the new JSON format and then deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"6.513"}
1+
{"version":"6.467"}

gamemode/cl_init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
DeriveGamemode("sandbox")
2-
include("shared.lua")
1+
DeriveGamemode("sandbox")
2+
include("shared.lua")

gamemode/core/derma/f1menu/cl_classes.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,4 @@ function PANEL:addJoinButton(parent, cl, canBe)
197197
end
198198
end
199199

200-
vgui.Register("liaClasses", PANEL, "EditablePanel")
200+
vgui.Register("liaClasses", PANEL, "EditablePanel")

gamemode/core/derma/f1menu/cl_information.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,4 @@ function PANEL:setup()
163163
end
164164
end
165165

166-
vgui.Register("liaCharInfo", PANEL, "EditablePanel")
166+
vgui.Register("liaCharInfo", PANEL, "EditablePanel")

gamemode/core/derma/f1menu/cl_menu.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ function PANEL:setActiveTab(key)
197197
if IsValid(tab) then
198198
tab:DoClick()
199199
tab:SetSelected(true)
200+
return
201+
end
202+
203+
for _, tabPanel in pairs(self.tabList) do
204+
if IsValid(tabPanel) and tabPanel:GetText() == key then
205+
tabPanel:DoClick()
206+
tabPanel:SetSelected(true)
207+
return
208+
end
200209
end
201210
end
202211

@@ -240,4 +249,4 @@ function PANEL:Paint()
240249
lia.util.drawBlackBlur(self, 1, 4, 255, 220)
241250
end
242251

243-
vgui.Register("liaMenu", PANEL, "EditablePanel")
252+
vgui.Register("liaMenu", PANEL, "EditablePanel")

gamemode/core/derma/mainmenu/bg_music.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ function PANEL:OnRemove()
5252
end)
5353
end
5454

55-
vgui.Register("liaCharBGMusic", PANEL, "DPanel")
55+
vgui.Register("liaCharBGMusic", PANEL, "DPanel")

gamemode/core/derma/mainmenu/character.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,4 +808,4 @@ function PANEL:Think()
808808
end
809809
end
810810

811-
vgui.Register("liaCharacter", PANEL, "EditablePanel")
811+
vgui.Register("liaCharacter", PANEL, "EditablePanel")

gamemode/core/derma/mainmenu/confirmation.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ function PANEL:onCancel(cb)
8787
return self
8888
end
8989

90-
vgui.Register("liaCharacterConfirm", PANEL, "SemiTransparentDFrame")
90+
vgui.Register("liaCharacterConfirm", PANEL, "SemiTransparentDFrame")

gamemode/core/derma/mainmenu/creation.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,4 @@ function PANEL:Init()
270270
timer.Simple(0.5, function() hook.Run("ModifyCharacterModel", self.model:GetEntity()) end)
271271
end
272272

273-
vgui.Register("liaCharacterCreation", PANEL, "EditablePanel")
273+
vgui.Register("liaCharacterCreation", PANEL, "EditablePanel")

0 commit comments

Comments
 (0)