-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.lua
More file actions
167 lines (137 loc) · 6.18 KB
/
Copy pathclient.lua
File metadata and controls
167 lines (137 loc) · 6.18 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
local animationDict = "pickup_object"
local animation = "pickup_low"
local function LoadModel(model)
while not HasModelLoaded(GetHashKey(model)) do
RequestModel(GetHashKey(model))
Wait(10)
end
end
local function LoadAnimationDict(dict)
while not HasAnimDictLoaded(dict) do
RequestAnimDict(dict)
Wait(10)
end
end
local function RequestNetworkControlOfObject(netId, itemEntity)
if NetworkDoesNetworkIdExist(netId) then
NetworkRequestControlOfNetworkId(netId)
while not NetworkHasControlOfNetworkId(netId) do
Wait(100)
NetworkRequestControlOfNetworkId(netId)
end
end
if DoesEntityExist(itemEntity) then
NetworkRequestControlOfEntity(itemEntity)
while not NetworkHasControlOfEntity(itemEntity) do
Wait(100)
NetworkRequestControlOfEntity(itemEntity)
end
end
end
-- Returns a table containing the metadata for the given bike entity
--- @param bikeEntity - The entity of the bike to get the metadata for
--- @return table - The metadata of the bike {plate, colorPrimary, colorSecondary, pearlescentColor, wheelColor, xenonColor}
local function getBikeMetadata(bikeEntity)
local bikePlate = GetVehicleNumberPlateText(bikeEntity)
local colorPrimary, colorSecondary = GetVehicleColours(bikeEntity)
local pearlescentColor, wheelColor = GetVehicleExtraColours(bikeEntity)
local xenonColor = GetVehicleXenonLightsColour(bikeEntity)
return {
plate = bikePlate,
colorPrimary = colorPrimary,
colorSecondary = colorSecondary,
pearlescentColor = pearlescentColor,
wheelColor = wheelColor,
xenonColor = xenonColor,
}
end
-- Sets the bike properties based on the given metadata
--- @param bike - The bike entity to set the properties on
--- @param bikeMetadata - The metadata of the bike {plate, colorPrimary, colorSecondary, pearlescentColor, wheelColor, xenonColor}, may be nil for frameworks that dont support item metadata (ex: ESX)
local function setBikeProperties(bike, bikeMetadata)
if not bikeMetadata then return end
local plate = bikeMetadata.plate
local colorPrimary = bikeMetadata.colorPrimary
local colorSecondary = bikeMetadata.colorSecondary
local pearlescentColor = bikeMetadata.pearlescentColor
local wheelColor = bikeMetadata.wheelColor
local xenonColor = bikeMetadata.xenonColor
-- If the bike item info already has a plate, use that otherwise create a new plate. This is important to handle owned bikes.
if not bikePlate then
bikePlate = "BICYCLE" .. math.random(1000, 9999)
end
SetVehicleNumberPlateText(bike, bikePlate)
SetVehicleColours(bike, colorPrimary, colorSecondary)
SetVehicleExtraColours(bike, pearlescentColor, wheelColor)
-- Xenon color is 0-12 if set or 255 if no color set
if xenonColor ~= 255 then
ToggleVehicleMod(bike, 22, true)
SetVehicleXenonLightsColour(bike, xenonColor)
end
end
-- Handles creating and placing the bike in the world
-- Adds a plate to the bike (either from the item metadata or a random plate) and sets the player as the owner/gives keys
-- bikeItemData info may be nil for frameworks that don't support item metadata (ex: ESX)
--- @param bikeModel string The model of the bike to spawn
--- @param bikeItemData table The item data of the bike to spawn
RegisterNetEvent("wp-pocketbikes:client:place", function(bikeModel, bikeItemData)
local ped = PlayerPedId()
local itemMetadata = GetItemMetadata(bikeItemData)
-- Request model and wait until its loaded
LoadModel(bikeModel)
ClearPedTasks(ped)
TaskPlayAnim(ped, animationDict, animation, 8.0, -8.0, -1, 0, 0, false, false, false)
-- Wait so the animation can play before immediately spawning the bike
Wait(500)
-- Spawn the bike in the world with an offset in front of the player and set it on the ground
local offsetCoords = GetOffsetFromEntityInWorldCoords(ped, 0.0, 1.0, 0.0)
local bike = CreateVehicle(bikeModel, offsetCoords, GetEntityHeading(ped), true, false)
SetVehicleOnGroundProperly(bike)
TriggerServerEvent("wp-pocketbikes:server:RemoveItem", bikeModel)
setBikeProperties(bike, itemMetadata)
SetModelAsNoLongerNeeded(bikeModel)
local bikePlate = GetVehicleNumberPlateText(bike)
if itemMetadata then
bikePlate = itemMetadata.plate
end
SetPlayerAsOwnerOfVehicleWithPlate(bikePlate)
end)
-- Handles picking up the bike and giving the player the item back
-- Applies metadata onto the item to store the plate and color
--- @param data table The data of the bike entity to pick up provided from the target event
RegisterNetEvent("wp-pocketbikes:client:pickup", function(data)
local ped = PlayerPedId()
local bikeEntity = data.entity
local bikeItem = data.itemName
if bikeEntity then
local bikeNetId = NetworkGetNetworkIdFromEntity(bikeEntity)
local bikeMetadata = getBikeMetadata(bikeEntity)
LoadAnimationDict(animationDict)
ClearPedTasks(ped)
TaskPlayAnim(ped, animationDict, animation, 8.0, -8.0, -1, 0, 0, false, false, false)
TriggerServerEvent("wp-pocketbikes:server:AddItem", bikeItem, bikeMetadata)
-- Must have network control of entity before being able to delete it
RequestNetworkControlOfObject(bikeNetId, bikeEntity)
DeleteEntity(bikeEntity)
end
end)
-- Setup each bike to be targettable
-- Itemname is in the options so we know which item to give back when picked up
for _, bike in pairs(Config.Bikes) do
local targetOptions = {
{
type = "client",
event = "wp-pocketbikes:client:pickup",
icon = "fas fa-bicycle",
label = "Pick up bike",
itemName = bike,
},
}
-- A model can only have one set of options, so if the model is defined twice, only the last one will be used
-- NOTE: If you have another script that adds target onto these bike models, this may conflict with it.
-- It can be resolved by combining the targetOptions and calling AddTargetModel one time (either in this script or the other one)
AddTargetModel(bike, {
options = targetOptions,
distance = 2.0,
})
end