Skip to content

Commit

Permalink
fix: rerender slots when using SetSlotCount while client is in inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed Dec 14, 2023
1 parent ee858b4 commit 5a2caa3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions modules/inventory/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions web/src/reducers/refreshSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ 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 };

interface Payload {
items?: ItemsPayload | ItemsPayload[];
itemCount?: Record<string, number>;
weightData?: { inventoryId: string; maxWeight: number };
slotsData?: { inventoryId: string; slots: number };
}

export const refreshSlotsReducer: CaseReducer<State, PayloadAction<Payload>> = (state, action) => {
Expand Down Expand Up @@ -64,4 +67,27 @@ export const refreshSlotsReducer: CaseReducer<State, PayloadAction<Payload>> = (

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,
},
});
}
};

0 comments on commit 5a2caa3

Please sign in to comment.