QuestTimer.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.quest;
  20. import java.util.concurrent.ScheduledFuture;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.gameserver.ThreadPoolManager;
  24. import com.l2jserver.gameserver.model.actor.L2Npc;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. public class QuestTimer
  27. {
  28. protected static final Logger _log = Logger.getLogger(QuestTimer.class.getName());
  29. public class ScheduleTimerTask implements Runnable
  30. {
  31. @Override
  32. public void run()
  33. {
  34. if (!getIsActive())
  35. {
  36. return;
  37. }
  38. try
  39. {
  40. if (!getIsRepeating())
  41. {
  42. cancelAndRemove();
  43. }
  44. getQuest().notifyEvent(getName(), getNpc(), getPlayer());
  45. }
  46. catch (Exception e)
  47. {
  48. _log.log(Level.SEVERE, "", e);
  49. }
  50. }
  51. }
  52. private boolean _isActive = true;
  53. private final String _name;
  54. private final Quest _quest;
  55. private final L2Npc _npc;
  56. private final L2PcInstance _player;
  57. private final boolean _isRepeating;
  58. private ScheduledFuture<?> _schedular;
  59. public QuestTimer(Quest quest, String name, long time, L2Npc npc, L2PcInstance player, boolean repeating)
  60. {
  61. _name = name;
  62. _quest = quest;
  63. _player = player;
  64. _npc = npc;
  65. _isRepeating = repeating;
  66. if (repeating)
  67. {
  68. _schedular = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new ScheduleTimerTask(), time, time); // Prepare auto end task
  69. }
  70. else
  71. {
  72. _schedular = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleTimerTask(), time); // Prepare auto end task
  73. }
  74. }
  75. public QuestTimer(Quest quest, String name, long time, L2Npc npc, L2PcInstance player)
  76. {
  77. this(quest, name, time, npc, player, false);
  78. }
  79. public QuestTimer(QuestState qs, String name, long time)
  80. {
  81. this(qs.getQuest(), name, time, null, qs.getPlayer(), false);
  82. }
  83. /**
  84. * Cancel this quest timer.
  85. */
  86. public void cancel()
  87. {
  88. _isActive = false;
  89. if (_schedular != null)
  90. {
  91. _schedular.cancel(false);
  92. }
  93. }
  94. /**
  95. * Cancel this quest timer and remove it from the associated quest.
  96. */
  97. public void cancelAndRemove()
  98. {
  99. cancel();
  100. _quest.removeQuestTimer(this);
  101. }
  102. /**
  103. * Compares if this timer matches with the key attributes passed.
  104. * @param quest the quest to which the timer is attached
  105. * @param name the name of the timer
  106. * @param npc the NPC attached to the desired timer (null if no NPC attached)
  107. * @param player the player attached to the desired timer (null if no player attached)
  108. * @return
  109. */
  110. public boolean isMatch(Quest quest, String name, L2Npc npc, L2PcInstance player)
  111. {
  112. if ((quest == null) || (name == null))
  113. {
  114. return false;
  115. }
  116. if ((quest != _quest) || !name.equalsIgnoreCase(getName()))
  117. {
  118. return false;
  119. }
  120. return ((npc == _npc) && (player == _player));
  121. }
  122. public final boolean getIsActive()
  123. {
  124. return _isActive;
  125. }
  126. public final boolean getIsRepeating()
  127. {
  128. return _isRepeating;
  129. }
  130. public final Quest getQuest()
  131. {
  132. return _quest;
  133. }
  134. public final String getName()
  135. {
  136. return _name;
  137. }
  138. public final L2Npc getNpc()
  139. {
  140. return _npc;
  141. }
  142. public final L2PcInstance getPlayer()
  143. {
  144. return _player;
  145. }
  146. @Override
  147. public final String toString()
  148. {
  149. return _name;
  150. }
  151. }