1
0

SpellbookSystem.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Content.Shared.Actions;
  2. using Content.Shared.DoAfter;
  3. using Content.Shared.Interaction.Events;
  4. using Content.Shared.Magic.Components;
  5. using Content.Shared.Mind;
  6. using Robust.Shared.Network;
  7. namespace Content.Shared.Magic;
  8. public sealed class SpellbookSystem : EntitySystem
  9. {
  10. [Dependency] private readonly SharedMindSystem _mind = default!;
  11. [Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
  12. [Dependency] private readonly SharedActionsSystem _actions = default!;
  13. [Dependency] private readonly ActionContainerSystem _actionContainer = default!;
  14. [Dependency] private readonly INetManager _netManager = default!;
  15. public override void Initialize()
  16. {
  17. SubscribeLocalEvent<SpellbookComponent, MapInitEvent>(OnInit, before: [typeof(SharedMagicSystem)]);
  18. SubscribeLocalEvent<SpellbookComponent, UseInHandEvent>(OnUse);
  19. SubscribeLocalEvent<SpellbookComponent, SpellbookDoAfterEvent>(OnDoAfter);
  20. }
  21. private void OnInit(Entity<SpellbookComponent> ent, ref MapInitEvent args)
  22. {
  23. foreach (var (id, charges) in ent.Comp.SpellActions)
  24. {
  25. var spell = _actionContainer.AddAction(ent, id);
  26. if (spell == null)
  27. continue;
  28. int? charge = charges;
  29. if (_actions.GetCharges(spell) != null)
  30. charge = _actions.GetCharges(spell);
  31. _actions.SetCharges(spell, charge < 0 ? null : charge);
  32. ent.Comp.Spells.Add(spell.Value);
  33. }
  34. }
  35. private void OnUse(Entity<SpellbookComponent> ent, ref UseInHandEvent args)
  36. {
  37. if (args.Handled)
  38. return;
  39. AttemptLearn(ent, args);
  40. args.Handled = true;
  41. }
  42. private void OnDoAfter<T>(Entity<SpellbookComponent> ent, ref T args) where T : DoAfterEvent // Sometimes i despise this language
  43. {
  44. if (args.Handled || args.Cancelled)
  45. return;
  46. args.Handled = true;
  47. if (!ent.Comp.LearnPermanently)
  48. {
  49. _actions.GrantActions(args.Args.User, ent.Comp.Spells, ent);
  50. return;
  51. }
  52. if (_mind.TryGetMind(args.Args.User, out var mindId, out _))
  53. {
  54. var mindActionContainerComp = EnsureComp<ActionsContainerComponent>(mindId);
  55. if (_netManager.IsServer)
  56. _actionContainer.TransferAllActionsWithNewAttached(ent, mindId, args.Args.User, newContainer: mindActionContainerComp);
  57. }
  58. else
  59. {
  60. foreach (var (id, charges) in ent.Comp.SpellActions)
  61. {
  62. EntityUid? actionId = null;
  63. if (_actions.AddAction(args.Args.User, ref actionId, id))
  64. _actions.SetCharges(actionId, charges < 0 ? null : charges);
  65. }
  66. }
  67. ent.Comp.SpellActions.Clear();
  68. }
  69. private void AttemptLearn(Entity<SpellbookComponent> ent, UseInHandEvent args)
  70. {
  71. var doAfterEventArgs = new DoAfterArgs(EntityManager, args.User, ent.Comp.LearnTime, new SpellbookDoAfterEvent(), ent, target: ent)
  72. {
  73. BreakOnMove = true,
  74. BreakOnDamage = true,
  75. NeedHand = true, //What, are you going to read with your eyes only??
  76. };
  77. _doAfter.TryStartDoAfter(doAfterEventArgs);
  78. }
  79. }