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

183 lines
7.7 KiB
Lua
Raw Normal View History

2025-06-26 03:23:18 +02:00
if GetResourceState('qb-core') == 'started' then
2025-06-26 04:08:54 +02:00
QBCore = exports['qb-core']:GetCoreObject()
2025-06-26 03:23:18 +02:00
elseif GetResourceState('es_extended') == 'started' then
2025-06-26 04:08:54 +02:00
ESX = exports['es_extended']:getSharedObject()
2025-06-26 03:23:18 +02:00
end
local resourceName = 'pl_printer'
lib.versionCheck('pulsepk/pl_printer')
2025-07-14 00:12:05 +02:00
-- Ensure database table exists
MySQL.ready(function()
MySQL.Async.execute([[
CREATE TABLE IF NOT EXISTS `printer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_name` varchar(255) NOT NULL,
`image_link` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)
]], {}, function(result)
print("[DEBUG] Printer database table check completed")
end)
end)
2025-06-26 03:23:18 +02:00
RegisterServerEvent('pl_printer:insertImageData')
AddEventHandler('pl_printer:insertImageData', function(imageUrl, amount)
local Player = getPlayer(source)
local account = Config.Print.Account
local TotalBill = Config.Print.Price*amount
2025-07-14 00:12:05 +02:00
print("[DEBUG] Player attempting to print image: " .. tostring(imageUrl))
print("[DEBUG] Amount: " .. tostring(amount) .. ", Total cost: " .. tostring(TotalBill))
2025-06-26 03:23:18 +02:00
if GetPlayerAccountMoney(Player,account,TotalBill) then
local imageName = imageUrl:match(".*/(.*)$")
2025-07-14 00:12:05 +02:00
print("[DEBUG] Extracted image name: " .. tostring(imageName))
AddItem(source, amount, imageName)
2025-06-26 03:23:18 +02:00
if imageUrl and amount then
MySQL.Async.execute('INSERT INTO printer (image_name, image_link) VALUES (@image_name, @image_link)', {
['@image_name'] = tostring(imageName),
['@image_link'] = imageUrl
}, function(rowsChanged)
2025-07-14 00:12:05 +02:00
print("[DEBUG] Image saved to database: " .. imageName .. ", Rows changed: " .. tostring(rowsChanged))
2025-06-26 03:23:18 +02:00
end)
RemovePlayerMoney(Player,account,TotalBill)
TriggerClientEvent('pl_printer:notification',source,Locale("Money_Removed") .. TotalBill,'success')
else
_debug('[DEBUG] '..' Invalid data received for image. '..'')
end
else
2025-07-14 00:12:05 +02:00
print("[DEBUG] Player doesn't have enough money. Required: " .. tostring(TotalBill))
2025-06-26 03:23:18 +02:00
TriggerClientEvent('pl_printer:notification',source,Locale("not_enough"),'error')
end
end)
RegisterServerEvent('pl_printer:fetchImageLink')
2025-06-26 04:08:54 +02:00
AddEventHandler('pl_printer:fetchImageLink', function(imageName, playerSource)
local src = playerSource or source
local hasItem = HasItem(src)
if not hasItem then
print("[DEBUG] Player doesn't have the required item")
return
end
print("[DEBUG] Fetching image with name: " .. tostring(imageName))
2025-06-26 03:23:18 +02:00
MySQL.Async.fetchScalar('SELECT image_link FROM printer WHERE image_name = @imageName', {
['@imageName'] = imageName
}, function(imageLink)
if imageLink then
2025-06-26 04:08:54 +02:00
print("[DEBUG] Found image link: " .. tostring(imageLink))
TriggerClientEvent('pl_printer:showImage', src, imageLink)
2025-06-26 03:23:18 +02:00
else
2025-06-26 04:08:54 +02:00
print("[DEBUG] No Image Link Found for " .. tostring(imageName))
_debug('[DEBUG] '..' No Image Link Found for '..tostring(imageName)..'')
2025-06-26 03:23:18 +02:00
end
end)
end)
function AddItem(source, amount, imageName)
local src = source
local info = {
id = imageName
}
2025-07-14 00:12:05 +02:00
if GetResourceState('tgiann-inventory') == 'started' then
-- Add support for tgiann-inventory
exports["tgiann-inventory"]:AddItem(src, Config.ItemName, amount, nil, info, false)
-- Notification for item added
TriggerClientEvent('pl_printer:notification', src, Locale("item_received") or "You received a printed document", 'success')
elseif GetResourceState('qb-inventory') == 'started' then
2025-06-26 03:23:18 +02:00
if lib.checkDependency('qb-inventory', '2.0.0') then
2025-06-26 04:08:54 +02:00
exports['qb-inventory']:AddItem(src, Config.ItemName, amount, false, info)
2025-06-26 03:23:18 +02:00
TriggerClientEvent('qb-inventory:client:ItemBox', src, QBCore.Shared.Items[Config.ItemName], 'add', amount)
else
local Player = getPlayer(src)
2025-06-26 04:08:54 +02:00
Player.Functions.AddItem(Config.ItemName, amount, false, info)
2025-06-26 03:23:18 +02:00
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[Config.ItemName], "add")
end
elseif GetResourceState('ox_inventory') == 'started' then
2025-06-26 04:08:54 +02:00
exports.ox_inventory:AddItem(src, Config.ItemName, amount, {type = imageName}, false)
2025-06-26 03:23:18 +02:00
elseif GetResourceState('qs-inventory') == 'started' then
2025-06-26 04:08:54 +02:00
local itemMetadata = {id = imageName}
exports['qs-inventory']:AddItem(src, Config.ItemName, amount, false, itemMetadata)
2025-06-26 03:23:18 +02:00
end
end
2025-06-26 04:08:54 +02:00
-- Registriere verwendbare Items für verschiedene Frameworks
AddEventHandler('onResourceStart', function(resourceName)
if GetCurrentResourceName() ~= resourceName then return end
-- QB-Core Item Registration
if GetResourceState('qb-core') == 'started' then
QBCore.Functions.CreateUseableItem(Config.ItemName, function(source, item)
local src = source
local imageName = item.info and item.info.id
if imageName then
TriggerEvent('pl_printer:fetchImageLink', imageName, src)
else
print("[DEBUG] No image ID found in item info")
end
end)
end
-- ESX Item Registration
if GetResourceState('es_extended') == 'started' then
ESX.RegisterUsableItem(Config.ItemName, function(source, item)
local src = source
local metadata = item.metadata or {}
local imageName = metadata.id
if imageName then
TriggerEvent('pl_printer:fetchImageLink', imageName, src)
else
print("[DEBUG] No image ID found in item metadata")
end
end)
end
-- ox_inventory export
2025-06-26 03:23:18 +02:00
if GetResourceState('ox_inventory') == 'started' then
2025-06-26 04:08:54 +02:00
exports(Config.ItemName, function(event, item, inventory, slot, data)
2025-06-26 03:23:18 +02:00
if event == 'usingItem' then
local item_metadata = exports.ox_inventory:GetSlot(inventory.id, slot)
2025-06-26 04:08:54 +02:00
if item_metadata and item_metadata.metadata and item_metadata.metadata.type then
TriggerEvent('pl_printer:fetchImageLink', item_metadata.metadata.type, inventory.id)
else
print("[DEBUG] No metadata type found in ox_inventory item")
end
2025-06-26 03:23:18 +02:00
end
end)
end
2025-07-14 00:12:05 +02:00
-- tgiann-inventory item registration
if GetResourceState('tgiann-inventory') == 'started' then
-- Register the usable item with tgiann-inventory
exports['tgiann-inventory']:RegisterUsableItem(Config.ItemName, function(source, itemData)
local src = source
local metadata = itemData.metadata or itemData.info or {}
local imageName = metadata.id
if imageName then
print("[DEBUG] tgiann-inventory: Using document with image ID: " .. tostring(imageName))
TriggerEvent('pl_printer:fetchImageLink', imageName, src)
else
print("[DEBUG] tgiann-inventory: No image ID found in item metadata")
end
end)
print("[DEBUG] Registered " .. Config.ItemName .. " as usable item in tgiann-inventory")
end
2025-06-26 03:23:18 +02:00
end)
local WaterMark = function()
SetTimeout(1500, function()
print('^1['..resourceName..'] ^2Thank you for Downloading the Script^0')
print('^1['..resourceName..'] ^2If you encounter any issues please Join the discord https://discord.gg/c6gXmtEf3H to get support..^0')
print('^1['..resourceName..'] ^2Enjoy a secret 20% OFF any script of your choice on https://pulsescripts.tebex.io/freescript^0')
print('^1['..resourceName..'] ^2Using the coupon code: SPECIAL20 (one-time use coupon, choose wisely)^0')
end)
end
WaterMark()