Make ItemCabinet use ItemSlots (#4771)
* pda slot names * cabinets use item slots * fix yaml * fix tests
This commit is contained in:
@@ -5,14 +5,14 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
/// <summary>
|
||||
/// Item was placed in or removed from one of the slots in <see cref="SharedItemSlotsComponent"/>
|
||||
/// </summary>
|
||||
public class ItemSlotChanged : EntityEventArgs
|
||||
public class ItemSlotChangedEvent : EntityEventArgs
|
||||
{
|
||||
public SharedItemSlotsComponent SlotsComponent;
|
||||
public string SlotName;
|
||||
public ItemSlot Slot;
|
||||
public readonly EntityUid? ContainedItem;
|
||||
|
||||
public ItemSlotChanged(SharedItemSlotsComponent slotsComponent, string slotName, ItemSlot slot)
|
||||
public ItemSlotChangedEvent(SharedItemSlotsComponent slotsComponent, string slotName, ItemSlot slot)
|
||||
{
|
||||
SlotsComponent = slotsComponent;
|
||||
SlotName = slotName;
|
||||
|
||||
@@ -2,6 +2,7 @@ using Content.Shared.Sound;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
@@ -31,9 +32,23 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
[ViewVariables] [DataField("insertSound")] public SoundSpecifier? InsertSound;
|
||||
[ViewVariables] [DataField("ejectSound")] public SoundSpecifier? EjectSound;
|
||||
|
||||
/// <summary>
|
||||
/// The name of this item slot. This will be shown to the user in the verb menu.
|
||||
/// </summary>
|
||||
[ViewVariables] public string Name
|
||||
{
|
||||
get => _name != string.Empty
|
||||
? Loc.GetString(_name)
|
||||
: ContainerSlot.ContainedEntity?.Name ?? string.Empty;
|
||||
set => _name = value;
|
||||
}
|
||||
[DataField("name")] private string _name = string.Empty;
|
||||
|
||||
[DataField("item", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
[ViewVariables] public string? StartingItem;
|
||||
|
||||
[ViewVariables] public ContainerSlot ContainerSlot = default!;
|
||||
|
||||
public bool HasEntity => ContainerSlot.ContainedEntity != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
var item = EntityManager.SpawnEntity(slot.StartingItem, itemSlots.Owner.Transform.Coordinates);
|
||||
slot.ContainerSlot.Insert(item);
|
||||
|
||||
RaiseLocalEvent(uid, new ItemSlotChanged(itemSlots, slotName, slot));
|
||||
RaiseLocalEvent(uid, new ItemSlotChangedEvent(itemSlots, slotName, slot));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,11 +76,9 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
continue;
|
||||
|
||||
Verb verb = new();
|
||||
// TODO ITEMSLOTS give item slot names localization strings?
|
||||
// Basically: its much nicer to have "insert ID" instead of the much longer "Eject <full-in-game-username>'s ID card (assistant)"
|
||||
verb.Text = slot.ContainerSlot.ContainedEntity.Name;
|
||||
verb.Text = slot.Name;
|
||||
verb.Category = VerbCategory.Eject;
|
||||
verb.Act = () => TryEjectContent(component, slotName, args.User);
|
||||
verb.Act = () => TryEjectContent(uid, slotName, args.User, component);
|
||||
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
@@ -100,9 +98,7 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
continue;
|
||||
|
||||
Verb verb = new();
|
||||
// TODO ITEMSLOTS give item slot names localization strings?
|
||||
// Basically: its much nicer to have "insert ID" instead of the much longer "Insert <full-in-game-username>'s ID card (assistant)"
|
||||
verb.Text = args.Using.Name;
|
||||
verb.Text = slot.Name != string.Empty ? slot.Name : args.Using.Name;
|
||||
verb.Category = VerbCategory.Insert;
|
||||
verb.Act = () => InsertContent(component, slot, slotName, args.Using);
|
||||
args.Verbs.Add(verb);
|
||||
@@ -114,15 +110,18 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
args.Handled = TryInsertContent(itemSlots, args.Used, args.User);
|
||||
args.Handled = TryInsertContent(uid, args.Used, args.User, itemSlots);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to insert or swap an item in any fitting item slot from users hand. If a valid slot already contains an item, it will swap it out.
|
||||
/// </summary>
|
||||
/// <returns>False if failed to insert item</returns>
|
||||
public bool TryInsertContent(SharedItemSlotsComponent itemSlots, IEntity item, IEntity user, SharedHandsComponent? hands = null)
|
||||
public bool TryInsertContent(EntityUid uid, IEntity item, IEntity user, SharedItemSlotsComponent? itemSlots = null, SharedHandsComponent? hands = null)
|
||||
{
|
||||
if (!Resolve(uid, ref itemSlots))
|
||||
return false;
|
||||
|
||||
if (!Resolve(user.Uid, ref hands))
|
||||
{
|
||||
itemSlots.Owner.PopupMessage(user, Loc.GetString("item-slots-try-insert-no-hands"));
|
||||
@@ -164,7 +163,7 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
{
|
||||
// insert item
|
||||
slot.ContainerSlot.Insert(item);
|
||||
RaiseLocalEvent(itemSlots.Owner.Uid, new ItemSlotChanged(itemSlots, slotName, slot));
|
||||
RaiseLocalEvent(itemSlots.Owner.Uid, new ItemSlotChangedEvent(itemSlots, slotName, slot));
|
||||
|
||||
// play sound
|
||||
if (slot.InsertSound != null)
|
||||
@@ -218,8 +217,11 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
/// <summary>
|
||||
/// Try to eject item from slot to users hands
|
||||
/// </summary>
|
||||
public bool TryEjectContent(SharedItemSlotsComponent itemSlots, string slotName, IEntity? user)
|
||||
public bool TryEjectContent(EntityUid uid, string slotName, IEntity? user, SharedItemSlotsComponent? itemSlots = null)
|
||||
{
|
||||
if (!Resolve(uid, ref itemSlots))
|
||||
return false;
|
||||
|
||||
if (!itemSlots.Slots.TryGetValue(slotName, out var slot))
|
||||
return false;
|
||||
|
||||
@@ -246,7 +248,7 @@ namespace Content.Shared.Containers.ItemSlots
|
||||
if (slot.EjectSound != null)
|
||||
SoundSystem.Play(Filter.Pvs(itemSlots.Owner), slot.EjectSound.GetSound(), itemSlots.Owner);
|
||||
|
||||
RaiseLocalEvent(itemSlots.Owner.Uid, new ItemSlotChanged(itemSlots, slotName, slot));
|
||||
RaiseLocalEvent(itemSlots.Owner.Uid, new ItemSlotChangedEvent(itemSlots, slotName, slot));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user