forked from Simnation/Main
144 lines
4.9 KiB
Lua
144 lines
4.9 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)
|
|
}
|
|
|
|
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
|
|
|
|
-- 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')
|