forked from jimathy/jim-shops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
260 lines (248 loc) · 11.4 KB
/
Copy pathserver.lua
File metadata and controls
260 lines (248 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
local QBCore = exports['qb-core']:GetCoreObject()
AddEventHandler('onResourceStart', function(resource) if GetCurrentResourceName() ~= resource then return end
TriggerEvent("jim-shops:MakeStash")
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("^5Debug^7: ^3Config^7.^3Products^7['^6"..k.."^7'] ^2can't find item^7: ^6"..Config.Products[k][i].name.."^7")
end
end
end
for k, v in pairs(Config.Locations) do
if v["products"] == nil then
print("^5Debug^7: ^3Config^7.^3Locations^7['^6"..k.."^7']^2 can't find its product table^7")
end
end
end)
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
end
end
return items
end
--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)
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 totalWeight = QBCore.Player.GetTotalWeight(Player.PlayerData.items)
local maxWeight = QBCore.Config.Player.MaxWeight
local slots = 0
for _ 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")
-- 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")
else
--Money Check
if balance <= (tonumber(price) * tonumber(amount)) then -- Check for money first if not enough, stop here
TriggerClientEvent("QBCore:Notify", src, "Not enough money", "error") return
end
-- If its a weapon or a unique item, do this:
if QBCore.Shared.Items[item].type == "weapon" or QBCore.Shared.Items[item].unique then
if QBCore.Shared.Items[item].type == "weapon" then info = nil end
for i = 1, amount do -- Make a loop to put items into different slots rather than full amount in 1 slot
if Player.Functions.AddItem(item, 1, nil, info) then
if tonumber(i) == tonumber(amount) then -- when its on its last loop do this
Player.Functions.RemoveMoney(tostring(billtype), (tonumber(price) * tonumber(amount)), 'ticket-payment')
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "add", amount)
TriggerClientEvent("jim-shops:SellAnim", src, {item = item, shoptable = shoptable})
end
else
TriggerClientEvent('QBCore:Notify', src, "Can't give item!", "error") break -- stop the item giving loop
end
Wait(5)
end
else
-- if its a normal item, do normal things
if Player.Functions.AddItem(item, amount, nil, info) then
Player.Functions.RemoveMoney(tostring(billtype), (tonumber(price) * tonumber(amount)), 'ticket-payment')
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "add", amount)
TriggerClientEvent("jim-shops:SellAnim", src, {item = item, shoptable = shoptable})
else
TriggerClientEvent('QBCore:Notify', source, "Can't give item!", "error")
end
end
--Remove item from stash
if Config.Limit and not nostash then
local stashname = ""
if num == "" then stashname = shop
else stashname = "["..shop.."("..num..")]" end
stashItems = GetStashItems(stashname)
if Config.Debug then print("^5Debug^7: ^2Retrieving stash info^7: '^6"..stashname.."^7'") end
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', stashname, stashItems)
if Config.Debug then print("^5Debug^7: ^2Removing ^7'^6"..QBCore.Shared.Items[item].label.." ^2x^6"..amount.." ^2from Shop's Stash^7: '^6"..stashname.."^7'") end
end
end
end
end
--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
if num == "" then data.vendID = shop data.vend = true
else data.k = shop data.l = num end
end
TriggerClientEvent('jim-shops:ShopMenu', src, data, custom)
end)
RegisterNetEvent("jim-shops:MakeStash", function()
local result = MySQL.Sync.fetchAll('SELECT * FROM stashitems', {1})
for _, v in pairs(result) do --Clear Vending Machine Stashes
if string.find(v.stash, "Vend") then MySQL.Async.execute('DELETE FROM stashitems WHERE stash= ?', { v.stash }) end
end
for k, v in pairs(Config.Locations) do
if k ~= "vendingmachine" then
local stashTable = {}
for l, b in pairs(v["coords"]) do
MySQL.Async.execute('DELETE FROM stashitems WHERE stash= ?', {"["..k.."("..l..")]"})
Wait(10)
for i = 1, #v["products"] do
if Config.Debug then print("^5Debug^7: ^3MakeStash ^7- ^2Searching for item ^7'^6"..v["products"][i].name.."^7'")
if not QBCore.Shared.Items[v["products"][i].name:lower()] then
print("^5Debug^7: ^3MakeStash ^7- ^2Can't find item ^7'^6"..v["products"][i].name.."^7'")
end
end
local itemInfo = QBCore.Shared.Items[v["products"][i].name:lower()]
if itemInfo then
stashTable[i] = {
name = itemInfo["name"],
amount = tonumber(v["products"][i].amount),
info = v["products"][i].info or nil,
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,
}
if Config.RandomAmount then stashTable[i].amount = math.random(1, tonumber(v["products"][i].amount)) end
if itemInfo["name"] == "casinochips" then stashTable[i].amount = v["products"][i].amount end
end
end
if Config.Limit then TriggerEvent('jim-shops:server:SaveStashItems', "["..k.."("..l..")]", stashTable)
elseif not Config.Limit then stashname = "["..k.."("..l..")]" MySQL.Async.execute('DELETE FROM stashitems WHERE stash= ?', {stashname}) end
end
end
end
end)
RegisterNetEvent("jim-shops:GenerateVend", function(data)
local stashTable = {}
local v = data[1].shoptable
for i = 1, #v["products"] do
local itemInfo = QBCore.Shared.Items[v["products"][i].name:lower()]
if not itemInfo then print("^5Debug^7: ^3MakeStash ^7- ^2Can't find item ^7'^6"..v["products"][i].name.."^7'")
elseif itemInfo then
stashTable[i] = {
name = itemInfo["name"],
amount = tonumber(v["products"][i].amount),
info = v["products"][i].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 = i,
}
if Config.RandomAmount then stashTable[i].amount = math.random(1, tonumber(v["products"][i].amount)) end
end
end
TriggerEvent('jim-shops:server:SaveStashItems', data[2], stashTable)
end)
--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
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("^5Debug^7: ^3RestockShopItems ^7- ^3Searching for item ^7'^6"..v["products"][i].name.."^7'")
if not QBCore.Shared.Items[v["products"][i].name:lower()] then
print("^5Debug^7: ^3RestockShopItems ^7- ^1Can't ^2find item ^7'^6"..v["products"][i].name.."^7'")
end
end
local itemInfo = QBCore.Shared.Items[Config.Locations[k]["products"][i].name:lower()]
if itemInfo then
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
end
if Config.Limit then TriggerEvent('jim-shops:server:SaveStashItems', "["..k.."("..l..")]", stashTable) end
end)
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)
end)
RegisterNetEvent('jim-shops:server:sellChips', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player.Functions.GetItemByName("casinochips") then TriggerClientEvent("QBCore:Notify", src, "You don't have any "..QBCore.Shared.Items["casinochips"].label.." to sell") return
elseif Player.Functions.GetItemByName("casinochips") then
local amount = Player.Functions.GetItemByName("casinochips").amount
local price = Config.SellCasinoChips.pricePer * amount
if Player.Functions.RemoveItem("casinochips", amount) then
TriggerClientEvent('QBCore:Notify', src, "You sold your chips for $"..price)
Player.Functions.AddMoney("cash", price, "sold-casino-chips")
end
end
end)
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)