From ca7482cc3bf60408c1f188b9dd78985ae2e6321f Mon Sep 17 00:00:00 2001 From: bitpredator <67551273+bitpredator@users.noreply.github.com> Date: Wed, 7 Aug 2024 10:33:28 +0200 Subject: [PATCH 1/3] convert: wasabi_backpack > bpt_backpack --- .../[bpt_addons]/bpt_backpack/README.md | 51 ++++++++ .../bpt_backpack}/client/client.lua | 38 ++++-- .../[bpt_addons]/bpt_backpack/config.lua | 13 ++ .../bpt_backpack}/fxmanifest.lua | 2 +- .../bpt_backpack/server/server.lua | 112 ++++++++++++++++++ .../[wasabi]/wasabi_backpack/README.md | 34 ------ .../[wasabi]/wasabi_backpack/config.lua | 10 -- .../wasabi_backpack/server/server.lua | 37 ------ 8 files changed, 202 insertions(+), 95 deletions(-) create mode 100644 server-data/resources/[bpt_addons]/bpt_backpack/README.md rename server-data/resources/{[wasabi]/wasabi_backpack => [bpt_addons]/bpt_backpack}/client/client.lua (51%) create mode 100644 server-data/resources/[bpt_addons]/bpt_backpack/config.lua rename server-data/resources/{[wasabi]/wasabi_backpack => [bpt_addons]/bpt_backpack}/fxmanifest.lua (94%) create mode 100644 server-data/resources/[bpt_addons]/bpt_backpack/server/server.lua delete mode 100644 server-data/resources/[wasabi]/wasabi_backpack/README.md delete mode 100644 server-data/resources/[wasabi]/wasabi_backpack/config.lua delete mode 100644 server-data/resources/[wasabi]/wasabi_backpack/server/server.lua diff --git a/server-data/resources/[bpt_addons]/bpt_backpack/README.md b/server-data/resources/[bpt_addons]/bpt_backpack/README.md new file mode 100644 index 000000000..fea4f6cdb --- /dev/null +++ b/server-data/resources/[bpt_addons]/bpt_backpack/README.md @@ -0,0 +1,51 @@ +

bpt_backpack

+

