QuestStateManager.java 3.5 KB

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