A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HandlerBuilder.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using Svelto.ECS;
  3. namespace GamecraftModdingAPI.Events
  4. {
  5. public class HandlerBuilder
  6. {
  7. private string name;
  8. private int? type;
  9. private Action<EntitiesDB> activated;
  10. private Action<EntitiesDB> destroyed;
  11. /// <summary>
  12. /// Create a new event handler builder.
  13. /// </summary>
  14. public HandlerBuilder()
  15. {
  16. }
  17. /// <summary>
  18. /// Create a new event handler builder.
  19. /// This is equivalent to new <code>HandlerBuilder().Name(name)</code>
  20. /// </summary>
  21. /// <param name="name">The handler name.</param>
  22. public HandlerBuilder(string name)
  23. {
  24. this.name = name;
  25. }
  26. /// <summary>
  27. /// Create and return an event handler builder.
  28. /// </summary>
  29. /// <returns>The builder.</returns>
  30. public static HandlerBuilder Builder()
  31. {
  32. return new HandlerBuilder();
  33. }
  34. /// <summary>
  35. /// Create and return an event handler builder.
  36. /// This is equivalent to <code>Builder().Name(name)</code>
  37. /// </summary>
  38. /// <returns>The builder.</returns>
  39. /// <param name="name">The handler name.</param>
  40. public static HandlerBuilder Builder(string name)
  41. {
  42. return new HandlerBuilder(name);
  43. }
  44. /// <summary>
  45. /// Name the event handler.
  46. /// </summary>
  47. /// <returns>The builder.</returns>
  48. /// <param name="name">The event handler name.</param>
  49. public HandlerBuilder Name(string name)
  50. {
  51. this.name = name;
  52. return this;
  53. }
  54. /// <summary>
  55. /// Set the action to perform on when the activated event occurs.
  56. /// </summary>
  57. /// <returns>The builder.</returns>
  58. /// <param name="action">The activated event action.</param>
  59. public HandlerBuilder OnActivation(Action action)
  60. {
  61. return OnActivation((_) => { action(); });
  62. }
  63. /// <summary>
  64. /// Set the action to perform on when the activated event occurs.
  65. /// </summary>
  66. /// <returns>The builder.</returns>
  67. /// <param name="action">The activated event action.</param>
  68. public HandlerBuilder OnActivation(Action<EntitiesDB> action)
  69. {
  70. this.activated = action;
  71. return this;
  72. }
  73. /// <summary>
  74. /// Set the action to perform when the destroyed event occurs.
  75. /// </summary>
  76. /// <returns>The builder.</returns>
  77. /// <param name="action">The destroyed event action.</param>
  78. public HandlerBuilder OnDestruction(Action action)
  79. {
  80. return OnDestruction((_) => { action(); });
  81. }
  82. /// <summary>
  83. /// Set the action to perform when the destroyed event occurs.
  84. /// </summary>
  85. /// <returns>The builder.</returns>
  86. /// <param name="action">The destroyed event action.</param>
  87. public HandlerBuilder OnDestruction(Action<EntitiesDB> action)
  88. {
  89. this.destroyed = action;
  90. return this;
  91. }
  92. /// <summary>
  93. /// Set the type of event to handle.
  94. /// </summary>
  95. /// <returns>The builder.</returns>
  96. /// <param name="eventType">The event type.</param>
  97. public HandlerBuilder Handle(EventType eventType)
  98. {
  99. return Handle((int)eventType);
  100. }
  101. /// <summary>
  102. /// Set the type of event to handle.
  103. /// </summary>
  104. /// <returns>The builder.</returns>
  105. /// <param name="eventType">The event type.</param>
  106. public HandlerBuilder Handle(int eventType)
  107. {
  108. this.type = eventType;
  109. return this;
  110. }
  111. /// <summary>
  112. /// Build the event handler.
  113. /// </summary>
  114. /// <returns>The event handler.</returns>
  115. /// <param name="register">Automatically register the event handler with EventManager.AddEventHandler().</param>
  116. public IEventHandlerEngine Build(bool register = true)
  117. {
  118. if (string.IsNullOrWhiteSpace(name))
  119. {
  120. throw new EventParameterMissingException("Event handler name must be defined before Build() is called");
  121. }
  122. if (activated == null && destroyed == null)
  123. {
  124. throw new EventParameterMissingException("Event handler destruction or activated event action must be defined before Build() is called");
  125. }
  126. if (!type.HasValue)
  127. {
  128. throw new EventParameterMissingException("Event handler event type must be defined before Build() is called");
  129. }
  130. Action<EntitiesDB> validActivated = activated;
  131. if (validActivated == null)
  132. {
  133. validActivated = (_) => { };
  134. }
  135. Action<EntitiesDB> validDestroyed = destroyed;
  136. if (validDestroyed == null)
  137. {
  138. validDestroyed = (_) => { };
  139. }
  140. SimpleEventHandlerEngine result = new SimpleEventHandlerEngine(validActivated, validDestroyed, type.Value, name);
  141. if (register)
  142. {
  143. EventManager.AddEventHandler(result);
  144. }
  145. return result;
  146. }
  147. }
  148. }