From dbb97663a5dc8d15751c14eaed262f874eed894b Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Mon, 13 May 2024 06:03:35 +1000 Subject: [PATCH] refactor(client): client.openInventory formatting tweaks Additionally adds a possible third response from the callback event, for handling server-side notifications. --- client.lua | 242 ++++++++++++++++++++++++++++------------------------- 1 file changed, 126 insertions(+), 116 deletions(-) diff --git a/client.lua b/client.lua index c67a8c138..cacdf7104 100644 --- a/client.lua +++ b/client.lua @@ -155,152 +155,162 @@ function client.openInventory(inv, data) return lib.notify({ id = 'inventory_right_access', type = 'error', description = locale('inventory_right_access') }) end - if canOpenInventory() then - local left, right + if not canOpenInventory() then + return lib.notify({ id = 'inventory_player_access', type = 'error', description = locale('inventory_player_access') }) + end - if inv == 'player' and data ~= cache.serverId then - local targetId, targetPed + local left, right, accessError - if not data then - targetId, targetPed = Utils.GetClosestPlayer() - data = targetId and GetPlayerServerId(targetId) - else - local serverId = type(data) == 'table' and data.id or data + if inv == 'player' and data ~= cache.serverId then + local targetId, targetPed - if serverId == cache.serverId then return end + if not data then + targetId, targetPed = Utils.GetClosestPlayer() + data = targetId and GetPlayerServerId(targetId) + else + local serverId = type(data) == 'table' and data.id or data - targetId = serverId and GetPlayerFromServerId(serverId) - targetPed = targetId and GetPlayerPed(targetId) - end + if serverId == cache.serverId then return end - local targetCoords = targetPed and GetEntityCoords(targetPed) + targetId = serverId and GetPlayerFromServerId(serverId) + targetPed = targetId and GetPlayerPed(targetId) + end - if not targetCoords or #(targetCoords - GetEntityCoords(playerPed)) > 1.8 or not (client.hasGroup(shared.police) or canOpenTarget(targetPed)) then - return lib.notify({ id = 'inventory_right_access', type = 'error', description = locale('inventory_right_access') }) - end - end + local targetCoords = targetPed and GetEntityCoords(targetPed) - if inv == 'shop' and invOpen == false then - if cache.vehicle then - return lib.notify({ id = 'cannot_perform', type = 'error', description = locale('cannot_perform') }) - end + if not targetCoords or #(targetCoords - GetEntityCoords(playerPed)) > 1.8 or not (client.hasGroup(shared.police) or canOpenTarget(targetPed)) then + return lib.notify({ id = 'inventory_right_access', type = 'error', description = locale('inventory_right_access') }) + end + end - left, right = lib.callback.await('ox_inventory:openShop', 200, data) - elseif inv == 'crafting' then - if cache.vehicle then - return lib.notify({ id = 'cannot_perform', type = 'error', description = locale('cannot_perform') }) - end + if inv == 'shop' and invOpen == false then + if cache.vehicle then + return lib.notify({ id = 'cannot_perform', type = 'error', description = locale('cannot_perform') }) + end - left = lib.callback.await('ox_inventory:openCraftingBench', 200, data.id, data.index) + left, right, accessError = lib.callback.await('ox_inventory:openShop', 200, data) + elseif inv == 'crafting' then + if cache.vehicle then + return lib.notify({ id = 'cannot_perform', type = 'error', description = locale('cannot_perform') }) + end - if left then - right = CraftingBenches[data.id] + left, right, accessError = lib.callback.await('ox_inventory:openCraftingBench', 200, data.id, data.index) - if not right?.items then return end + if left then + right = CraftingBenches[data.id] - local coords, distance + if not right?.items then return end - if not right.zones and not right.points then - coords = GetEntityCoords(cache.ped) - distance = 2 - else - coords = shared.target and right.zones and right.zones[data.index].coords or right.points and right.points[data.index] - distance = coords and shared.target and right.zones[data.index].distance or 2 - end + local coords, distance - right = { - type = 'crafting', - id = data.id, - label = right.label or locale('crafting_bench'), - index = data.index, - slots = right.slots, - items = right.items, - coords = coords, - distance = distance - } - end - elseif invOpen ~= nil then - if inv == 'policeevidence' then - if not data then - local input = lib.inputDialog(locale('police_evidence'), { - { label = locale('locker_number'), type = 'number', required = true, icon = 'calculator' } - }) --[[@as number[]? ]] + if not right.zones and not right.points then + coords = GetEntityCoords(cache.ped) + distance = 2 + else + coords = shared.target and right.zones and right.zones[data.index].coords or right.points and right.points[data.index] + distance = coords and shared.target and right.zones[data.index].distance or 2 + end - if not input then return end + right = { + type = 'crafting', + id = data.id, + label = right.label or locale('crafting_bench'), + index = data.index, + slots = right.slots, + items = right.items, + coords = coords, + distance = distance + } + end + elseif invOpen ~= nil then + if inv == 'policeevidence' then + if not data then + local input = lib.inputDialog(locale('police_evidence'), { + { label = locale('locker_number'), type = 'number', required = true, icon = 'calculator' } + }) --[[@as number[]? ]] - data = input[1] - end - end + if not input then return end - left, right = lib.callback.await('ox_inventory:openInventory', false, inv, data) - end + data = input[1] + end + end - if left then - if not cache.vehicle then - if inv == 'player' then - Utils.PlayAnim(0, 'mp_common', 'givetake1_a', 8.0, 1.0, 2000, 50, 0.0, 0, 0, 0) - elseif inv ~= 'trunk' then - Utils.PlayAnim(0, 'pickup_object', 'putdown_low', 5.0, 1.5, 1000, 48, 0.0, 0, 0, 0) - end - end + left, right, accessError = lib.callback.await('ox_inventory:openInventory', false, inv, data) + end - plyState.invOpen = true + if accessError then + return lib.notify({ id = accessError, type = 'error', description = locale(accessError) }) + end - SetInterval(client.interval, 100) - SetNuiFocus(true, true) - SetNuiFocusKeepInput(true) - closeTrunk() + -- Stash does not exist + if not left then + if left == false then return false end - if client.screenblur then TriggerScreenblurFadeIn(0) end + if invOpen == false then + return lib.notify({ id = 'inventory_right_access', type = 'error', description = locale('inventory_right_access') }) + end - currentInventory = right or defaultInventory - left.items = PlayerData.inventory - left.groups = PlayerData.groups + if invOpen then return client.closeInventory() end + end - SendNUIMessage({ - action = 'setupInventory', - data = { - leftInventory = left, - rightInventory = currentInventory - } - }) - if not currentInventory.coords and not inv == 'container' then - currentInventory.coords = GetEntityCoords(playerPed) - end + if not cache.vehicle then + if inv == 'player' then + Utils.PlayAnim(0, 'mp_common', 'givetake1_a', 8.0, 1.0, 2000, 50, 0.0, 0, 0, 0) + elseif inv ~= 'trunk' then + Utils.PlayAnim(0, 'pickup_object', 'putdown_low', 5.0, 1.5, 1000, 48, 0.0, 0, 0, 0) + end + end - if inv == 'trunk' then - SetTimeout(200, function() - ---@todo animation for vans? - Utils.PlayAnim(0, 'anim@heists@prison_heiststation@cop_reactions', 'cop_b_idle', 3.0, 3.0, -1, 49, 0.0, 0, 0, 0) + plyState.invOpen = true - local entity = data.entity or NetworkGetEntityFromNetworkId(data.netid) - currentInventory.entity = entity - currentInventory.door = data.door + SetInterval(client.interval, 100) + SetNuiFocus(true, true) + SetNuiFocusKeepInput(true) + closeTrunk() - if not currentInventory.door then - local vehicleHash = GetEntityModel(entity) - local vehicleClass = GetVehicleClass(entity) - currentInventory.door = vehicleClass == 12 and { 2, 3 } or Vehicles.Storage[vehicleHash] and 4 or 5 - end + if client.screenblur then TriggerScreenblurFadeIn(0) end - while currentInventory?.entity == entity and invOpen and DoesEntityExist(entity) and Inventory.CanAccessTrunk(entity) do - Wait(100) - end + currentInventory = right or defaultInventory + left.items = PlayerData.inventory + left.groups = PlayerData.groups + + SendNUIMessage({ + action = 'setupInventory', + data = { + leftInventory = left, + rightInventory = currentInventory + } + }) - if invOpen then client.closeInventory() end - end) + if not currentInventory.coords and not inv == 'container' then + currentInventory.coords = GetEntityCoords(playerPed) + end + + if inv == 'trunk' then + SetTimeout(200, function() + ---@todo animation for vans? + Utils.PlayAnim(0, 'anim@heists@prison_heiststation@cop_reactions', 'cop_b_idle', 3.0, 3.0, -1, 49, 0.0, 0, 0, 0) + + local entity = data.entity or NetworkGetEntityFromNetworkId(data.netid) + currentInventory.entity = entity + currentInventory.door = data.door + + if not currentInventory.door then + local vehicleHash = GetEntityModel(entity) + local vehicleClass = GetVehicleClass(entity) + currentInventory.door = vehicleClass == 12 and { 2, 3 } or Vehicles.Storage[vehicleHash] and 4 or 5 end - -- Stash exists (useful for custom stashes) - return true - else - -- Stash does not exist - if left == false then return false end - if invOpen == false then lib.notify({ id = 'inventory_right_access', type = 'error', description = locale('inventory_right_access') }) end - if invOpen then client.closeInventory() end - end - else lib.notify({ id = 'inventory_player_access', type = 'error', description = locale('inventory_player_access') }) end + while currentInventory?.entity == entity and invOpen and DoesEntityExist(entity) and Inventory.CanAccessTrunk(entity) do + Wait(100) + end + + if invOpen then client.closeInventory() end + end) + end + + return true end RegisterNetEvent('ox_inventory:openInventory', client.openInventory) @@ -657,7 +667,7 @@ local function useSlot(slot, noAnim) elseif not data.ammo and not data.component then useItem(data) end - end + end end exports('useSlot', useSlot)