diff --git a/client/main.lua b/client/main.lua index 106e0032..d768fc58 100644 --- a/client/main.lua +++ b/client/main.lua @@ -393,53 +393,26 @@ if Config.EnableHud then end) end --- Save loadout +-- Keep track of ammo usage Citizen.CreateThread(function() - local lastLoadout = {} - while true do - Citizen.Wait(5000) - local playerPed, loadout, loadoutChanged = PlayerPedId(), {}, false + Citizen.Wait(0) - for k,v in ipairs(Config.Weapons) do - local weaponName = v.name - local weaponHash = GetHashKey(weaponName) + if isDead then + Citizen.Wait(500) + else + local playerPed = PlayerPedId() - if HasPedGotWeapon(playerPed, weaponHash, false) then - local ammo, tintIndex, weaponComponents = GetAmmoInPedWeapon(playerPed, weaponHash), GetPedWeaponTintIndex(playerPed, weaponHash), {} + if IsPedShooting(playerPed) then + local _,weaponHash = GetCurrentPedWeapon(playerPed, true) + local weapon = ESX.GetWeaponFromHash(weaponHash) - for k2,v2 in ipairs(v.components) do - if HasPedGotWeaponComponent(playerPed, weaponHash, v2.hash) then - table.insert(weaponComponents, v2.name) - end + if weapon then + local ammoCount = GetAmmoInPedWeapon(playerPed, weaponHash) + TriggerServerEvent('esx:updateWeaponAmmo', weapon.name, ammoCount) end - - if not lastLoadout[weaponName] or lastLoadout[weaponName] ~= ammo then - loadoutChanged = true - end - - lastLoadout[weaponName] = ammo - - table.insert(loadout, { - name = weaponName, - ammo = ammo, - label = v.label, - components = weaponComponents, - tintIndex = tintIndex - }) - else - if lastLoadout[weaponName] then - loadoutChanged = true - end - - lastLoadout[weaponName] = nil end end - - if loadoutChanged and isLoadoutLoaded then - ESX.PlayerData.loadout = loadout - TriggerServerEvent('esx:updateLoadout', loadout) - end end end) diff --git a/common/functions.lua b/common/functions.lua index ed9deb82..e47b5b7f 100644 --- a/common/functions.lua +++ b/common/functions.lua @@ -20,11 +20,18 @@ end ESX.GetWeapon = function(weaponName) weaponName = string.upper(weaponName) - local weapons = ESX.GetWeaponList() - for i=1, #weapons, 1 do - if weapons[i].name == weaponName then - return i, weapons[i] + for k,v in ipairs(Config.Weapons) do + if v.name == weaponName then + return k, v + end + end +end + +ESX.GetWeaponFromHash = function(weaponHash) + for k,v in ipairs(Config.Weapons) do + if GetHashKey(v.name) == weaponHash then + return v end end end @@ -35,24 +42,23 @@ end ESX.GetWeaponLabel = function(weaponName) weaponName = string.upper(weaponName) - local weapons = ESX.GetWeaponList() - for i=1, #weapons, 1 do - if weapons[i].name == weaponName then - return weapons[i].label + for k,v in ipairs(Config.Weapons) do + if v.name == weaponName then + return v.label end end end ESX.GetWeaponComponent = function(weaponName, weaponComponent) weaponName = string.upper(weaponName) - local weapons = ESX.GetWeaponList() + local weapons = Config.Weapons - for i=1, #weapons, 1 do - if weapons[i].name == weaponName then - for j=1, #weapons[i].components, 1 do - if weapons[i].components[j].name == weaponComponent then - return weapons[i].components[j] + for k,v in ipairs(Config.Weapons) do + if v.name == weaponName then + for k2,v2 in ipairs(v.components) do + if v2.name == weaponComponent then + return v2 end end end diff --git a/server/classes/player.lua b/server/classes/player.lua index 52f661e2..0705204c 100644 --- a/server/classes/player.lua +++ b/server/classes/player.lua @@ -367,6 +367,16 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory, end end + self.updateWeaponAmmo = function(weaponName, ammoCount) + local loadoutNum, weapon = self.getWeapon(weaponName) + + if weapon then + if ammoCount < weapon.ammo then + weapon.ammo = ammoCount + end + end + end + self.setWeaponTint = function(weaponName, weaponTintIndex) local loadoutNum, weapon = self.getWeapon(weaponName) diff --git a/server/main.lua b/server/main.lua index 5e6b7b61..4d63e12a 100644 --- a/server/main.lua +++ b/server/main.lua @@ -225,10 +225,13 @@ AddEventHandler('esx:updateCoords', function(coords) end end) -RegisterNetEvent('esx:updateLoadout') -AddEventHandler('esx:updateLoadout', function(loadout) +RegisterNetEvent('esx:updateWeaponAmmo') +AddEventHandler('esx:updateWeaponAmmo', function(weaponName, ammoCount) local xPlayer = ESX.GetPlayerFromId(source) - xPlayer.loadout = loadout + + if xPlayer then + xPlayer.updateWeaponAmmo(weaponName, ammoCount) + end end) RegisterNetEvent('esx:giveInventoryItem')