mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 09:18:53 +00:00
Initial support for weapon components
This commit is contained in:
@@ -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
|
||||
|
||||
+50
-12
@@ -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
|
||||
|
||||
+38
-19
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
+30
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user