QuestTimer.java 4.1 KB

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