1
0

SelfEquipOnlySystem.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Content.Shared.ActionBlocker;
  2. using Content.Shared.Clothing.Components;
  3. using Content.Shared.Inventory.Events;
  4. namespace Content.Shared.Inventory;
  5. public sealed class SelfEquipOnlySystem : EntitySystem
  6. {
  7. [Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
  8. /// <inheritdoc/>
  9. public override void Initialize()
  10. {
  11. SubscribeLocalEvent<SelfEquipOnlyComponent, BeingEquippedAttemptEvent>(OnBeingEquipped);
  12. SubscribeLocalEvent<SelfEquipOnlyComponent, BeingUnequippedAttemptEvent>(OnBeingUnequipped);
  13. }
  14. private void OnBeingEquipped(Entity<SelfEquipOnlyComponent> ent, ref BeingEquippedAttemptEvent args)
  15. {
  16. if (args.Cancelled)
  17. return;
  18. if (TryComp<ClothingComponent>(ent, out var clothing) && (clothing.Slots & args.SlotFlags) == SlotFlags.NONE)
  19. return;
  20. if (args.Equipee != args.EquipTarget)
  21. args.Cancel();
  22. }
  23. private void OnBeingUnequipped(Entity<SelfEquipOnlyComponent> ent, ref BeingUnequippedAttemptEvent args)
  24. {
  25. if (args.Cancelled)
  26. return;
  27. if (args.Unequipee == args.UnEquipTarget)
  28. return;
  29. if (TryComp<ClothingComponent>(ent, out var clothing) && (clothing.Slots & args.SlotFlags) == SlotFlags.NONE)
  30. return;
  31. if (ent.Comp.UnequipRequireConscious && !_actionBlocker.CanConsciouslyPerformAction(args.UnEquipTarget))
  32. return;
  33. args.Cancel();
  34. }
  35. }