Skip to content

Commit

Permalink
General code cleanup
Browse files Browse the repository at this point in the history
Fixed the issue of being able to put the wrong attachment on a gun. (for example: pistol ext clip on smg)
  • Loading branch information
GhzGarage committed Oct 19, 2021
1 parent 4ff2a85 commit da18ca8
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 321 deletions.
285 changes: 126 additions & 159 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
local isLoggedIn = true
local CurrentWeaponData = {}
local PlayerData = {}
local CanShoot = true
-- Variables
local QBCore = exports['qb-core']:GetCoreObject()
local PlayerData, CurrentWeaponData, CanShoot, MultiplierAmount = {}, {}, true, 0

function DrawText3Ds(x, y, z, text)
-- Handlers

AddEventHandler('QBCore:Client:OnPlayerLoaded', function()
PlayerData = QBCore.Functions.GetPlayerData()
QBCore.Functions.TriggerCallback("weapons:server:GetConfig", function(RepairPoints)
for k, data in pairs(RepairPoints) do
Config.WeaponRepairPoints[k].IsRepairing = data.IsRepairing
Config.WeaponRepairPoints[k].RepairingData = data.RepairingData
end
end)
end)

RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
for k, v in pairs(Config.WeaponRepairPoints) do
Config.WeaponRepairPoints[k].IsRepairing = false
Config.WeaponRepairPoints[k].RepairingData = {}
end
end)

-- Functions

local function DrawText3Ds(x, y, z, text)
SetTextScale(0.35, 0.35)
SetTextFont(4)
SetTextProportional(1)
Expand All @@ -18,75 +38,45 @@ function DrawText3Ds(x, y, z, text)
ClearDrawOrigin()
end

local MultiplierAmount = 0
-- Events

