Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@ This resource was created as a free script for backpacks using ox_inventory
- 0.0 ms Usage
- Perisistent backpack prop added to back when in inventory
- Customizable item name and storage parameters
- Compatibility for ox_core, ESX, QBCore, whatever else running ox_inventory
- Compatibility for ox_core, ESX, QBCore, QBox whatever else running ox_inventory

## Installation

- Download this script
- Add backpack to inventory as it is in "Extra Information" below
- Add backpack image to inventory images (found in `wasabi_backpack/_inventory_images/backpack.png`)
- Put script in your `resources` directory
- ensure `wasabi_backpack` *after* `ox_lib` but *before* `ox_inventory`
- Add / Create items in ox_inventory as required.
- ensure `wasabi_backpack` *after* `ox_lib` AND `ox_inventory` (the way dependencies work)

# Dependencies
- ox_inventory
- ox_lib

## Extra Information
Item to add to `ox_inventory/data/items.lua`
```
['backpack'] = {
label = 'Backpack',
['backpack_small'] = {
label = 'Bag',
weight = 220,
stack = false,
consume = 0,
},
['backpack_Duffel'] = {
label = 'Bag',
weight = 220,
stack = false,
consume = 0,
client = {
export = 'wasabi_backpack.openBackpack'
}
},
```

Expand Down
168 changes: 107 additions & 61 deletions client/client.lua
Original file line number Diff line number Diff line change
@@ -1,77 +1,123 @@
-----------------For support, scripts, and more----------------
--------------- https://discord.gg/wasabiscripts -------------
---------------------------------------------------------------
lib.locale()
local config = lib.require('config')

local bagEquipped, bagObj
local hash = `p_michael_backpack_s`
local ox_inventory = exports.ox_inventory
local ped = cache.ped
local justConnect = true
if config.createBagObject then
local ox_inventory = exports.ox_inventory

local filtered1 = {}
local filtered2 = {}

