From 5a2caa3821edef2400d75f820c350e2c9c2c55ca Mon Sep 17 00:00:00 2001 From: Luke <39926192+LukeWasTakenn@users.noreply.github.com> Date: Thu, 14 Dec 2023 11:58:07 +0100 Subject: [PATCH] fix: rerender slots when using SetSlotCount while client is in inventory --- modules/inventory/client.lua | 12 ++++++++++++ modules/inventory/server.lua | 12 ++++++++++++ web/src/reducers/refreshSlots.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/modules/inventory/client.lua b/modules/inventory/client.lua index 69e5f22df..cff71f701 100644 --- a/modules/inventory/client.lua +++ b/modules/inventory/client.lua @@ -397,4 +397,16 @@ RegisterNetEvent('ox_inventory:refreshMaxWeight', function(data) }) end) +RegisterNetEvent('ox_inventory:refreshSlotCount', function(data) + SendNUIMessage({ + action = 'refreshSlots', + data = { + slotsData = { + inventoryId = data.inventoryId, + slots = data.slots + } + } + }) +end) + return Inventory diff --git a/modules/inventory/server.lua b/modules/inventory/server.lua index 06468bc1a..6c26c2de8 100644 --- a/modules/inventory/server.lua +++ b/modules/inventory/server.lua @@ -1013,6 +1013,18 @@ function Inventory.SetSlotCount(inv, slots) inv.changed = true inv.slots = slots + + if inv.player then + print('inv.player') + TriggerClientEvent('ox_inventory:refreshSlotCount', inv.id, {inventoryId = inv.id, slots = inv.slots}) + end + + for playerId in pairs(inv.openedBy) do + if playerId ~= inv.id then + print('playerId') + TriggerClientEvent('ox_inventory:refreshSlotCount', playerId, {inventoryId = inv.id, slots = inv.slots}) + end + end end exports('SetSlotCount', Inventory.SetSlotCount) diff --git a/web/src/reducers/refreshSlots.ts b/web/src/reducers/refreshSlots.ts index 45faa5f93..0c28a81d4 100644 --- a/web/src/reducers/refreshSlots.ts +++ b/web/src/reducers/refreshSlots.ts @@ -2,6 +2,8 @@ import { CaseReducer, PayloadAction } from '@reduxjs/toolkit'; import { itemDurability } from '../helpers'; import { Items } from '../store/items'; import { InventoryType, Slot, State } from '../typings'; +import { inventorySlice } from '../store/inventory'; +import RightInventory from '../components/inventory/RightInventory'; export type ItemsPayload = { item: Slot; inventory?: InventoryType }; @@ -9,6 +11,7 @@ interface Payload { items?: ItemsPayload | ItemsPayload[]; itemCount?: Record; weightData?: { inventoryId: string; maxWeight: number }; + slotsData?: { inventoryId: string; slots: number }; } export const refreshSlotsReducer: CaseReducer> = (state, action) => { @@ -64,4 +67,27 @@ export const refreshSlotsReducer: CaseReducer> = ( state[inv].maxWeight = inventoryMaxWeight; } + + if (action.payload.slotsData) { + const { inventoryId } = action.payload.slotsData; + const { slots } = action.payload.slotsData; + + const inv = + inventoryId === state.leftInventory.id + ? 'leftInventory' + : inventoryId === state.rightInventory.id + ? 'rightInventory' + : null; + + if (!inv) return; + + state[inv].slots = slots; + inventorySlice.caseReducers.setupInventory(state, { + type: 'setupInventory', + payload: { + leftInventory: inv === 'leftInventory' ? state[inv] : undefined, + rightInventory: inv === 'rightInventory' ? state[inv] : undefined, + }, + }); + } };