From 9624e088e062fe0f0c1a348473997ee382693357 Mon Sep 17 00:00:00 2001 From: Tom <111147401+1OSaft@users.noreply.github.com> Date: Sat, 30 Dec 2023 00:30:38 +0100 Subject: [PATCH] Added the support of multiple bag Items --- README.md | 56 +++++++++++++++++++++++++++--- client/client.lua | 80 ++++++++++++++++++++++++------------------ config.lua | 56 +++++++++++++++++++----------- fxmanifest.lua | 5 +-- server/server.lua | 86 +++++++++++++--------------------------------- server/updater.lua | 15 ++++---- 6 files changed, 163 insertions(+), 135 deletions(-) diff --git a/README.md b/README.md index 91eb4e7..def8614 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# wasabi_backpack +# unr3al_backpack This resource was created as a free script for backpacks using ox_inventory @@ -11,25 +11,71 @@ This resource was created as a free script for backpacks using ox_inventory - 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`) +- Add backpack image to inventory images (found in `unr3al_backpack/_inventory_images/backpack.png`) - Put script in your `resources` directory -- ensure `wasabi_backpack` *after* `ox_lib` but *before* `ox_inventory` +- ensure `unr3al_backpack` *after* `ox_lib` but *before* `ox_inventory` # Dependencies - ox_inventory ## Extra Information + +You can add infinite backpacks, just adjust the config for your liking. +Then add the export to your `ox_inventory/data/items.lua`. The ending of the export is always the itemname you defined in your config.lua + + + Item to add to `ox_inventory/data/items.lua` ``` - ['backpack'] = { + ['bag1'] = { label = 'Backpack', weight = 220, stack = false, consume = 0, client = { - export = 'wasabi_backpack.openBackpack' + export = 'unr3al_backpack.openBackpack_bag1' } }, + ['bag2'] = { + label = 'Backpack', + weight = 220, + stack = false, + consume = 0, + client = { + export = 'unr3al_backpack.openBackpack_bag2' + } + }, +``` +Configure the bags in `unr3al_backpack/config.lua` +``` + ['bag1'] = { + Slots = 35, + Weight = 20000, + Uniform = { + Male = { + ['bags_1'] = 41, + ['bags_2'] = 0, + }, + Female = { + ['bags_1'] = 41, + ['bags_2'] = 0, + } + } + }, + ['bag2'] = { + Slots = 15, + Weight = 5000, + Uniform = { + Male = { + ['bags_1'] = 41, + ['bags_2'] = 0, + }, + Female = { + ['bags_1'] = 41, + ['bags_2'] = 0, + } + } + }, ``` ## Preview from Wasabi diff --git a/client/client.lua b/client/client.lua index c5e7460..44f9525 100644 --- a/client/client.lua +++ b/client/client.lua @@ -1,22 +1,19 @@ ------------------For support, scripts, and more---------------- ---------------- https://discord.gg/wasabiscripts ------------- ---------------------------------------------------------------- - -local bagEquipped, bagObj -local hash = `p_michael_backpack_s` +local bagEquipped, bagObj, skin local ox_inventory = exports.ox_inventory local ped = cache.ped local justConnect = true -local skin +local count = 0 -local function PutOnBag() - print("Putting on Backpack") +local function PutOnBag(bagtype) + bagtype = bagtype + if Config.Debug then print("Putting on Backpack") end + if Config.Debug then print("Bag type: "..bagtype) end TriggerEvent('skinchanger:getSkin', function(skin) if skin.sex == 0 then - TriggerEvent('skinchanger:loadClothes', skin, Config.Uniforms.Male) + TriggerEvent('skinchanger:loadClothes', skin, Config.Backpacks[bagtype].Uniform.Male) else - TriggerEvent('skinchanger:loadClothes', skin, Config.Uniforms.Female) + TriggerEvent('skinchanger:loadClothes', skin, Config.Backpacks[bagtype].Uniform.Female) end saveSkin() end) @@ -27,12 +24,12 @@ saveSkin = function() Wait(100) TriggerEvent('skinchanger:getSkin', function(skin) - TriggerServerEvent('wasabi_backpack:save', skin) + TriggerServerEvent('unr3al_backpack:save', skin) end) end local function RemoveBag() - print("Removing Backpack") + if Config.Debug then print("Removing Backpack") end TriggerEvent('skinchanger:getSkin', function(skin) if skin.sex == 0 then TriggerEvent('skinchanger:loadClothes', skin, Config.CleanUniforms.Male) @@ -50,24 +47,31 @@ AddEventHandler('ox_inventory:updateInventory', function(changes) Wait(4500) justConnect = nil end + if Config.Debug then print("Update Inv") end for k, v in pairs(changes) do + if Config.Debug then print("V: "..tostring(v)) end 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() + for vbag in pairs(Config.Backpacks) do + count = count + ox_inventory:Search('count', vbag) + if count > 0 and (not bagEquipped or not bagObj) then + for vbag in pairs(Config.Backpacks) do + bagcount = ox_inventory:GetItemCount(vbag) + if bagcount >= 1 then + PutOnBag(vbag) + if Config.Debug then print("Count: "..bagcount) end + end + end + elseif count < 1 and bagEquipped then + RemoveBag() + end end + end if type(v) == 'boolean' then - local count = ox_inventory:Search('count', 'backpack') - if count < 1 and bagEquipped then - RemoveBag() - end + RemoveBag() end end end) - lib.onCache('ped', function(value) ped = value end) @@ -77,19 +81,27 @@ lib.onCache('vehicle', function(value) if value then RemoveBag() else - local count = ox_inventory:Search('count', 'backpack') + for vbag in pairs(Config.Backpacks) do + count = count + ox_inventory:Search('count', vbag) + end if count and count >= 1 then - PutOnBag() + PutOnBag(bagtype) 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) - else - TriggerServerEvent('wasabi_backpack:openBackpack', slot.metadata.identifier) - ox_inventory:openInventory('stash', 'bag_'..slot.metadata.identifier) - end -end) +for kbag in pairs(Config.Backpacks) do + local bagtype = kbag + exports('openBackpack_'..bagtype, function(data, slot) + if Config.Debug then print("Export "..bagtype.." Triggered") end + if not slot?.metadata?.identifier then + local identifier = lib.callback.await('unr3al_backpack:getNewIdentifier', 100, data.slot, bagtype) + ox_inventory:openInventory('stash', bagtype..'_'..identifier) + if Config.Debug then print("Registered new identifier") end + else + TriggerServerEvent('unr3al_backpack:openBackpack', slot.metadata.identifier, bagtype) + ox_inventory:openInventory('stash', bagtype..'_'..slot.metadata.identifier) + if Config.Debug then print("Triggering open backpack") end + end + end) +end \ No newline at end of file diff --git a/config.lua b/config.lua index 538fc51..2252a25 100644 --- a/config.lua +++ b/config.lua @@ -1,27 +1,12 @@ ------------------For support, scripts, and more---------------- ---------------- https://discord.gg/wasabiscripts ------------- ---------------------------------------------------------------- Config = {} Config.checkForUpdates = true -- Check for updates? +Config.Debug = false -Config.OneBagInInventory = true -- Allow only one bag in inventory? - -Config.BackpackStorage = { - slots = 35, -- Slots of backpack storage - weight = 20000 -- Total weight for backpack -} - - -Config.Uniforms = { - - Male = { - ['bags_1'] = 41, - ['bags_2'] = 0, - }, - Female = { - ['bags_1'] = 41, - ['bags_2'] = 0, +Config.Filter = { -- Items not allowed in your bags + itemFilter = { + bag1 = true, + bag2 = true } } @@ -37,6 +22,37 @@ Config.CleanUniforms = { } } +Config.Backpacks = { + ['bag1'] = { + Slots = 35, + Weight = 20000, + Uniform = { + Male = { + ['bags_1'] = 41, + ['bags_2'] = 0, + }, + Female = { + ['bags_1'] = 41, + ['bags_2'] = 0, + } + } + }, + ['bag2'] = { + Slots = 15, + Weight = 5000, + Uniform = { + Male = { + ['bags_1'] = 41, + ['bags_2'] = 0, + }, + Female = { + ['bags_1'] = 41, + ['bags_2'] = 0, + } + } + }, +} + Strings = { -- Notification strings action_incomplete = 'Action Incomplete', one_backpack_only = 'You can only have 1x backpack!', diff --git a/fxmanifest.lua b/fxmanifest.lua index 4908b2b..f4419e0 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -1,12 +1,9 @@ ------------------For support, scripts, and more---------------- ---------------- https://discord.gg/wasabiscripts ------------- ---------------------------------------------------------------- fx_version 'cerulean' game 'gta5' lua54 'yes' description 'Wasabi Backpack for Ox Inventory' -version '1.0.4' +version '1.0.1' client_scripts { 'client/**.lua' diff --git a/server/server.lua b/server/server.lua index 9213101..1e4c6e5 100644 --- a/server/server.lua +++ b/server/server.lua @@ -1,11 +1,8 @@ ------------------For support, scripts, and more---------------- ---------------- https://discord.gg/wasabiscripts ------------- ---------------------------------------------------------------- local registeredStashes = {} local ox_inventory = exports.ox_inventory +local count_bagpacks = 0 - -local function GenerateText(num) -- Thnx Linden +local function GenerateText(num) local str repeat str = {} for i = 1, num do str[i] = string.char(math.random(65, 90)) end @@ -14,36 +11,42 @@ local function GenerateText(num) -- Thnx Linden return str end -local function GenerateSerial(text) -- Thnx Again +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) +RegisterServerEvent('unr3al_backpack:openBackpack') +AddEventHandler('unr3al_backpack:openBackpack', function(identifier, bagtype) + bagtype = bagtype if not registeredStashes[identifier] then - ox_inventory:RegisterStash('bag_'..identifier, 'Backpack', Config.BackpackStorage.slots, Config.BackpackStorage.weight, false) + ox_inventory:RegisterStash(bagtype..'_'..identifier, 'Backpack'..bagtype, Config.Backpacks[bagtype].Slots, Config.Backpacks[bagtype].Weight, false) registeredStashes[identifier] = true end end) -lib.callback.register('wasabi_backpack:getNewIdentifier', function(source, slot) +lib.callback.register('unr3al_backpack:getNewIdentifier', function(source, slot, bagtype) + bagtype = bagtype local newId = GenerateSerial() ox_inventory:SetMetadata(source, slot, {identifier = newId}) - ox_inventory:RegisterStash('bag_'..newId, 'Backpack', Config.BackpackStorage.slots, Config.BackpackStorage.weight, false) + ox_inventory:RegisterStash(bagtype..'_'..newId, 'Backpack'..bagtype, Config.Backpacks[bagtype].Slots, Config.Backpacks[bagtype].Weight, false) registeredStashes[newId] = true return newId end) CreateThread(function() while GetResourceState('ox_inventory') ~= 'started' do Wait(500) end + if Config.Debug then print("Inventory Started") 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 + local start, destination, move_type, count_bagpacks = payload.fromInventory, payload.toInventory, payload.tobagtype, 0 + if Config.Debug then print("Swap Alive") end + for vbag in pairs(Config.Backpacks) do + count_bagpacks = count_bagpacks + ox_inventory:GetItem(payload.source, vbag, nil, true) + end + if Config.Debug then print("Count: "..count_bagpacks) end + 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 @@ -58,61 +61,18 @@ CreateThread(function() end, { print = false, itemFilter = { - backpack = true, + bag1 = true, + bag2 = 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) -RegisterServerEvent('wasabi_backpack:save') -AddEventHandler('wasabi_backpack:save', function(skin) +RegisterServerEvent('unr3al_backpack:save') +AddEventHandler('unr3al_backpack:save', function(skin) local src = source local xPlayer = ESX.GetPlayerFromId(src) diff --git a/server/updater.lua b/server/updater.lua index 67a741e..4fcf816 100644 --- a/server/updater.lua +++ b/server/updater.lua @@ -1,19 +1,16 @@ ------------------For support, scripts, and more---------------- ---------------- https://discord.gg/wasabiscripts ------------- ---------------------------------------------------------------- if Config.checkForUpdates then local curVersion = GetResourceMetadata(GetCurrentResourceName(), "version") - local resourceName = "wasabi_backpack" + local resourceName = "unr3al_backpack" CreateThread(function() - if GetCurrentResourceName() ~= "wasabi_backpack" then - resourceName = "wasabi_backpack (" .. GetCurrentResourceName() .. ")" + if GetCurrentResourceName() ~= "unr3al_backpack" then + resourceName = "unr3al_backpack (" .. GetCurrentResourceName() .. ")" end end) CreateThread(function() while true do - PerformHttpRequest("https://api.github.com/repos/1OSaft/wasabi_backpack/releases/latest", CheckVersion, "GET") + PerformHttpRequest("https://api.github.com/repos/1OSaft/unr3al_backpack/releases/latest", CheckVersion, "GET") Wait(3600000) end end) @@ -38,7 +35,7 @@ if Config.checkForUpdates then GetRepoInformations = function() local repoVersion, repoURL, repoBody = nil, nil, nil - PerformHttpRequest("https://api.github.com/repos/1OSaft/wasabi_backpack/releases/latest", function(err, response, headers) + PerformHttpRequest("https://api.github.com/repos/1OSaft/unr3al_backpack/releases/latest", function(err, response, headers) if err == 200 then local data = json.decode(response) @@ -47,7 +44,7 @@ if Config.checkForUpdates then repoBody = data.body else repoVersion = curVersion - repoURL = "https://github.com/1OSaft/wasabi_backpack" + repoURL = "https://github.com/1OSaft/unr3al_backpack" end end, "GET")