Skip to content

Commit fef5682

Browse files
authored
Add qbox support (#5)
1 parent a3bd0a3 commit fef5682

6 files changed

Lines changed: 159 additions & 74 deletions

File tree

README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ This is a simple addon for [Waypoint Placeables](https://github.com/WaypointRP/w
2424
2. Enable the script in your server.cfg
2525
- Be sure to start this script after `wp-placeables`
2626

27-
3. Add this to your items.lua:
27+
3. Add the items to your inventory items.lua:
28+
<details>
29+
<summary> QB Inventory </summary>
30+
2831
```lua
2932
printerdocument = {name = "printerdocument", label = "Document", weight = 500, type = "item", image = "printerdocument.png", unique = true, useable = true, shouldClose = true, combinable = nil, description = "A nice document"},
3033
printer = {name = "printer", label = "Printer", weight = 5000, type = "item", image = "printer1.png", unique = true, useable = true, shouldClose = true, combinable = nil, description = "Print a nice document"},
@@ -33,6 +36,59 @@ This is a simple addon for [Waypoint Placeables](https://github.com/WaypointRP/w
3336
printer4 = {name = "printer4", label = "Printer", weight = 5000, type = "item", image = "printer4.png", unique = true, useable = true, shouldClose = true, combinable = nil, description = "Print a nice document"},
3437
photocopier = {name = "photocopier", label = "Photocopier", weight = 5000, type = "item", image = "photocopier.png", unique = true, useable = true, shouldClose = true, combinable = nil, description = "Make a lot of copies"},
3538
```
39+
</details>
40+
41+
<details>
42+
<summary> OX Inventory </summary>
43+
44+
```lua
45+
["printerdocument"] = {
46+
label = "Document",
47+
weight = 500,
48+
stack = false,
49+
close = true,
50+
description = "A nice document"
51+
},
52+
["printer"] = {
53+
label = "Printer",
54+
weight = 5000,
55+
stack = true,
56+
close = true,
57+
description = "Print a nice document",
58+
client = {
59+
image = 'printer1.png',
60+
}
61+
},
62+
["printer2"] = {
63+
label = "Printer",
64+
weight = 5000,
65+
stack = true,
66+
close = true,
67+
description = "Print a nice document"
68+
},
69+
["printer3"] = {
70+
label = "Printer",
71+
weight = 5000,
72+
stack = true,
73+
close = true,
74+
description = "Print a nice document",
75+
},
76+
["printer4"] = {
77+
label = "Printer",
78+
weight = 5000,
79+
stack = true,
80+
close = true,
81+
description = "Print a nice document",
82+
},
83+
["photocopier"] = {
84+
label = "Photocopier",
85+
weight = 5000,
86+
stack = true,
87+
close = true,
88+
description = "Make a lot of copies",
89+
},
90+
```
91+
</details>
3692
4. In `wp-placeables/shared/config.lua`, search for `-- Uncomment this line if you are using wp-printer` and uncomment the following lines:
3793
```lua
3894
local printerCustomTargetOptions = {
@@ -53,7 +109,7 @@ This is a simple addon for [Waypoint Placeables](https://github.com/WaypointRP/w
53109

54110
## Dependencies
55111
- [Waypoint Placeables](https://github.com/WaypointRP/wp-placeables)
56-
- QBCore / ESX / Or other frameworks (must implement framework specific solutions in framework.lua)
112+
- QBCore / Qbox / ESX / Or other frameworks (must implement framework specific solutions in framework.lua)
57113
- QBCore / ESX / OX for Notifications
58114

59115
## Credit

client.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
RegisterNetEvent('wp-printer:client:UseDocument', function(itemData)
1+
RegisterNetEvent("wp-printer:client:UseDocument", function(itemData)
22
local itemMetadata = GetItemMetadata(itemData)
33
local documentUrl = itemMetadata.url ~= nil and itemMetadata.url or false
44

55
SendNUIMessage({
66
action = "open",
7-
url = documentUrl
7+
url = documentUrl,
88
})
99
SetNuiFocus(true, false)
1010
end)
1111

12-
RegisterNetEvent('wp-printer:client:UsePrinter', function()
12+
RegisterNetEvent("wp-printer:client:UsePrinter", function()
1313
SendNUIMessage({
14-
action = "start"
14+
action = "start",
1515
})
1616
SetNuiFocus(true, true)
1717
end)
1818

19-
RegisterNUICallback('SaveDocument', function(data)
19+
RegisterNUICallback("SaveDocument", function(data)
2020
if data.url ~= nil then
21-
TriggerServerEvent('wp-printer:server:SaveDocument', data.url)
21+
TriggerServerEvent("wp-printer:server:SaveDocument", data.url)
2222
end
2323
end)
2424

25-
RegisterNUICallback('CloseDocument', function()
25+
RegisterNUICallback("CloseDocument", function()
2626
SetNuiFocus(false, false)
2727
end)

fxmanifest.lua

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
fx_version 'cerulean'
2-
game 'gta5'
1+
fx_version "cerulean"
2+
game "gta5"
33

4-
description 'Waypoint Printer'
5-
author 'BackSH00TER - Waypoint RP'
6-
version '1.0.2'
4+
description "Waypoint Printer"
5+
author "BackSH00TER - Waypoint RP"
6+
version "1.0.3"
77

88
shared_script {
99
-- '@ox_lib/init.lua', -- Uncomment this if you are planning to use any ox scripts (such as ox notify)
10-
'shared/config.lua',
11-
'shared/framework.lua',
10+
"shared/config.lua",
11+
"shared/framework.lua",
1212
}
1313

1414
client_scripts {
15-
'client.lua',
15+
"client.lua",
1616
}
1717

1818
server_scripts {
19-
'server.lua',
19+
"server.lua",
2020
}
2121

2222
ui_page "html/index.html"
@@ -25,4 +25,8 @@ files {
2525
"html/*",
2626
}
2727

28-
lua54 'yes'
28+
dependencies {
29+
"wp-placeables",
30+
}
31+
32+
lua54 "yes"

server.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
CreateUseableItem("printerdocument", function(source, item)
2-
TriggerClientEvent('wp-printer:client:UseDocument', source, item)
2+
TriggerClientEvent("wp-printer:client:UseDocument", source, item)
33
end)
44

5-
RegisterServerEvent('wp-printer:server:SaveDocument', function(url)
5+
RegisterServerEvent("wp-printer:server:SaveDocument", function(url)
66
local src = source
77
if url ~= nil then
88
local info = {}
99
info.url = url
10-
AddItem(src, 'printerdocument', 1, info)
10+
AddItem(src, "printerdocument", 1, info)
1111
end
1212
end)

shared/config.lua

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
Config = {}
22

3-
-- Frameworks
4-
-- Supported framework options are listed next to each option
5-
-- If the framework you are using is not listed, you will need to modify the framework.lua code to work with your framework
3+
------------------------------------
4+
-- FRAMEWORK / SCRIPT CONFIGURATION
5+
-- Adjust these settings to match the framework and scripts you are using
66
-- Note: If using ox for any option, enable @ox_lib/init.lua in the manifest!
7+
------------------------------------
78

8-
Config.Framework = 'qb' -- 'qb', 'esx'
9-
Config.Notify = 'qb' -- 'qb', 'esx', 'ox'
10-
Config.Inventory = 'qb' -- 'qb', 'esx', 'ox'
9+
--- @type "qb" | "qbx" | "esx"
10+
Config.Framework = "qb"
11+
12+
-- The notification script you are using.
13+
--- @type "qb" | "esx" | "ox" | "none"
14+
Config.Notify = "qb"
15+
16+
-- The inventory script you are using.
17+
--- @type "qb" | "esx" | "ox"
18+
Config.Inventory = "qb"
19+
20+
------------------------------------
21+
--- END FRAMEWORK / SCRIPT CONFIGURATION
22+
------------------------------------

shared/framework.lua

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,133 @@
11
-- IsDuplicityVersion - is used to determine if the function is called by the server or the client (true == from server)
22

33
--------------------- SHARED FUNCTIONS ---------------------
4-
Core = nil
5-
---@return table Core The core object of the framework
4+
local Core = nil
5+
--- @return table Core The core object of the framework
66
function GetCoreObject()
77
if not Core then
8-
if Config.Framework == 'esx' then
9-
Core = exports['es_extended']:getSharedObject()
10-
elseif Config.Framework == 'qb' then
11-
Core = exports['qb-core']:GetCoreObject()
8+
if Config.Framework == "esx" then
9+
Core = exports["es_extended"]:getSharedObject()
10+
elseif Config.Framework == "qb" or Config.Framework == "qbx" then
11+
Core = exports["qb-core"]:GetCoreObject()
1212
end
1313
end
1414
return Core
1515
end
1616

17-
Core = Config.Framework ~= 'none' and GetCoreObject() or nil
17+
Core = Config.Framework ~= "none" and GetCoreObject() or nil
1818

19-
---@param text string The text to show in the notification
20-
---@param notificationType string The type of notification to show ex: 'success', 'error', 'info'
21-
---@param src - number|nil The source of the player - only required when called from server side
19+
--- @param text string The text to show in the notification
20+
--- @param notificationType string The type of notification to show ex: 'success', 'error', 'info'
21+
--- @param src - number|nil The source of the player - only required when called from server side
2222
function Notify(text, notificationType, src)
2323
if IsDuplicityVersion() then
24-
if Config.Notify == 'esx' then
24+
if Config.Notify == "esx" then
2525
TriggerClientEvent("esx:showNotification", src, text)
26-
elseif Config.Notify == 'qb' then
27-
TriggerClientEvent('QBCore:Notify', src, text, notificationType)
28-
elseif Config.Notify == 'ox' then
26+
elseif Config.Notify == "qb" then
27+
TriggerClientEvent("QBCore:Notify", src, text, notificationType)
28+
elseif Config.Notify == "ox" then
2929
TriggerClientEvent("ox_lib:notify", src, {
3030
description = text,
31-
type = notificationType
31+
type = notificationType,
3232
})
3333
end
3434
else
35-
if Config.Notify == 'esx' then
35+
if Config.Notify == "esx" then
3636
Core.ShowNotification(text)
37-
elseif Config.Notify == 'qb' then
37+
elseif Config.Notify == "qb" then
3838
Core.Functions.Notify(text, notificationType)
39-
elseif Config.Notify == 'ox' then
39+
elseif Config.Notify == "ox" then
4040
lib.notify({
4141
description = text,
42-
type = notificationType
42+
type = notificationType,
4343
})
4444
end
4545
end
4646
end
4747

48-
---@param source number|nil The source of the player
49-
---@return table PlayerData The player data of the player
48+
--- @param source number|nil The source of the player
49+
--- @return table PlayerData The player data of the player
5050
function GetPlayerData(source)
5151
local Core = GetCoreObject()
5252
if IsDuplicityVersion() then
53-
if Config.Framework == 'esx' then
53+
if Config.Framework == "esx" then
5454
return Core.GetPlayerFromId(source)
55-
elseif Config.Framework == 'qb' then
56-
return Core.Functions.GetPlayer(source)
55+
elseif Config.Framework == "qb" then
56+
return Core.Functions.GetPlayer(source).PlayerData
57+
elseif Config.Framework == "qbx" then
58+
return exports.qbx_core:GetPlayer(source).PlayerData
5759
end
5860
else
59-
if Config.Framework == 'esx' then
61+
if Config.Framework == "esx" then
6062
return Core.GetPlayerData()
61-
elseif Config.Framework == 'qb' then
63+
elseif Config.Framework == "qb" then
6264
return Core.Functions.GetPlayerData()
65+
elseif Config.Framework == "qbx" then
66+
return exports.qbx_core:GetPlayerData()
6367
end
6468
end
6569
end
6670

6771
--------------------- CLIENT FUNCTIONS ---------------------
6872

6973
-- Returns the item metadata
70-
---@param itemData table The item data from using the item
71-
---@return table The metadata of the item
72-
function GetItemMetadata(itemData)
73-
if Config.Inventory == 'esx' then
74+
--- @param itemData table The item data from using the item
75+
--- @return table The metadata of the item
76+
function GetItemMetadata(itemData)
77+
if Config.Inventory == "esx" then
7478
-- ESX inventory does not support metadata by default, recommended to use ox_inventory instead
7579
print("GetItemMetadata is missing an implementation in the framework file for esx")
76-
elseif Config.Inventory == 'qb' then
80+
elseif Config.Inventory == "qb" then
7781
return itemData.info
78-
elseif Config.Inventory == 'ox' then
82+
elseif Config.Inventory == "ox" then
7983
return itemData.metadata
84+
else
85+
warn("Invalid Config.Inventory: <" .. tostring(Config.Inventory) .. ">. Update GetItemMetadata in framework.lua.")
8086
end
8187
end
8288

8389
--------------------- SERVER FUNCTIONS ---------------------
8490

8591
-- Registers a useable item
86-
---@param itemName string The name of the item to register
87-
---@param callbackFn function The function to call when the item is used
92+
--- @param itemName string The name of the item to register
93+
--- @param callbackFn function The function to call when the item is used
8894
function CreateUseableItem(itemName, callbackFn)
8995
if not IsDuplicityVersion() then return end
90-
if Config.Framework == 'esx' then
96+
if Config.Framework == "esx" then
9197
-- ESX returns the itemName as the second parameter, itemdata as the third parameter when calling the callback function
9298
-- We are interested in the itemData for our callback
93-
local function ESXCallback(source, itemName, itemData)
99+
local function ESXCallback(source, itemName, itemData)
94100
callbackFn(source, itemData)
95101
end
96102

97103
return Core.RegisterUsableItem(itemName, ESXCallback)
98-
elseif Config.Framework == 'qb' then
104+
elseif Config.Framework == "qb" then
99105
return Core.Functions.CreateUseableItem(itemName, callbackFn)
106+
107+
elseif Config.Framework == "qbx" then
108+
return exports.qbx_core:CreateUseableItem(itemName, callbackFn)
109+
else
110+
warn("Invalid Config.Framework: <" .. tostring(Config.Framework) .. ">. Update CreateUseableItem in framework.lua.")
100111
end
101112
end
102113

103114
-- Adds item to the players inventory
104-
---@param source number The source of the player
105-
---@param itemName string The name of the item to add
106-
---@param amount number The amount of the item to add
107-
---@param info table Metadata to add to the item
115+
--- @param source number The source of the player
116+
--- @param itemName string The name of the item to add
117+
--- @param amount number The amount of the item to add
118+
--- @param info table Metadata to add to the item
108119
function AddItem(source, itemName, amount, info)
109120
if not IsDuplicityVersion() then return end
110-
if Config.Inventory == 'esx' then
121+
if Config.Inventory == "esx" then
111122
local xPlayer = Core.GetPlayerFromId(source)
112123
return xPlayer.addInventoryItem(itemName, amount)
113-
elseif Config.Inventory == 'qb' then
124+
elseif Config.Inventory == "qb" then
114125
local Player = Core.Functions.GetPlayer(source)
115-
TriggerClientEvent('inventory:client:ItemBox', source, Core.Shared.Items[itemName], "add")
116-
return Player.Functions.AddItem(itemName, amount, nil, info)
117-
elseif Config.Inventory == 'ox' then
126+
TriggerClientEvent("inventory:client:ItemBox", source, Core.Shared.Items[itemName], "add")
127+
return Player.Functions.AddItem(itemName, amount, nil, info)
128+
elseif Config.Inventory == "ox" then
118129
exports.ox_inventory:AddItem(source, itemName, amount, info)
130+
else
131+
warn("Invalid Config.Inventory: <" .. tostring(Config.Inventory) .. ">. Update AddItem in framework.lua.")
119132
end
120133
end

0 commit comments

Comments
 (0)