Skip to content

Commit

Permalink
Merge branch 'feat-admin-commands-hooks' of https://github.com/hedge-…
Browse files Browse the repository at this point in the history
…code/qbx_vehiclekeys into hedge-code-feat-admin-commands-hooks
  • Loading branch information
artur-michalak committed May 12, 2024
2 parents 976ceaa + bb68347 commit e4ef778
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 35 deletions.
3 changes: 2 additions & 1 deletion client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ local islockpickingProcessLocked = false -- lock flag
function public.lockpickDoor(isAdvancedLockedpick, maxDistance, customChallenge)
maxDistance = maxDistance or 2
local pedCoords = GetEntityCoords(cache.ped)
local vehicle = lib.getClosestVehicle(pedCoords, 4, false)
local vehicle = lib.getClosestVehicle(pedCoords, maxDistance * 2, false) -- The difference between the door and the center of the vehicle

if not vehicle then return end

local plate = qbx.getVehiclePlate(vehicle)
Expand Down
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
"carjack_failed": "Carjacking failed!",
"failed_lockedpick": "You failed to lockpick.",
"failed_keys": "You fail to find the keys and get frustrated.",
"fpid": "Fill out the player ID and Plate arguments",
"gave_keys": "You hand over the keys.",
"added_keys": "You gave a copy of the keys to vehicle %s to player %s!",
"keys_taken": "You get keys to the vehicle!",
"keys_removed": "You lost keys to the vehicle!",
"no_keys": "You don't have keys to this vehicle.",
"not_near": "There is nobody nearby to hand keys to",
"vehicle_locked": "Vehicle locked!",
Expand Down
54 changes: 39 additions & 15 deletions server/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ lib.addCommand('givekeys', {
TriggerClientEvent('qb-vehiclekeys:client:GiveKeys', source, id, args[locale('addcom.givekeys_plate')])
end)

--- Gets the plate of the vehicle in which the executor is, if args.plate is nil
---@param source number ID of the player
---@param plate string? The plate number of the vehicle.
---@return string?
local function getPlayersVehiclePlate(source, plate)
if not plate then
local ped = GetPlayerPed(source)
local vehicle = GetVehiclePedIsIn(ped, false)

if vehicle == 0 then return end
plate = GetVehicleNumberPlateText(vehicle)
end

return plate
end

lib.addCommand('addkeys', {
help = locale('addcom.addkeys'),
params = {
Expand All @@ -41,14 +57,18 @@ lib.addCommand('addkeys', {
},
restricted = 'group.admin',
}, function (source, args)
local id = args[locale('addcom.addkeys_id')]
local plate = args[locale('addcom.addkeys_plate')]
local success = GiveKeys(id, plate)
if success then
exports.qbx_core:Notify(source, locale('notify.added_keys', plate, id), 'success')
else
exports.qbx_core:Notify(source, locale('notify.player_offline'), 'error')
local playerId = args[locale('addcom.addkeys_id')] or source
local plate = getPlayersVehiclePlate(source, args[locale('addcom.addkeys_plate')])

if not playerId or not plate then
return exports.qbx_core:Notify(source, locale('notify.fpid'), 'error')
end

if GiveKeys(playerId, plate) then
return exports.qbx_core:Notify(source, locale('notify.added_keys', plate, playerId), 'success')
end

exports.qbx_core:Notify(source, locale('notify.player_offline'), 'error')
end)

lib.addCommand('removekeys', {
Expand All @@ -67,12 +87,16 @@ lib.addCommand('removekeys', {
},
restricted = 'group.admin',
}, function (source, args)
local id = args[locale('addcom.removekeys_id')]
local plate = args[locale('addcom.removekeys_plate')]
local success = RemoveKeys(id, plate)
if success then
exports.qbx_core:Notify(source, locale('notify.removed_keys', plate, id), 'success')
else
exports.qbx_core:Notify(source, locale('notify.player_offline'), 'error')
local playerId = args[locale('addcom.removekeys_id')] or source
local plate = getPlayersVehiclePlate(source, args[locale('addcom.removekeys_plate')])

if not playerId or not plate then
return exports.qbx_core:Notify(source, locale('notify.fpid'), 'error')
end
end)

if RemoveKeys(playerId, plate) then
return exports.qbx_core:Notify(source, locale('notify.removed_keys', plate, playerId), 'success')
end

exports.qbx_core:Notify(source, locale('notify.player_offline'), 'error')
end)
47 changes: 32 additions & 15 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ local keysList = {}
---- Functions ----
-----------------------

---Gets Citizen Id based on source
---@param source number ID of the player
---@return string citizenid The CitizenID of the player whose key is being added.
local function getCitizenId(source)
local player = exports.qbx_core:GetPlayer(source)
if not player then return end
Expand Down Expand Up @@ -56,38 +59,54 @@ local function removePlayer(src)
Player(src).state:set('keysList', nil, true)
end

---Gives the user the keys to the vehicle
---@param source number ID of the player
---@param plate string The plate number of the vehicle.
function GiveKeys(source, plate)
local citizenid = getCitizenId(source)

if not citizenid then return end

local keys = Player(source).state.keysList or {}
if keys[plate] then return true end

if keys[plate] then return end
keys[plate] = true
Player(source).state:set('keysList', keys, true)

local citizenid = getCitizenId(source)
if not citizenid then return false end
Player(source).state:set('keysList', keys, true)

if not keysList[citizenid] then
keysList[citizenid] = {}
keysList[citizenid] = {plate = true}
else
keysList[citizenid][plate] = true
end

keysList[citizenid][plate] = true
exports.qbx_core:Notify(source, locale('notify.keys_taken'))

return true
end

exports('GiveKeys', GiveKeys)

--- Removing the vehicle keys from the user
---@param source number ID of the player
---@param plate string The plate number of the vehicle.
function RemoveKeys(source, plate)
local state = Player(source).state
if not state.keysList[plate] then return true end

local citizenid = getCitizenId(source)
if not citizenid then return false end

keysList[citizenid][plate] = nil
state:set('keysList', keysList[citizenid], true)
exports.qbx_core:Notify(source, locale('notify.removed_keys_player', plate))
if not citizenid then return end

local keys = Player(source).state.keysList or {}

if not keys[plate] then return end
keys[plate] = nil

Player(source).state:set('keysList', keys, true)

if keysList and keysList[citizenid] then
keysList[citizenid][plate] = nil
end

exports.qbx_core:Notify(source, locale('notify.keys_removed'))

return true
end
Expand Down Expand Up @@ -143,8 +162,6 @@ RegisterNetEvent('qb-vehiclekeys:server:GiveKey', function(id, netId, doorState)
end
end)

exports('GiveKey', GiveKey)

---Removes a key from an entity based on the player's CitizenID.
---@param id integer The player's ID.
---@param netId number The network ID of the entity.
Expand Down
8 changes: 4 additions & 4 deletions shared/functions.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local public = {}

--- Checks if the given two coordinates are close to each other based on distance.
--- @param coord1 vector3[] The first set of coordinates.
--- @param coord2 vector3[] The second set of coordinates.
--- @param distance number The maximum allowed distance for them to be considered close.
--- @return boolean true if the distance between two entities is less than the distance parameter.
---@param coord1 vector3[] The first set of coordinates.
---@param coord2 vector3[] The second set of coordinates.
---@param distance number The maximum allowed distance for them to be considered close.
---@return boolean true if the distance between two entities is less than the distance parameter.
function public.isCloseToCoords(coord1, coord2, distance)
return #(coord1 - coord2) < distance
end
Expand Down

0 comments on commit e4ef778

Please sign in to comment.