From caec0579541fa760d7b7af3329da4f7081327cb0 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Sat, 29 Dec 2018 02:10:37 +0100 Subject: [PATCH] Initial support for weapon components --- client/functions.lua | 15 ---------- client/main.lua | 62 +++++++++++++++++++++++++++++++-------- server/classes/player.lua | 57 +++++++++++++++++++++++------------ server/functions.lua | 15 ---------- shared/functions.lua | 31 +++++++++++++++++++- 5 files changed, 118 insertions(+), 62 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index a4f82218..0a12aa14 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -251,21 +251,6 @@ ESX.UI.ShowInventoryItemNotification = function(add, item, count) }) end -ESX.GetWeaponList = function() - return Config.Weapons -end - -ESX.GetWeaponLabel = function(name) - name = string.upper(name) - local weapons = ESX.GetWeaponList() - - for i=1, #weapons, 1 do - if weapons[i].name == name then - return weapons[i].label - end - end -end - ESX.Game.GetPedMugshot = function(ped) local mugshot = RegisterPedheadshot(ped) while not IsPedheadshotReady(mugshot) do diff --git a/client/main.lua b/client/main.lua index 92d63cd9..47d8bc09 100644 --- a/client/main.lua +++ b/client/main.lua @@ -99,10 +99,19 @@ AddEventHandler('esx:restoreLoadout', function() RemoveAllPedWeapons(playerPed, true) for i=1, #ESX.PlayerData.loadout, 1 do - local weaponHash = GetHashKey(ESX.PlayerData.loadout[i].name) + local weaponName = ESX.PlayerData.loadout[i].name + local weaponHash = GetHashKey(weaponName) + GiveWeaponToPed(playerPed, weaponHash, 0, false, false) local ammoType = GetPedAmmoTypeFromWeapon(playerPed, weaponHash) + for j=1, #ESX.PlayerData.loadout[i].components, 1 do + local weaponComponent = ESX.PlayerData.loadout[i].components[j] + local componentHash = ESX.GetWeaponComponentHash(weaponName, weaponComponent) + + GiveWeaponComponentToPed(playerPed, weaponHash, componentHash) + end + if not ammoTypes[ammoType] then AddAmmoToPed(playerPed, weaponHash, ESX.PlayerData.loadout[i].ammo) ammoTypes[ammoType] = true @@ -179,6 +188,15 @@ AddEventHandler('esx:addWeapon', function(weaponName, ammo) --AddAmmoToPed(playerPed, weaponHash, ammo) possibly not needed end) +RegisterNetEvent('esx:addWeaponComponent') +AddEventHandler('esx:addWeaponComponent', function(weaponName, weaponComponent) + local playerPed = PlayerPedId() + local weaponHash = GetHashKey(weaponName) + local componentHash = ESX.GetWeaponComponentHash(weaponName, weaponComponent) + + GiveWeaponComponentToPed(playerPed, weaponHash, componentHash) +end) + RegisterNetEvent('esx:removeWeapon') AddEventHandler('esx:removeWeapon', function(weaponName, ammo) local playerPed = PlayerPedId() @@ -187,7 +205,7 @@ AddEventHandler('esx:removeWeapon', function(weaponName, ammo) RemoveWeaponFromPed(playerPed, weaponHash) if ammo then - local pedAmmo = GetAmmoInPedWeapon(playerPed, weaponHash) + local pedAmmo = GetAmmoInPedWeapon(playerPed, weaponHash) local finalAmmo = math.floor(pedAmmo - ammo) SetPedAmmo(playerPed, weaponHash, finalAmmo) else @@ -195,6 +213,16 @@ AddEventHandler('esx:removeWeapon', function(weaponName, ammo) end end) + +RegisterNetEvent('esx:removeWeaponComponent') +AddEventHandler('esx:removeWeaponComponent', function(weaponName, weaponComponent) + local playerPed = PlayerPedId() + local weaponHash = GetHashKey(weaponName) + local componentHash = ESX.GetWeaponComponentHash(weaponName, weaponComponent) + + RemoveWeaponComponentFromPed(playerPed, weaponHash, componentHash) +end) + -- Commands RegisterNetEvent('esx:teleport') AddEventHandler('esx:teleport', function(pos) @@ -392,7 +420,7 @@ end Citizen.CreateThread(function() while true do - Citizen.Wait(500) + Citizen.Wait(5000) local playerPed = PlayerPedId() local loadout = {} @@ -404,28 +432,38 @@ Citizen.CreateThread(function() for i=1, #Config.Weapons, 1 do - local weaponHash = GetHashKey(Config.Weapons[i].name) + local weaponName = Config.Weapons[i].name + local weaponHash = GetHashKey(weaponName) + local weaponComponents = {} - if HasPedGotWeapon(playerPed, weaponHash, false) and Config.Weapons[i].name ~= 'WEAPON_UNARMED' then + if HasPedGotWeapon(playerPed, weaponHash, false) and weaponName ~= 'WEAPON_UNARMED' then local ammo = GetAmmoInPedWeapon(playerPed, weaponHash) + local components = Config.Weapons[i].components - if LastLoadout[Config.Weapons[i].name] == nil or LastLoadout[Config.Weapons[i].name] ~= ammo then + for j=1, #components, 1 do + if HasPedGotWeaponComponent(playerPed, weaponHash, components[j].hash) then + table.insert(weaponComponents, components[j].name) + end + end + + if LastLoadout[weaponName] == nil or LastLoadout[weaponName] ~= ammo then loadoutChanged = true end - LastLoadout[Config.Weapons[i].name] = ammo + LastLoadout[weaponName] = ammo table.insert(loadout, { - name = Config.Weapons[i].name, - ammo = ammo, - label = Config.Weapons[i].label + name = weaponName, + ammo = ammo, + label = Config.Weapons[i].label, + components = weaponComponents }) else - if LastLoadout[Config.Weapons[i].name] ~= nil then + if LastLoadout[weaponName] ~= nil then loadoutChanged = true end - LastLoadout[Config.Weapons[i].name] = nil + LastLoadout[weaponName] = nil end end diff --git a/server/classes/player.lua b/server/classes/player.lua index ec0cf554..76d35e24 100644 --- a/server/classes/player.lua +++ b/server/classes/player.lua @@ -380,20 +380,14 @@ function CreateExtendedPlayer(player, accounts, inventory, job, loadout, name, l end self.addWeapon = function(weaponName, ammo) - local weaponLabel = weaponName - - for i=1, #Config.Weapons, 1 do - if Config.Weapons[i].name == weaponName then - weaponLabel = Config.Weapons[i].label - break - end - end + local weaponLabel = ESX.GetWeaponLabel(weaponName) if not self.hasWeapon(weaponName) then table.insert(self.loadout, { - name = weaponName, - ammo = ammo, - label = weaponLabel + name = weaponName, + ammo = ammo, + label = weaponLabel, + components = {} }) end @@ -401,18 +395,20 @@ function CreateExtendedPlayer(player, accounts, inventory, job, loadout, name, l TriggerClientEvent('esx:addInventoryItem', self.source, {label = weaponLabel}, 1) end - self.removeWeapon = function(weaponName, ammo) - local weaponLabel = weaponName + self.addWeaponComponent = function(weaponName, weaponComponent) + local loadoutNum, weapon = self.getWeapon(weaponName) + table.insert(self.loadout[loadoutNum].components, weaponComponent) - for i=1, #Config.Weapons, 1 do - if Config.Weapons[i].name == weaponName then - weaponLabel = Config.Weapons[i].label - break - end - end + TriggerClientEvent('esx:addWeaponComponent', self.source, weaponName, weaponComponent) + end + + self.removeWeapon = function(weaponName, ammo) + local weaponLabel for i=1, #self.loadout, 1 do if self.loadout[i].name == weaponName then + weaponLabel = self.loadout[i].label + table.remove(self.loadout, i) break end @@ -422,6 +418,19 @@ function CreateExtendedPlayer(player, accounts, inventory, job, loadout, name, l TriggerClientEvent('esx:removeInventoryItem', self.source, {label = weaponLabel}, 1) end + self.removeWeaponComponent = function(weaponName, weaponComponent) + local loadoutNum, weapon = self.getWeapon(weaponName) + + for i=1, #self.loadout[loadoutNum].components, 1 do + if self.loadout[loadoutNum].components.name == weaponComponent then + table.remove(self.loadout[loadoutNum].components, i) + break + end + end + + TriggerClientEvent('esx:removeWeaponComponent', self.source, weaponName, weaponComponent) + end + self.hasWeapon = function(weaponName) for i=1, #self.loadout, 1 do if self.loadout[i].name == weaponName then @@ -432,5 +441,15 @@ function CreateExtendedPlayer(player, accounts, inventory, job, loadout, name, l return false end + self.getWeapon = function(weaponName) + for i=1, #self.loadout, 1 do + if self.loadout[i].name == weaponName then + return i, self.loadout[i] + end + end + + return nil + end + return self end diff --git a/server/functions.lua b/server/functions.lua index 3d682d5c..36da07c0 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -174,21 +174,6 @@ ESX.GetItemLabel = function(item) end end -ESX.GetWeaponList = function() - return Config.Weapons -end - -ESX.GetWeaponLabel = function(name) - name = string.upper(name) - local weapons = ESX.GetWeaponList() - - for i=1, #weapons, 1 do - if weapons[i].name == name then - return weapons[i].label - end - end -end - ESX.CreatePickup = function(type, name, count, label, player) local pickupId = (ESX.PickupId == 65635 and 0 or ESX.PickupId + 1) diff --git a/shared/functions.lua b/shared/functions.lua index 84f3ec3b..268d1d67 100644 --- a/shared/functions.lua +++ b/shared/functions.lua @@ -18,6 +18,36 @@ ESX.GetConfig = function() return Config end +ESX.GetWeaponList = function() + return Config.Weapons +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 + end + end +end + +ESX.GetWeaponComponentHash = function(weaponName, weaponComponent) + weaponName = string.upper(weaponName) + local weapons = ESX.GetWeaponList() + + 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].hash + end + end + end + end +end + ESX.TableContainsValue = function(table, value) for k, v in pairs(table) do if v == value then @@ -58,7 +88,6 @@ ESX.DumpTable = function(table, nb) end end - ESX.Round = function(value, numDecimalPlaces) return ESX.Math.Round(value, numDecimalPlaces) end