NextAction.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.ai;
  16. import java.util.ArrayList;
  17. /**
  18. * Class for AI action after some event.<br>
  19. * Has 2 array list for "work" and "break".
  20. * @author Yaroslav
  21. */
  22. public class NextAction
  23. {
  24. public interface NextActionCallback
  25. {
  26. public void doWork();
  27. }
  28. private ArrayList<CtrlEvent> _events;
  29. private ArrayList<CtrlIntention> _intentions;
  30. private NextActionCallback _callback;
  31. /**
  32. * Main constructor.
  33. * @param events
  34. * @param intentions
  35. * @param callback
  36. */
  37. public NextAction(ArrayList<CtrlEvent> events, ArrayList<CtrlIntention> intentions, NextActionCallback callback)
  38. {
  39. _events = events;
  40. _intentions = intentions;
  41. setCallback(callback);
  42. }
  43. /**
  44. * Single constructor.
  45. * @param event
  46. * @param intention
  47. * @param callback
  48. */
  49. public NextAction(CtrlEvent event, CtrlIntention intention, NextActionCallback callback)
  50. {
  51. if (_events == null)
  52. {
  53. _events = new ArrayList<>();
  54. }
  55. if (_intentions == null)
  56. {
  57. _intentions = new ArrayList<>();
  58. }
  59. if (event != null)
  60. {
  61. _events.add(event);
  62. }
  63. if (intention != null)
  64. {
  65. _intentions.add(intention);
  66. }
  67. setCallback(callback);
  68. }
  69. /**
  70. * Do action.
  71. */
  72. public void doAction()
  73. {
  74. if (_callback != null)
  75. {
  76. _callback.doWork();
  77. }
  78. }
  79. /**
  80. * @return the _event
  81. */
  82. public ArrayList<CtrlEvent> getEvents()
  83. {
  84. // If null return empty list.
  85. if (_events == null)
  86. {
  87. _events = new ArrayList<>();
  88. }
  89. return _events;
  90. }
  91. /**
  92. * @param event the event to set.
  93. */
  94. public void setEvents(ArrayList<CtrlEvent> event)
  95. {
  96. _events = event;
  97. }
  98. /**
  99. * @param event
  100. */
  101. public void addEvent(CtrlEvent event)
  102. {
  103. if (_events == null)
  104. {
  105. _events = new ArrayList<>();
  106. }
  107. if (event != null)
  108. {
  109. _events.add(event);
  110. }
  111. }
  112. /**
  113. * @param event
  114. */
  115. public void removeEvent(CtrlEvent event)
  116. {
  117. if (_events == null)
  118. {
  119. return;
  120. }
  121. _events.remove(event);
  122. }
  123. /**
  124. * @return the _callback
  125. */
  126. public NextActionCallback getCallback()
  127. {
  128. return _callback;
  129. }
  130. /**
  131. * @param callback the callback to set.
  132. */
  133. public void setCallback(NextActionCallback callback)
  134. {
  135. _callback = callback;
  136. }
  137. /**
  138. * @return the _intentions
  139. */
  140. public ArrayList<CtrlIntention> getIntentions()
  141. {
  142. // If null return empty list.
  143. if (_intentions == null)
  144. {
  145. _intentions = new ArrayList<>();
  146. }
  147. return _intentions;
  148. }
  149. /**
  150. * @param intentions the intention to set.
  151. */
  152. public void setIntentions(ArrayList<CtrlIntention> intentions)
  153. {
  154. _intentions = intentions;
  155. }
  156. /**
  157. * @param intention
  158. */
  159. public void addIntention(CtrlIntention intention)
  160. {
  161. if (_intentions == null)
  162. {
  163. _intentions = new ArrayList<>();
  164. }
  165. if (intention != null)
  166. {
  167. _intentions.add(intention);
  168. }
  169. }
  170. /**
  171. * @param intention
  172. */
  173. public void removeIntention(CtrlIntention intention)
  174. {
  175. if (_intentions == null)
  176. {
  177. return;
  178. }
  179. _intentions.remove(intention);
  180. }
  181. }