AdminQuest.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Map.Entry;
  20. import java.util.Set;
  21. import javax.script.ScriptException;
  22. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  23. import com.l2jserver.gameserver.instancemanager.QuestManager;
  24. import com.l2jserver.gameserver.model.actor.L2Npc;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.quest.Quest;
  27. import com.l2jserver.gameserver.model.quest.Quest.QuestEventType;
  28. import com.l2jserver.gameserver.model.quest.QuestTimer;
  29. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  30. import com.l2jserver.gameserver.scripting.L2ScriptEngineManager;
  31. import com.l2jserver.gameserver.util.Util;
  32. public class AdminQuest implements IAdminCommandHandler
  33. {
  34. private static final String[] ADMIN_COMMANDS =
  35. {
  36. "admin_quest_reload",
  37. "admin_script_load",
  38. "admin_script_unload",
  39. "admin_show_quests",
  40. "admin_quest_info"
  41. };
  42. @Override
  43. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  44. {
  45. if (activeChar == null)
  46. {
  47. return false;
  48. }
  49. // syntax will either be:
  50. // //quest_reload <id>
  51. // //quest_reload <questName>
  52. // The questName MUST start with a non-numeric character for this to work,
  53. // regardless which of the two formats is used.
  54. // Example: //quest_reload orc_occupation_change_1
  55. // Example: //quest_reload chests
  56. // Example: //quest_reload SagasSuperclass
  57. // Example: //quest_reload 12
  58. if (command.startsWith("admin_quest_reload"))
  59. {
  60. String[] parts = command.split(" ");
  61. if (parts.length < 2)
  62. {
  63. activeChar.sendMessage("Usage: //quest_reload <questFolder>.<questSubFolders...>.questName> or //quest_reload <id>");
  64. }
  65. else
  66. {
  67. // try the first param as id
  68. try
  69. {
  70. int questId = Integer.parseInt(parts[1]);
  71. if (QuestManager.getInstance().reload(questId))
  72. {
  73. activeChar.sendMessage("Quest Reloaded Successfully.");
  74. }
  75. else
  76. {
  77. activeChar.sendMessage("Quest Reloaded Failed");
  78. }
  79. }
  80. catch (NumberFormatException e)
  81. {
  82. if (QuestManager.getInstance().reload(parts[1]))
  83. {
  84. activeChar.sendMessage("Quest Reloaded Successfully.");
  85. }
  86. else
  87. {
  88. activeChar.sendMessage("Quest Reloaded Failed");
  89. }
  90. }
  91. }
  92. }
  93. // script load should NOT be used in place of reload. If a script is already loaded
  94. // successfully, quest_reload ought to be used. The script_load command should only
  95. // be used for scripts that failed to load altogether (eg. due to errors) or that
  96. // did not at all exist during server boot. Using script_load to re-load a previously
  97. // loaded script may cause unpredictable script flow, minor loss of data, and more.
  98. // This provides a way to load new scripts without having to reboot the server.
  99. else if (command.startsWith("admin_script_load"))
  100. {
  101. String[] parts = command.split(" ");
  102. if (parts.length < 2)
  103. {
  104. // activeChar.sendMessage("Example: //script_load <questFolder>/<questSubFolders...>/<filename>.<ext> ");
  105. activeChar.sendMessage("Example: //script_load quests/SagasSuperclass/__init__.py");
  106. }
  107. else
  108. {
  109. File file = new File(L2ScriptEngineManager.SCRIPT_FOLDER, parts[1]);
  110. // Trying to reload by script name.
  111. if (!file.exists())
  112. {
  113. Quest quest = QuestManager.getInstance().getQuest(parts[1]);
  114. if (quest != null)
  115. {
  116. file = new File(L2ScriptEngineManager.SCRIPT_FOLDER, quest.getClass().getName().replaceAll("\\.", "/") + ".java");
  117. }
  118. }
  119. // Reloading by full path
  120. if (file.isFile())
  121. {
  122. try
  123. {
  124. L2ScriptEngineManager.getInstance().executeScript(file);
  125. // This part should be called only when the script is successfuly loaded.
  126. activeChar.sendMessage("Script Successfully Loaded.");
  127. }
  128. catch (ScriptException e)
  129. {
  130. activeChar.sendMessage("Failed loading: " + parts[1]);
  131. L2ScriptEngineManager.getInstance().reportScriptFileError(file, e);
  132. }
  133. catch (Exception e)
  134. {
  135. activeChar.sendMessage("Failed loading: " + parts[1]);
  136. }
  137. }
  138. else
  139. {
  140. activeChar.sendMessage("File Not Found: " + parts[1]);
  141. }
  142. }
  143. }
  144. else if (command.startsWith("admin_script_unload"))
  145. {
  146. String[] parts = command.split(" ");
  147. if (parts.length < 2)
  148. {
  149. activeChar.sendMessage("Example: //script_unload questName/questId");
  150. }
  151. else
  152. {
  153. Quest q = Util.isDigit(parts[1]) ? QuestManager.getInstance().getQuest(Integer.parseInt(parts[1])) : QuestManager.getInstance().getQuest(parts[1]);
  154. if (q != null)
  155. {
  156. if (q.unload())
  157. {
  158. activeChar.sendMessage("Script Successfully Unloaded [" + q.getName() + "/" + q.getQuestIntId() + "]");
  159. }
  160. else
  161. {
  162. activeChar.sendMessage("Failed unloading [" + q.getName() + "/" + q.getQuestIntId() + "].");
  163. }
  164. }
  165. else
  166. {
  167. activeChar.sendMessage("The quest [" + parts[1] + "] was not found!.");
  168. }
  169. }
  170. }
  171. else if (command.startsWith("admin_show_quests"))
  172. {
  173. if (activeChar.getTarget() == null)
  174. {
  175. activeChar.sendMessage("Get a target first.");
  176. }
  177. else if (!activeChar.getTarget().isNpc())
  178. {
  179. activeChar.sendMessage("Invalid Target.");
  180. }
  181. else
  182. {
  183. L2Npc npc = L2Npc.class.cast(activeChar.getTarget());
  184. NpcHtmlMessage msg = new NpcHtmlMessage(npc.getObjectId(), 1);
  185. msg.setFile(activeChar.getHtmlPrefix(), "data/html/admin/npc-quests.htm");
  186. StringBuilder sb = new StringBuilder();
  187. Set<String> questset = new HashSet<>();
  188. for (Entry<QuestEventType, List<Quest>> quests : npc.getTemplate().getEventQuests().entrySet())
  189. {
  190. for (Quest quest : quests.getValue())
  191. {
  192. if (questset.contains(quest.getName()))
  193. {
  194. continue;
  195. }
  196. questset.add(quest.getName());
  197. sb.append("<tr><td colspan=\"4\"><font color=\"LEVEL\"><a action=\"bypass -h admin_quest_info " + quest.getName() + "\">" + quest.getName() + "</a></font></td></tr>");
  198. }
  199. }
  200. msg.replace("%quests%", sb.toString());
  201. msg.replace("%tmplid%", Integer.toString(npc.getTemplate().getNpcId()));
  202. msg.replace("%questName%", "");
  203. activeChar.sendPacket(msg);
  204. questset.clear();
  205. }
  206. }
  207. else if (command.startsWith("admin_quest_info "))
  208. {
  209. if (activeChar.getTarget() == null)
  210. {
  211. activeChar.sendMessage("Get a target first.");
  212. }
  213. else if (!activeChar.getTarget().isNpc())
  214. {
  215. activeChar.sendMessage("Invalid Target.");
  216. }
  217. else
  218. {
  219. String questName = command.substring("admin_quest_info ".length());
  220. Quest quest = QuestManager.getInstance().getQuest(questName);
  221. if (quest == null)
  222. {
  223. return false;
  224. }
  225. L2Npc npc = L2Npc.class.cast(activeChar.getTarget());
  226. StringBuilder sb = new StringBuilder();
  227. NpcHtmlMessage msg = new NpcHtmlMessage(npc.getObjectId(), 1);
  228. msg.setFile(activeChar.getHtmlPrefix(), "data/html/admin/npc-quests.htm");
  229. String events = "", npcs = "", items = "", timers = "";
  230. for (QuestEventType type : npc.getTemplate().getEventQuests().keySet())
  231. {
  232. events += ", " + type.toString();
  233. }
  234. events = events.substring(2);
  235. if (quest.getQuestInvolvedNpcs().size() < 100)
  236. {
  237. for (int npcId : quest.getQuestInvolvedNpcs())
  238. {
  239. npcs += ", " + npcId;
  240. }
  241. npcs = npcs.substring(2);
  242. }
  243. if (quest.getRegisteredItemIds() != null)
  244. {
  245. for (int itemId : quest.getRegisteredItemIds())
  246. {
  247. items += ", " + itemId;
  248. }
  249. items = items.substring(2);
  250. }
  251. for (List<QuestTimer> list : quest.getQuestTimers().values())
  252. {
  253. for (QuestTimer timer : list)
  254. {
  255. timers += "<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">" + timer.getName() + ":</font> <font color=00FF00>Active: " + timer.getIsActive() + " Repeatable: " + timer.getIsRepeating() + " Player: " + timer.getPlayer() + " Npc: " + timer.getNpc() + "</font></td></tr></table></td></tr>";
  256. }
  257. }
  258. sb.append("<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">ID:</font> <font color=00FF00>" + quest.getQuestIntId() + "</font></td></tr></table></td></tr>");
  259. sb.append("<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">Name:</font> <font color=00FF00>" + quest.getName() + "</font></td></tr></table></td></tr>");
  260. sb.append("<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">Descr:</font> <font color=00FF00>" + quest.getDescr() + "</font></td></tr></table></td></tr>");
  261. sb.append("<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">Path:</font> <font color=00FF00>" + quest.getClass().getName().substring(0, quest.getClass().getName().lastIndexOf('.')).replaceAll("\\.", "/") + "</font></td></tr></table></td></tr>");
  262. sb.append("<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">Events:</font> <font color=00FF00>" + events + "</font></td></tr></table></td></tr>");
  263. if (!npcs.isEmpty())
  264. {
  265. sb.append("<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">NPCs:</font> <font color=00FF00>" + npcs + "</font></td></tr></table></td></tr>");
  266. }
  267. if (!items.isEmpty())
  268. {
  269. sb.append("<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">Items:</font> <font color=00FF00>" + items + "</font></td></tr></table></td></tr>");
  270. }
  271. if (!timers.isEmpty())
  272. {
  273. sb.append("<tr><td colspan=\"4\"><table width=270 border=0 bgcolor=131210><tr><td width=270><font color=\"LEVEL\">Timers:</font> <font color=00FF00></font></td></tr></table></td></tr>");
  274. sb.append(timers);
  275. }
  276. msg.replace("%quests%", sb.toString());
  277. msg.replace("%tmplid%", Integer.toString(npc.getNpcId()));
  278. msg.replace("%questName%", "<table><tr><td width=\"50\" align=\"left\"><a action=\"bypass -h admin_script_load " + quest.getName() + "\">Reload</a></td> <td width=\"150\" align=\"center\"><a action=\"bypass -h admin_quest_info " + quest.getName() + "\">" + quest.getName() + "</a></td> <td width=\"50\" align=\"right\"><a action=\"bypass -h admin_script_unload " + quest.getName() + "\">Unload</a></tr></td></table>");
  279. activeChar.sendPacket(msg);
  280. }
  281. }
  282. return true;
  283. }
  284. @Override
  285. public String[] getAdminCommandList()
  286. {
  287. return ADMIN_COMMANDS;
  288. }
  289. }