1
0
Fork 0
forked from Simnation/Main
Main/resources/[inventory]/nordi_trashcan/server.lua
2025-07-28 02:37:52 +02:00

229 lines
8.2 KiB
Lua

local QBCore = exports['qb-core']:GetCoreObject()
-- Table to track all trash cans in the world
local trashCans = {}
-- Initialize the script
Citizen.CreateThread(function()
print("^2[TRASH-SYSTEM]^7 Initializing trash can emptying system...")
-- Load existing trash cans from database if needed
InitializeTrashCans()
-- Start the emptying cycle
StartEmptyingCycle()
end)
-- Initialize trash cans
function InitializeTrashCans()
-- You could load from database here if you want persistence between server restarts
-- For now, we'll just initialize an empty table
trashCans = {}
print("^2[TRASH-SYSTEM]^7 Trash cans initialized")
end
-- Register a new trash can when it's first used
RegisterNetEvent('trash:server:registerTrashCan', function(containerID)
if not trashCans[containerID] then
trashCans[containerID] = {
lastEmptied = os.time(),
nextEmptyTime = os.time() + (Config.EmptyInterval * 60)
}
print("^2[TRASH-SYSTEM]^7 New trash can registered: " .. containerID)
end
end)
-- Container inventory open
RegisterNetEvent('trash:server:openInventory', function(containerID)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
-- Register this trash can if it's new
TriggerEvent('trash:server:registerTrashCan', containerID)
-- Open the inventory
exports["tgiann-inventory"]:OpenInventory(src, "stash", containerID, {
maxweight = Config.TrashCanInventory.maxweight,
slots = Config.TrashCanInventory.slots,
label = Config.TrashCanInventory.label
})
end)
-- Start the emptying cycle
function StartEmptyingCycle()
Citizen.CreateThread(function()
while true do
-- Check based on configured frequency
Citizen.Wait(Config.CheckFrequency * 60 * 1000)
EmptyTrashCans()
end
end)
print("^2[TRASH-SYSTEM]^7 Trash emptying cycle started - will check every " .. Config.CheckFrequency .. " minutes")
end
-- Empty trash cans that are due
function EmptyTrashCans()
local currentTime = os.time()
local emptiedCount = 0
for containerID, data in pairs(trashCans) do
if currentTime >= data.nextEmptyTime then
-- Empty this trash can
EmptyTrashCan(containerID)
-- Update the timing
trashCans[containerID] = {
lastEmptied = currentTime,
nextEmptyTime = currentTime + (Config.EmptyInterval * 60)
}
-- Spawn random items if enabled
if Config.RandomItems.enabled then
SpawnRandomItems(containerID)
end
emptiedCount = emptiedCount + 1
end
end
if emptiedCount > 0 then
print("^2[TRASH-SYSTEM]^7 Emptied " .. emptiedCount .. " trash cans")
end
end
-- Empty a specific trash can
function EmptyTrashCan(containerID)
-- Get all items in the container
local items = exports["tgiann-inventory"]:GetSecondaryInventoryItems("stash", containerID)
if not items or next(items) == nil then
return -- No items to remove
end
local disposedItems = {}
local totalItems = 0
-- Dispose all items
for slot, item in pairs(items) do
if item and item.amount and item.amount > 0 then
local success = exports["tgiann-inventory"]:RemoveItemFromSecondaryInventory("stash", containerID, item.name, item.amount, slot)
if success then
table.insert(disposedItems, {name = item.name, amount = item.amount})
totalItems = totalItems + item.amount
end
end
end
if #disposedItems > 0 then
-- Log the emptying
print("^3[TRASH-SYSTEM]^7 Automatically emptied trash can " .. containerID .. " (" .. totalItems .. " items)")
-- Discord Webhook with item list if enabled
if Config.Logs.enabled then
local itemList = ""
for _, item in pairs(disposedItems) do
itemList = itemList .. '' .. item.amount .. 'x ' .. item.name .. '\n'
end
TriggerEvent('qb-log:server:CreateLog', Config.Logs.webhookName,
Config.Logs.emptyTitle,
Config.Logs.emptyColor,
'**Trash Can:** ' .. containerID ..
'\n**Action:** Automatic emptying' ..
'\n**Total Items:** ' .. totalItems ..
'\n**Items:**\n' .. itemList)
end
end
end
-- Spawn random items in a trash can with simplified probability system
function SpawnRandomItems(containerID)
-- Check if we should spawn items based on chance
if math.random(1, 100) > Config.RandomItems.chanceToSpawn then
return
end
-- Determine how many items to spawn
local itemCount = math.random(Config.RandomItems.minItems, Config.RandomItems.maxItems)
local spawnedItems = {}
-- Try to spawn each item based on its individual chance
for i = 1, itemCount do
-- Randomly select an item to try spawning
local itemToTry = Config.RandomItems.items[math.random(1, #Config.RandomItems.items)]
-- Check if this item should spawn based on its chance
if math.random(1, 100) <= itemToTry.chance then
-- Determine amount
local amount = math.random(itemToTry.amount[1], itemToTry.amount[2])
-- Add to inventory
exports["tgiann-inventory"]:AddItemToSecondaryInventory("stash", containerID, itemToTry.name, amount)
-- Track for logging
table.insert(spawnedItems, {name = itemToTry.name, amount = amount})
end
end
-- Log the spawned items
if #spawnedItems > 0 and Config.Logs.enabled then
local itemList = ""
for _, item in pairs(spawnedItems) do
itemList = itemList .. '' .. item.amount .. 'x ' .. item.name .. '\n'
end
TriggerEvent('qb-log:server:CreateLog', Config.Logs.webhookName,
Config.Logs.spawnTitle,
Config.Logs.spawnColor,
'**Trash Can:** ' .. containerID ..
'\n**Action:** Random items spawned' ..
'\n**Total Items:** ' .. #spawnedItems ..
'\n**Items:**\n' .. itemList)
end
end
-- Command to manually trigger trash emptying (admin only)
QBCore.Commands.Add('emptytrash', 'Empty all trash cans (Admin Only)', {}, false, function(source, args)
local Player = QBCore.Functions.GetPlayer(source)
if Player.PlayerData.permission == "admin" or Player.PlayerData.permission == "god" then
EmptyTrashCans()
TriggerClientEvent('ox_lib:notify', source, Config.Notifications.manualEmptySuccess)
else
TriggerClientEvent('ox_lib:notify', source, Config.Notifications.noPermission)
end
end, 'admin')
-- Command to manually spawn items in a trash can (admin only)
QBCore.Commands.Add('filltrash', 'Fill trash can with random items (Admin Only)', {}, false, function(source, args)
local Player = QBCore.Functions.GetPlayer(source)
if Player.PlayerData.permission == "admin" or Player.PlayerData.permission == "god" then
-- Get the trash can the player is looking at
TriggerClientEvent('trash:client:getTargetTrashCan', source)
else
TriggerClientEvent('ox_lib:notify', source, Config.Notifications.noPermission)
end
end, 'admin')
-- Receive the target trash can from client
RegisterNetEvent('trash:server:fillTargetTrashCan', function(containerID)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player or not containerID then return end
if Player.PlayerData.permission == "admin" or Player.PlayerData.permission == "god" then
-- Register this trash can if it's new
if not trashCans[containerID] then
TriggerEvent('trash:server:registerTrashCan', containerID)
end
-- Spawn items in it
SpawnRandomItems(containerID)
TriggerClientEvent('ox_lib:notify', src, {
title = 'System',
description = 'Mülltonne wurde mit zufälligen Items gefüllt',
type = 'success'
})
end
end)