From 24a830a13e263d1cb94e89a1c7274173962ce581 Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Fri, 17 Nov 2023 17:41:15 +0100 Subject: [PATCH] Update v1.4 --- VERSION | 2 +- client.lua | 15 +++++++++++++++ config.lua | 10 ++++++++++ fxmanifest.lua | 2 +- server.lua | 26 ++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index a58941b..840ca8c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3 \ No newline at end of file +1.4 \ No newline at end of file diff --git a/client.lua b/client.lua index cfcff99..f5e0460 100644 --- a/client.lua +++ b/client.lua @@ -1,5 +1,20 @@ ESX = exports["es_extended"]:getSharedObject() +RegisterNetEvent('msk_weaponammoitem:checkAmmo') +AddEventHandler('msk_weaponammoitem:checkAmmo', function(item, data) + local playerPed = PlayerPedId() + local hash = GetSelectedPedWeapon(playerPed) + + if IsPedArmed(playerPed, 4) then + if hash then + if not Config.AmmoPacks[item] then return print(('[ITEM] [Add Ammo] Item ^3%s^0 is not configured in config.lua'):format(item)) end + local weapon = ESX.GetWeaponFromHash(hash) + + TriggerServerEvent('msk_weaponammoitem:addWeaponAmmo', weapon.name, item, data) + end + end +end) + CreateThread(function() while true do local sleep = 100 -- Please don't touch otherwise you will break the Script diff --git a/config.lua b/config.lua index a3d0b96..26a054f 100644 --- a/config.lua +++ b/config.lua @@ -2,6 +2,16 @@ Config = {} ---------------------------------------------------------------- Config.VersionChecker = true ---------------------------------------------------------------- +Config.AmmoPacks = { + -- Upload this item into your database! + + ['9mm_magazine'] = {item = '9mm', amount = 12}, + ['12gauge_magazine'] = {item = '12gauge', amount = 25}, + ['556mm_magazine'] = {item = '556mm', amount = 30}, + ['762mm_magazine'] = {item = '762mm', amount = 30}, + ['68kal_magazine'] = {item = '68kal', amount = 30}, +} + Config.Ammunition = { ['9mm'] = { -- Upload this item into your database! 'WEAPON_PISTOL', diff --git a/fxmanifest.lua b/fxmanifest.lua index 0dd0c8f..e8958ee 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -4,7 +4,7 @@ games { 'gta5' } author 'Musiker15 - MSK Scripts' name 'msk_weaponammoitems' description 'Weapon Ammunition with Items' -version '1.3' +version '1.4' lua54 'yes' diff --git a/server.lua b/server.lua index b99f6be..265e6b9 100644 --- a/server.lua +++ b/server.lua @@ -1,5 +1,31 @@ ESX = exports["es_extended"]:getSharedObject() +for item, v in pairs(Config.AmmoPacks) do + ESX.RegisterUsableItem(item, function(playerId) + local src = playerId + local xPlayer = ESX.GetPlayerFromId(src) + + xPlayer.triggerEvent('msk_weaponammoitem:checkAmmo', item, v) + end) +end + +RegisterNetEvent('msk_weaponammoitem:addWeaponAmmo') +AddEventHandler('msk_weaponammoitem:addWeaponAmmo', function(weaponName, item, data) + local src = source + local xPlayer = ESX.GetPlayerFromId(src) + local hasItem = xPlayer.getInventoryItem(item) + if not hasItem or hasItem.count == 0 then return end + + xPlayer.removeInventoryItem(item, 1) + xPlayer.addInventoryItem(data.item, data.amount) + + local hasAmmo = xPlayer.getInventoryItem(data.item) + if not hasAmmo or hasAmmo.count == 0 then return end + + xPlayer.updateWeaponAmmo(weaponName, hasAmmo.count + data.amount) + SetPedAmmo(GetPlayerPed(xPlayer.source), joaat(weaponName), hasAmmo.count + data.amount) +end) + RegisterNetEvent('msk_weaponammoitem:updateWeaponAmmo') AddEventHandler('msk_weaponammoitem:updateWeaponAmmo', function(item, weaponName, isShooting) local src = source