1
0

SericultureSystem.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Content.Shared.Actions;
  2. using Content.Shared.DoAfter;
  3. using Content.Shared.Nutrition.EntitySystems;
  4. using Robust.Shared.Serialization;
  5. using Content.Shared.Popups;
  6. using Robust.Shared.Network;
  7. using Content.Shared.Nutrition.Components;
  8. using Content.Shared.Stacks;
  9. namespace Content.Shared.Sericulture;
  10. /// <summary>
  11. /// Allows mobs to produce materials with <see cref="SericultureComponent"/>.
  12. /// </summary>
  13. public abstract partial class SharedSericultureSystem : EntitySystem
  14. {
  15. // Managers
  16. [Dependency] private readonly INetManager _netManager = default!;
  17. // Systems
  18. [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
  19. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  20. [Dependency] private readonly HungerSystem _hungerSystem = default!;
  21. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  22. [Dependency] private readonly SharedStackSystem _stackSystem = default!;
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. SubscribeLocalEvent<SericultureComponent, MapInitEvent>(OnMapInit);
  27. SubscribeLocalEvent<SericultureComponent, ComponentShutdown>(OnCompRemove);
  28. SubscribeLocalEvent<SericultureComponent, SericultureActionEvent>(OnSericultureStart);
  29. SubscribeLocalEvent<SericultureComponent, SericultureDoAfterEvent>(OnSericultureDoAfter);
  30. }
  31. /// <summary>
  32. /// Giveths the action to preform sericulture on the entity
  33. /// </summary>
  34. private void OnMapInit(EntityUid uid, SericultureComponent comp, MapInitEvent args)
  35. {
  36. _actionsSystem.AddAction(uid, ref comp.ActionEntity, comp.Action);
  37. }
  38. /// <summary>
  39. /// Takeths away the action to preform sericulture from the entity.
  40. /// </summary>
  41. private void OnCompRemove(EntityUid uid, SericultureComponent comp, ComponentShutdown args)
  42. {
  43. _actionsSystem.RemoveAction(uid, comp.ActionEntity);
  44. }
  45. private void OnSericultureStart(EntityUid uid, SericultureComponent comp, SericultureActionEvent args)
  46. {
  47. if (TryComp<HungerComponent>(uid, out var hungerComp)
  48. && _hungerSystem.IsHungerBelowState(uid,
  49. comp.MinHungerThreshold,
  50. _hungerSystem.GetHunger(hungerComp) - comp.HungerCost,
  51. hungerComp))
  52. {
  53. _popupSystem.PopupClient(Loc.GetString(comp.PopupText), uid, uid);
  54. return;
  55. }
  56. var doAfter = new DoAfterArgs(EntityManager, uid, comp.ProductionLength, new SericultureDoAfterEvent(), uid)
  57. { // I'm not sure if more things should be put here, but imo ideally it should probably be set in the component/YAML. Not sure if this is currently possible.
  58. BreakOnMove = true,
  59. BlockDuplicate = true,
  60. BreakOnDamage = true,
  61. CancelDuplicate = true,
  62. };
  63. _doAfterSystem.TryStartDoAfter(doAfter);
  64. }
  65. private void OnSericultureDoAfter(EntityUid uid, SericultureComponent comp, SericultureDoAfterEvent args)
  66. {
  67. if (args.Cancelled || args.Handled || comp.Deleted)
  68. return;
  69. if (TryComp<HungerComponent>(uid,
  70. out var hungerComp) // A check, just incase the doafter is somehow performed when the entity is not in the right hunger state.
  71. && _hungerSystem.IsHungerBelowState(uid,
  72. comp.MinHungerThreshold,
  73. _hungerSystem.GetHunger(hungerComp) - comp.HungerCost,
  74. hungerComp))
  75. {
  76. _popupSystem.PopupClient(Loc.GetString(comp.PopupText), uid, uid);
  77. return;
  78. }
  79. _hungerSystem.ModifyHunger(uid, -comp.HungerCost);
  80. if (!_netManager.IsClient) // Have to do this because spawning stuff in shared is CBT.
  81. {
  82. var newEntity = Spawn(comp.EntityProduced, Transform(uid).Coordinates);
  83. _stackSystem.TryMergeToHands(newEntity, uid);
  84. }
  85. args.Repeat = true;
  86. }
  87. }
  88. /// <summary>
  89. /// Should be relayed upon using the action.
  90. /// </summary>
  91. public sealed partial class SericultureActionEvent : InstantActionEvent { }
  92. /// <summary>
  93. /// Is relayed at the end of the sericulturing doafter.
  94. /// </summary>
  95. [Serializable, NetSerializable]
  96. public sealed partial class SericultureDoAfterEvent : SimpleDoAfterEvent { }