Skip to content

Commit d53372b

Browse files
committed
chore: InventoryWindow cleanup
1 parent 0fa73ac commit d53372b

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

Intersect.Client.Core/Interface/Game/Inventory/InventoryWindow.cs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using Intersect.Client.Framework.Gwen.Control;
55
using Intersect.Client.General;
66
using Intersect.Client.Localization;
7+
using Intersect.Core;
78
using Intersect.GameObjects;
9+
using Microsoft.Extensions.Logging;
810

911
namespace Intersect.Client.Interface.Game.Inventory;
1012

@@ -108,7 +110,7 @@ public void OpenContextMenu(int slot)
108110
{
109111
mUseItemContextItem.SetText(Strings.ItemContextMenu.Equip.ToString(item.Name));
110112
}
111-
113+
112114
break;
113115
}
114116

@@ -207,35 +209,70 @@ public void Update()
207209

208210
mInventoryWindow.IsClosable = Globals.CanCloseInventory;
209211

210-
for (var i = 0; i < Options.Instance.Player.MaxInventory; i++)
212+
if (Globals.Me?.Inventory is not { } inventory)
213+
{
214+
return;
215+
}
216+
217+
var slotCount = Math.Min(Options.Instance.Player.MaxInventory, Items.Count);
218+
for (var slotIndex = 0; slotIndex < slotCount; slotIndex++)
211219
{
212-
var item = ItemBase.Get(Globals.Me.Inventory[i].ItemId);
213-
if (item != null)
220+
var slotComponent = Items[slotIndex];
221+
var slotLabel = mValues[slotIndex];
222+
223+
var inventorySlot = inventory[slotIndex];
224+
if (!ItemBase.TryGet(inventorySlot.ItemId, out var itemDescriptor))
214225
{
215-
Items[i].Pnl.IsHidden = false;
216-
if (item.IsStackable)
226+
if (inventorySlot.ItemId != default)
217227
{
218-
mValues[i].IsHidden = Globals.Me.Inventory[i].Quantity <= 1;
219-
mValues[i].Text = Strings.FormatQuantityAbbreviated(Globals.Me.Inventory[i].Quantity);
228+
ApplicationContext.CurrentContext.Logger.LogWarning(
229+
"Inventory slot {SlotIndex} refers to missing Item descriptor {DescriptorId}",
230+
slotIndex,
231+
inventorySlot.ItemId
232+
);
220233
}
221-
else
234+
235+
if (slotComponent.Pnl.IsVisible)
222236
{
223-
mValues[i].IsHidden = true;
237+
slotComponent.Pnl.IsHidden = true;
224238
}
225239

226-
if (Items[i].IsDragging)
240+
if (slotLabel.IsVisible)
227241
{
228-
Items[i].Pnl.IsHidden = true;
229-
mValues[i].IsHidden = true;
242+
slotLabel.IsHidden = true;
230243
}
244+
continue;
245+
}
246+
247+
if (slotComponent.Pnl.IsHidden)
248+
{
249+
slotComponent.Pnl.IsVisible = true;
250+
}
231251

232-
Items[i].Update();
252+
var shouldHideLabel = !itemDescriptor.IsStackable || inventorySlot.Quantity <= 1;
253+
if (shouldHideLabel)
254+
{
255+
if (slotLabel.IsVisible)
256+
{
257+
slotLabel.IsVisible = false;
258+
}
233259
}
234260
else
235261
{
236-
Items[i].Pnl.IsHidden = true;
237-
mValues[i].IsHidden = true;
262+
if (slotLabel.IsHidden)
263+
{
264+
slotLabel.IsVisible = true;
265+
}
266+
slotLabel.Text = Strings.FormatQuantityAbbreviated(inventorySlot.Quantity);
238267
}
268+
269+
if (slotComponent.IsDragging)
270+
{
271+
slotComponent.Pnl.IsHidden = true;
272+
slotLabel.IsHidden = true;
273+
}
274+
275+
slotComponent.Update();
239276
}
240277
}
241278

0 commit comments

Comments
 (0)