QuestManager.java 4.7 KB

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