for item_name, _ in pairs(config.backpacks) do
filtered1[item_name] = true
filtered2[#filtered2+1] = item_name
end



local function PutOnBag()
local x, y, z = table.unpack(GetOffsetFromEntityInWorldCoords(ped,0.0,3.0,0.5))
lib.requestModel(hash, 100)
bagObj = CreateObjectNoOffset(hash, x, y, z, true, false)
AttachEntityToEntity(bagObj, ped, GetPedBoneIndex(ped, 24818), 0.07, -0.11, -0.05, 0.0, 90.0, 175.0, true, true, false, true, 1, true)
bagEquipped = true
end

local function RemoveBag()
if DoesEntityExist(bagObj) then
DeleteObject(bagObj)
local currentBagObject = nil
local function onLoad()
LocalPlayer.state:set('wsb_bag', nil, true)
end
SetModelAsNoLongerNeeded(hash)
bagObj = nil
bagEquipped = nil
end

AddEventHandler('ox_inventory:updateInventory', function(changes)
if justConnect then
Wait(4500)
justConnect = nil
end
for k, v in pairs(changes) do
if type(v) == 'table' then
local count = ox_inventory:Search('count', 'backpack')
if count > 0 and (not bagEquipped or not bagObj) then
PutOnBag()
elseif count < 1 and bagEquipped then
RemoveBag()
AddEventHandler("Characters:Client:Spawn", onLoad)
RegisterNetEvent('esx:playerLoaded', onLoad)
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', onLoad)
RegisterNetEvent('ox:playerLoaded', onLoad)

AddEventHandler('onResourceStop', function(resourceName)
if resourceName == GetCurrentResourceName() then
if currentBagObject then
DeleteEntity(currentBagObject)
ClearPedTasks(cache.ped)
end
end
if type(v) == 'boolean' then
local count = ox_inventory:Search('count', 'backpack')
if count < 1 and bagEquipped then
RemoveBag()
end)

---@param data table?
local putOnBag = function(data)
if currentBagObject then return end
if not data then
local has_item = ox_inventory:Search('count', filtered2)
if has_item then
for name, count in pairs(has_item) do
if count and count > 0 then
data = config.createBagObject[name]
break
end
end
end
elseif type(data) == 'string' then
data = config.createBagObject[data]
else
return
end
end
end)
if not data then return end

lib.onCache('ped', function(value)
ped = value
end)
local x, y, z = table.unpack(GetOffsetFromEntityInWorldCoords(cache.ped, 0.0, 3.0, 0.5))

lib.onCache('vehicle', function(value)
if GetResourceState('ox_inventory') ~= 'started' then return end
if value then
RemoveBag()
else
local count = ox_inventory:Search('count', 'backpack')
if count and count >= 1 then
PutOnBag()
end
currentBagObject = {}

lib.requestModel(data.hash, 100)
currentBagObject.object = CreateObjectNoOffset(data.hash, x, y, z, true, false, false)
AttachEntityToEntity(currentBagObject.object, cache.ped, GetPedBoneIndex(cache.ped, data.bone), data.offest.x, data.offest.y, data.offest.z, data.rotation.x, data.rotation.y, data.rotation.z, true, true, false, true, 1, true)

currentBagObject.hash = data.hash
end
end)

exports('openBackpack', function(data, slot)
if not slot?.metadata?.identifier then
local identifier = lib.callback.await('wasabi_backpack:getNewIdentifier', 100, data.slot)
ox_inventory:openInventory('stash', 'bag_'..identifier)
else
TriggerServerEvent('wasabi_backpack:openBackpack', slot.metadata.identifier)
ox_inventory:openInventory('stash', 'bag_'..slot.metadata.identifier)
local removeBag = function()
if not currentBagObject then return end
if DoesEntityExist(currentBagObject.object) then
DeleteObject(currentBagObject.object)
end
SetModelAsNoLongerNeeded(currentBagObject.hash)

currentBagObject = nil
end
end)

local justConnect = true

AddEventHandler('ox_inventory:updateInventory', function(changes)
if justConnect then
Wait(4500)
justConnect = false
end
for _, v in pairs(changes) do
if type(v) == 'table' then
local has_item = ox_inventory:Search('count', filtered2)
if has_item and (not currentBagObject) then
for name, count in pairs(has_item) do
if count and count > 0 then
putOnBag(name)
break
end
end
else
removeBag()
end

local count = ox_inventory:Search('count', filtered2)
if count > 0 and (not currentBagObject) then
putOnBag()
elseif count < 1 and currentBagObject then
removeBag()
end
end
if type(v) == 'boolean' then
local count = ox_inventory:Search('count', filtered2)
if count < 1 and currentBagObject then
removeBag()
end
end
end
end)

lib.onCache('vehicle', function(value)
if GetResourceState('ox_inventory') ~= 'started' then return end
if value then
removeBag()
else
putOnBag()
end
end)
end
43 changes: 29 additions & 14 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
-----------------For support, scripts, and more----------------
--------------- https://discord.gg/wasabiscripts -------------
---------------------------------------------------------------
Config = {}

Config.checkForUpdates = true -- Check for updates?
return {
checkForUpdates = true, -- Check for updates?
oneBagInInventory = true, -- Allow only one bag in inventory?

Config.OneBagInInventory = true -- Allow only one bag in inventory?
createBagObject = false, -- alternatively, use sleepless_inventory_addons's itemCarry feature.
--createBagObject = {
-- ['p_michael_backpack_s'] = {
-- bone = 24818,
-- offset = vec3(0.07, -0.11, -0.05),
-- rotation = vec3(0.0, 90.0, 175.0)
-- }
--}

Config.BackpackStorage = {
slots = 8, -- Slots of backpack storage
weight = 10000 -- Total weight for backpack
}

Strings = { -- Notification strings
action_incomplete = 'Action Incomplete',
one_backpack_only = 'You can only have 1x backpack!',
backpack_in_backpack = 'You can\'t place a backpack within another!',

}
backpacks = {
backpack_small = { -- The item-name. must be and match ox_inventory item name.
slots = 10,
weight = 10000,
description = 'A Small Backpack', -- Optional?: forces this description on the item when the item is created.
label = 'Small Backpack', -- Optional?: forces this label on the item when the item is created.
image = 'small_backpack', -- Optional?: forces this image on the item when the item is created.
},
backpack_duffel = {
slots = 15,
weight = 17500,
bagModel = '', -- if createBagObject is true
description = 'A Duffel Bag', -- Optional?: forces this description on the item when the item is created.
label = 'Duffel Bag', -- Optional?: forces this label on the item when the item is created.
image = 'small_backpack', -- Optional?: forces this image on the item when the item is created.
},
}
}
22 changes: 16 additions & 6 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@
---------------------------------------------------------------
fx_version 'cerulean'
game 'gta5'

lua54 'yes'
use_experimental_fxv2_oal 'yes'

description 'Wasabi Backpack for Ox Inventory'
version '1.0.4'
version '2.0.0-ALPHA'

client_scripts {
'client/**.lua'
}

server_scripts {
'server/**.lua'
files {
'locales/*.lua'
}

shared_scripts {
'@ox_lib/init.lua',
'config.lua'
}

client_scripts {
'client/**.lua'
}

server_scripts {
'server/**.lua'
}

dependencies {
'/server:5848',
'/onesync',
'ox_lib',
'ox_inventory'
}
6 changes: 6 additions & 0 deletions locales/en.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- English
return {
action_incomplete = 'Action Incomplete',
one_backpack_only = 'You can only have 1x backpack!',
backpack_in_backpack = 'You can\'t place a backpack within another!',
}
Loading