QuestTimer.java 4.0 KB

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