ActionGrantSystem.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. namespace Content.Shared.Actions;
  2. /// <summary>
  3. /// <see cref="ActionGrantComponent"/>
  4. /// </summary>
  5. public sealed class ActionGrantSystem : EntitySystem
  6. {
  7. [Dependency] private readonly SharedActionsSystem _actions = default!;
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeLocalEvent<ActionGrantComponent, MapInitEvent>(OnMapInit);
  12. SubscribeLocalEvent<ActionGrantComponent, ComponentShutdown>(OnShutdown);
  13. SubscribeLocalEvent<ItemActionGrantComponent, GetItemActionsEvent>(OnItemGet);
  14. }
  15. private void OnItemGet(Entity<ItemActionGrantComponent> ent, ref GetItemActionsEvent args)
  16. {
  17. if (!TryComp(ent.Owner, out ActionGrantComponent? grant))
  18. return;
  19. foreach (var action in grant.ActionEntities)
  20. {
  21. args.AddAction(action);
  22. }
  23. }
  24. private void OnMapInit(Entity<ActionGrantComponent> ent, ref MapInitEvent args)
  25. {
  26. foreach (var action in ent.Comp.Actions)
  27. {
  28. EntityUid? actionEnt = null;
  29. _actions.AddAction(ent.Owner, ref actionEnt, action);
  30. if (actionEnt != null)
  31. ent.Comp.ActionEntities.Add(actionEnt.Value);
  32. }
  33. }
  34. private void OnShutdown(Entity<ActionGrantComponent> ent, ref ComponentShutdown args)
  35. {
  36. foreach (var actionEnt in ent.Comp.ActionEntities)
  37. {
  38. _actions.RemoveAction(ent.Owner, actionEnt);
  39. }
  40. }
  41. }