2
0

QuestStateManager.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.List;
  17. import javolution.util.FastList;
  18. import net.sf.l2j.gameserver.ThreadPoolManager;
  19. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  20. public class QuestStateManager
  21. {
  22. // =========================================================
  23. // Schedule Task
  24. public class ScheduleTimerTask implements Runnable
  25. {
  26. public void run()
  27. {
  28. try
  29. {
  30. cleanUp();
  31. ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleTimerTask(), 60000);
  32. }
  33. catch (Throwable t)
  34. {
  35. }
  36. }
  37. }
  38. // =========================================================
  39. // Data Field
  40. private static QuestStateManager _instance;
  41. private List<QuestState> _questStates = new FastList<QuestState>();
  42. // =========================================================
  43. // Constructor
  44. public QuestStateManager()
  45. {
  46. ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleTimerTask(), 60000);
  47. }
  48. // =========================================================
  49. // Method - Public
  50. /**
  51. * Add QuestState for the specified player instance
  52. */
  53. public void addQuestState(Quest quest, L2PcInstance player, byte state)
  54. {
  55. QuestState qs = getQuestState(player);
  56. if (qs == null)
  57. qs = new QuestState(quest, player, state);
  58. }
  59. /**
  60. * Remove all QuestState for all player instance that does not exist
  61. */
  62. public void cleanUp()
  63. {
  64. for (int i = getQuestStates().size() - 1; i >= 0; i--)
  65. {
  66. if (getQuestStates().get(i).getPlayer() == null)
  67. {
  68. removeQuestState(getQuestStates().get(i));
  69. getQuestStates().remove(i);
  70. }
  71. }
  72. }
  73. // =========================================================
  74. // Method - Private
  75. /**
  76. * Remove QuestState instance
  77. */
  78. private void removeQuestState(QuestState qs)
  79. {
  80. qs = null;
  81. }
  82. // =========================================================
  83. // Property - Public
  84. public static final QuestStateManager getInstance()
  85. {
  86. if (_instance == null)
  87. _instance = new QuestStateManager();
  88. return _instance;
  89. }
  90. /**
  91. * Return QuestState for specified player instance
  92. */
  93. public QuestState getQuestState(L2PcInstance player)
  94. {
  95. for (int i = 0; i < getQuestStates().size(); i++)
  96. {
  97. if (getQuestStates().get(i).getPlayer() != null && getQuestStates().get(i).getPlayer().getObjectId() == player.getObjectId())
  98. return getQuestStates().get(i);
  99. }
  100. return null;
  101. }
  102. /**
  103. * Return all QuestState
  104. */
  105. public List<QuestState> getQuestStates()
  106. {
  107. if (_questStates == null)
  108. _questStates = new FastList<QuestState>();
  109. return _questStates;
  110. }
  111. }