1
0
Fork 0
forked from Simnation/Main
Main/resources/[inventory]/pickle_consumables/modules/pfandsystem/server.lua
2025-07-28 23:54:18 +02:00

126 lines
4.6 KiB
Lua

-- modules/pfandsystem/server.lua (vereinfacht mit QBCore)
local QBCore = exports['qb-core']:GetCoreObject()
-- Pfand einlösen
RegisterNetEvent('pickle_consumables: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
-- Prüfe ob alle Items verfügbar sind
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('QBCore:Notify', src, 'Du hast nicht genug ' .. (Config.PfandItems[itemName].label or itemName), "error")
return
end
end
end
if canRedeem then
-- Entferne Items und berechne Pfand
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
-- Gebe Geld
if Config.PfandSystem.currency == 'cash' then
Player.Functions.AddMoney('cash', totalPfand)
else
Player.Functions.AddMoney('bank', totalPfand)
end
-- Formatiere Geld für Anzeige
local moneyString = string.format("€%.2f", totalPfand / 100)
TriggerClientEvent('QBCore:Notify', src, string.format(_L('pfand_success'), moneyString, totalItems), "success")
end
end
end)
-- Hole verfügbare Pfand Items des Spielers
lib.callback.register('pickle_consumables:server:getPfandItems', function(source)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local pfandItems = {}
if not Player then return pfandItems 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
return pfandItems
end)
-- Füge einen direkten Event-Handler für die Item-Nutzung hinzu
RegisterNetEvent('pickle_consumables:server:useItem')
AddEventHandler('pickle_consumables:server:useItem', function(itemName)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
-- Prüfe ob das Item ein Pfand-Item generiert
local itemConfig = Config.Items[itemName]
if itemConfig and itemConfig.pfandItem and Config.PfandItems[itemConfig.pfandItem] then
-- Warte kurz, damit das Item erst konsumiert wird
Wait(500)
-- Gebe Pfand-Item
Player.Functions.AddItem(itemConfig.pfandItem, 1)
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemConfig.pfandItem], "add", 1)
if Config.PfandSystem.showNotification then
local pfandLabel = Config.PfandItems[itemConfig.pfandItem].label
TriggerClientEvent('QBCore:Notify', src, string.format(_L('pfand_received'), pfandLabel), "success")
end
end
end)
-- Überwache alle möglichen Events für Item-Nutzung
RegisterNetEvent('QBCore:Server:UseItem')
AddEventHandler('QBCore:Server:UseItem', function(item)
local src = source
local itemName = type(item) == "table" and item.name or item
TriggerEvent('pickle_consumables:server:useItem', itemName)
end)
RegisterNetEvent('inventory:server:UseItem')
AddEventHandler('inventory:server:UseItem', function(source, item)
local itemName = type(item) == "table" and item.name or item
TriggerEvent('pickle_consumables:server:useItem', itemName)
end)
-- Hook für Pickle Consumables Item-Nutzung
AddEventHandler('pickle_consumables:itemUsed', function(source, itemName, itemData, slot)
TriggerEvent('pickle_consumables:server:useItem', itemName)
end)