Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
feat(server/commands): new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Space V committed Jul 28, 2023
1 parent c20d382 commit eea5d95
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 30 deletions.
17 changes: 7 additions & 10 deletions client/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ inventory:displayMetadata('plate', locale('plate_tooltip'))
Performs an animation for using a key fob.
]]
local function performKeyFobAnimation()
local animationDict <const> = "anim@mp_player_intmenu@key_fob@"
local animationDict <const> = 'anim@mp_player_intmenu@key_fob@'

while not HasAnimDictLoaded(animationDict) do
RequestAnimDict(animationDict)
Wait(0)
end
lib.requestAnimDict(animationDict)

TaskPlayAnim(cache.ped, animationDict, "fob_click", 3.0, 3.0, -1, 48, 0.0, false, false, false)
TaskPlayAnim(cache.ped, animationDict, 'fob_click', 3.0, 3.0, -1, 48, 0.0, false, false, false)

local modelHash <const> = GetHashKey("lr_prop_carkey_fob")
local modelHash <const> = GetHashKey('lr_prop_carkey_fob')
RequestModel(modelHash)

while not HasModelLoaded(modelHash) do
Expand Down Expand Up @@ -69,13 +66,13 @@ end
Key mapping: "keys" - Binds the action to the "x" key by default on the keyboard.
Command: "keys" - Triggers a server event to check the player's key ownership.
]]
RegisterKeyMapping("keys", locale('key_mapping'), "keyboard", IDEV.Keys.ControlKey)
RegisterKeyMapping('keys', locale('key_mapping'), 'keyboard', IDEV.Keys.ControlKey)

RegisterCommand("keys", function()
RegisterCommand('keys', function()
if not (inventory:GetItemCount('keys', nil, false)) then return end
if (player.invBusy) or (player.invOpen) then return end
if not (IDEV.Keys.EnableKeyUsageInsideVehicle) and (cache.vehicle) then return end
TriggerServerEvent("idev_keys:check")
TriggerServerEvent('idev_keys:check')
end, false)

--[[
Expand Down
3 changes: 3 additions & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ description 'A key system in item for vehicles made for ox_inventory.'
dependencies {
'/server:5848',
'/onesync',
'ox_lib',
'ox_inventory',
'es_extended'
}


Expand Down
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
"command_help_playerID": "The player ID of the target",
"command_addkeytoplayer": "Add a key for the car in which the target is located.",
"command_removekeytoplayer": "Remove all keys for the car in which the target is located.",
"command_removekeytoplayers": "Remove all keys for the car in which the target is located from all players' inventories.",
"command_getkeycountfromplayer": "Get the number of keys that exist in the players' inventories for the car in which the target is located.",
"command_keycount_result_target": "%s keys were found in the inventories of the players",
"command_keycount_result_plate": "%s keys were found with the plate %s in all inventories of the players",
"command_keyremove_players": "%s keys were removed with the plate %s in all inventories of the players",
"command_help_count_add": "Numbers of keys to add.",
"command_help_block_key": "Prevent the key from being removed from the target's inventory. Use '0' to disable and '1' to enable.",
"command_block_key_invalid": "The value must be 0 or 1",
Expand Down
21 changes: 18 additions & 3 deletions server/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,27 @@ end, false, {help = locale('command_removekeytoplayer'), arguments = {
{name = 'playerId', help = locale('command_help_playerID'), type = 'number'},
}})

ESX.RegisterCommand({'removekeytoplayers'}, 'admin', function(xPlayer, args, showError)
local success <const> = RemoveKeysFromPlayersFromVehicle(args.plate)
ESX.RegisterCommand({'removekeysfromplayers'}, 'admin', function(xPlayer, args, showError)
local player <const> = GetPlayerPed(args.target)
local vehicle <const> = GetVehiclePedIsIn(player, false)
if not (DoesEntityExist(vehicle)) or (vehicle == 0) then
return addMessage(xPlayer.source, 'error', locale('command_not_in_a_vehicle'))
end
local success <const>, keyCount <const>, plate <const> = RemoveKeysFromPlayersFromVehicle(vehicle)
if not (success) then
return addMessage(xPlayer.source, 'error', locale('command_key_not_found'))
end
addMessage(xPlayer.source, 'success', locale('command_key_has_been_removed'))
addMessage(xPlayer.source, 'success', locale('command_keyremove_players'):format(keyCount, plate))
end, false, {help = locale('command_removekeytoplayers'), arguments = {
{name = 'target', help = locale('command_help_playerID'), type = 'number'},
}})

ESX.RegisterCommand({'removekeysfromplayersfromplate'}, 'admin', function(xPlayer, args, showError)
local success, keyCount <const> = RemoveKeysFromPlayersFromPlate(args.plate)
if not (success) then
return addMessage(xPlayer.source, 'error', locale('command_key_not_found'))
end
addMessage(xPlayer.source, 'success', locale('command_keyremove_players'):format(keyCount, args.plate))
end, false, {help = locale('command_removekeytoplayers'), arguments = {
{name = 'plate', help = locale('command_plate_help'), type = 'string'},
}})
Expand Down
28 changes: 16 additions & 12 deletions server/exports.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ local function addKeyToPlayerInternal(identifier, target, count, blockKey)
local metadata <const> = { plate = checkPlate(identifier) }
local canAddKey <const> = Inventory:CanCarryItem(target, 'keys', count, metadata, true)
if not (canAddKey) then
PrintErrorMessage("The player does not have enough space in their inventory", "addKeyToPlayerInternal")
PrintErrorMessage('The player does not have enough space in their inventory', 'addKeyToPlayerInternal')
return false
end

Expand All @@ -68,7 +68,7 @@ local function addKeyToPlayerInternal(identifier, target, count, blockKey)

if not (success) then
print(response)
PrintErrorMessage("Error adding the key to the player", "addKeyToPlayerInternal")
PrintErrorMessage('Error adding the key to the player', 'addKeyToPlayerInternal')
return false
end

Expand All @@ -86,12 +86,12 @@ end
]]
function AddKeyToPlayerFromVehicle(vehicle, target, count, blockKey)
if not (DoesEntityExist(vehicle) )then
PrintErrorMessage("The vehicle does not exist", "AddKeyToPlayerFromVehicle")
PrintErrorMessage('The vehicle does not exist', 'AddKeyToPlayerFromVehicle')
return false
end

if (type(target) ~= "number") then
PrintErrorMessage("The target is not a number", "AddKeyToPlayerFromVehicle")
if (type(target) ~= 'number') then
PrintErrorMessage('The target is not a number', 'AddKeyToPlayerFromVehicle')
return false
end

Expand Down Expand Up @@ -144,7 +144,7 @@ end
]]
function RemoveKeyFromPlayerFromVehicle(target, vehicle)
if not (DoesEntityExist(vehicle)) then
PrintErrorMessage("The vehicle does not exist", "RemoveKeyFromPlayerFromVehicle")
PrintErrorMessage('The vehicle does not exist', 'RemoveKeyFromPlayerFromVehicle')
return false
end

