1
0
Fork 0
forked from Simnation/Main
Main/resources/[inventory]/pickle_consumables/modules/pfandsystem/server.lua

127 lines
4.6 KiB
Lua
Raw Normal View History

2025-07-28 23:39:25 +02:00
-- modules/pfandsystem/server.lua (vereinfacht mit QBCore)
2025-07-28 23:26:56 +02:00
local QBCore = exports['qb-core']:GetCoreObject()
2025-07-28 23:17:52 +02:00
-- Pfand einlösen
RegisterNetEvent('pickle_consumables:server:redeemPfand', function(selectedItems)
local src = source
2025-07-28 23:26:56 +02:00
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
2025-07-28 23:17:52 +02:00
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
2025-07-28 23:39:25 +02:00
local item = Player.Functions.GetItemByName(itemName)
if not item or item.amount < quantity then
2025-07-28 23:17:52 +02:00
canRedeem = false
2025-07-28 23:26:56 +02:00
TriggerClientEvent('QBCore:Notify', src, 'Du hast nicht genug ' .. (Config.PfandItems[itemName].label or itemName), "error")
2025-07-28 23:17:52 +02:00
return
end
end
end
if canRedeem then
-- Entferne Items und berechne Pfand
for itemName, quantity in pairs(selectedItems) do
if Config.PfandItems[itemName] then
2025-07-28 23:39:25 +02:00
local removed = Player.Functions.RemoveItem(itemName, quantity)
2025-07-28 23:17:52 +02:00
if removed then
2025-07-28 23:39:25 +02:00
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[itemName], "remove", quantity)
2025-07-28 23:17:52 +02:00
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
2025-07-28 23:26:56 +02:00
Player.Functions.AddMoney('cash', totalPfand)
2025-07-28 23:17:52 +02:00
else
2025-07-28 23:26:56 +02:00
Player.Functions.AddMoney('bank', totalPfand)
2025-07-28 23:17:52 +02:00
end
-- Formatiere Geld für Anzeige
local moneyString = string.format("€%.2f", totalPfand / 100)
2025-07-28 23:26:56 +02:00
TriggerClientEvent('QBCore:Notify', src, string.format(_L('pfand_success'), moneyString, totalItems), "success")
2025-07-28 23:17:52 +02:00
end
end
end)
-- Hole verfügbare Pfand Items des Spielers
2025-07-28 23:22:32 +02:00
lib.callback.register('pickle_consumables:server:getPfandItems', function(source)
2025-07-28 23:17:52 +02:00
local src = source
2025-07-28 23:39:25 +02:00
local Player = QBCore.Functions.GetPlayer(src)
2025-07-28 23:17:52 +02:00
local pfandItems = {}
2025-07-28 23:39:25 +02:00
if not Player then return pfandItems end
2025-07-28 23:17:52 +02:00
for itemName, itemConfig in pairs(Config.PfandItems) do
2025-07-28 23:39:25 +02:00
local item = Player.Functions.GetItemByName(itemName)
2025-07-28 23:17:52 +02:00
2025-07-28 23:39:25 +02:00
if item and item.amount > 0 then
2025-07-28 23:17:52 +02:00
pfandItems[itemName] = {
2025-07-28 23:39:25 +02:00
count = item.amount,
2025-07-28 23:17:52 +02:00
label = itemConfig.label,
pfandwert = itemConfig.pfandwert,
2025-07-28 23:39:25 +02:00
totalWert = itemConfig.pfandwert * item.amount
2025-07-28 23:17:52 +02:00
}
end
end
2025-07-28 23:22:32 +02:00
return pfandItems
2025-07-28 23:17:52 +02:00
end)
2025-07-28 23:39:25 +02:00
-- 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
2025-07-28 23:17:52 +02:00
-- 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
2025-07-28 23:39:25 +02:00
-- 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)
2025-07-28 23:17:52 +02:00
if Config.PfandSystem.showNotification then
local pfandLabel = Config.PfandItems[itemConfig.pfandItem].label
2025-07-28 23:39:25 +02:00
TriggerClientEvent('QBCore:Notify', src, string.format(_L('pfand_received'), pfandLabel), "success")
2025-07-28 23:17:52 +02:00
end
end
end)
2025-07-28 23:39:25 +02:00
-- Ü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)
2025-07-28 23:49:42 +02:00