1
0
Fork 0
forked from Simnation/Main
Main/resources/[inventory]/nordi_pfand/server/main.lua

254 lines
9.3 KiB
Lua
Raw Normal View History

2025-07-29 00:00:56 +02:00
local QBCore = exports['qb-core']:GetCoreObject()
2025-07-29 00:02:35 +02:00
-- Store previous inventory states for all players
local playerInventories = {}
-- Event when an item is consumed
2025-07-29 00:00:56 +02:00
RegisterNetEvent('qb-pfandsystem:server:itemConsumed', function(itemName)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
2025-07-29 00:02:35 +02:00
-- Check if the consumed item generates a deposit
2025-07-29 00:00:56 +02:00
if Config.ConsumableItems[itemName] then
local consumableConfig = Config.ConsumableItems[itemName]
local pfandItem = consumableConfig.pfandItem
2025-07-29 00:02:35 +02:00
-- Wait briefly so the original item is consumed first
2025-07-29 00:00:56 +02:00
Wait(500)
2025-07-29 00:02:35 +02:00
-- Use Player.Functions.AddItem
2025-07-29 00:00:56 +02:00
local success = Player.Functions.AddItem(pfandItem, 1)
if success then
-- Trigger inventory update
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[pfandItem], "add", 1)
if Config.ShowPfandNotification then
local pfandLabel = Config.PfandItems[pfandItem] and Config.PfandItems[pfandItem].label or pfandItem
TriggerClientEvent('ox_lib:notify', src, {
title = 'Pfandsystem',
description = string.format(Config.Locale['pfand_received'], pfandLabel),
type = 'success',
duration = 3000
})
end
if Config.Debug then
print(string.format('[Pfandsystem] Spieler %s hat %s konsumiert und %s erhalten',
Player.PlayerData.name, itemName, pfandItem))
end
else
if Config.Debug then
print(string.format('[Pfandsystem] Fehler beim Hinzufügen von %s für Spieler %s', pfandItem, Player.PlayerData.name))
end
end
end
end)
2025-07-29 00:02:35 +02:00
-- Function to monitor inventory changes and handle deposit items
RegisterNetEvent('inventory:server:SetInventory', function(playerId, inventory)
local Player = QBCore.Functions.GetPlayer(playerId)
if not Player then return end
-- Initialize player's inventory tracking if it doesn't exist
if not playerInventories[playerId] then
playerInventories[playerId] = {}
for _, item in pairs(Player.PlayerData.items) do
if item then
playerInventories[playerId][item.name] = (playerInventories[playerId][item.name] or 0) + item.amount
end
end
return
end
-- Check for removed consumable items
for itemName, prevAmount in pairs(playerInventories[playerId]) do
if Config.ConsumableItems[itemName] then
local currentItem = Player.Functions.GetItemByName(itemName)
local currentAmount = currentItem and currentItem.amount or 0
-- If the amount decreased, it might have been consumed
if currentAmount < prevAmount then
local removedAmount = prevAmount - currentAmount
-- Schedule adding the deposit item with delay
SetTimeout(Config.PfandDelay or 1000, function()
local pfandItem = Config.ConsumableItems[itemName].pfandItem
if Config.Debug then
print(string.format('[Pfandsystem] Detected %d %s removed, adding %s after delay',
removedAmount, itemName, pfandItem))
end
-- Add the deposit item
local success = Player.Functions.AddItem(pfandItem, removedAmount)
if success then
TriggerClientEvent('inventory:client:ItemBox', playerId, QBCore.Shared.Items[pfandItem], "add", removedAmount)
if Config.ShowPfandNotification then
local pfandLabel = Config.PfandItems[pfandItem] and Config.PfandItems[pfandItem].label or pfandItem
TriggerClientEvent('ox_lib:notify', playerId, {
title = 'Pfandsystem',
description = string.format(Config.Locale['pfand_received'], pfandLabel),
type = 'success',
duration = 3000
})
end
end
end)
end
end
end
-- Update the previous inventory state
playerInventories[playerId] = {}
for _, item in pairs(Player.PlayerData.items) do
if item then
playerInventories[playerId][item.name] = (playerInventories[playerId][item.name] or 0) + item.amount
end
end
end)
-- Clean up player inventory tracking when they disconnect
AddEventHandler('playerDropped', function()
local src = source
if playerInventories[src] then
playerInventories[src] = nil
end
end)
-- Redeem deposit
2025-07-29 00:00:56 +02:00
RegisterNetEvent('qb-pfandsystem:server:redeemPfand', function(selectedItems)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local totalPfand = 0
local totalItems = 0
local canRedeem = true
2025-07-29 00:02:35 +02:00
-- Check if all items are available
2025-07-29 00:00:56 +02:00
for itemName, quantity in pairs(selectedItems) do
if Config.PfandItems[itemName] then
local item = Player.Functions.GetItemByName(itemName)
if not item or item.amount < quantity then
canRedeem = false
TriggerClientEvent('ox_lib:notify', src, {
title = 'Pfandsystem',
description = 'Du hast nicht genug ' .. (Config.PfandItems[itemName].label or itemName),
type = 'error'
})
return
end
end
end
if canRedeem then
2025-07-29 00:02:35 +02:00
-- Remove items and calculate deposit
2025-07-29 00:00:56 +02:00
for itemName, quantity in pairs(selectedItems) do
if Config.PfandItems[itemName] then
local removed = Player.Functions.RemoveItem(itemName, quantity)
if removed then
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "remove", quantity)
local pfandWert = Config.PfandItems[itemName].pfandwert * quantity
totalPfand = totalPfand + pfandWert
totalItems = totalItems + quantity
end
end
end
if totalPfand > 0 then
2025-07-29 00:02:35 +02:00
-- Give money
2025-07-29 00:00:56 +02:00
if Config.Currency == 'cash' then
Player.Functions.AddMoney('cash', totalPfand)
else
Player.Functions.AddMoney('bank', totalPfand)
end
2025-07-29 00:02:35 +02:00
-- Format money for display
2025-07-29 00:00:56 +02:00
local moneyString = string.format("€%.2f", totalPfand / 100)
TriggerClientEvent('ox_lib:notify', src, {
title = 'Pfandsystem',
description = string.format(Config.Locale['pfand_success'], moneyString, totalItems),
type = 'success'
})
if Config.Debug then
print(string.format('[Pfandsystem] Spieler %s hat %d Items für €%.2f eingelöst',
Player.PlayerData.name, totalItems, totalPfand / 100))
end
end
end
end)
2025-07-29 00:02:35 +02:00
-- Get available deposit items for the player
2025-07-29 00:00:56 +02:00
QBCore.Functions.CreateCallback('qb-pfandsystem:server:getPfandItems', function(source, cb)
local src = source
local pfandItems = {}
local Player = QBCore.Functions.GetPlayer(src)
if not Player then
cb(pfandItems)
return
end
for itemName, itemConfig in pairs(Config.PfandItems) do
local item = Player.Functions.GetItemByName(itemName)
if item and item.amount > 0 then
pfandItems[itemName] = {
count = item.amount,
label = itemConfig.label,
pfandwert = itemConfig.pfandwert,
totalWert = itemConfig.pfandwert * item.amount
}
end
end
cb(pfandItems)
end)
2025-07-29 00:02:35 +02:00
-- Additional event handlers for various inventory systems
2025-07-29 00:00:56 +02:00
RegisterNetEvent('inventory:server:UseItem')
AddEventHandler('inventory:server:UseItem', function(source, item)
local itemName = type(item) == "table" and item.name or item
if Config.ConsumableItems[itemName] then
SetTimeout(100, function()
TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName)
end)
end
end)
2025-07-29 00:02:35 +02:00
-- For tgiann-inventory
2025-07-29 00:00:56 +02:00
RegisterNetEvent('tgiann-inventory:itemUsed')
AddEventHandler('tgiann-inventory:itemUsed', function(source, itemName)
if Config.ConsumableItems[itemName] then
SetTimeout(100, function()
TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName)
end)
end
end)
2025-07-29 00:02:35 +02:00
-- Debug command for manual triggering
2025-07-29 00:00:56 +02:00
RegisterCommand('pfanditem', function(source, args)
if Config.Debug then
if args[1] and source > 0 then
TriggerEvent('qb-pfandsystem:server:itemConsumed', args[1])
end
end
end)
2025-07-29 00:02:35 +02:00
-- Export for other scripts
2025-07-29 00:00:56 +02:00
exports('ConsumePfandItem', function(source, itemName)
TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName)
end)