-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.lua
More file actions
351 lines (279 loc) · 12.8 KB
/
server.lua
File metadata and controls
351 lines (279 loc) · 12.8 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
-- ══════════════════════════════════════════════════════════════
-- uv-books · server.lua
-- Supports: QBCore, QBox (qbx_core), ox_inventory, jaksam_inventory, qb-inventory
-- ══════════════════════════════════════════════════════════════
local MAX_PAGES = 20
local MAX_CHARS = 800
-- ── Framework & inventory detection ──────────────────────────
local Framework = nil -- "qbx" | "qb"
local Inventory = nil -- "ox" | "qb" | "jaksam"
local QBCore = nil -- only populated when running plain QBCore
CreateThread(function()
-- Framework
if GetResourceState("qbx_core") == "started" then
Framework = "qbx"
print("[uv-books] Framework detected: QBox (qbx_core)")
elseif GetResourceState("qb-core") == "started" then
Framework = "qb"
QBCore = exports["qb-core"]:GetCoreObject()
print("[uv-books] Framework detected: QBCore")
else
print("[uv-books] ^1WARNING: No supported framework found!^0")
end
-- Inventory
if GetResourceState("jaksam_inventory") == "started" then
Inventory = "jaksam"
print("[uv-books] Inventory detected: jaksam_inventory (ox-compatible)")
elseif GetResourceState("ox_inventory") == "started" then
Inventory = "ox"
print("[uv-books] Inventory detected: ox_inventory")
elseif GetResourceState("qb-inventory") == "started" or
GetResourceState("qs-inventory") == "started" or
GetResourceState("ps-inventory") == "started" or
GetResourceState("lj-inventory") == "started" then
Inventory = "qb"
print("[uv-books] Inventory detected: qb-style inventory")
else
Inventory = (Framework == "qbx") and "ox" or "qb"
print("[uv-books] Inventory fallback: " .. Inventory)
end
end)
-- ── Helper: get player object ────────────────────────────────
local function GetPlayer(src)
if Framework == "qbx" then
return exports.qbx_core:GetPlayer(src)
elseif Framework == "qb" and QBCore then
return QBCore.Functions.GetPlayer(src)
end
return nil
end
-- ── Helper: send notification ────────────────────────────────
local function Notify(src, msg, nType)
if GetResourceState("ox_lib") == "started" then
TriggerClientEvent("ox_lib:notify", src, {
description = msg,
type = nType or "info",
})
elseif Framework == "qb" then
TriggerClientEvent("QBCore:Notify", src, msg, nType)
elseif Framework == "qbx" then
exports.qbx_core:Notify(src, msg, nType)
end
end
-- ── Helper: add item to player ───────────────────────────────
local function AddItem(src, item, count, metadata)
if Inventory == "ox" then
return exports.ox_inventory:AddItem(src, item, count, metadata)
elseif Inventory == "jaksam" then
return exports.jaksam_inventory:AddItem(src, item, count, metadata)
else
local Player = GetPlayer(src)
if Player then
return Player.Functions.AddItem(item, count, false, metadata)
end
end
return false
end
-- ── Helper: remove item from player ──────────────────────────
local function RemoveItem(src, item, count, slot)
if Inventory == "ox" then
return exports.ox_inventory:RemoveItem(src, item, count, nil, slot)
elseif Inventory == "jaksam" then
return exports.jaksam_inventory:RemoveItem(src, item, count, nil, slot)
else
local Player = GetPlayer(src)
if Player then
return Player.Functions.RemoveItem(item, count, slot)
end
end
return false
end
-- ── Helper: update item metadata ─────────────────────────────
local function SetMetadata(src, slot, metadata)
if Inventory == "ox" then
exports.ox_inventory:SetMetadata(src, slot, metadata)
return true
elseif Inventory == "jaksam" then
exports.jaksam_inventory:SetMetadata(src, slot, metadata)
return true
else
-- qb-style: remove and re-add with new metadata
local success = RemoveItem(src, "book", 1, slot)
if success then
return AddItem(src, "book", 1, metadata)
end
end
return false
end
-- ══════════════════════════════════════════════════════════════
-- Track active writers (slot info for draft saving)
-- ══════════════════════════════════════════════════════════════
local ActiveWriters = {} -- [src] = { slot = N }
-- ══════════════════════════════════════════════════════════════
-- Create book event
-- ══════════════════════════════════════════════════════════════
RegisterNetEvent("uv-books:server:createBook", function(bookData)
local src = source
local Player = GetPlayer(src)
if not Player then return end
if not bookData then return end
if not bookData.pages then return end
if type(bookData.pages) ~= "table" then
print("[uv-books] Exploit attempt (pages not table) from " .. src)
return
end
if #bookData.pages > MAX_PAGES then
print("[uv-books] Exploit attempt (too many pages) from " .. src)
return
end
local hasContent = false
for i = 0, MAX_PAGES - 1 do
local page = bookData.pages[i] or bookData.pages[tostring(i)] or ""
if type(page) ~= "string" then
print("[uv-books] Invalid page type from " .. src)
return
end
if string.len(page) > MAX_CHARS then
print("[uv-books] Exploit attempt (page too long) from " .. src)
return
end
if page ~= "" then hasContent = true end
end
if not hasContent then
Notify(src, "You can't publish an empty book.", "error")
return
end
local pageCount = 0
for _, page in pairs(bookData.pages) do
if type(page) == "string" and page ~= "" then
pageCount = pageCount + 1
end
end
local author = (bookData.signed and bookData.signature ~= "") and bookData.signature or "Unknown"
local genre = (bookData.genre and bookData.genre ~= "") and bookData.genre or nil
-- Build inventory description
local desc = '"' .. (bookData.title or "Untitled Book") .. '" by ' .. author
if genre then
desc = desc .. " · " .. genre
end
-- Remove the old book (blank or draft) from the slot they were writing in
local writerInfo = ActiveWriters[src]
if writerInfo and writerInfo.slot then
RemoveItem(src, "book", 1, writerInfo.slot)
end
ActiveWriters[src] = nil
local info = {
title = bookData.title or "Untitled Book",
author = author,
content = bookData.pages,
images = bookData.images or {},
genre = genre or "",
font = bookData.font or "",
signed = bookData.signed or false,
signature = bookData.signature or "",
description = desc,
}
local success = AddItem(src, "book", 1, info)
Notify(src, success and "Book published!" or "Failed to create book!", success and "success" or "error")
end)
-- ══════════════════════════════════════════════════════════════
-- Save draft event
-- ══════════════════════════════════════════════════════════════
RegisterNetEvent("uv-books:server:saveDraft", function(draftData)
local src = source
local Player = GetPlayer(src)
if not Player then return end
if not draftData then return end
local writerInfo = ActiveWriters[src]
if not writerInfo or not writerInfo.slot then
Notify(src, "Could not save draft.", "error")
return
end
local draftMeta = {
draft = {
title = draftData.title or "",
pages = draftData.pages or {},
images = draftData.images or {},
font = draftData.font or "",
}
}
local success = SetMetadata(src, writerInfo.slot, draftMeta)
ActiveWriters[src] = nil
if success then
Notify(src, "Draft saved!", "success")
else
Notify(src, "Failed to save draft.", "error")
end
end)
-- ══════════════════════════════════════════════════════════════
-- Register "book" as a useable item
-- ══════════════════════════════════════════════════════════════
local function OnBookUsed(src, item)
local info = (item and item.info) or (item and item.metadata) or {}
local slot = item and item.slot
-- Track the slot for draft saving
ActiveWriters[src] = { slot = slot }
if info.content and type(info.content) == "table" and next(info.content) ~= nil then
-- Published book → reader
ActiveWriters[src] = nil
TriggerClientEvent("uv-books:client:readBook", src, info)
elseif info.draft and type(info.draft) == "table" then
-- Draft book → writer with draft data loaded
TriggerClientEvent("uv-books:client:startWriting", src, info.draft)
else
-- Blank book → fresh writer
TriggerClientEvent("uv-books:client:startWriting", src)
end
end
-- ══════════════════════════════════════════════════════════════
-- 📚 Useable item — registration varies by inventory
-- ══════════════════════════════════════════════════════════════
-- ox_inventory export
exports("book", function(event, item, inventory, slot, data)
if event == "usingItem" then
local src = inventory.id
local slotData = exports.ox_inventory:GetSlot(src, slot)
local info = (slotData and slotData.metadata) or {}
-- Track slot
ActiveWriters[src] = { slot = slot }
if info.content and type(info.content) == "table" and next(info.content) ~= nil then
ActiveWriters[src] = nil
TriggerClientEvent("uv-books:client:readBook", src, info)
elseif info.draft and type(info.draft) == "table" then
TriggerClientEvent("uv-books:client:startWriting", src, info.draft)
else
TriggerClientEvent("uv-books:client:startWriting", src)
end
end
end)
-- For non-ox inventories
CreateThread(function()
while Framework == nil do Wait(100) end
if Inventory == "jaksam" then
exports["jaksam_inventory"]:registerUsableItem("book", function(playerId, item)
OnBookUsed(playerId, item)
end)
print("[uv-books] Registered useable item via jaksam_inventory")
elseif Inventory == "qb" then
if Framework == "qb" and QBCore then
QBCore.Functions.CreateUseableItem("book", function(source, item)
OnBookUsed(source, item)
end)
print("[uv-books] Registered useable item via QBCore")
elseif Framework == "qbx" then
local core = exports["qb-core"]:GetCoreObject()
if core then
core.Functions.CreateUseableItem("book", function(source, item)
OnBookUsed(source, item)
end)
print("[uv-books] Registered useable item via QBox bridge")
end
end
else
print("[uv-books] Using ox_inventory export for item registration")
end
end)
-- Clean up on player disconnect
AddEventHandler("playerDropped", function()
ActiveWriters[source] = nil
end)