QuestManager.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; either version 2, or (at your option)
  4. * any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14. * 02111-1307, USA.
  15. *
  16. * http://www.gnu.org/copyleft/gpl.html
  17. */
  18. package net.sf.l2j.gameserver.instancemanager;
  19. import java.util.logging.Logger;
  20. import java.util.Map;
  21. import javolution.util.FastMap;
  22. import net.sf.l2j.Config;
  23. import net.sf.l2j.gameserver.model.quest.Quest;
  24. import net.sf.l2j.gameserver.model.quest.jython.QuestJython;
  25. public class QuestManager
  26. {
  27. protected static final Logger _log = Logger.getLogger(QuestManager.class.getName());
  28. // =========================================================
  29. private static QuestManager _instance;
  30. public static final QuestManager getInstance()
  31. {
  32. if (_instance == null)
  33. {
  34. System.out.println("Initializing QuestManager");
  35. _instance = new QuestManager();
  36. if (!Config.ALT_DEV_NO_QUESTS)
  37. _instance.load();
  38. }
  39. return _instance;
  40. }
  41. // =========================================================
  42. // =========================================================
  43. // Data Field
  44. private Map<String, Quest> _quests = new FastMap<String, Quest>();
  45. // =========================================================
  46. // Constructor
  47. public QuestManager()
  48. {
  49. }
  50. // =========================================================
  51. // Method - Public
  52. public final boolean reload(String questFolder)
  53. {
  54. if(questFolder.indexOf('.') < 0)
  55. questFolder = "quests."+questFolder;
  56. Quest q = getQuest(questFolder);
  57. if (q!=null)
  58. q.saveGlobalData();
  59. return QuestJython.reloadQuest(questFolder);
  60. }
  61. /**
  62. * Reloads a the quest given by questId.<BR>
  63. * <B>NOTICE: Will only work if the quest name is equal the quest folder name</B>
  64. * @param questId The id of the quest to be reloaded
  65. * @return true if reload was succesful, false otherwise
  66. */
  67. public final boolean reload(int questId)
  68. {
  69. Quest q = this.getQuest(questId);
  70. if (q == null)
  71. {
  72. return false;
  73. }
  74. q.saveGlobalData();
  75. return QuestJython.reloadQuest("quests."+q.getName());
  76. }
  77. // =========================================================
  78. // Method - Private
  79. private final void load()
  80. {
  81. QuestJython.init();
  82. System.out.println("Loaded: " + getQuests().size() + " quests");
  83. }
  84. public final void save()
  85. {
  86. for(Quest q: getQuests().values())
  87. q.saveGlobalData();
  88. }
  89. // =========================================================
  90. // Property - Public
  91. public final Quest getQuest(String name)
  92. {
  93. if(name.indexOf('.') < 0)
  94. return getQuests().get("quests."+name);
  95. else
  96. return getQuests().get(name);
  97. }
  98. public final Quest getQuest(int questId)
  99. {
  100. for (Quest q: getQuests().values())
  101. {
  102. if (q.getQuestIntId() == questId)
  103. return q;
  104. }
  105. return null;
  106. }
  107. public final void addQuest(Quest newQuest)
  108. {
  109. if (getQuests().containsKey(newQuest.getName()))
  110. _log.info("Replaced: "+newQuest.getName()+" with a new version");
  111. // Given the quest instance, create a string representing the path and questName
  112. // like a simplified version of a canonical class name. That is, if a script is in
  113. // DATAPACK_PATH/jscript/quests/abc the result will be quests.abc
  114. // Similarly, for a script in DATAPACK_PATH/jscript/ai/individual/myClass.py
  115. // the result will be ai.individual.myClass
  116. // All quests are to be indexed, processed, and reloaded by this form of pathname.
  117. StringBuffer a = new StringBuffer(newQuest.getClass().getCanonicalName());
  118. a.delete(0, a.indexOf(".jscript.")+9);
  119. a.delete(a.indexOf(newQuest.getClass().getSimpleName()), a.length());
  120. a.append(newQuest.getName());
  121. // Note: FastMap will replace the old value if the key already exists
  122. // so there is no need to explicitly try to remove the old reference.
  123. getQuests().put(a.toString(), newQuest);
  124. }
  125. public final FastMap<String, Quest> getQuests()
  126. {
  127. if (_quests == null) _quests = new FastMap<String, Quest>();
  128. return (FastMap<String, Quest>) _quests;
  129. }
  130. }