From 398518a2e7bac897eebe37ebb752c22263e622a7 Mon Sep 17 00:00:00 2001 From: Giana Date: Sun, 19 Feb 2023 23:14:14 -0600 Subject: [PATCH] Implement item rewards --- config.lua | 55 ++++++++++++++++++++++++++++++++++++++++--------- server/main.lua | 43 +++++++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 17 deletions(-) diff --git a/config.lua b/config.lua index 02bb4ae..5dddee4 100644 --- a/config.lua +++ b/config.lua @@ -54,6 +54,11 @@ Config.NpcRenderDistance = 60 -- Render distance for NPCs (if NPC enabled for - 'itemName': Name of item - sell_quantity: Amount to sell in increments of - money_amount: Amount of money to reward per sell_quantity + - item_rewards: + - ['itemName'] (name of item) = itemAmount (amount of item to reward per sell_quantity) + - Example: + - ['sandwich'] = 2, + ['joint'] = math.random(1, 2) ]] Config.SellLocations = { [1] = { @@ -97,27 +102,45 @@ Config.SellLocations = { sellable_items = { ['weed_white-widow'] = { sell_quantity = 5, - money_amount = math.random(75, 120) + money_amount = math.random(75, 120), + item_rewards = { + ['joint'] = math.random(1, 2) + } }, ['weed_skunk'] = { sell_quantity = 5, - money_amount = math.random(75, 120) + money_amount = math.random(75, 120), + item_rewards = { + ['joint'] = math.random(1, 2) + } }, ['weed_purple-haze'] = { sell_quantity = 5, - money_amount = math.random(75, 120) + money_amount = math.random(75, 120), + item_rewards = { + ['joint'] = math.random(1, 2) + } }, ['weed_og-kush'] = { sell_quantity = 5, - money_amount = math.random(75, 120) + money_amount = math.random(75, 120), + item_rewards = { + ['joint'] = math.random(1, 2) + } }, ['weed_amnesia'] = { sell_quantity = 5, - money_amount = math.random(75, 120) + money_amount = math.random(75, 120), + item_rewards = { + ['joint'] = math.random(1, 2), + } }, ['weed_brick'] = { sell_quantity = 10, - money_amount = math.random(2000, 3000) + money_amount = math.random(2000, 3000), + item_rewards = { + ['joint'] = math.random(1, 8), + } } } }, @@ -162,7 +185,10 @@ Config.SellLocations = { sellable_items = { ['meth'] = { sell_quantity = 5, - money_amount = math.random(75, 120) + money_amount = math.random(75, 120), + item_rewards = { + + } } } }, @@ -207,7 +233,10 @@ Config.SellLocations = { sellable_items = { ['crack_baggy'] = { sell_quantity = 5, - money_amount = math.random(90, 170) + money_amount = math.random(90, 170), + item_rewards = { + + } } } }, @@ -252,11 +281,17 @@ Config.SellLocations = { sellable_items = { ['cokebaggy'] = { sell_quantity = 5, - money_amount = math.random(90, 185) + money_amount = math.random(90, 185), + item_rewards = { + + } }, ['coke_brick'] = { sell_quantity = 10, - money_amount = math.random(8500, 10000) + money_amount = math.random(8500, 10000), + item_rewards = { + + } } } } diff --git a/server/main.lua b/server/main.lua index 553ac2f..062fc0a 100644 --- a/server/main.lua +++ b/server/main.lua @@ -16,11 +16,23 @@ function rollForNotifyingPolice(source, policeAlertChance) end end +function rewardItems(source, items, itemNotificationsEnabled) + local src = source + local player = QBCore.Functions.GetPlayer(src) + for k, v in pairs(items) do + player.Functions.AddItem(v.name, v.amount) + if itemNotificationsEnabled then + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[v.name], 'add', v.amount) + end + end +end + -- Events -- RegisterNetEvent('g-drugselling:server:sellItems', function(sellLocationIndex, items, moneyType) local src = source local player = QBCore.Functions.GetPlayer(src) + local itemNotificationsEnabled = Config.SellLocations[sellLocationIndex].itemNotificationsEnabled if CachedPolice[src] == nil then DropPlayer(src, "Exploiting") return @@ -29,26 +41,33 @@ RegisterNetEvent('g-drugselling:server:sellItems', function(sellLocationIndex, i for k, v in pairs(items) do if player.Functions.RemoveItem(v.name, v.sellableQuantity) then Citizen.Wait(900) - if not player.Functions.AddMoney(moneyType, v.price) then + if v.price > 0 and not player.Functions.AddMoney(moneyType, v.price) then player.Functions.AddItem(v.name, v.sellableQuantity) - return + goto skipItem end - if Config.SellLocations[sellLocationIndex].itemNotificationsEnabled then + if itemNotificationsEnabled then TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[v.name], 'remove', v.sellableQuantity) end + if v.rewardItems and #v.rewardItems > 0 then + rewardItems(src, v.rewardItems, itemNotificationsEnabled) + end end + :: skipItem :: end rollForNotifyingPolice(src, Config.SellLocations[sellLocationIndex].policeAlertChance) else if player.Functions.RemoveItem(items[1].name, items[1].sellableQuantity) then - Citizen.Wait(800) - if not player.Functions.AddMoney(moneyType, items[1].price) then + Citizen.Wait(900) + if items[1].price > 0 and not player.Functions.AddMoney(moneyType, items[1].price) then player.Functions.AddItem(items[1].name, items[1].sellableQuantity) return end - if Config.SellLocations[sellLocationIndex].itemNotificationsEnabled then + if itemNotificationsEnabled then TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[items[1].name], 'remove', items[1].sellableQuantity) end + if items[1].rewardItems and #items[1].rewardItems > 0 then + rewardItems(src, items[1].rewardItems, itemNotificationsEnabled) + end end rollForNotifyingPolice(src, Config.SellLocations[sellLocationIndex].policeAlertChance) end @@ -67,10 +86,20 @@ QBCore.Functions.CreateCallback('g-drugselling:server:getSellableItems', functio local sellableQuantity = bundlesToSell * v.sell_quantity local price = v.money_amount * bundlesToSell local item = {} + local rewardItems = {} item.name = k item.sellableQuantity = sellableQuantity item.price = price - table.insert(items, item) + for k2, v2 in pairs(v.item_rewards) do + local rewardItem = {} + rewardItem.name = k2 + rewardItem.amount = v2 * bundlesToSell + table.insert(rewardItems, rewardItem) + end + item.rewardItems = rewardItems + if item.price > 0 or (item.price == 0 and (item.rewardItems and #item.rewardItems > 0)) then + table.insert(items, item) + end end end cb(items)