ItemCreatorSystem.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Content.Shared.Actions;
  2. using Content.Shared.Ninja.Components;
  3. namespace Content.Shared.Ninja.Systems;
  4. /// <summary>
  5. /// Handles predicting that the action exists, creating items is done serverside.
  6. /// </summary>
  7. public abstract class SharedItemCreatorSystem : EntitySystem
  8. {
  9. [Dependency] private readonly ActionContainerSystem _actionContainer = default!;
  10. public override void Initialize()
  11. {
  12. base.Initialize();
  13. SubscribeLocalEvent<ItemCreatorComponent, MapInitEvent>(OnMapInit);
  14. SubscribeLocalEvent<ItemCreatorComponent, GetItemActionsEvent>(OnGetActions);
  15. }
  16. private void OnMapInit(Entity<ItemCreatorComponent> ent, ref MapInitEvent args)
  17. {
  18. var (uid, comp) = ent;
  19. // test funny dont mind me
  20. if (string.IsNullOrEmpty(comp.Action))
  21. return;
  22. _actionContainer.EnsureAction(uid, ref comp.ActionEntity, comp.Action);
  23. Dirty(uid, comp);
  24. }
  25. private void OnGetActions(Entity<ItemCreatorComponent> ent, ref GetItemActionsEvent args)
  26. {
  27. if (CheckItemCreator(ent, args.User))
  28. args.AddAction(ent.Comp.ActionEntity);
  29. }
  30. public bool CheckItemCreator(EntityUid uid, EntityUid user)
  31. {
  32. var ev = new CheckItemCreatorEvent(user);
  33. RaiseLocalEvent(uid, ref ev);
  34. return !ev.Cancelled;
  35. }
  36. }
  37. /// <summary>
  38. /// Raised on the item creator before adding the action.
  39. /// </summary>
  40. [ByRefEvent]
  41. public record struct CheckItemCreatorEvent(EntityUid User, bool Cancelled = false);
  42. /// <summary>
  43. /// Raised on the item creator before creating an item.
  44. /// </summary>
  45. [ByRefEvent]
  46. public record struct CreateItemAttemptEvent(EntityUid User, bool Cancelled = false);