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

@@ -59,7 +59,7 @@ public sealed class ClientClothingSystem : ClothingSystem
base.Initialize(); base.Initialize();
SubscribeLocalEvent<ClothingComponent, GetEquipmentVisualsEvent>(OnGetVisuals); SubscribeLocalEvent<ClothingComponent, GetEquipmentVisualsEvent>(OnGetVisuals);
SubscribeLocalEvent<ClothingComponent, InventoryTemplateUpdated>(OnInventoryTemplateUpdated); SubscribeLocalEvent<InventoryComponent, InventoryTemplateUpdated>(OnInventoryTemplateUpdated);
SubscribeLocalEvent<InventoryComponent, VisualsChangedEvent>(OnVisualsChanged); SubscribeLocalEvent<InventoryComponent, VisualsChangedEvent>(OnVisualsChanged);
SubscribeLocalEvent<SpriteComponent, DidUnequipEvent>(OnDidUnequip); SubscribeLocalEvent<SpriteComponent, DidUnequipEvent>(OnDidUnequip);
@@ -82,20 +82,19 @@ public sealed class ClientClothingSystem : ClothingSystem
} }
} }
private void OnInventoryTemplateUpdated(Entity<ClothingComponent> ent, ref InventoryTemplateUpdated args) private void OnInventoryTemplateUpdated(Entity<InventoryComponent> ent, ref InventoryTemplateUpdated args)
{ {
UpdateAllSlots(ent.Owner, clothing: ent.Comp); UpdateAllSlots(ent.Owner, ent.Comp);
} }
private void UpdateAllSlots( private void UpdateAllSlots(
EntityUid uid, EntityUid uid,
InventoryComponent? inventoryComponent = null, InventoryComponent? inventoryComponent = null)
ClothingComponent? clothing = null)
{ {
var enumerator = _inventorySystem.GetSlotEnumerator((uid, inventoryComponent)); var enumerator = _inventorySystem.GetSlotEnumerator((uid, inventoryComponent));
while (enumerator.NextItem(out var item, out var slot)) while (enumerator.NextItem(out var item, out var slot))
{ {
RenderEquipment(uid, item, slot.Name, inventoryComponent, clothingComponent: clothing); RenderEquipment(uid, item, slot.Name, inventoryComponent);
} }
} }

View File

