QuestManager.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. Quest q = getQuest(questFolder);
  55. if (q!=null)
  56. q.saveGlobalData();
  57. return QuestJython.reloadQuest(q.getPrefixPath()+questFolder);
  58. }
  59. /**
  60. * Reloads a the quest given by questId.<BR>
  61. * <B>NOTICE: Will only work if the quest name is equal the quest folder name</B>
  62. * @param questId The id of the quest to be reloaded
  63. * @return true if reload was succesful, false otherwise
  64. */
  65. public final boolean reload(int questId)
  66. {
  67. Quest q = this.getQuest(questId);
  68. if (q == null)
  69. {
  70. return false;
  71. }
  72. q.saveGlobalData();
  73. return QuestJython.reloadQuest(q.getPrefixPath()+q.getName());
  74. }
  75. // =========================================================
  76. // Method - Private
  77. private final void load()
  78. {
  79. QuestJython.init();
  80. System.out.println("Loaded: " + getQuests().size() + " quests");
  81. }
  82. public final void save()
  83. {
  84. for(Quest q: getQuests().values())
  85. q.saveGlobalData();
  86. }
  87. // =========================================================
  88. // Property - Public
  89. public final Quest getQuest(String name)
  90. {
  91. return getQuests().get(name);
  92. }
  93. public final Quest getQuest(int questId)
  94. {
  95. for (Quest q: getQuests().values())
  96. {
  97. if (q.getQuestIntId() == questId)
  98. return q;
  99. }
  100. return null;
  101. }
  102. public final void addQuest(Quest newQuest)
  103. {
  104. if (getQuests().containsKey(newQuest.getName()))
  105. _log.info("Replaced: "+newQuest.getName()+" with a new version");
  106. // Note: FastMap will replace the old value if the key already exists
  107. // so there is no need to explicitly try to remove the old reference.
  108. getQuests().put(newQuest.getName(), newQuest);
  109. }
  110. public final FastMap<String, Quest> getQuests()
  111. {
  112. if (_quests == null) _quests = new FastMap<String, Quest>();
  113. return (FastMap<String, Quest>) _quests;
  114. }
  115. }