Expand Down Expand Up @@ -177,7 +177,7 @@ end

function GetKeyCountFromVehicle(vehicle)
if not (DoesEntityExist(vehicle) or vehicle == 0) then
PrintErrorMessage("The vehicle does not exist", "GetKeyCountFromVehicle")
PrintErrorMessage('The vehicle does not exist', 'GetKeyCountFromVehicle')
return false
end

Expand All @@ -192,30 +192,34 @@ end
local function removeKeyFromPlayersInternal(identifier)
local success = true
local metadata <const> = {plate = checkPlate(identifier)}
local count = 0
for _, players in ipairs(GetPlayers()) do
local keyCount <const> = Inventory:GetItemCount(tonumber(players), 'keys', metadata, true)
local keyCount = Inventory:GetItemCount(tonumber(players), 'keys', metadata, true)
if (keyCount > 0) then
local removeSuccess <const>, response <const> = Inventory:RemoveItem(tonumber(players), 'keys', keyCount, metadata)
count += keyCount
if not (removeSuccess) then
print(response)
PrintErrorMessage('Error removing the key from the player [ID: ' .. players .. ']', 'removeKeyFromPlayersInternal')
success = false
end
end
end
return success
return success, count
end

function RemoveKeysFromPlayersFromVehicle(vehicle)
if not (DoesEntityExist(vehicle) or vehicle == 0) then
PrintErrorMessage("The vehicle does not exist", "RemoveKeysFromPlayersFromVehicle")
PrintErrorMessage('The vehicle does not exist', 'RemoveKeysFromPlayersFromVehicle')
return false
end

local plate <const> = GetVehicleNumberPlateText(vehicle)
return removeKeyFromPlayersInternal(plate)
local success <const>, keyCount <const> = removeKeyFromPlayersInternal(plate)
return success, keyCount, plate
end

function RemoveKeysFromPlayersFromPlate(plate)
return removeKeyFromPlayersInternal(plate)
local success <const>, keyCount <const> = removeKeyFromPlayersInternal(plate)
return success, keyCount
end
9 changes: 4 additions & 5 deletions server/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ end
and returns false.
]]
function PrintErrorMessage(message, functionName)
if (IDEV.Keys.Debug) then
print(GetCurrentResourceName(), message, "function:", functionName)
end
if not (IDEV.Keys.Debug) then return end
print(GetCurrentResourceName(), message, 'function:', functionName)
end


Expand Down Expand Up @@ -54,8 +53,8 @@ AddEventHandler('idev_keys:check', function()
local doorState <const> = GetVehicleDoorLockStatus(vehicle)
local isLocked <const> = (doorState == 1) or (doorState == 0) -- based on fivem docs GetVehicleDoorLockStatus should return only 0 when the vehicle is unlocked because of the game who going to sync both state and use 0, however i couldn't get the 0 state so i added it here just in case
SetVehicleDoorsLocked(vehicle, isLocked and 2 or 1)
Entity(vehicle).state.isLocked = isLocked
TriggerClientEvent('idev_keys:anim:vehicle', source)
Entity(vehicle).state?.isLocked = isLocked
TriggerClientEvent('idev_keys:anim:vehicle', player.source)
--[[
Does animations are synced like that (i don't think so?)
Maybe trigger the animation on the scope of the original client (players in the scope of the player who is using the key)
Expand Down

0 comments on commit eea5d95

Please sign in to comment.