@@ -1,3 +1,4 @@
using System.Linq;
using Content.Client.Clothing; using Content.Client.Clothing;
using Content.Client.Examine; using Content.Client.Examine;
using Content.Client.Verbs.UI; using Content.Client.Verbs.UI;
@@ -11,6 +12,7 @@ using Robust.Client.UserInterface;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.Input.Binding; using Robust.Shared.Input.Binding;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Client.Inventory namespace Content.Client.Inventory
{ {
@@ -19,7 +21,7 @@ namespace Content.Client.Inventory
{ {
[Dependency] private readonly IPlayerManager _playerManager = default!; [Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IUserInterfaceManager _ui = default!; [Dependency] private readonly IUserInterfaceManager _ui = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly ClientClothingSystem _clothingVisualsSystem = default!; [Dependency] private readonly ClientClothingSystem _clothingVisualsSystem = default!;
[Dependency] private readonly ExamineSystem _examine = default!; [Dependency] private readonly ExamineSystem _examine = default!;
@@ -91,6 +93,14 @@ namespace Content.Client.Inventory
private void OnShutdown(EntityUid uid, InventoryComponent component, ComponentShutdown args) 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) if (uid == _playerManager.LocalEntity)
OnUnlinkInventory?.Invoke(); OnUnlinkInventory?.Invoke();
} }
@@ -102,23 +112,6 @@ namespace Content.Client.Inventory
private void OnPlayerAttached(EntityUid uid, InventorySlotsComponent component, LocalPlayerAttachedEvent args) 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); OnLinkInventorySlots?.Invoke(uid, component);
} }
@@ -128,20 +121,6 @@ namespace Content.Client.Inventory
base.Shutdown(); 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) public void ReloadInventory(InventorySlotsComponent? component = null)
{ {
var player = _playerManager.LocalEntity; var player = _playerManager.LocalEntity;
@@ -165,7 +144,10 @@ namespace Content.Client.Inventory
public void UpdateSlot(EntityUid owner, InventorySlotsComponent component, string slotName, public void UpdateSlot(EntityUid owner, InventorySlotsComponent component, string slotName,
bool? blocked = null, bool? highlight = null) 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 newHighlight = oldData.Highlighted;
var newBlocked = oldData.Blocked; var newBlocked = oldData.Blocked;
@@ -181,14 +163,28 @@ namespace Content.Client.Inventory
EntitySlotUpdate?.Invoke(newData); 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 (!ent.Comp.SlotData.TryAdd(newSlotData.SlotName, newSlotData))
if (!component.SlotData.TryAdd(newSlotDef.Name, newSlotData))
return false; 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); 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; return true;
} }
@@ -239,33 +235,52 @@ namespace Content.Client.Inventory
{ {
base.UpdateInventoryTemplate(ent); 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)
{ {
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) foreach (var slot in ent.Comp.Slots)
{ {
if (inventorySlots.SlotData.TryGetValue(slot.Name, out var slotData)) if (inventorySlots.SlotData.TryGetValue(slot.Name, out var slotData))
slotData.SlotDef = slot; 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 sealed class SlotData
{ {
public SlotDefinition SlotDef; [ViewVariables] public SlotDefinition SlotDef;
public EntityUid? HeldEntity => Container?.ContainedEntity; [ViewVariables] public EntityUid? HeldEntity => Container?.ContainedEntity;
public bool Blocked; [ViewVariables] public bool Blocked;
public bool Highlighted; [ViewVariables] public bool Highlighted;
[ViewVariables] public ContainerSlot? Container;
[ViewVariables] [ViewVariables] public bool HasSlotGroup => SlotDef.SlotGroup != "Default";
public ContainerSlot? Container; [ViewVariables] public Vector2i ButtonOffset => SlotDef.UIWindowPosition;
public bool HasSlotGroup => SlotDef.SlotGroup != "Default"; [ViewVariables] public string SlotName => SlotDef.Name;
public Vector2i ButtonOffset => SlotDef.UIWindowPosition; [ViewVariables] public bool ShowInWindow => SlotDef.ShowInWindow;
public string SlotName => SlotDef.Name; [ViewVariables] public string SlotGroup => SlotDef.SlotGroup;
public bool ShowInWindow => SlotDef.ShowInWindow; [ViewVariables] public string SlotDisplayName => SlotDef.DisplayName;
public string SlotGroup => SlotDef.SlotGroup; [ViewVariables] public string TextureName => "Slots/" + SlotDef.TextureName;
public string SlotDisplayName => SlotDef.DisplayName; [ViewVariables] public string FullTextureName => SlotDef.FullTextureName;
public string TextureName => "Slots/" + SlotDef.TextureName;
public string FullTextureName => SlotDef.FullTextureName;
public SlotData(SlotDefinition slotDef, ContainerSlot? container = null, bool highlighted = false, public SlotData(SlotDefinition slotDef, ContainerSlot? container = null, bool highlighted = false,
bool blocked = false) bool blocked = false)

View File

@@ -1,6 +1,8 @@
using Robust.Shared.Serialization;
namespace Content.Shared.DisplacementMap; namespace Content.Shared.DisplacementMap;
[DataDefinition] [DataDefinition, Serializable, NetSerializable]
public sealed partial class DisplacementData public sealed partial class DisplacementData
{ {
/// <summary> /// <summary>

View File

@@ -1,7 +1,7 @@
using Content.Shared.DisplacementMap; using Content.Shared.DisplacementMap;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Prototypes;
namespace Content.Shared.Inventory; namespace Content.Shared.Inventory;
@@ -10,28 +10,46 @@ namespace Content.Shared.Inventory;
[AutoGenerateComponentState(true)] [AutoGenerateComponentState(true)]
public sealed partial class InventoryComponent : Component public sealed partial class InventoryComponent : Component
{ {
[DataField("templateId", customTypeSerializer: typeof(PrototypeIdSerializer<InventoryTemplatePrototype>))] /// <summary>
[AutoNetworkedField] /// The template defining how the inventory layout will look like.
public string TemplateId { get; set; } = "human"; /// </summary>
[DataField, AutoNetworkedField]
[ViewVariables] // use the API method
public ProtoId<InventoryTemplatePrototype> TemplateId = "human";
[DataField("speciesId")] public string? SpeciesId { get; set; } /// <summary>
/// For setting the TemplateId.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public ProtoId<InventoryTemplatePrototype> TemplateIdVV
{
get => TemplateId;
set => IoCManager.Resolve<IEntityManager>().System<InventorySystem>().SetTemplateId((Owner, this), value);
}
[DataField, AutoNetworkedField]
public string? SpeciesId;
[ViewVariables]
public SlotDefinition[] Slots = Array.Empty<SlotDefinition>(); public SlotDefinition[] Slots = Array.Empty<SlotDefinition>();
[ViewVariables]
public ContainerSlot[] Containers = Array.Empty<ContainerSlot>(); public ContainerSlot[] Containers = Array.Empty<ContainerSlot>();
[DataField] [DataField, AutoNetworkedField]
public Dictionary<string, DisplacementData> Displacements = new(); public Dictionary<string, DisplacementData> Displacements = new();
/// <summary> /// <summary>
/// Alternate displacement maps, which if available, will be selected for the player of the appropriate gender. /// Alternate displacement maps, which if available, will be selected for the player of the appropriate gender.
/// </summary> /// </summary>
[DataField] [DataField, AutoNetworkedField]
public Dictionary<string, DisplacementData> FemaleDisplacements = new(); public Dictionary<string, DisplacementData> FemaleDisplacements = new();
/// <summary> /// <summary>
/// Alternate displacement maps, which if available, will be selected for the player of the appropriate gender. /// Alternate displacement maps, which if available, will be selected for the player of the appropriate gender.
/// </summary> /// </summary>
[DataField] [DataField, AutoNetworkedField]
public Dictionary<string, DisplacementData> MaleDisplacements = new(); public Dictionary<string, DisplacementData> MaleDisplacements = new();
} }

View File

@@ -43,7 +43,7 @@ public partial class InventorySystem : EntitySystem
if (!TryComp<T>(item, out var required)) if (!TryComp<T>(item, out var required))
continue; continue;
if ((((IClothingSlots) required).Slots & slot.SlotFlags) == 0x0) if ((((IClothingSlots)required).Slots & slot.SlotFlags) == 0x0)
continue; continue;
target = (item, required); target = (item, required);
@@ -55,20 +55,9 @@ public partial class InventorySystem : EntitySystem
return false; return false;
} }
protected virtual void OnInit(EntityUid uid, InventoryComponent component, ComponentInit args) private void OnInit(Entity<InventoryComponent> ent, ref ComponentInit args)
{ {
if (!_prototypeManager.TryIndex(component.TemplateId, out InventoryTemplatePrototype? invTemplate)) UpdateInventoryTemplate(ent);
return;
component.Slots = invTemplate.Slots;
component.Containers = new ContainerSlot[component.Slots.Length];
for (var i = 0; i < component.Containers.Length; i++)
{
var slot = component.Slots[i];
var container = _containerSystem.EnsureContainer<ContainerSlot>(uid, slot.Name);
container.OccludesLight = false;
component.Containers[i] = container;
}
} }
private void AfterAutoState(Entity<InventoryComponent> ent, ref AfterAutoHandleStateEvent args) private void AfterAutoState(Entity<InventoryComponent> ent, ref AfterAutoHandleStateEvent args)
@@ -78,18 +67,31 @@ public partial class InventorySystem : EntitySystem
protected virtual void UpdateInventoryTemplate(Entity<InventoryComponent> ent) protected virtual void UpdateInventoryTemplate(Entity<InventoryComponent> ent)
{ {
if (ent.Comp.LifeStage < ComponentLifeStage.Initialized) if (!_prototypeManager.Resolve(ent.Comp.TemplateId, out var invTemplate))
return; return;
if (!_prototypeManager.TryIndex(ent.Comp.TemplateId, out InventoryTemplatePrototype? invTemplate)) // Remove any containers that aren't in the new template.
return; foreach (var container in ent.Comp.Containers)
{
if (invTemplate.Slots.Any(s => s.Name == container.ID))
continue;
DebugTools.Assert(ent.Comp.Slots.Length == invTemplate.Slots.Length); // Empty container before deletion so the contents don't get deleted.
// For cases when we update the template while items are already worn.
_containerSystem.EmptyContainer(container);
_containerSystem.ShutdownContainer(container);
}
// Ensure the containers from the template.
ent.Comp.Slots = invTemplate.Slots; ent.Comp.Slots = invTemplate.Slots;
ent.Comp.Containers = new ContainerSlot[ent.Comp.Slots.Length];
var ev = new InventoryTemplateUpdated(); for (var i = 0; i < ent.Comp.Containers.Length; i++)
RaiseLocalEvent(ent, ref ev); {
var slot = ent.Comp.Slots[i];
var container = _containerSystem.EnsureContainer<ContainerSlot>(ent.Owner, slot.Name);
container.OccludesLight = false;
ent.Comp.Containers[i] = container;
}
} }
private void OnOpenSlotStorage(OpenSlotStorageNetworkMessage ev, EntitySessionEventArgs args) private void OnOpenSlotStorage(OpenSlotStorageNetworkMessage ev, EntitySessionEventArgs args)
@@ -195,27 +197,21 @@ public partial class InventorySystem : EntitySystem
} }
/// <summary> /// <summary>
/// Change the inventory template ID an entity is using. The new template must be compatible. /// Change the inventory template ID an entity is using
/// and drop any item that does not have a slot according to the new template.
/// This will update the client-side UI accordingly.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para>
/// For an inventory template to be compatible with another, it must have exactly the same slot names.
/// All other changes are rejected.
/// </para>
/// </remarks> /// </remarks>
/// <param name="ent">The entity to update.</param> /// <param name="ent">The entity to update.</param>
/// <param name="newTemplate">The ID of the new inventory template prototype.</param> /// <param name="newTemplate">The ID of the new inventory template prototype.</param>
/// <exception cref="ArgumentException">
/// Thrown if the new template is not compatible with the existing one.
/// </exception>
public void SetTemplateId(Entity<InventoryComponent> ent, ProtoId<InventoryTemplatePrototype> newTemplate) public void SetTemplateId(Entity<InventoryComponent> ent, ProtoId<InventoryTemplatePrototype> newTemplate)
{ {
var newPrototype = _prototypeManager.Index(newTemplate); if (ent.Comp.TemplateId == newTemplate)
return;
if (!newPrototype.Slots.Select(x => x.Name).SequenceEqual(ent.Comp.Slots.Select(x => x.Name)))
throw new ArgumentException("Incompatible inventory template!");
ent.Comp.TemplateId = newTemplate; ent.Comp.TemplateId = newTemplate;
UpdateInventoryTemplate(ent);
Dirty(ent); Dirty(ent);
} }