base.Initialize();
SubscribeLocalEvent<ClothingComponent, GetEquipmentVisualsEvent>(OnGetVisuals);
- SubscribeLocalEvent<ClothingComponent, InventoryTemplateUpdated>(OnInventoryTemplateUpdated);
+ SubscribeLocalEvent<InventoryComponent, InventoryTemplateUpdated>(OnInventoryTemplateUpdated);
SubscribeLocalEvent<InventoryComponent, VisualsChangedEvent>(OnVisualsChanged);
SubscribeLocalEvent<SpriteComponent, DidUnequipEvent>(OnDidUnequip);
}
}
- 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(
EntityUid uid,
- InventoryComponent? inventoryComponent = null,
- ClothingComponent? clothing = null)
+ InventoryComponent? inventoryComponent = null)
{
var enumerator = _inventorySystem.GetSlotEnumerator((uid, inventoryComponent));
while (enumerator.NextItem(out var item, out var slot))
{
- RenderEquipment(uid, item, slot.Name, inventoryComponent, clothingComponent: clothing);
+ RenderEquipment(uid, item, slot.Name, inventoryComponent);
}
}
+using System.Linq;
using Content.Client.Clothing;
using Content.Client.Examine;
using Content.Client.Verbs.UI;
using Robust.Shared.Containers;
using Robust.Shared.Input.Binding;
using Robust.Shared.Player;
+using Robust.Shared.Timing;
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!;
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();
}
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);
}
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;
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;
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;
}
{
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)
+using Robust.Shared.Serialization;
+
namespace Content.Shared.DisplacementMap;
-[DataDefinition]
+[DataDefinition, Serializable, NetSerializable]
public sealed partial class DisplacementData
{
/// <summary>
using Content.Shared.DisplacementMap;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
-using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
+using Robust.Shared.Prototypes;
namespace Content.Shared.Inventory;
[AutoGenerateComponentState(true)]
public sealed partial class InventoryComponent : Component
{
- [DataField("templateId", customTypeSerializer: typeof(PrototypeIdSerializer<InventoryTemplatePrototype>))]
- [AutoNetworkedField]
- public string TemplateId { get; set; } = "human";
+ /// <summary>
+ /// The template defining how the inventory layout will look like.
+ /// </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>();
+
+ [ViewVariables]
public ContainerSlot[] Containers = Array.Empty<ContainerSlot>();
- [DataField]
+ [DataField, AutoNetworkedField]
public Dictionary<string, DisplacementData> Displacements = new();
/// <summary>
/// Alternate displacement maps, which if available, will be selected for the player of the appropriate gender.
/// </summary>
- [DataField]
+ [DataField, AutoNetworkedField]
public Dictionary<string, DisplacementData> FemaleDisplacements = new();
/// <summary>
/// Alternate displacement maps, which if available, will be selected for the player of the appropriate gender.
/// </summary>
- [DataField]
+ [DataField, AutoNetworkedField]
public Dictionary<string, DisplacementData> MaleDisplacements = new();
}
if (!TryComp<T>(item, out var required))
continue;
- if ((((IClothingSlots) required).Slots & slot.SlotFlags) == 0x0)
+ if ((((IClothingSlots)required).Slots & slot.SlotFlags) == 0x0)
continue;
target = (item, required);
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))
- 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;
- }
+ UpdateInventoryTemplate(ent);
}
private void AfterAutoState(Entity<InventoryComponent> ent, ref AfterAutoHandleStateEvent args)
protected virtual void UpdateInventoryTemplate(Entity<InventoryComponent> ent)
{
- if (ent.Comp.LifeStage < ComponentLifeStage.Initialized)
+ if (!_prototypeManager.Resolve(ent.Comp.TemplateId, out var invTemplate))
return;
- if (!_prototypeManager.TryIndex(ent.Comp.TemplateId, out InventoryTemplatePrototype? invTemplate))
- return;
+ // Remove any containers that aren't in the new template.
+ 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;
-
- var ev = new InventoryTemplateUpdated();
- RaiseLocalEvent(ent, ref ev);
+ ent.Comp.Containers = new ContainerSlot[ent.Comp.Slots.Length];
+ for (var i = 0; i < ent.Comp.Containers.Length; i++)
+ {
+ 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)
}
/// <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>
/// <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>
/// <param name="ent">The entity to update.</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)
{
- var newPrototype = _prototypeManager.Index(newTemplate);
-
- if (!newPrototype.Slots.Select(x => x.Name).SequenceEqual(ent.Comp.Slots.Select(x => x.Name)))
- throw new ArgumentException("Incompatible inventory template!");
+ if (ent.Comp.TemplateId == newTemplate)
+ return;
ent.Comp.TemplateId = newTemplate;
+ UpdateInventoryTemplate(ent);
Dirty(ent);
}
private int _nextIdx = 0;
public static InventorySlotEnumerator Empty = new(Array.Empty<SlotDefinition>(), Array.Empty<ContainerSlot>());
- public InventorySlotEnumerator(InventoryComponent inventory, SlotFlags flags = SlotFlags.All)
+ public InventorySlotEnumerator(InventoryComponent inventory, SlotFlags flags = SlotFlags.All)
: this(inventory.Slots, inventory.Containers, flags)
{
}
- public InventorySlotEnumerator(SlotDefinition[] slots, ContainerSlot[] containers, SlotFlags flags = SlotFlags.All)
+ public InventorySlotEnumerator(SlotDefinition[] slots, ContainerSlot[] containers, SlotFlags flags = SlotFlags.All)
{
DebugTools.Assert(flags != SlotFlags.NONE);
DebugTools.AssertEqual(slots.Length, containers.Length);