From eea5d95aea5dcb903fc12e7213913f89bf54cac9 Mon Sep 17 00:00:00 2001 From: Space V Date: Fri, 28 Jul 2023 19:28:09 +0200 Subject: [PATCH] feat(server/commands): new commands --- client/keys.lua | 17 +++++++---------- fxmanifest.lua | 3 +++ locales/en.json | 2 ++ server/commands.lua | 21 ++++++++++++++++++--- server/exports.lua | 28 ++++++++++++++++------------ server/keys.lua | 9 ++++----- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/client/keys.lua b/client/keys.lua index 4b8e5ef..a79176b 100644 --- a/client/keys.lua +++ b/client/keys.lua @@ -10,16 +10,13 @@ inventory:displayMetadata('plate', locale('plate_tooltip')) Performs an animation for using a key fob. ]] local function performKeyFobAnimation() - local animationDict = "anim@mp_player_intmenu@key_fob@" + local animationDict = '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 = GetHashKey("lr_prop_carkey_fob") + local modelHash = GetHashKey('lr_prop_carkey_fob') RequestModel(modelHash) while not HasModelLoaded(modelHash) do @@ -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) --[[ diff --git a/fxmanifest.lua b/fxmanifest.lua index b99faf3..e88b09c 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -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' } diff --git a/locales/en.json b/locales/en.json index d982356..4795490 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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", diff --git a/server/commands.lua b/server/commands.lua index ec56c25..6f1d15f 100644 --- a/server/commands.lua +++ b/server/commands.lua @@ -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 = RemoveKeysFromPlayersFromVehicle(args.plate) +ESX.RegisterCommand({'removekeysfromplayers'}, 'admin', function(xPlayer, args, showError) + local player = GetPlayerPed(args.target) + local vehicle = 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 , keyCount , plate = 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 = 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'}, }}) diff --git a/server/exports.lua b/server/exports.lua index b41e126..d4f1e42 100644 --- a/server/exports.lua +++ b/server/exports.lua @@ -41,7 +41,7 @@ local function addKeyToPlayerInternal(identifier, target, count, blockKey) local metadata = { plate = checkPlate(identifier) } local canAddKey = 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -192,10 +192,12 @@ end local function removeKeyFromPlayersInternal(identifier) local success = true local metadata = {plate = checkPlate(identifier)} + local count = 0 for _, players in ipairs(GetPlayers()) do - local keyCount = Inventory:GetItemCount(tonumber(players), 'keys', metadata, true) + local keyCount = Inventory:GetItemCount(tonumber(players), 'keys', metadata, true) if (keyCount > 0) then local removeSuccess , response = 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') @@ -203,19 +205,21 @@ local function removeKeyFromPlayersInternal(identifier) 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 = GetVehicleNumberPlateText(vehicle) - return removeKeyFromPlayersInternal(plate) + local success , keyCount = removeKeyFromPlayersInternal(plate) + return success, keyCount, plate end function RemoveKeysFromPlayersFromPlate(plate) - return removeKeyFromPlayersInternal(plate) + local success , keyCount = removeKeyFromPlayersInternal(plate) + return success, keyCount end \ No newline at end of file diff --git a/server/keys.lua b/server/keys.lua index 6703cb5..ae88e64 100644 --- a/server/keys.lua +++ b/server/keys.lua @@ -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 @@ -54,8 +53,8 @@ AddEventHandler('idev_keys:check', function() local doorState = GetVehicleDoorLockStatus(vehicle) local isLocked = (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)