QuestManager.java 4.9 KB

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