QuestTimer.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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()) return;
  29. try
  30. {
  31. getQuest().notifyEvent(getName(), getNpc(), getPlayer());
  32. if (!getIsRepeating())
  33. cancel();
  34. }
  35. catch (Throwable t)
  36. {
  37. }
  38. }
  39. }
  40. // =========================================================
  41. // Data Field
  42. private boolean _isActive = true;
  43. private String _name;
  44. private Quest _quest;
  45. private L2NpcInstance _npc;
  46. private L2PcInstance _player;
  47. private boolean _isRepeating;
  48. private ScheduledFuture<?> _schedular;
  49. // =========================================================
  50. // Constructor
  51. public QuestTimer(Quest quest, String name, long time, L2NpcInstance npc, L2PcInstance player, boolean repeating)
  52. {
  53. _name = name;
  54. _quest = quest;
  55. _player = player;
  56. _npc = npc;
  57. _isRepeating = repeating;
  58. if (repeating)
  59. _schedular = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new ScheduleTimerTask(), time, time); // Prepare auto end task
  60. else
  61. _schedular = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleTimerTask(), time); // Prepare auto end task
  62. }
  63. public QuestTimer(Quest quest, String name, long time, L2NpcInstance npc, L2PcInstance player)
  64. {
  65. this(quest, name, time, npc, player, false);
  66. }
  67. public QuestTimer(QuestState qs, String name, long time)
  68. {
  69. this(qs.getQuest(), name, time, null, qs.getPlayer(), false);
  70. }
  71. // =========================================================
  72. // Method - Public
  73. public void cancel()
  74. {
  75. _isActive = false;
  76. if (_schedular != null) _schedular.cancel(true);
  77. getQuest().removeQuestTimer(this);
  78. }
  79. // public method to compare if this timer matches with the key attributes passed.
  80. // a quest and a name are required.
  81. // null npc or player act as wildcards for the match
  82. public boolean isMatch(Quest quest, String name, L2NpcInstance npc, L2PcInstance player)
  83. {
  84. if ((quest == null) || (name == null))
  85. return false;
  86. if ( (quest != getQuest()) || name.compareToIgnoreCase(getName())!=0 )
  87. return false;
  88. return (( npc==null || getNpc()==null || npc==getNpc() ) && ( player==null || getPlayer()==null || player==getPlayer() ));
  89. }
  90. // =========================================================
  91. // Property - Public
  92. public final boolean getIsActive()
  93. {
  94. return _isActive;
  95. }
  96. public final boolean getIsRepeating()
  97. {
  98. return _isRepeating;
  99. }
  100. public final Quest getQuest()
  101. {
  102. return _quest;
  103. }
  104. public final String getName()
  105. {
  106. return _name;
  107. }
  108. public final L2NpcInstance getNpc()
  109. {
  110. return _npc;
  111. }
  112. public final L2PcInstance getPlayer()
  113. {
  114. return _player;
  115. }
  116. @Override
  117. public final String toString()
  118. {
  119. return _name;
  120. }
  121. }