QuestManager.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.gameserver.model.quest.Quest;
  27. import com.l2jserver.gameserver.scripting.L2ScriptEngineManager;
  28. import com.l2jserver.gameserver.scripting.ScriptManager;
  29. import com.l2jserver.util.L2FastMap;
  30. public class QuestManager extends ScriptManager<Quest>
  31. {
  32. protected static final Logger _log = Logger.getLogger(QuestManager.class.getName());
  33. private final Map<String, Quest> _quests = new L2FastMap<>(true);
  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 = 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(getClass().getSimpleName() + ": 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, getClass().getSimpleName() + ": Failed loading scripts.cfg, no script going to be loaded!", e);
  82. }
  83. }
  84. public final void report()
  85. {
  86. _log.info(getClass().getSimpleName() + ": 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. {
  105. return q;
  106. }
  107. }
  108. return null;
  109. }
  110. public final void addQuest(Quest newQuest)
  111. {
  112. if (newQuest == null)
  113. {
  114. throw new IllegalArgumentException("Quest argument cannot be null");
  115. }
  116. Quest old = _quests.get(newQuest.getName());
  117. // FIXME: unloading the old quest at this point is a tad too late.
  118. // the new quest has already initialized itself and read the data, starting
  119. // an unpredictable number of tasks with that data. The old quest will now
  120. // save data which will never be read.
  121. // However, requesting the newQuest to re-read the data is not necessarily a
  122. // good option, since the newQuest may have already started timers, spawned NPCs
  123. // or taken any other action which it might re-take by re-reading the data.
  124. // the current solution properly closes the running tasks of the old quest but
  125. // ignores the data; perhaps the least of all evils...
  126. if (old != null)
  127. {
  128. old.unload();
  129. _log.info(getClass().getSimpleName() + ": Replaced: (" + old.getName() + ") with a new version (" + newQuest.getName() + ")");
  130. }
  131. _quests.put(newQuest.getName(), newQuest);
  132. }
  133. public final boolean removeQuest(Quest q)
  134. {
  135. return _quests.remove(q.getName()) != null;
  136. }
  137. @Override
  138. public Iterable<Quest> getAllManagedScripts()
  139. {
  140. return _quests.values();
  141. }
  142. @Override
  143. public boolean unload(Quest ms)
  144. {
  145. ms.saveGlobalData();
  146. return removeQuest(ms);
  147. }
  148. @Override
  149. public String getScriptManagerName()
  150. {
  151. return getClass().getSimpleName();
  152. }
  153. public static final QuestManager getInstance()
  154. {
  155. return SingletonHolder._instance;
  156. }
  157. private static class SingletonHolder
  158. {
  159. protected static final QuestManager _instance = new QuestManager();
  160. }
  161. }