QuestTimer.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.model.quest;
  16. import java.util.concurrent.ScheduledFuture;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.gameserver.ThreadPoolManager;
  20. import com.l2jserver.gameserver.model.actor.L2Npc;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. public class QuestTimer
  23. {
  24. protected static final Logger _log = Logger.getLogger(QuestTimer.class.getName());
  25. // =========================================================
  26. // Schedule Task
  27. public class ScheduleTimerTask implements Runnable
  28. {
  29. @Override
  30. public void run()
  31. {
  32. if (this == null || !getIsActive())
  33. return;
  34. try
  35. {
  36. if (!getIsRepeating())
  37. cancel();
  38. getQuest().notifyEvent(getName(), getNpc(), getPlayer());
  39. }
  40. catch (Exception e)
  41. {
  42. _log.log(Level.SEVERE, "", e);
  43. }
  44. }
  45. }
  46. // =========================================================
  47. // Data Field
  48. private boolean _isActive = true;
  49. private String _name;
  50. private Quest _quest;
  51. private L2Npc _npc;
  52. private L2PcInstance _player;
  53. private boolean _isRepeating;
  54. private ScheduledFuture<?> _schedular;
  55. // =========================================================
  56. // Constructor
  57. public QuestTimer(Quest quest, String name, long time, L2Npc npc, L2PcInstance player, boolean repeating)
  58. {
  59. _name = name;
  60. _quest = quest;
  61. _player = player;
  62. _npc = npc;
  63. _isRepeating = repeating;
  64. if (repeating)
  65. _schedular = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new ScheduleTimerTask(), time, time); // Prepare auto end task
  66. else
  67. _schedular = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleTimerTask(), time); // Prepare auto end task
  68. }
  69. public QuestTimer(Quest quest, String name, long time, L2Npc npc, L2PcInstance player)
  70. {
  71. this(quest, name, time, npc, player, false);
  72. }
  73. public QuestTimer(QuestState qs, String name, long time)
  74. {
  75. this(qs.getQuest(), name, time, null, qs.getPlayer(), false);
  76. }
  77. // =========================================================
  78. // Method - Public
  79. public void cancel()
  80. {
  81. _isActive = false;
  82. if (_schedular != null)
  83. _schedular.cancel(false);
  84. getQuest().removeQuestTimer(this);
  85. }
  86. /**
  87. * public method to compare if this timer matches with the key attributes passed.
  88. * @param quest : Quest instance to which the timer is attached
  89. * @param name : Name of the timer
  90. * @param npc : Npc instance attached to the desired timer (null if no npc attached)
  91. * @param player : Player instance attached to the desired timer (null if no player attached)
  92. * @return
  93. */
  94. public boolean isMatch(Quest quest, String name, L2Npc npc, L2PcInstance player)
  95. {
  96. if ((quest == null) || (name == null))
  97. return false;
  98. if ((quest != getQuest()) || name.compareToIgnoreCase(getName()) != 0)
  99. return false;
  100. return ((npc == getNpc()) && (player == getPlayer()));
  101. }
  102. // =========================================================
  103. // Property - Public
  104. public final boolean getIsActive()
  105. {
  106. return _isActive;
  107. }
  108. public final boolean getIsRepeating()
  109. {
  110. return _isRepeating;
  111. }
  112. public final Quest getQuest()
  113. {
  114. return _quest;
  115. }
  116. public final String getName()
  117. {
  118. return _name;
  119. }
  120. public final L2Npc getNpc()
  121. {
  122. return _npc;
  123. }
  124. public final L2PcInstance getPlayer()
  125. {
  126. return _player;
  127. }
  128. @Override
  129. public final String toString()
  130. {
  131. return _name;
  132. }
  133. }