Discord + +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 + +Features: +- 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' + } + }, +``` diff --git a/server-data/resources/[wasabi]/wasabi_backpack/client/client.lua b/server-data/resources/[bpt_addons]/bpt_backpack/client/client.lua similarity index 51% rename from server-data/resources/[wasabi]/wasabi_backpack/client/client.lua rename to server-data/resources/[bpt_addons]/bpt_backpack/client/client.lua index 272d01450..7e2e6220e 100644 --- a/server-data/resources/[wasabi]/wasabi_backpack/client/client.lua +++ b/server-data/resources/[bpt_addons]/bpt_backpack/client/client.lua @@ -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) @@ -21,22 +21,22 @@ 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 @@ -44,16 +44,28 @@ AddEventHandler("ox_inventory:updateInventory", function(changes) 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) diff --git a/server-data/resources/[bpt_addons]/bpt_backpack/config.lua b/server-data/resources/[bpt_addons]/bpt_backpack/config.lua new file mode 100644 index 000000000..413d4f403 --- /dev/null +++ b/server-data/resources/[bpt_addons]/bpt_backpack/config.lua @@ -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!", +} diff --git a/server-data/resources/[wasabi]/wasabi_backpack/fxmanifest.lua b/server-data/resources/[bpt_addons]/bpt_backpack/fxmanifest.lua similarity index 94% rename from server-data/resources/[wasabi]/wasabi_backpack/fxmanifest.lua rename to server-data/resources/[bpt_addons]/bpt_backpack/fxmanifest.lua index dca7a7e90..059f7a183 100644 --- a/server-data/resources/[wasabi]/wasabi_backpack/fxmanifest.lua +++ b/server-data/resources/[bpt_addons]/bpt_backpack/fxmanifest.lua @@ -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", diff --git a/server-data/resources/[bpt_addons]/bpt_backpack/server/server.lua b/server-data/resources/[bpt_addons]/bpt_backpack/server/server.lua new file mode 100644 index 000000000..4128797a1 --- /dev/null +++ b/server-data/resources/[bpt_addons]/bpt_backpack/server/server.lua @@ -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) diff --git a/server-data/resources/[wasabi]/wasabi_backpack/README.md b/server-data/resources/[wasabi]/wasabi_backpack/README.md deleted file mode 100644 index c24788b22..000000000 --- a/server-data/resources/[wasabi]/wasabi_backpack/README.md +++ /dev/null @@ -1,34 +0,0 @@ -# wasabi_backpack - -This resource was created as a free script for backpacks using ox_inventory - -Features: -- 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 -- 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` - -# 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 = 'wasabi_backpack.openBackpack' - } - }, -``` diff --git a/server-data/resources/[wasabi]/wasabi_backpack/config.lua b/server-data/resources/[wasabi]/wasabi_backpack/config.lua deleted file mode 100644 index 39ca7984e..000000000 --- a/server-data/resources/[wasabi]/wasabi_backpack/config.lua +++ /dev/null @@ -1,10 +0,0 @@ -Config = {} - -Config.checkForUpdates = false -- Check for updates? - -Config.BackpackItem = "backpack" -- Item name of backpack - -Config.BackpackStorage = { - slots = 8, -- Slots of backpack storage - weight = 10000, -- Total weight for backpack -} diff --git a/server-data/resources/[wasabi]/wasabi_backpack/server/server.lua b/server-data/resources/[wasabi]/wasabi_backpack/server/server.lua deleted file mode 100644 index a96559f22..000000000 --- a/server-data/resources/[wasabi]/wasabi_backpack/server/server.lua +++ /dev/null @@ -1,37 +0,0 @@ -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("wasabi_backpack:openBackpack") -AddEventHandler("wasabi_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("wasabi_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) From e4bd745939dfcd9463033c5fec303a6a2ad3afce Mon Sep 17 00:00:00 2001 From: bitpredator <67551273+bitpredator@users.noreply.github.com> Date: Wed, 7 Aug 2024 10:34:09 +0200 Subject: [PATCH 2/3] update the export method in bpt_backpack --- server-data/resources/[ox]/ox_inventory/data/items.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server-data/resources/[ox]/ox_inventory/data/items.lua b/server-data/resources/[ox]/ox_inventory/data/items.lua index 349b1b442..e3cc149c8 100644 --- a/server-data/resources/[ox]/ox_inventory/data/items.lua +++ b/server-data/resources/[ox]/ox_inventory/data/items.lua @@ -458,7 +458,7 @@ return { stack = false, consume = 0, client = { - export = "wasabi_backpack.openBackpack", + export = "bpt_backpack.openBackpack", }, }, From 1ec6bee8ab55038eff4c9b25d608388885315238 Mon Sep 17 00:00:00 2001 From: bitpredator <67551273+bitpredator@users.noreply.github.com> Date: Wed, 7 Aug 2024 10:39:27 +0200 Subject: [PATCH 3/3] convert: wasabi_backpack > bpt_backpack --- server-data/resources/[bpt_addons]/bpt_backpack/fxmanifest.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server-data/resources/[bpt_addons]/bpt_backpack/fxmanifest.lua b/server-data/resources/[bpt_addons]/bpt_backpack/fxmanifest.lua index 059f7a183..944a127ae 100644 --- a/server-data/resources/[bpt_addons]/bpt_backpack/fxmanifest.lua +++ b/server-data/resources/[bpt_addons]/bpt_backpack/fxmanifest.lua @@ -2,7 +2,7 @@ fx_version("cerulean") game("gta5") lua54("yes") -description("Wasabi Backpack for Ox Inventory") +description("bpt_backpack for Ox Inventory") version("1.0.2") client_scripts({