Skip to content

Commit

Permalink
convert: wasabi_backpack > bpt_backpack
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpredator committed Aug 7, 2024
1 parent a040603 commit ca7482c
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 95 deletions.
51 changes: 51 additions & 0 deletions server-data/resources/[bpt_addons]/bpt_backpack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<h1 align='center'>bpt_backpack</a></h1>
<p align='center'><a href='https://discord.gg/ksGfNvDEfq'>Discord</a>

Copyright (C) 2024 bitpredator

This program Is free software: you can redistribute it And/Or modify it under the terms Of the GNU General Public License As published by the Free Software Foundation, either version 3 Of the License, Or (at your option) any later version.

This program Is distributed In the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty Of MERCHANTABILITY Or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License For more details.

ATTENTION:
You are not authorized to change the name of the resource and the resources within it.

If you want to contribute you can open a pull request.

You are not authorized to sell this software (this is free project).

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

This resource was created as a free script for backpacks using ox_inventory

<b>Features:</b>
- 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

## Installation

- Download this script
- Add backpack to inventory as it is in "Extra Information" below
- Put script in your `resources` directory
- ensure `bpt_backpack` *after* `ox_lib` but *before* `ox_inventory`

# Dependencies
- ox_inventory

## Extra Information
Item to add to `ox_inventory/data/items.lua`
```
['backpack'] = {
label = 'Backpack',
weight = 220,
stack = false,
consume = 0,
client = {
export = 'bpt_backpack.openBackpack'
}
},
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local ped = cache.ped
local justConnect = true

local function PutOnBag()
local x, y, z = table.unpack(GetOffsetFromEntityInWorldCoords(ped, 0.0, 3.0, 0.5))
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)
Expand All @@ -21,39 +21,51 @@ local function RemoveBag()
bagEquipped = nil
end

AddEventHandler("ox_inventory:updateInventory", function(changes)
AddEventHandler('ox_inventory:updateInventory', function(changes)
if justConnect then
Wait(4500)
justConnect = nil
end
for _, v in pairs(changes) do
if type(v) == "table" then
local count = ox_inventory:Search("count", Config.BackpackItem)
if count > 0 and (not bagEquipped or not bagObj) then
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()
end
end
if type(v) == "boolean" then
local count = ox_inventory:Search("count", Config.BackpackItem)
if type(v) == 'boolean' then
local count = ox_inventory:Search('count', 'backpack')
if count < 1 and bagEquipped then
RemoveBag()
end
end
end
end)

lib.onCache("ped", function(value)
lib.onCache('ped', function(value)
ped = value
end)

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
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)
if not slot?.metadata?.identifier then
local identifier = lib.callback.await("bpt_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)
TriggerServerEvent("bpt_backpack:openBackpack", slot.metadata.identifier)
ox_inventory:openInventory("stash", "bag_"..slot.metadata.identifier)
end
end)
13 changes: 13 additions & 0 deletions server-data/resources/[bpt_addons]/bpt_backpack/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Config = {}
Config.OneBagInInventory = true -- Allow only one bag in inventory?

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!",
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ game("gta5")
lua54("yes")

description("Wasabi Backpack for Ox Inventory")
version("1.0.3")
version("1.0.2")

client_scripts({
"client/**.lua",
Expand Down
112 changes: 112 additions & 0 deletions server-data/resources/[bpt_addons]/bpt_backpack/server/server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
local registeredStashes = {}
local ox_inventory = exports.ox_inventory

local function GenerateText(num)
local str
repeat
str = {}
for i = 1, num do
str[i] = string.char(math.random(65, 90))
end
str = table.concat(str)
until str ~= "POL" and str ~= "EMS"
return str
end

local function GenerateSerial(text)
if text and text:len() > 3 then
return text
end
return ("%s%s%s"):format(math.random(100000, 999999), text == nil and GenerateText(3) or text, math.random(100000, 999999))
end

RegisterServerEvent("bpt_backpack:openBackpack")
AddEventHandler("bpt_backpack:openBackpack", function(identifier)
if not registeredStashes[identifier] then
ox_inventory:RegisterStash("bag_" .. identifier, "Backpack", Config.BackpackStorage.slots, Config.BackpackStorage.weight, false)
registeredStashes[identifier] = true
end
end)

lib.callback.register("bpt_backpack:getNewIdentifier", function(source, slot)
local newId = GenerateSerial()
ox_inventory:SetMetadata(source, slot, { identifier = newId })
ox_inventory:RegisterStash("bag_" .. newId, "Backpack", Config.BackpackStorage.slots, Config.BackpackStorage.weight, false)
registeredStashes[newId] = true
return newId
end)

CreateThread(function()
while GetResourceState("ox_inventory") ~= "started" do
Wait(500)
end
local swapHook = ox_inventory:registerHook("swapItems", function(payload)
local start, destination, move_type = payload.fromInventory, payload.toInventory, payload.toType
local count_bagpacks = ox_inventory:GetItem(payload.source, "backpack", nil, true)

if string.find(destination, "bag_") then
TriggerClientEvent("ox_lib:notify", payload.source, { type = "error", title = Strings.action_incomplete, description = Strings.backpack_in_backpack })
return false
end
if Config.OneBagInInventory then
if count_bagpacks > 0 and move_type == "player" and destination ~= start then
TriggerClientEvent("ox_lib:notify", payload.source, { type = "error", title = Strings.action_incomplete, description = Strings.one_backpack_only })
return false
end
end

return true
end, {
print = false,
itemFilter = {
backpack = true,
},
})

local createHook
if Config.OneBagInInventory then
createHook = exports.ox_inventory:registerHook("createItem", function(payload)
local count_bagpacks = ox_inventory:GetItem(payload.inventoryId, "backpack", nil, true)
local playerItems = ox_inventory:GetInventoryItems(payload.inventoryId)

if count_bagpacks > 0 then
local slot = nil

for i, k in pairs(playerItems) do
if k.name == "backpack" then
slot = k.slot
break
end
end

Citizen.CreateThread(function()
local inventoryId = payload.inventoryId
local dontRemove = slot
Citizen.Wait(1000)

for i, k in pairs(ox_inventory:GetInventoryItems(inventoryId)) do
if k.name == "backpack" and dontRemove ~= nil and k.slot ~= dontRemove then
local success = ox_inventory:RemoveItem(inventoryId, "backpack", 1, nil, k.slot)
if success then
TriggerClientEvent("ox_lib:notify", inventoryId, { type = "error", title = Strings.action_incomplete, description = Strings.one_backpack_only })
end
break
end
end
end)
end
end, {
print = false,
itemFilter = {
backpack = true,
},
})
end

AddEventHandler("onResourceStop", function()
ox_inventory:removeHooks(swapHook)
if Config.OneBagInInventory then
ox_inventory:removeHooks(createHook)
end
end)
end)
34 changes: 0 additions & 34 deletions server-data/resources/[wasabi]/wasabi_backpack/README.md

This file was deleted.

10 changes: 0 additions & 10 deletions server-data/resources/[wasabi]/wasabi_backpack/config.lua

This file was deleted.

37 changes: 0 additions & 37 deletions server-data/resources/[wasabi]/wasabi_backpack/server/server.lua

This file was deleted.

0 comments on commit ca7482c

Please sign in to comment.