public sealed class WieldableSystem : EntitySystem
{
- [Dependency] private readonly SharedVirtualItemSystem _virtualItemSystem = default!;
- [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
- [Dependency] private readonly SharedItemSystem _itemSystem = default!;
- [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
- [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
- [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
- [Dependency] private readonly UseDelaySystem _delay = default!;
- [Dependency] private readonly SharedGunSystem _gun = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly INetManager _netManager = default!;
+ [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
+ [Dependency] private readonly SharedAudioSystem _audio = default!;
+ [Dependency] private readonly SharedGunSystem _gun = default!;
+ [Dependency] private readonly SharedHandsSystem _hands = default!;
+ [Dependency] private readonly SharedItemSystem _item = default!;
+ [Dependency] private readonly SharedPopupSystem _popup = default!;
+ [Dependency] private readonly SharedVirtualItemSystem _virtualItem = default!;
+ [Dependency] private readonly UseDelaySystem _delay = default!;
public override void Initialize()
{
{
component.LastPopup = time;
var message = Loc.GetString("wieldable-component-requires", ("item", uid));
- _popupSystem.PopupClient(message, args.Used, args.User);
+ _popup.PopupClient(message, args.Used, args.User);
}
}
}
private void OnDeselectWieldable(EntityUid uid, WieldableComponent component, HandDeselectedEvent args)
{
- if (!component.Wielded ||
- _handsSystem.EnumerateHands(args.User).Count() > 2)
+ if (_hands.EnumerateHands(args.User).Count() > 2)
return;
TryUnwield(uid, component, args.User);
if (args.Hands == null || !args.CanAccess || !args.CanInteract)
return;
- if (!_handsSystem.IsHolding(args.User, uid, out _, args.Hands))
+ if (!_hands.IsHolding(args.User, uid, out _, args.Hands))
return;
// TODO VERB TOOLTIPS Make CanWield or some other function return string, set as verb tooltip and disable
public bool CanWield(EntityUid uid, WieldableComponent component, EntityUid user, bool quiet = false)
{
// Do they have enough hands free?
- if (!EntityManager.TryGetComponent<HandsComponent>(user, out var hands))
+ if (!TryComp<HandsComponent>(user, out var hands))
{
if (!quiet)
- _popupSystem.PopupClient(Loc.GetString("wieldable-component-no-hands"), user, user);
+ _popup.PopupClient(Loc.GetString("wieldable-component-no-hands"), user, user);
return false;
}
// Is it.. actually in one of their hands?
- if (!_handsSystem.IsHolding(user, uid, out _, hands))
+ if (!_hands.IsHolding(user, uid, out _, hands))
{
if (!quiet)
- _popupSystem.PopupClient(Loc.GetString("wieldable-component-not-in-hands", ("item", uid)), user, user);
+ _popup.PopupClient(Loc.GetString("wieldable-component-not-in-hands", ("item", uid)), user, user);
return false;
}
- if (_handsSystem.CountFreeableHands((user, hands)) < component.FreeHandsRequired)
+ if (_hands.CountFreeableHands((user, hands)) < component.FreeHandsRequired)
{
if (!quiet)
{
var message = Loc.GetString("wieldable-component-not-enough-free-hands",
("number", component.FreeHandsRequired), ("item", uid));
- _popupSystem.PopupClient(message, user, user);
+ _popup.PopupClient(message, user, user);
}
return false;
}
if (!CanWield(used, component, user))
return false;
- var ev = new BeforeWieldEvent();
- RaiseLocalEvent(used, ev);
+ if (TryComp(used, out UseDelayComponent? useDelay)
+ && !_delay.TryResetDelay((used, useDelay), true))
+ return false;
+
+ var attemptEv = new WieldAttemptEvent(user);
+ RaiseLocalEvent(used, ref attemptEv);
- if (ev.Cancelled)
+ if (attemptEv.Cancelled)
return false;
if (TryComp<ItemComponent>(used, out var item))
{
component.OldInhandPrefix = item.HeldPrefix;
- _itemSystem.SetHeldPrefix(used, component.WieldedInhandPrefix, component: item);
+ _item.SetHeldPrefix(used, component.WieldedInhandPrefix, component: item);
}
- component.Wielded = true;
+ SetWielded((used, component), true);
if (component.WieldSound != null)
- _audioSystem.PlayPredicted(component.WieldSound, used, user);
+ _audio.PlayPredicted(component.WieldSound, used, user);
//This section handles spawning the virtual item(s) to occupy the required additional hand(s).
//Since the client can't currently predict entity spawning, only do this if this is running serverside.
var virtuals = new List<EntityUid>();
for (var i = 0; i < component.FreeHandsRequired; i++)
{
- if (_virtualItemSystem.TrySpawnVirtualItemInHand(used, user, out var virtualItem, true))
+ if (_virtualItem.TrySpawnVirtualItemInHand(used, user, out var virtualItem, true))
{
virtuals.Add(virtualItem.Value);
continue;
}
}
- if (TryComp(used, out UseDelayComponent? useDelay)
- && !_delay.TryResetDelay((used, useDelay), true))
- return false;
-
var selfMessage = Loc.GetString("wieldable-component-successful-wield", ("item", used));
var othersMessage = Loc.GetString("wieldable-component-successful-wield-other", ("user", Identity.Entity(user, EntityManager)), ("item", used));
- _popupSystem.PopupPredicted(selfMessage, othersMessage, user, user);
+ _popup.PopupPredicted(selfMessage, othersMessage, user, user);
- var targEv = new ItemWieldedEvent();
- RaiseLocalEvent(used, ref targEv);
+ var ev = new ItemWieldedEvent(user);
+ RaiseLocalEvent(used, ref ev);
- Dirty(used, component);
return true;
}
/// <summary>
- /// Attempts to unwield an item, with no DoAfter.
+ /// Attempts to unwield an item, with no use delay.
/// </summary>
/// <returns>True if the attempt wasn't blocked.</returns>
- public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user)
+ public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user, bool force = false)
{
- var ev = new BeforeUnwieldEvent();
- RaiseLocalEvent(used, ev);
+ if (!component.Wielded)
+ return false; // already unwielded
- if (ev.Cancelled)
- return false;
+ if (!force)
+ {
+ var attemptEv = new UnwieldAttemptEvent(user);
+ RaiseLocalEvent(used, ref attemptEv);
+
+ if (attemptEv.Cancelled)
+ return false;
+ }
- component.Wielded = false;
- var targEv = new ItemUnwieldedEvent(user);
+ SetWielded((used, component), false);
- RaiseLocalEvent(used, targEv);
+ var ev = new ItemUnwieldedEvent(user, force);
+ RaiseLocalEvent(used, ref ev);
return true;
}
+ /// <summary>
+ /// Sets wielded without doing any checks.
+ /// </summary>
+ private void SetWielded(Entity<WieldableComponent> ent, bool wielded)
+ {
+ ent.Comp.Wielded = wielded;
+ Dirty(ent);
+ _appearance.SetData(ent, WieldableVisuals.Wielded, wielded);
+ }
+
private void OnItemUnwielded(EntityUid uid, WieldableComponent component, ItemUnwieldedEvent args)
{
- if (args.User == null)
- return;
+ _item.SetHeldPrefix(uid, component.OldInhandPrefix);
- if (TryComp<ItemComponent>(uid, out var item))
- {
- _itemSystem.SetHeldPrefix(uid, component.OldInhandPrefix, component: item);
- }
+ var user = args.User;
+ _virtualItem.DeleteInHandsMatching(user, uid);
if (!args.Force) // don't play sound/popup if this was a forced unwield
{
if (component.UnwieldSound != null)
- _audioSystem.PlayPredicted(component.UnwieldSound, uid, args.User);
+ _audio.PlayPredicted(component.UnwieldSound, uid, user);
var selfMessage = Loc.GetString("wieldable-component-failed-wield", ("item", uid));
- var othersMessage = Loc.GetString("wieldable-component-failed-wield-other", ("user", Identity.Entity(args.User.Value, EntityManager)), ("item", uid));
- _popupSystem.PopupPredicted(selfMessage, othersMessage, args.User.Value, args.User.Value);
+ var othersMessage = Loc.GetString("wieldable-component-failed-wield-other", ("user", Identity.Entity(args.User, EntityManager)), ("item", uid));
+ _popup.PopupPredicted(selfMessage, othersMessage, user, user);
}
-
- _appearance.SetData(uid, WieldableVisuals.Wielded, false);
-
- Dirty(uid, component);
- _virtualItemSystem.DeleteInHandsMatching(args.User.Value, uid);
}
private void OnItemLeaveHand(EntityUid uid, WieldableComponent component, GotUnequippedHandEvent args)
{
- if (!component.Wielded || uid != args.Unequipped)
- return;
-
- RaiseLocalEvent(uid, new ItemUnwieldedEvent(args.User, force: true), true);
+ if (uid == args.Unequipped)
+ TryUnwield(uid, component, args.User, force: true);
}
private void OnVirtualItemDeleted(EntityUid uid, WieldableComponent component, VirtualItemDeletedEvent args)
{
- if (args.BlockingEntity == uid && component.Wielded)
- TryUnwield(args.BlockingEntity, component, args.User);
+ if (args.BlockingEntity == uid)
+ TryUnwield(uid, component, args.User, force: true);
}
private void OnGetMeleeDamage(EntityUid uid, IncreaseDamageOnWieldComponent component, ref GetMeleeDamageEvent args)