AdminQuest.java 11 KB

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