Handle inventory template updating V2 (#39246)

This commit is contained in:
slarticodefast
2025-07-27 23:05:58 +02:00
committed by GitHub
parent a789341b2f
commit 2ac9948ba0
5 changed files with 138 additions and 108 deletions

View File

@@ -1,3 +1,4 @@
using System.Linq;
using Content.Client.Clothing;
using Content.Client.Examine;
using Content.Client.Verbs.UI;
@@ -11,6 +12,7 @@ using Robust.Client.UserInterface;
using Robust.Shared.Containers;
using Robust.Shared.Input.Binding;
using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Client.Inventory
{
@@ -19,7 +21,7 @@ namespace Content.Client.Inventory
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IUserInterfaceManager _ui = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly ClientClothingSystem _clothingVisualsSystem = default!;
[Dependency] private readonly ExamineSystem _examine = default!;
@@ -91,6 +93,14 @@ namespace Content.Client.Inventory
private void OnShutdown(EntityUid uid, InventoryComponent component, ComponentShutdown args)
{
if (TryComp(uid, out InventorySlotsComponent? inventorySlots))
{
foreach (var slot in component.Slots)
{
TryRemoveSlotData((uid, inventorySlots), (SlotData)slot);
}
}
if (uid == _playerManager.LocalEntity)
OnUnlinkInventory?.Invoke();
}
@@ -102,23 +112,6 @@ namespace Content.Client.Inventory
private void OnPlayerAttached(EntityUid uid, InventorySlotsComponent component, LocalPlayerAttachedEvent args)
{
if (TryGetSlots(uid, out var definitions))
{
foreach (var definition in definitions)
{
if (!TryGetSlotContainer(uid, definition.Name, out var container, out _))
continue;
if (!component.SlotData.TryGetValue(definition.Name, out var data))
{
data = new SlotData(definition);
component.SlotData[definition.Name] = data;
}
data.Container = container;
}
}
OnLinkInventorySlots?.Invoke(uid, component);
}
@@ -128,20 +121,6 @@ namespace Content.Client.Inventory
base.Shutdown();
}
protected override void OnInit(EntityUid uid, InventoryComponent component, ComponentInit args)
{
base.OnInit(uid, component, args);
_clothingVisualsSystem.InitClothing(uid, component);
if (!TryComp(uid, out InventorySlotsComponent? inventorySlots))
return;
foreach (var slot in component.Slots)
{
TryAddSlotDef(uid, inventorySlots, slot);
}
}
public void ReloadInventory(InventorySlotsComponent? component = null)
{
var player = _playerManager.LocalEntity;
@@ -165,7 +144,10 @@ namespace Content.Client.Inventory
public void UpdateSlot(EntityUid owner, InventorySlotsComponent component, string slotName,
bool? blocked = null, bool? highlight = null)
{
var oldData = component.SlotData[slotName];
// The slot might have been removed when changing templates, which can cause items to be dropped.
if (!component.SlotData.TryGetValue(slotName, out var oldData))
return;
var newHighlight = oldData.Highlighted;
var newBlocked = oldData.Blocked;
@@ -181,14 +163,28 @@ namespace Content.Client.Inventory
EntitySlotUpdate?.Invoke(newData);
}
public bool TryAddSlotDef(EntityUid owner, InventorySlotsComponent component, SlotDefinition newSlotDef)
public bool TryAddSlotData(Entity<InventorySlotsComponent> ent, SlotData newSlotData)
{
SlotData newSlotData = newSlotDef; //convert to slotData
if (!component.SlotData.TryAdd(newSlotDef.Name, newSlotData))
if (!ent.Comp.SlotData.TryAdd(newSlotData.SlotName, newSlotData))
return false;
if (owner == _playerManager.LocalEntity)
if (TryGetSlotContainer(ent.Owner, newSlotData.SlotName, out var newContainer, out _))
ent.Comp.SlotData[newSlotData.SlotName].Container = newContainer;
if (ent.Owner == _playerManager.LocalEntity)
OnSlotAdded?.Invoke(newSlotData);
return true;
}
public bool TryRemoveSlotData(Entity<InventorySlotsComponent> ent, SlotData removedSlotData)
{
if (!ent.Comp.SlotData.Remove(removedSlotData.SlotName))
return false;
if (ent.Owner == _playerManager.LocalEntity)
OnSlotRemoved?.Invoke(removedSlotData);
return true;
}
@@ -239,33 +235,52 @@ namespace Content.Client.Inventory
{
base.UpdateInventoryTemplate(ent);
if (TryComp(ent, out InventorySlotsComponent? inventorySlots))
if (!TryComp<InventorySlotsComponent>(ent, out var inventorySlots))
return;
List<SlotData> slotDataToRemove = new(); // don't modify dict while iterating
foreach (var slotData in inventorySlots.SlotData.Values)
{
foreach (var slot in ent.Comp.Slots)
{
if (inventorySlots.SlotData.TryGetValue(slot.Name, out var slotData))
slotData.SlotDef = slot;
}
if (!ent.Comp.Slots.Any(s => s.Name == slotData.SlotName))
slotDataToRemove.Add(slotData);
}
// remove slots that are no longer in the new template
foreach (var slotData in slotDataToRemove)
{
TryRemoveSlotData((ent.Owner, inventorySlots), slotData);
}
// update existing slots or add them if they don't exist yet
foreach (var slot in ent.Comp.Slots)
{
if (inventorySlots.SlotData.TryGetValue(slot.Name, out var slotData))
slotData.SlotDef = slot;
else
TryAddSlotData((ent.Owner, inventorySlots), (SlotData)slot);
}
_clothingVisualsSystem.InitClothing(ent, ent.Comp);
if (ent.Owner == _playerManager.LocalEntity)
ReloadInventory(inventorySlots);
}
public sealed class SlotData
{
public SlotDefinition SlotDef;
public EntityUid? HeldEntity => Container?.ContainedEntity;
public bool Blocked;
public bool Highlighted;
[ViewVariables]
public ContainerSlot? Container;
public bool HasSlotGroup => SlotDef.SlotGroup != "Default";
public Vector2i ButtonOffset => SlotDef.UIWindowPosition;
public string SlotName => SlotDef.Name;
public bool ShowInWindow => SlotDef.ShowInWindow;
public string SlotGroup => SlotDef.SlotGroup;
public string SlotDisplayName => SlotDef.DisplayName;
public string TextureName => "Slots/" + SlotDef.TextureName;
public string FullTextureName => SlotDef.FullTextureName;
[ViewVariables] public SlotDefinition SlotDef;
[ViewVariables] public EntityUid? HeldEntity => Container?.ContainedEntity;
[ViewVariables] public bool Blocked;
[ViewVariables] public bool Highlighted;
[ViewVariables] public ContainerSlot? Container;
[ViewVariables] public bool HasSlotGroup => SlotDef.SlotGroup != "Default";
[ViewVariables] public Vector2i ButtonOffset => SlotDef.UIWindowPosition;
[ViewVariables] public string SlotName => SlotDef.Name;
[ViewVariables] public bool ShowInWindow => SlotDef.ShowInWindow;
[ViewVariables] public string SlotGroup => SlotDef.SlotGroup;
[ViewVariables] public string SlotDisplayName => SlotDef.DisplayName;
[ViewVariables] public string TextureName => "Slots/" + SlotDef.TextureName;
[ViewVariables] public string FullTextureName => SlotDef.FullTextureName;
public SlotData(SlotDefinition slotDef, ContainerSlot? container = null, bool highlighted = false,
bool blocked = false)