AdminQuest.java 12 KB

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