-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.lua
More file actions
125 lines (109 loc) · 3.7 KB
/
client.lua
File metadata and controls
125 lines (109 loc) · 3.7 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
local RSGCore = exports['rsg-core']:GetCoreObject()
local Config = require 'config'
local activeBag = nil
---@param model string The model name of the handbag to attach
---@return number|nil The entity handle of the created prop, or nil if failed
local function attachHandbag(model)
-- Delete any existing bag first
if activeBag and DoesEntityExist(activeBag) then
DeleteObject(activeBag)
activeBag = nil
end
local player = PlayerPedId()
local coords = GetEntityCoords(player)
-- Request and load the model
local modelHash = GetHashKey(model)
RequestModel(modelHash)
-- Wait for model to load with a timeout
local timeout = 500
while not HasModelLoaded(modelHash) and timeout > 0 do
Wait(10)
timeout = timeout - 10
end
-- Check if model loaded successfully
if not HasModelLoaded(modelHash) then
lib.notify({
title = Config.Notifications.error.title,
description = Config.Notifications.error.modelLoad,
type = 'error'
})
return nil
end
-- Create the object
local prop = CreateObject(modelHash, coords.x, coords.y, coords.z, true, true, true)
if not DoesEntityExist(prop) then
lib.notify({
title = Config.Notifications.error.title,
description = Config.Notifications.error.createBag,
type = 'error'
})
return nil
end
-- Set as mission entity to prevent automatic cleanup
SetEntityAsMissionEntity(prop, true, true)
-- Build attachment data from config
local attachData = {}
for _, handbag in ipairs(Config.Handbags) do
attachData[handbag.model] = {
bone = handbag.bone,
pos = handbag.pos,
rot = handbag.rot
}
end
-- Get attachment data for the model
local data = attachData[model]
if not data then
lib.notify({
title = Config.Notifications.error.title,
description = Config.Notifications.error.unknownModel,
type = 'error'
})
DeleteObject(prop)
return nil
end
-- Attach the object to the player
local boneIndex = GetEntityBoneIndexByName(player, data.bone)
AttachEntityToEntity(
prop, player, boneIndex,
data.pos.x, data.pos.y, data.pos.z,
data.rot.x, data.rot.y, data.rot.z,
true, true, false, true, 1, true
)
return prop
end
---@param bagType string The type of handbag to equip
RegisterNetEvent('bs-handbags:client:toggleBag', function(modelName)
-- If we already have a bag equipped
if activeBag and DoesEntityExist(activeBag) then
-- Remove the current bag
DeleteObject(activeBag)
activeBag = nil
lib.notify({
title = Config.Notifications.success.title,
description = Config.Notifications.success.stowed,
type = 'inform'
})
else
-- Equip the new bag
activeBag = attachHandbag(modelName)
if activeBag then
lib.notify({
title = Config.Notifications.success.title,
description = Config.Notifications.success.equipped,
type = 'success'
})
end
end
end)
-- Handle resource stop to clean up props
AddEventHandler('onResourceStop', function(resourceName)
if resourceName ~= GetCurrentResourceName() then return end
if activeBag and DoesEntityExist(activeBag) then
DeleteObject(activeBag)
activeBag = nil
end
end)
-- Update core object when it changes
RegisterNetEvent('RSGCore:Client:UpdateObject', function()
RSGCore = exports['rsg-core']:GetCoreObject()
end)