Files
jim-payments/server.lua

205 lines
8.2 KiB
Lua
Raw Normal View History

2022-02-05 15:44:29 +00:00
local QBCore = exports['qb-core']:GetCoreObject()
2022-06-10 14:32:05 +01:00
AddEventHandler('onResourceStart', function(resource) if GetCurrentResourceName() == resource then TriggerEvent("jim-shops:MakeStash") end
for k, v in pairs(Config.Products) do
for i = 1, #v do
if not QBCore.Shared.Items[Config.Products[k][i].name] then
print("Config.Products['"..k.."'] can't find item: "..Config.Products[k][i].name)
end
end
end
for k, v in pairs(Config.Locations) do
if v["products"] == nil then
print("Config.Locations['"..k.."'] can't find its product table")
end
end
2022-04-25 10:01:51 +01:00
end)
2022-06-10 14:32:05 +01:00
local function GetStashItems(stashId)
local items = {}
local result = MySQL.Sync.fetchScalar('SELECT items FROM stashitems WHERE stash = ?', {stashId})
if result then
local stashItems = json.decode(result)
if stashItems then
for k, item in pairs(stashItems) do
local itemInfo = QBCore.Shared.Items[item.name:lower()]
if itemInfo then
items[item.slot] = {
name = itemInfo["name"],
amount = tonumber(item.amount),
info = item.info ~= nil and item.info or "",
label = itemInfo["label"],
description = itemInfo["description"] ~= nil and itemInfo["description"] or "",
weight = itemInfo["weight"],
type = itemInfo["type"],
unique = itemInfo["unique"],
useable = itemInfo["useable"],
image = itemInfo["image"],
slot = item.slot,
}
end
end
2022-06-06 20:42:02 +01:00
end
2022-04-11 18:59:47 +01:00
end
2022-06-10 14:32:05 +01:00
return items
end
2022-06-06 20:42:02 +01:00
2022-06-10 14:32:05 +01:00
--Wrapper converting for opening shops externally
RegisterServerEvent('jim-shops:ShopOpen', function(shop, name, shoptable)
local data = { shoptable = { products = shoptable.items, label = shoptable.label, }, custom = true }
TriggerClientEvent('jim-shops:ShopMenu', source, data, true)
end)
2022-06-06 20:42:02 +01:00
2022-06-10 14:32:05 +01:00
RegisterServerEvent('jim-shops:GetItem', function(amount, billtype, item, shoptable, price, info, shop, num, nostash)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
--Inventory space checks
local give = true
local totalWeight = QBCore.Player.GetTotalWeight(Player.PlayerData.items)
local maxWeight = QBCore.Config.Player.MaxWeight
local slots = 0
for k, v in pairs(Player.PlayerData.items) do slots = slots +1 end
slots = Config.MaxSlots - slots
local balance = Player.Functions.GetMoney(tostring(billtype))
-- If too heavy:
if (totalWeight + (QBCore.Shared.Items[item].weight * amount)) > maxWeight then
TriggerClientEvent("QBCore:Notify", src, "Not enough space in inventory", "error") give = false
-- If unique and it would poof away:
elseif QBCore.Shared.Items[item].unique and (tonumber(slots) < tonumber(amount)) then
TriggerClientEvent("QBCore:Notify", src, "Not enough slots in inventory", "error") give = false
else
-- If its a weapon, do this:
if QBCore.Shared.Items[item].type == "weapon" then
if QBCore.Shared.Items[item].unique then
for i = 1, amount do
if Player.Functions.AddItem(item, 1) then
else TriggerClientEvent('QBCore:Notify', src, "Can't give item!", "error") give = false break end
Wait(10)
end
end
else
-- If item is unique:
if QBCore.Shared.Items[item].unique then
for i = 1, amount do
if Player.Functions.AddItem(item, 1, nil, info) then
else TriggerClientEvent('QBCore:Notify', src, "Can't give item!", "error") give = false break end
Wait(10)
end
else
if Player.Functions.AddItem(item, amount, false, info) then
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "add", amount)
else
TriggerClientEvent('QBCore:Notify', source, "Can't give item!", "error") give = false
2022-04-11 18:59:47 +01:00
end
end
end
2022-06-10 14:32:05 +01:00
if Config.Limit and not nostash then
stashItems = GetStashItems("["..shop.."("..num..")]")
for i = 1, #stashItems do
if stashItems[i].name == item then
if (stashItems[i].amount - amount) <= 0 then stashItems[i].amount = 0 else stashItems[i].amount = stashItems[i].amount - amount end
TriggerEvent('jim-shops:server:SaveStashItems', "["..shop.."("..num..")]", stashItems)
if Config.Debug then print("Removing "..QBCore.Shared.Items[item].label.." x"..amount.." from Shop's Stash: '["..shop.."("..num..")]") end
2022-02-20 15:30:15 +00:00
end
end
end
2022-06-10 14:32:05 +01:00
--Money checks
if give then
if balance >= (tonumber(price) * tonumber(amount)) then
Player.Functions.RemoveMoney(tostring(billtype), (tonumber(price) * tonumber(amount)), 'ticket-payment')
else
TriggerClientEvent("QBCore:Notify", src, "Not enough money", "error") return
end
end
2022-02-20 15:30:15 +00:00
end
2022-06-10 14:32:05 +01:00
--Make data to send back to main shop menu
local data = {}
data.shoptable = shoptable
custom = true
if Config.Limit and not nostash then
custom = nil
data.k = shop
data.l = num
2022-02-05 15:44:29 +00:00
end
2022-06-10 14:32:05 +01:00
TriggerClientEvent('jim-shops:ShopMenu', src, data, custom)
2022-02-05 15:44:29 +00:00
end)
2022-06-10 14:32:05 +01:00
RegisterNetEvent("jim-shops:MakeStash", function()
for k, v in pairs(Config.Locations) do
local stashTable = {}
for l, b in pairs(v["coords"]) do
for i = 1, #v["products"] do
if Config.Debug then print("MakeStash - Searching for item '"..v["products"][i].name.."'")
if not QBCore.Shared.Items[v["products"][i].name:lower()] then
print ("MakeStash - Can't find item '"..v["products"][i].name.."'")
end
2022-04-21 19:53:48 +01:00
end
2022-06-10 14:32:05 +01:00
local itemInfo = QBCore.Shared.Items[v["products"][i].name:lower()]
stashTable[i] = {
name = itemInfo["name"],
amount = tonumber(v["products"][i].amount),
info = {},
label = itemInfo["label"],
description = itemInfo["description"] ~= nil and itemInfo["description"] or "",
weight = itemInfo["weight"],
type = itemInfo["type"],
unique = itemInfo["unique"],
useable = itemInfo["useable"],
image = itemInfo["image"],
slot = i,
}
end
2022-06-10 14:32:05 +01:00
if Config.Limit then TriggerEvent('jim-shops:server:SaveStashItems', "["..k.."("..l..")]", stashTable)
elseif Config.Limit == false then stashname = "["..k.."("..l..")]" MySQL.Async.execute('DELETE FROM stashitems WHERE stash= ?', {stashname}) end
2022-02-05 15:44:29 +00:00
end
2022-06-10 14:32:05 +01:00
end
2022-02-05 15:44:29 +00:00
end)
2022-06-10 14:32:05 +01:00
--Compatability Wrapper Event for qb-truckerjob to refill shop stashes
RegisterNetEvent("qb-shops:server:RestockShopItems", function(storeinfo)
local k, l = nil
local storename = storeinfo
if string.find(storename, "247supermarket") then k = "247supermarket"
elseif string.find(storename, "hardware") then k = "hardware"
elseif string.find(storename, "robsliquor") then k = "robsliquor"
elseif string.find(storename, "ltdgasoline") then k = "ltdgasoline"
end
2022-06-10 14:32:05 +01:00
l = storename:gsub(k,"")
if l == "" then l = 1 end
local stashTable = {}
for i = 1, #Config.Locations[k]["products"] do
if Config.Debug then print("RestockShopItems - Searching for item '"..v["products"][i].name.."'")
if not QBCore.Shared.Items[v["products"][i].name:lower()] then
print ("RestockShopItems - Can't find item '"..v["products"][i].name.."'")
end
end
local itemInfo = QBCore.Shared.Items[Config.Locations[k]["products"][i].name:lower()]
stashTable[i] = {
name = itemInfo["name"],
amount = tonumber(Config.Locations[k]["products"][i].amount),
info = {},
label = itemInfo["label"],
description = itemInfo["description"] ~= nil and itemInfo["description"] or "",
weight = itemInfo["weight"],
type = itemInfo["type"],
unique = itemInfo["unique"],
useable = itemInfo["useable"],
image = itemInfo["image"],
slot = i,
}
end
if Config.Limit then TriggerEvent('jim-shops:server:SaveStashItems', "["..k.."("..l..")]", stashTable) end
end)
2022-06-10 14:32:05 +01:00
QBCore.Functions.CreateCallback('jim-shops:server:getLicenseStatus', function(source, cb)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local licenseTable = Player.PlayerData.metadata["licences"]
local licenseItem = Player.Functions.GetItemByName("weaponlicense")
cb(licenseTable.weapon, licenseItem)
2022-06-08 22:15:39 +01:00
end)
2022-06-10 14:32:05 +01:00
QBCore.Functions.CreateCallback('jim-shops:server:GetStashItems', function(source, cb, stashId) cb(GetStashItems(stashId)) end)
RegisterNetEvent('jim-shops:server:SaveStashItems', function(stashId, items) MySQL.Async.insert('INSERT INTO stashitems (stash, items) VALUES (:stash, :items) ON DUPLICATE KEY UPDATE items = :items', { ['stash'] = stashId, ['items'] = json.encode(items) }) end)