QuestManager.java 5.3 KB

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