QuestManager.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.instancemanager;
  16. import java.util.Map;
  17. import java.util.logging.Logger;
  18. import javolution.util.FastMap;
  19. import net.sf.l2j.gameserver.model.quest.Quest;
  20. import net.sf.l2j.gameserver.scripting.ScriptManager;
  21. public class QuestManager extends ScriptManager<Quest>
  22. {
  23. protected static final Logger _log = Logger.getLogger(QuestManager.class.getName());
  24. // =========================================================
  25. private static QuestManager _instance;
  26. public static final QuestManager getInstance()
  27. {
  28. if (_instance == null)
  29. {
  30. _log.info("Initializing QuestManager");
  31. _instance = new QuestManager();
  32. }
  33. return _instance;
  34. }
  35. // =========================================================
  36. // =========================================================
  37. // Data Field
  38. private Map<String, Quest> _quests = new FastMap<String, Quest>();
  39. // =========================================================
  40. // Constructor
  41. public QuestManager()
  42. {
  43. }
  44. // =========================================================
  45. // Method - Public
  46. public final boolean reload(String questFolder)
  47. {
  48. Quest q = getQuest(questFolder);
  49. if (q == null)
  50. {
  51. return false;
  52. }
  53. return q.reload();
  54. }
  55. /**
  56. * Reloads a the quest given by questId.<BR>
  57. * <B>NOTICE: Will only work if the quest name is equal the quest folder name</B>
  58. * @param questId The id of the quest to be reloaded
  59. * @return true if reload was succesful, false otherwise
  60. */
  61. public final boolean reload(int questId)
  62. {
  63. Quest q = this.getQuest(questId);
  64. if (q == null)
  65. {
  66. return false;
  67. }
  68. return q.reload();
  69. }
  70. public final void report()
  71. {
  72. _log.info("Loaded: " + getQuests().size() + " quests");
  73. }
  74. public final void save()
  75. {
  76. for (Quest q: getQuests().values())
  77. {
  78. q.saveGlobalData();
  79. }
  80. }
  81. // =========================================================
  82. // Property - Public
  83. public final Quest getQuest(String name)
  84. {
  85. return getQuests().get(name);
  86. }
  87. public final Quest getQuest(int questId)
  88. {
  89. for (Quest q: getQuests().values())
  90. {
  91. if (q.getQuestIntId() == questId)
  92. return q;
  93. }
  94. return null;
  95. }
  96. public final void addQuest(Quest newQuest)
  97. {
  98. if (newQuest == null)
  99. {
  100. throw new IllegalArgumentException("Quest argument cannot be null");
  101. }
  102. Quest old = this.getQuests().get(newQuest.getName());
  103. // FIXME: unloading the old quest at this point is a tad too late.
  104. // the new quest has already initialized itself and read the data, starting
  105. // an unpredictable number of tasks with that data. The old quest will now
  106. // save data which will never be read.
  107. // However, requesting the newQuest to re-read the data is not necessarily a
  108. // good option, since the newQuest may have already started timers, spawned NPCs
  109. // or taken any other action which it might re-take by re-reading the data.
  110. // the current solution properly closes the running tasks of the old quest but
  111. // ignores the data; perhaps the least of all evils...
  112. if (old != null)
  113. {
  114. old.unload();
  115. _log.info("Replaced: ("+old.getName()+") with a new version ("+newQuest.getName()+")");
  116. }
  117. this.getQuests().put(newQuest.getName(), newQuest);
  118. }
  119. public final boolean removeQuest(Quest q)
  120. {
  121. return this.getQuests().remove(q.getName()) != null;
  122. }
  123. public final FastMap<String, Quest> getQuests()
  124. {
  125. if (_quests == null) _quests = new FastMap<String, Quest>();
  126. return (FastMap<String, Quest>) _quests;
  127. }
  128. /**
  129. * @see net.sf.l2j.gameserver.scripting.ScriptManager#getAllManagedScripts()
  130. */
  131. @Override
  132. public Iterable<Quest> getAllManagedScripts()
  133. {
  134. return _quests.values();
  135. }
  136. /**
  137. * @see net.sf.l2j.gameserver.scripting.ScriptManager#unload(net.sf.l2j.gameserver.scripting.ManagedScript)
  138. */
  139. @Override
  140. public boolean unload(Quest ms)
  141. {
  142. ms.saveGlobalData();
  143. return this.removeQuest(ms);
  144. }
  145. /**
  146. * @see net.sf.l2j.gameserver.scripting.ScriptManager#getScriptManagerName()
  147. */
  148. @Override
  149. public String getScriptManagerName()
  150. {
  151. return "QuestManager";
  152. }
  153. }