diff --git a/resources/[inventory]/nordi_vending/server.lua b/resources/[inventory]/nordi_vending/server.lua index dc425d6dc..9e877da76 100644 --- a/resources/[inventory]/nordi_vending/server.lua +++ b/resources/[inventory]/nordi_vending/server.lua @@ -77,6 +77,67 @@ RegisterNetEvent('vending:server:registerMachine', function(coords, prop) TriggerClientEvent('vending:client:refreshTargets', -1) end) +-- Sell vending machine +RegisterNetEvent('vending:server:sellMachine', function(coords, machineId) + local src = source + local Player = QBCore.Functions.GetPlayer(src) + if not Player then return end + + if not machineId then + machineId = getMachineIdByCoords(coords) + end + + if not machineId then + TriggerClientEvent('QBCore:Notify', src, 'Automat nicht gefunden!', 'error') + return + end + + local machine = vendingMachines[machineId] + + -- Check if player is owner + if machine.owner ~= Player.PlayerData.citizenid then + TriggerClientEvent('QBCore:Notify', src, 'Du bist nicht der Besitzer dieses Automaten!', 'error') + return + end + + -- Calculate sell price + local sellPrice = math.floor(Config.VendingMachinePrice * Config.SellBackPercentage / 100) + + -- Add money from machine to sell price + sellPrice = sellPrice + machine.money + + -- Give money to player + Player.Functions.AddMoney('cash', sellPrice) + + -- Empty stash first + local stashItems = exports["tgiann-inventory"]:GetSecondaryInventoryItems("stash", machine.stash) + if stashItems then + for slot, item in pairs(stashItems) do + if item.amount > 0 then + -- Try to add to player inventory first + if Player.Functions.AddItem(item.name, item.amount) then + exports["tgiann-inventory"]:RemoveItemFromSecondaryInventory("stash", machine.stash, item.name, item.amount, slot) + TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item.name], 'add', item.amount) + else + -- If player inventory is full, create a drop + exports["tgiann-inventory"]:RemoveItemFromSecondaryInventory("stash", machine.stash, item.name, item.amount, slot) + TriggerClientEvent('QBCore:Notify', src, 'Einige Items wurden auf den Boden fallen gelassen!', 'info') + -- If you have a drop system, you can create a drop here + end + end + end + end + + -- Delete from database + MySQL.Async.execute('DELETE FROM vending_machines WHERE id = ?', {machineId}) + + -- Remove from memory + vendingMachines[machineId] = nil + + TriggerClientEvent('QBCore:Notify', src, 'Automat verkauft für $' .. sellPrice .. '!', 'success') + TriggerClientEvent('vending:client:refreshTargets', -1) +end) + -- Check if player can manage machine function canManageMachine(playerId, machineId) local Player = QBCore.Functions.GetPlayer(playerId) @@ -149,7 +210,6 @@ RegisterNetEvent('vending:server:openStash', function(coords) label = 'Vending Machine #' .. machine.id }) end) - -- Set item price RegisterNetEvent('vending:server:setItemPrice', function(coords, itemName, price) local src = source @@ -651,3 +711,5 @@ QBCore.Commands.Add('vendingdebug', 'Debug vending machines (Admin Only)', {}, f TriggerClientEvent('QBCore:Notify', source, 'Keine Berechtigung!', 'error') end end, 'admin') + +