AdminQuest.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 handlers.admincommandhandlers;
  16. import java.io.File;
  17. import javax.script.ScriptException;
  18. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  19. import com.l2jserver.gameserver.instancemanager.QuestManager;
  20. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  21. import com.l2jserver.gameserver.model.quest.Quest;
  22. import com.l2jserver.gameserver.scripting.L2ScriptEngineManager;
  23. import com.l2jserver.gameserver.util.Util;
  24. public class AdminQuest implements IAdminCommandHandler
  25. {
  26. private static final String[] ADMIN_COMMANDS =
  27. {
  28. "admin_quest_reload",
  29. "admin_script_load",
  30. "admin_script_unload",
  31. };
  32. @Override
  33. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  34. {
  35. if (activeChar == null)
  36. {
  37. return false;
  38. }
  39. // syntax will either be:
  40. // //quest_reload <id>
  41. // //quest_reload <questName>
  42. // The questName MUST start with a non-numeric character for this to work,
  43. // regardless which of the two formats is used.
  44. // Example: //quest_reload orc_occupation_change_1
  45. // Example: //quest_reload chests
  46. // Example: //quest_reload SagasSuperclass
  47. // Example: //quest_reload 12
  48. if (command.startsWith("admin_quest_reload"))
  49. {
  50. String[] parts = command.split(" ");
  51. if (parts.length < 2)
  52. {
  53. activeChar.sendMessage("Usage: //quest_reload <questFolder>.<questSubFolders...>.questName> or //quest_reload <id>");
  54. }
  55. else
  56. {
  57. // try the first param as id
  58. try
  59. {
  60. int questId = Integer.parseInt(parts[1]);
  61. if (QuestManager.getInstance().reload(questId))
  62. {
  63. activeChar.sendMessage("Quest Reloaded Successfully.");
  64. }
  65. else
  66. {
  67. activeChar.sendMessage("Quest Reloaded Failed");
  68. }
  69. }
  70. catch (NumberFormatException e)
  71. {
  72. if (QuestManager.getInstance().reload(parts[1]))
  73. {
  74. activeChar.sendMessage("Quest Reloaded Successfully.");
  75. }
  76. else
  77. {
  78. activeChar.sendMessage("Quest Reloaded Failed");
  79. }
  80. }
  81. }
  82. }
  83. // script load should NOT be used in place of reload. If a script is already loaded
  84. // successfully, quest_reload ought to be used. The script_load command should only
  85. // be used for scripts that failed to load altogether (eg. due to errors) or that
  86. // did not at all exist during server boot. Using script_load to re-load a previously
  87. // loaded script may cause unpredictable script flow, minor loss of data, and more.
  88. // This provides a way to load new scripts without having to reboot the server.
  89. else if (command.startsWith("admin_script_load"))
  90. {
  91. String[] parts = command.split(" ");
  92. if (parts.length < 2)
  93. {
  94. // activeChar.sendMessage("Example: //script_load <questFolder>/<questSubFolders...>/<filename>.<ext> ");
  95. activeChar.sendMessage("Example: //script_load quests/SagasSuperclass/__init__.py");
  96. }
  97. else
  98. {
  99. File file = new File(L2ScriptEngineManager.SCRIPT_FOLDER, parts[1]);
  100. // Trying to reload by script name.
  101. if (!file.exists())
  102. {
  103. Quest quest = QuestManager.getInstance().getQuest(parts[1]);
  104. if (quest != null)
  105. {
  106. file = new File(L2ScriptEngineManager.SCRIPT_FOLDER, quest.getClass().getName().replaceAll("\\.", "/") + ".java");
  107. }
  108. }
  109. // Reloading by full path
  110. if (file.isFile())
  111. {
  112. try
  113. {
  114. L2ScriptEngineManager.getInstance().executeScript(file);
  115. // This part should be called only when the script is successfuly loaded.
  116. activeChar.sendMessage("Script Successfully Loaded.");
  117. }
  118. catch (ScriptException e)
  119. {
  120. activeChar.sendMessage("Failed loading: " + parts[1]);
  121. L2ScriptEngineManager.getInstance().reportScriptFileError(file, e);
  122. }
  123. catch (Exception e)
  124. {
  125. activeChar.sendMessage("Failed loading: " + parts[1]);
  126. }
  127. }
  128. else
  129. {
  130. activeChar.sendMessage("File Not Found: " + parts[1]);
  131. }
  132. }
  133. }
  134. else if (command.startsWith("admin_script_unload"))
  135. {
  136. String[] parts = command.split(" ");
  137. if (parts.length < 2)
  138. {
  139. activeChar.sendMessage("Example: //script_unload questName/questId");
  140. }
  141. else
  142. {
  143. Quest q = Util.isDigit(parts[1]) ? QuestManager.getInstance().getQuest(Integer.parseInt(parts[1])) : QuestManager.getInstance().getQuest(parts[1]);
  144. if (q != null)
  145. {
  146. if (q.unload())
  147. {
  148. activeChar.sendMessage("Script Successfully Unloaded [" + q.getName() + "/" + q.getQuestIntId() + "]");
  149. }
  150. else
  151. {
  152. activeChar.sendMessage("Failed unloading [" + q.getName() + "/" + q.getQuestIntId() + "].");
  153. }
  154. }
  155. else
  156. {
  157. activeChar.sendMessage("The quest [" + parts[1] + "] was not found!.");
  158. }
  159. }
  160. }
  161. return true;
  162. }
  163. @Override
  164. public String[] getAdminCommandList()
  165. {
  166. return ADMIN_COMMANDS;
  167. }
  168. }