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

136 lines
4.5 KiB
Lua
Raw Normal View History

2025-07-28 21:56:56 +02:00
local QBCore = exports['qb-core']:GetCoreObject()
-- Event wenn ein Item konsumiert wird
RegisterNetEvent('qb-pfandsystem:server:itemConsumed', function(itemName)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
-- Prüfe ob das konsumierte Item Pfand generiert
if Config.ConsumableItems[itemName] then
local consumableConfig = Config.ConsumableItems[itemName]
local pfandItem = consumableConfig.pfandItem
local chance = consumableConfig.chance or 100
-- Würfle ob Pfand erhalten wird
local randomChance = math.random(1, 100)
if randomChance <= chance then
2025-07-28 22:31:38 +02:00
-- Gebe Pfanditem mit tgiann-inventory Event
2025-07-28 22:23:18 +02:00
TriggerEvent('tgiann-inventory:server:addItem', src, pfandItem, 1)
2025-07-28 21:56:56 +02:00
2025-07-28 22:23:18 +02:00
if Config.ShowPfandNotification then
2025-07-28 21:56:56 +02:00
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
end
end
end)
2025-07-28 22:31:38 +02:00
-- Vereinfachte Pfand-Einlösung
2025-07-28 21:56: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
2025-07-28 22:31:38 +02:00
-- Berechne Gesamtpfand und entferne Items
2025-07-28 21:56:56 +02:00
for itemName, quantity in pairs(selectedItems) do
if Config.PfandItems[itemName] then
2025-07-28 22:31:38 +02:00
-- Entferne Item direkt
TriggerEvent('tgiann-inventory:server:removeItem', src, itemName, quantity)
local pfandWert = Config.PfandItems[itemName].pfandwert * quantity
totalPfand = totalPfand + pfandWert
totalItems = totalItems + quantity
2025-07-28 21:56:56 +02:00
end
end
2025-07-28 22:31:38 +02:00
if totalPfand > 0 then
-- Gebe Geld
if Config.Currency == 'cash' then
Player.Functions.AddMoney('cash', totalPfand)
else
Player.Functions.AddMoney('bank', totalPfand)
2025-07-28 21:56:56 +02:00
end
2025-07-28 22:31:38 +02:00
-- Formatiere Geld für Anzeige
local moneyString = string.format("€%.2f", totalPfand / 100)
2025-07-28 21:56:56 +02:00
2025-07-28 22:31:38 +02:00
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))
2025-07-28 21:56:56 +02:00
end
2025-07-28 22:31:38 +02:00
end
2025-07-28 21:56:56 +02:00
end)
2025-07-28 22:31:38 +02:00
-- Vereinfachte Item-Abfrage
2025-07-28 21:56:56 +02:00
QBCore.Functions.CreateCallback('qb-pfandsystem:server:getPfandItems', function(source, cb)
local src = source
local pfandItems = {}
2025-07-28 22:31:38 +02:00
-- Nutze Player.PlayerData.items direkt
local Player = QBCore.Functions.GetPlayer(src)
if not Player then
2025-07-28 22:23:18 +02:00
cb(pfandItems)
2025-07-28 22:31:38 +02:00
return
2025-07-28 21:56:56 +02:00
end
2025-07-28 22:23:18 +02:00
for itemName, itemConfig in pairs(Config.PfandItems) do
2025-07-28 22:31:38 +02:00
local itemCount = 0
-- Durchsuche Spieler-Inventar
for slot, item in pairs(Player.PlayerData.items) do
if item and item.name == itemName then
itemCount = itemCount + item.amount
2025-07-28 22:23:18 +02:00
end
2025-07-28 22:31:38 +02:00
end
if itemCount > 0 then
pfandItems[itemName] = {
count = itemCount,
label = itemConfig.label,
pfandwert = itemConfig.pfandwert,
totalWert = itemConfig.pfandwert * itemCount
}
end
2025-07-28 22:23:18 +02:00
end
2025-07-28 22:31:38 +02:00
cb(pfandItems)
2025-07-28 21:56:56 +02:00
end)
2025-07-28 22:31:38 +02:00
-- Event-Handler für Item-Nutzung
2025-07-28 22:23:18 +02:00
RegisterNetEvent('tgiann-inventory:server:itemUsed', function(source, itemName, itemData)
if Config.ConsumableItems[itemName] then
SetTimeout(100, function()
TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName)
end)
end
end)
-- Export für andere Scripts
2025-07-28 21:56:56 +02:00
exports('ConsumePfandItem', function(source, itemName)
TriggerEvent('qb-pfandsystem:server:itemConsumed', itemName)
end)