AdminShowQuests.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  25. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  26. import com.l2jserver.gameserver.instancemanager.QuestManager;
  27. import com.l2jserver.gameserver.model.L2Object;
  28. import com.l2jserver.gameserver.model.L2World;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.quest.Quest;
  31. import com.l2jserver.gameserver.model.quest.QuestState;
  32. import com.l2jserver.gameserver.model.quest.State;
  33. import com.l2jserver.gameserver.network.SystemMessageId;
  34. import com.l2jserver.gameserver.network.serverpackets.ExShowQuestMark;
  35. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  36. import com.l2jserver.gameserver.network.serverpackets.QuestList;
  37. /**
  38. * TODO: Rework and cleanup.
  39. * @author Korvin, Zoey76
  40. */
  41. public class AdminShowQuests implements IAdminCommandHandler
  42. {
  43. private static final Logger _log = Logger.getLogger(AdminShowQuests.class.getName());
  44. private static final String[] ADMIN_COMMANDS =
  45. {
  46. "admin_charquestmenu",
  47. "admin_setcharquest"
  48. };
  49. private static final String[] _states =
  50. {
  51. "CREATED",
  52. "STARTED",
  53. "COMPLETED"
  54. };
  55. @Override
  56. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  57. {
  58. String[] cmdParams = command.split(" ");
  59. L2PcInstance target = null;
  60. L2Object targetObject = null;
  61. String[] val = new String[4];
  62. val[0] = null;
  63. if (cmdParams.length > 1)
  64. {
  65. target = L2World.getInstance().getPlayer(cmdParams[1]);
  66. if (cmdParams.length > 2)
  67. {
  68. if (cmdParams[2].equals("0"))
  69. {
  70. val[0] = "var";
  71. val[1] = "Start";
  72. }
  73. if (cmdParams[2].equals("1"))
  74. {
  75. val[0] = "var";
  76. val[1] = "Started";
  77. }
  78. if (cmdParams[2].equals("2"))
  79. {
  80. val[0] = "var";
  81. val[1] = "Completed";
  82. }
  83. if (cmdParams[2].equals("3"))
  84. {
  85. val[0] = "full";
  86. }
  87. if (cmdParams[2].indexOf("_") != -1)
  88. {
  89. val[0] = "name";
  90. val[1] = cmdParams[2];
  91. }
  92. if (cmdParams.length > 3)
  93. {
  94. if (cmdParams[3].equals("custom"))
  95. {
  96. val[0] = "custom";
  97. val[1] = cmdParams[2];
  98. }
  99. }
  100. }
  101. }
  102. else
  103. {
  104. targetObject = activeChar.getTarget();
  105. if ((targetObject != null) && targetObject.isPlayer())
  106. {
  107. target = targetObject.getActingPlayer();
  108. }
  109. }
  110. if (target == null)
  111. {
  112. activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
  113. return false;
  114. }
  115. if (command.startsWith("admin_charquestmenu"))
  116. {
  117. if (val[0] != null)
  118. {
  119. showQuestMenu(target, activeChar, val);
  120. }
  121. else
  122. {
  123. showFirstQuestMenu(target, activeChar);
  124. }
  125. }
  126. else if (command.startsWith("admin_setcharquest"))
  127. {
  128. if (cmdParams.length >= 5)
  129. {
  130. val[0] = cmdParams[2];
  131. val[1] = cmdParams[3];
  132. val[2] = cmdParams[4];
  133. if (cmdParams.length == 6)
  134. {
  135. val[3] = cmdParams[5];
  136. }
  137. setQuestVar(target, activeChar, val);
  138. }
  139. else
  140. {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. private static void showFirstQuestMenu(L2PcInstance target, L2PcInstance actor)
  147. {
  148. StringBuilder replyMSG = new StringBuilder("<html><body><table width=270><tr><td width=45><button value=\"Main\" action=\"bypass -h admin_admin\" width=45 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td width=180><center>Player: " + target.getName() + "</center></td><td width=45><button value=\"Back\" action=\"bypass -h admin_admin6\" width=45 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table>");
  149. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  150. int ID = target.getObjectId();
  151. replyMSG.append("Quest Menu for <font color=\"LEVEL\">" + target.getName() + "</font> (ID:" + ID + ")<br><center>");
  152. replyMSG.append("<table width=250><tr><td><button value=\"CREATED\" action=\"bypass -h admin_charquestmenu " + target.getName() + " 0\" width=85 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  153. replyMSG.append("<tr><td><button value=\"STARTED\" action=\"bypass -h admin_charquestmenu " + target.getName() + " 1\" width=85 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  154. replyMSG.append("<tr><td><button value=\"COMPLETED\" action=\"bypass -h admin_charquestmenu " + target.getName() + " 2\" width=85 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  155. replyMSG.append("<tr><td><br><button value=\"All\" action=\"bypass -h admin_charquestmenu " + target.getName() + " 3\" width=85 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  156. replyMSG.append("<tr><td><br><br>Manual Edit by Quest number:<br></td></tr>");
  157. replyMSG.append("<tr><td><edit var=\"qn\" width=50 height=15><br><button value=\"Edit\" action=\"bypass -h admin_charquestmenu " + target.getName() + " $qn custom\" width=50 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  158. replyMSG.append("</table></center></body></html>");
  159. adminReply.setHtml(replyMSG.toString());
  160. actor.sendPacket(adminReply);
  161. }
  162. private static void showQuestMenu(L2PcInstance target, L2PcInstance actor, String[] val)
  163. {
  164. // TODO(Zoey76): Refactor this into smaller methods and separate database access logic from HTML creation.
  165. try (Connection con = ConnectionFactory.getInstance().getConnection())
  166. {
  167. ResultSet rs;
  168. PreparedStatement req;
  169. int ID = target.getObjectId();
  170. StringBuilder replyMSG = new StringBuilder("<html><body>");
  171. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  172. switch (val[0])
  173. {
  174. case "full":
  175. {
  176. replyMSG.append("<table width=250><tr><td>Full Quest List for <font color=\"LEVEL\">" + target.getName() + "</font> (ID:" + ID + ")</td></tr>");
  177. req = con.prepareStatement("SELECT DISTINCT name FROM character_quests WHERE charId='" + ID + "' AND var='<state>' ORDER by name");
  178. req.execute();
  179. rs = req.getResultSet();
  180. while (rs.next())
  181. {
  182. replyMSG.append("<tr><td><a action=\"bypass -h admin_charquestmenu " + target.getName() + " " + rs.getString(1) + "\">" + rs.getString(1) + "</a></td></tr>");
  183. }
  184. replyMSG.append("</table></body></html>");
  185. break;
  186. }
  187. case "name":
  188. {
  189. QuestState qs = target.getQuestState(val[1]);
  190. String state = (qs != null) ? _states[qs.getState()] : "CREATED";
  191. replyMSG.append("Character: <font color=\"LEVEL\">" + target.getName() + "</font><br>Quest: <font color=\"LEVEL\">" + val[1] + "</font><br>State: <font color=\"LEVEL\">" + state + "</font><br><br>");
  192. replyMSG.append("<center><table width=250><tr><td>Var</td><td>Value</td><td>New Value</td><td>&nbsp;</td></tr>");
  193. req = con.prepareStatement("SELECT var,value FROM character_quests WHERE charId='" + ID + "' and name='" + val[1] + "'");
  194. req.execute();
  195. rs = req.getResultSet();
  196. while (rs.next())
  197. {
  198. String var_name = rs.getString(1);
  199. if (var_name.equals("<state>"))
  200. {
  201. continue;
  202. }
  203. replyMSG.append("<tr><td>" + var_name + "</td><td>" + rs.getString(2) + "</td><td><edit var=\"var" + var_name + "\" width=80 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + val[1] + " " + var_name + " $var" + var_name + "\" width=30 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td><button value=\"Del\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + val[1] + " " + var_name + " delete\" width=30 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  204. }
  205. replyMSG.append("</table><br><br><table width=250><tr><td>Repeatable quest:</td><td>Unrepeatable quest:</td></tr>");
  206. replyMSG.append("<tr><td><button value=\"Quest Complete\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + val[1] + " state COMPLETED 1\" width=120 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  207. replyMSG.append("<td><button value=\"Quest Complete\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + val[1] + " state COMPLETED 0\" width=120 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  208. replyMSG.append("</table><br><br><font color=\"ff0000\">Delete Quest from DB:</font><br><button value=\"Quest Delete\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + val[1] + " state DELETE\" width=120 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">");
  209. replyMSG.append("</center></body></html>");
  210. break;
  211. }
  212. case "var":
  213. {
  214. replyMSG.append("Character: <font color=\"LEVEL\">" + target.getName() + "</font><br>Quests with state: <font color=\"LEVEL\">" + val[1] + "</font><br>");
  215. replyMSG.append("<table width=250>");
  216. req = con.prepareStatement("SELECT DISTINCT name FROM character_quests WHERE charId='" + ID + "' and var='<state>' and value='" + val[1] + "'");
  217. req.execute();
  218. rs = req.getResultSet();
  219. while (rs.next())
  220. {
  221. replyMSG.append("<tr><td><a action=\"bypass -h admin_charquestmenu " + target.getName() + " " + rs.getString(1) + "\">" + rs.getString(1) + "</a></td></tr>");
  222. }
  223. replyMSG.append("</table></body></html>");
  224. break;
  225. }
  226. case "custom":
  227. {
  228. boolean exqdb = true;
  229. boolean exqch = true;
  230. int qnumber = Integer.parseInt(val[1]);
  231. String state = null;
  232. String qname = null;
  233. QuestState qs = null;
  234. Quest quest = QuestManager.getInstance().getQuest(qnumber);
  235. if (quest != null)
  236. {
  237. qname = quest.getName();
  238. qs = target.getQuestState(qname);
  239. }
  240. else
  241. {
  242. exqdb = false;
  243. }
  244. if (qs != null)
  245. {
  246. state = _states[qs.getState()];
  247. }
  248. else
  249. {
  250. exqch = false;
  251. state = "N/A";
  252. }
  253. if (exqdb)
  254. {
  255. if (exqch)
  256. {
  257. replyMSG.append("Character: <font color=\"LEVEL\">" + target.getName() + "</font><br>Quest: <font color=\"LEVEL\">" + qname + "</font><br>State: <font color=\"LEVEL\">" + state + "</font><br><br>");
  258. replyMSG.append("<center><table width=250><tr><td>Var</td><td>Value</td><td>New Value</td><td>&nbsp;</td></tr>");
  259. req = con.prepareStatement("SELECT var,value FROM character_quests WHERE charId='" + ID + "' and name='" + qname + "'");
  260. req.execute();
  261. rs = req.getResultSet();
  262. while (rs.next())
  263. {
  264. String var_name = rs.getString(1);
  265. if (var_name.equals("<state>"))
  266. {
  267. continue;
  268. }
  269. replyMSG.append("<tr><td>" + var_name + "</td><td>" + rs.getString(2) + "</td><td><edit var=\"var" + var_name + "\" width=80 height=15></td><td><button value=\"Set\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + qname + " " + var_name + " $var" + var_name + "\" width=30 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td><button value=\"Del\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + qname + " " + var_name + " delete\" width=30 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  270. }
  271. replyMSG.append("</table><br><br><table width=250><tr><td>Repeatable quest:</td><td>Unrepeatable quest:</td></tr>");
  272. replyMSG.append("<tr><td><button value=\"Quest Complete\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + qname + " state COMPLETED 1\" width=100 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  273. replyMSG.append("<td><button value=\"Quest Complete\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + qname + " state COMPLETED 0\" width=100 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>");
  274. replyMSG.append("</table><br><br><font color=\"ff0000\">Delete Quest from DB:</font><br><button value=\"Quest Delete\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + qname + " state DELETE\" width=100 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">");
  275. replyMSG.append("</center></body></html>");
  276. }
  277. else
  278. {
  279. replyMSG.append("Character: <font color=\"LEVEL\">" + target.getName() + "</font><br>Quest: <font color=\"LEVEL\">" + qname + "</font><br>State: <font color=\"LEVEL\">" + state + "</font><br><br>");
  280. replyMSG.append("<center>Start this Quest for player:<br>");
  281. replyMSG.append("<button value=\"Create Quest\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + qnumber + " state CREATE\" width=100 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"><br><br>");
  282. replyMSG.append("<font color=\"ee0000\">Only for Unrepeateble quests:</font><br>");
  283. replyMSG.append("<button value=\"Create & Complete\" action=\"bypass -h admin_setcharquest " + target.getName() + " " + qnumber + " state CC\" width=130 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"><br><br>");
  284. replyMSG.append("</center></body></html>");
  285. }
  286. }
  287. else
  288. {
  289. replyMSG.append("<center><font color=\"ee0000\">Quest with number </font><font color=\"LEVEL\">" + qnumber + "</font><font color=\"ee0000\"> doesn't exist!</font></center></body></html>");
  290. }
  291. break;
  292. }
  293. }
  294. adminReply.setHtml(replyMSG.toString());
  295. actor.sendPacket(adminReply);
  296. }
  297. catch (Exception e)
  298. {
  299. actor.sendMessage("There was an error.");
  300. _log.warning(AdminShowQuests.class.getSimpleName() + ": " + e.getMessage());
  301. }
  302. }
  303. private static void setQuestVar(L2PcInstance target, L2PcInstance actor, String[] val)
  304. {
  305. QuestState qs = target.getQuestState(val[0]);
  306. String[] outval = new String[3];
  307. if (val[1].equals("state"))
  308. {
  309. switch (val[2])
  310. {
  311. case "COMPLETED":
  312. {
  313. qs.exitQuest((val[3].equals("1")) ? true : false);
  314. break;
  315. }
  316. case "DELETE":
  317. {
  318. Quest.deleteQuestInDb(qs, true);
  319. qs.exitQuest(true);
  320. target.sendPacket(new QuestList());
  321. target.sendPacket(new ExShowQuestMark(qs.getQuest().getId()));
  322. break;
  323. }
  324. case "CREATE":
  325. {
  326. qs = QuestManager.getInstance().getQuest(Integer.parseInt(val[0])).newQuestState(target);
  327. qs.setState(State.STARTED);
  328. qs.set("cond", "1");
  329. target.sendPacket(new QuestList());
  330. target.sendPacket(new ExShowQuestMark(qs.getQuest().getId()));
  331. val[0] = qs.getQuest().getName();
  332. break;
  333. }
  334. case "CC":
  335. {
  336. qs = QuestManager.getInstance().getQuest(Integer.parseInt(val[0])).newQuestState(target);
  337. qs.exitQuest(false);
  338. target.sendPacket(new QuestList());
  339. target.sendPacket(new ExShowQuestMark(qs.getQuest().getId()));
  340. val[0] = qs.getQuest().getName();
  341. break;
  342. }
  343. }
  344. }
  345. else
  346. {
  347. if (val[2].equals("delete"))
  348. {
  349. qs.unset(val[1]);
  350. }
  351. else
  352. {
  353. qs.set(val[1], val[2]);
  354. }
  355. target.sendPacket(new QuestList());
  356. target.sendPacket(new ExShowQuestMark(qs.getQuest().getId()));
  357. }
  358. actor.sendMessage("");
  359. outval[0] = "name";
  360. outval[1] = val[0];
  361. showQuestMenu(target, actor, outval);
  362. }
  363. @Override
  364. public String[] getAdminCommandList()
  365. {
  366. return ADMIN_COMMANDS;
  367. }
  368. }