Citizen.CreateThread(function()
while true do
if isLoggedIn then
local ped = PlayerPedId()
if CurrentWeaponData ~= nil and next(CurrentWeaponData) ~= nil then
if IsPedShooting(ped) or IsControlJustPressed(0, 24) then
if CanShoot then
local weapon = GetSelectedPedWeapon(ped)
local ammo = GetAmmoInPedWeapon(ped, weapon)
if QBCore.Shared.Weapons[weapon]["name"] == "weapon_snowball" then
TriggerServerEvent('QBCore:Server:RemoveItem', "snowball", 1)
elseif QBCore.Shared.Weapons[weapon]["name"] == "weapon_pipebomb" then
TriggerServerEvent('QBCore:Server:RemoveItem', "weapon_pipebomb", 1)
elseif QBCore.Shared.Weapons[weapon]["name"] == "weapon_molotov" then
TriggerServerEvent('QBCore:Server:RemoveItem', "weapon_molotov", 1)
elseif QBCore.Shared.Weapons[weapon]["name"] == "weapon_stickybomb" then
TriggerServerEvent('QBCore:Server:RemoveItem', "weapon_stickybomb", 1)
else
if ammo > 0 then
MultiplierAmount = MultiplierAmount + 1
end
end
else
local weapon = GetSelectedPedWeapon(ped)
if weapon ~= -1569615261 then
TriggerEvent('inventory:client:CheckWeapon', QBCore.Shared.Weapons[weapon]["name"])
QBCore.Functions.Notify("This weapon is broken and can not be used.", "error")
MultiplierAmount = 0
end
end
end
end
end
Citizen.Wait(0)
end
RegisterNetEvent("weapons:client:SyncRepairShops", function(NewData, key)
Config.WeaponRepairPoints[key].IsRepairing = NewData.IsRepairing
Config.WeaponRepairPoints[key].RepairingData = NewData.RepairingData
end)

Citizen.CreateThread(function()
SetWeaponsNoAutoswap(true)
RegisterNetEvent("addAttachment", function(component)
local ped = PlayerPedId()
local weapon = GetSelectedPedWeapon(ped)
local WeaponData = QBCore.Shared.Weapons[weapon]
GiveWeaponComponentToPed(ped, GetHashKey(WeaponData.name), GetHashKey(component))
end)

Citizen.CreateThread(function()
while true do
local ped = PlayerPedId()
if IsControlJustReleased(0, 24) or IsDisabledControlJustReleased(0, 24) then
local weapon = GetSelectedPedWeapon(ped)
local ammo = GetAmmoInPedWeapon(ped, weapon)
--if ammo > 0 then
TriggerServerEvent("weapons:server:UpdateWeaponAmmo", CurrentWeaponData, tonumber(ammo))
--else
--TriggerEvent('inventory:client:CheckWeapon')
--end
RegisterNetEvent('weapons:client:EquipTint', function(tint)
local player = PlayerPedId()
local weapon = GetSelectedPedWeapon(player)
SetPedWeaponTintIndex(player, weapon, tint)
end)

if MultiplierAmount > 0 then
TriggerServerEvent("weapons:server:UpdateWeaponQuality", CurrentWeaponData, MultiplierAmount)
MultiplierAmount = 0
end
end
Citizen.Wait(1)
RegisterNetEvent('weapons:client:SetCurrentWeapon', function(data, bool)
if data ~= false then
CurrentWeaponData = data
else
CurrentWeaponData = {}
end
CanShoot = bool
end)

RegisterNetEvent('weapons:client:SetWeaponQuality', function(amount)
if CurrentWeaponData and next(CurrentWeaponData) then
TriggerServerEvent("weapons:server:SetWeaponQuality", CurrentWeaponData, amount)
end
end)

RegisterNetEvent('weapon:client:AddAmmo')
AddEventHandler('weapon:client:AddAmmo', function(type, amount, itemData)
RegisterNetEvent('weapon:client:AddAmmo', function(type, amount, itemData)
local ped = PlayerPedId()
local weapon = GetSelectedPedWeapon(ped)
if CurrentWeaponData ~= nil then
if CurrentWeaponData then
if QBCore.Shared.Weapons[weapon]["name"] ~= "weapon_unarmed" and QBCore.Shared.Weapons[weapon]["ammotype"] == type:upper() then
local total = GetAmmoInPedWeapon(ped, weapon)
local found, maxAmmo = GetMaxAmmo(ped, weapon)
Expand All @@ -98,7 +88,7 @@ AddEventHandler('weapon:client:AddAmmo', function(type, amount, itemData)
disableMouse = false,
disableCombat = true,
}, {}, {}, {}, function() -- Done
if QBCore.Shared.Weapons[weapon] ~= nil then
if QBCore.Shared.Weapons[weapon] then
AddAmmoToPed(ped,weapon,amount)
TaskReloadWeapon(ped)
TriggerServerEvent("weapons:server:AddWeaponAmmo", CurrentWeaponData, total + amount)
Expand All @@ -120,74 +110,93 @@ AddEventHandler('weapon:client:AddAmmo', function(type, amount, itemData)
end
end)

RegisterNetEvent('QBCore:Client:OnPlayerLoaded')
AddEventHandler('QBCore:Client:OnPlayerLoaded', function()
isLoggedIn = true
PlayerData = QBCore.Functions.GetPlayerData()

QBCore.Functions.TriggerCallback("weapons:server:GetConfig", function(RepairPoints)
for k, data in pairs(RepairPoints) do
Config.WeaponRepairPoints[k].IsRepairing = data.IsRepairing
Config.WeaponRepairPoints[k].RepairingData = data.RepairingData
end
end)
end)

AddEventHandler('onResourceStart', function(resource)
if resource == GetCurrentResourceName() then
Wait(1000)
isLoggedIn = true
PlayerData = QBCore.Functions.GetPlayerData()

QBCore.Functions.TriggerCallback("weapons:server:GetConfig", function(RepairPoints)
for k, data in pairs(RepairPoints) do
Config.WeaponRepairPoints[k].IsRepairing = data.IsRepairing
Config.WeaponRepairPoints[k].RepairingData = data.RepairingData
RegisterNetEvent("weapons:client:EquipAttachment", function(ItemData, attachment)
local ped = PlayerPedId()
local weapon = GetSelectedPedWeapon(ped)
local WeaponData = QBCore.Shared.Weapons[weapon]
if weapon ~= GetHashKey("WEAPON_UNARMED") then
WeaponData.name = WeaponData.name:upper()
if WeaponAttachments[WeaponData.name] then
if WeaponAttachments[WeaponData.name][attachment]['item'] == ItemData.name then
TriggerServerEvent("weapons:server:EquipAttachment", ItemData, CurrentWeaponData, WeaponAttachments[WeaponData.name][attachment])
else
QBCore.Functions.Notify("This weapon does not support this attachment.", "error")
end
end)
end
end)

RegisterNetEvent('weapons:client:SetCurrentWeapon')
AddEventHandler('weapons:client:SetCurrentWeapon', function(data, bool)
if data ~= false then
CurrentWeaponData = data
end
else
CurrentWeaponData = {}
QBCore.Functions.Notify("You dont have a weapon in your hand.", "error")
end
CanShoot = bool
end)

RegisterNetEvent('QBCore:Client:OnPlayerUnload')
AddEventHandler('QBCore:Client:OnPlayerUnload', function()
isLoggedIn = false
-- Threads

for k, v in pairs(Config.WeaponRepairPoints) do
Config.WeaponRepairPoints[k].IsRepairing = false
Config.WeaponRepairPoints[k].RepairingData = {}
CreateThread(function()
SetWeaponsNoAutoswap(true)
end)

CreateThread(function()
while true do
local ped = PlayerPedId()
if IsPedArmed(ped, 7) == 1 and (IsControlJustReleased(0, 24) or IsDisabledControlJustReleased(0, 24)) then
local weapon = GetSelectedPedWeapon(ped)
local ammo = GetAmmoInPedWeapon(ped, weapon)
TriggerServerEvent("weapons:server:UpdateWeaponAmmo", CurrentWeaponData, tonumber(ammo))
if MultiplierAmount > 0 then
TriggerServerEvent("weapons:server:UpdateWeaponQuality", CurrentWeaponData, MultiplierAmount)
MultiplierAmount = 0
end
end
Wait(1)
end
end)

RegisterNetEvent('weapons:client:SetWeaponQuality')
AddEventHandler('weapons:client:SetWeaponQuality', function(amount)
if CurrentWeaponData ~= nil and next(CurrentWeaponData) ~= nil then
TriggerServerEvent("weapons:server:SetWeaponQuality", CurrentWeaponData, amount)
CreateThread(function()
while true do
if LocalPlayer.state['isLoggedIn'] then
local ped = PlayerPedId()
if CurrentWeaponData and next(CurrentWeaponData) then
if IsPedShooting(ped) or IsControlJustPressed(0, 24) then
if CanShoot then
local weapon = GetSelectedPedWeapon(ped)
local ammo = GetAmmoInPedWeapon(ped, weapon)
if QBCore.Shared.Weapons[weapon]["name"] == "weapon_snowball" then
TriggerServerEvent('QBCore:Server:RemoveItem', "snowball", 1)
elseif QBCore.Shared.Weapons[weapon]["name"] == "weapon_pipebomb" then
TriggerServerEvent('QBCore:Server:RemoveItem', "weapon_pipebomb", 1)
elseif QBCore.Shared.Weapons[weapon]["name"] == "weapon_molotov" then
TriggerServerEvent('QBCore:Server:RemoveItem', "weapon_molotov", 1)
elseif QBCore.Shared.Weapons[weapon]["name"] == "weapon_stickybomb" then
TriggerServerEvent('QBCore:Server:RemoveItem', "weapon_stickybomb", 1)
else
if ammo > 0 then
MultiplierAmount = MultiplierAmount + 1
end
end
else
local weapon = GetSelectedPedWeapon(ped)
if weapon ~= -1569615261 then
TriggerEvent('inventory:client:CheckWeapon', QBCore.Shared.Weapons[weapon]["name"])
QBCore.Functions.Notify("This weapon is broken and can not be used.", "error")
MultiplierAmount = 0
end
end
end
end
end
Wait(1)
end
end)

Citizen.CreateThread(function()
CreateThread(function()
while true do
if isLoggedIn then
if LocalPlayer.state['isLoggedIn'] then
local inRange = false
local ped = PlayerPedId()
local pos = GetEntityCoords(ped)

for k, data in pairs(Config.WeaponRepairPoints) do
local distance = #(pos - vector3(data.coords.x, data.coords.y, data.coords.z))

local distance = #(pos - data.coords)
if distance < 10 then
inRange = true

if distance < 1 then
if data.IsRepairing then
if data.RepairingData.CitizenId ~= PlayerData.citizenid then
Expand All @@ -200,7 +209,7 @@ Citizen.CreateThread(function()
end
end
else
if CurrentWeaponData ~= nil and next(CurrentWeaponData) ~= nil then
if CurrentWeaponData and next(CurrentWeaponData) then
if not data.RepairingData.Ready then
local WeaponData = QBCore.Shared.Weapons[GetHashKey(CurrentWeaponData.name)]
local WeaponClass = (QBCore.Shared.SplitStr(WeaponData.ammotype, "_")[2]):lower()
Expand Down Expand Up @@ -236,52 +245,10 @@ Citizen.CreateThread(function()
end
end
end

if not inRange then
Citizen.Wait(1000)
end
end
Citizen.Wait(3)
end
end)

RegisterNetEvent("weapons:client:SyncRepairShops")
AddEventHandler("weapons:client:SyncRepairShops", function(NewData, key)
Config.WeaponRepairPoints[key].IsRepairing = NewData.IsRepairing
Config.WeaponRepairPoints[key].RepairingData = NewData.RepairingData
end)

RegisterNetEvent("weapons:client:EquipAttachment")
AddEventHandler("weapons:client:EquipAttachment", function(ItemData, attachment)
local ped = PlayerPedId()
local weapon = GetSelectedPedWeapon(ped)
local WeaponData = QBCore.Shared.Weapons[weapon]

if weapon ~= GetHashKey("WEAPON_UNARMED") then
WeaponData.name = WeaponData.name:upper()
if WeaponAttachments[WeaponData.name] ~= nil then
if WeaponAttachments[WeaponData.name][attachment] ~= nil then
TriggerServerEvent("weapons:server:EquipAttachment", ItemData, CurrentWeaponData, WeaponAttachments[WeaponData.name][attachment])
else
QBCore.Functions.Notify("This weapon does not support this attachment.", "error")
Wait(1000)
end
end
else
QBCore.Functions.Notify("You dont have a weapon in your hand.", "error")
Wait(3)
end
end)

RegisterNetEvent("addAttachment")
AddEventHandler("addAttachment", function(component)
local ped = PlayerPedId()
local weapon = GetSelectedPedWeapon(ped)
local WeaponData = QBCore.Shared.Weapons[weapon]
GiveWeaponComponentToPed(ped, GetHashKey(WeaponData.name), GetHashKey(component))
end)

RegisterNetEvent('weapons:client:EquipTint')
AddEventHandler('weapons:client:EquipTint', function(tint)
local player = PlayerPedId()
local weapon = GetSelectedPedWeapon(player)
SetPedWeaponTintIndex(player, weapon, tint)
end)
end)
4 changes: 2 additions & 2 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Config.DurabilityMultiplier = {

Config.WeaponRepairPoints = {
[1] = {
coords = vector4(964.02, -1267.41, 34.97, 35.5),
coords = vector3(964.02, -1267.41, 34.97),
IsRepairing = false,
RepairingData = {},
}
Expand Down Expand Up @@ -138,7 +138,7 @@ WeaponAttachments = {
component = 'COMPONENT_PISTOL_VARMOD_LUXE',
label = 'Luxury Finish',
item = 'pistol_luxuryfinish',
},
},
},
['WEAPON_COMBATPISTOL'] = {
['defaultclip'] = {
Expand Down
Loading

0 comments on commit da18ca8

Please sign in to comment.