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.

167 lines
4.8KB

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