AdminShowQuests.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.L2DatabaseFactory;
  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. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  165. {
  166. ResultSet rs;
  167. PreparedStatement req;
  168. int ID = target.getObjectId();
  169. StringBuilder replyMSG = new StringBuilder("<html><body>");
  170. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  171. switch (val[0])
  172. {
  173. case "full":
  174. {
  175. replyMSG.append("<table width=250><tr><td>Full Quest List for <font color=\"LEVEL\">" + target.getName() + "</font> (ID:" + ID + ")</td></tr>");
  176. req = con.prepareStatement("SELECT DISTINCT name FROM character_quests WHERE charId='" + ID + "' AND var='<state>' ORDER by name");
  177. req.execute();
  178. rs = req.getResultSet();
  179. while (rs.next())
  180. {
  181. replyMSG.append("<tr><td><a action=\"bypass -h admin_charquestmenu " + target.getName() + " " + rs.getString(1) + "\">" + rs.getString(1) + "</a></td></tr>");
  182. }
  183. replyMSG.append("</table></body></html>");
  184. break;
  185. }
  186. case "name":
  187. {
  188. QuestState qs = target.getQuestState(val[1]);
  189. String state = (qs != null) ? _states[qs.getState()] : "CREATED";
  190. 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>");
  191. replyMSG.append("<center><table width=250><tr><td>Var</td><td>Value</td><td>New Value</td><td>&nbsp;</td></tr>");
  192. req = con.prepareStatement("SELECT var,value FROM character_quests WHERE charId='" + ID + "' and name='" + val[1] + "'");
  193. req.execute();
  194. rs = req.getResultSet();
  195. while (rs.next())
  196. {
  197. String var_name = rs.getString(1);
  198. if (var_name.equals("<state>"))
  199. {
  200. continue;
  201. }
  202. 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>");
  203. }
  204. replyMSG.append("</table><br><br><table width=250><tr><td>Repeatable quest:</td><td>Unrepeatable quest:</td></tr>");
  205. 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>");
  206. 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>");
  207. 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\">");
  208. replyMSG.append("</center></body></html>");
  209. break;
  210. }
  211. case "var":
  212. {
  213. replyMSG.append("Character: <font color=\"LEVEL\">" + target.getName() + "</font><br>Quests with state: <font color=\"LEVEL\">" + val[1] + "</font><br>");
  214. replyMSG.append("<table width=250>");
  215. req = con.prepareStatement("SELECT DISTINCT name FROM character_quests WHERE charId='" + ID + "' and var='<state>' and value='" + val[1] + "'");
  216. req.execute();
  217. rs = req.getResultSet();
  218. while (rs.next())
  219. {
  220. replyMSG.append("<tr><td><a action=\"bypass -h admin_charquestmenu " + target.getName() + " " + rs.getString(1) + "\">" + rs.getString(1) + "</a></td></tr>");
  221. }
  222. replyMSG.append("</table></body></html>");
  223. break;
  224. }
  225. case "custom":
  226. {
  227. boolean exqdb = true;
  228. boolean exqch = true;
  229. int qnumber = Integer.parseInt(val[1]);
  230. String state = null;
  231. String qname = null;
  232. QuestState qs = null;
  233. Quest quest = QuestManager.getInstance().getQuest(qnumber);
  234. if (quest != null)
  235. {
  236. qname = quest.getName();
  237. qs = target.getQuestState(qname);
  238. }
  239. else
  240. {
  241. exqdb = false;
  242. }
  243. if (qs != null)
  244. {
  245. state = _states[qs.getState()];
  246. }
  247. else
  248. {
  249. exqch = false;
  250. state = "N/A";
  251. }
  252. if (exqdb)
  253. {
  254. if (exqch)
  255. {
  256. 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>");
  257. replyMSG.append("<center><table width=250><tr><td>Var</td><td>Value</td><td>New Value</td><td>&nbsp;</td></tr>");
  258. req = con.prepareStatement("SELECT var,value FROM character_quests WHERE charId='" + ID + "' and name='" + qname + "'");
  259. req.execute();
  260. rs = req.getResultSet();
  261. while (rs.next())
  262. {
  263. String var_name = rs.getString(1);
  264. if (var_name.equals("<state>"))
  265. {
  266. continue;
  267. }
  268. 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>");
  269. }
  270. replyMSG.append("</table><br><br><table width=250><tr><td>Repeatable quest:</td><td>Unrepeatable quest:</td></tr>");
  271. 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>");
  272. 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>");
  273. 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\">");
  274. replyMSG.append("</center></body></html>");
  275. }
  276. else
  277. {
  278. 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>");
  279. replyMSG.append("<center>Start this Quest for player:<br>");
  280. 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>");
  281. replyMSG.append("<font color=\"ee0000\">Only for Unrepeateble quests:</font><br>");
  282. 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>");
  283. replyMSG.append("</center></body></html>");
  284. }
  285. }
  286. else
  287. {
  288. 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>");
  289. }
  290. break;
  291. }
  292. }
  293. adminReply.setHtml(replyMSG.toString());
  294. actor.sendPacket(adminReply);
  295. }
  296. catch (Exception e)
  297. {
  298. actor.sendMessage("There was an error.");
  299. _log.warning(AdminShowQuests.class.getSimpleName() + ": " + e.getMessage());
  300. }
  301. }
  302. private static void setQuestVar(L2PcInstance target, L2PcInstance actor, String[] val)
  303. {
  304. QuestState qs = target.getQuestState(val[0]);
  305. String[] outval = new String[3];
  306. if (val[1].equals("state"))
  307. {
  308. switch (val[2])
  309. {
  310. case "COMPLETED":
  311. {
  312. qs.exitQuest((val[3].equals("1")) ? true : false);
  313. break;
  314. }
  315. case "DELETE":
  316. {
  317. Quest.deleteQuestInDb(qs, true);
  318. qs.exitQuest(true);
  319. target.sendPacket(new QuestList());
  320. target.sendPacket(new ExShowQuestMark(qs.getQuest().getId()));
  321. break;
  322. }
  323. case "CREATE":
  324. {
  325. qs = QuestManager.getInstance().getQuest(Integer.parseInt(val[0])).newQuestState(target);
  326. qs.setState(State.STARTED);
  327. qs.set("cond", "1");
  328. target.sendPacket(new QuestList());
  329. target.sendPacket(new ExShowQuestMark(qs.getQuest().getId()));
  330. val[0] = qs.getQuest().getName();
  331. break;
  332. }
  333. case "CC":
  334. {
  335. qs = QuestManager.getInstance().getQuest(Integer.parseInt(val[0])).newQuestState(target);
  336. qs.exitQuest(false);
  337. target.sendPacket(new QuestList());
  338. target.sendPacket(new ExShowQuestMark(qs.getQuest().getId()));
  339. val[0] = qs.getQuest().getName();
  340. break;
  341. }
  342. }
  343. }
  344. else
  345. {
  346. if (val[2].equals("delete"))
  347. {
  348. qs.unset(val[1]);
  349. }
  350. else
  351. {
  352. qs.set(val[1], val[2]);
  353. }
  354. target.sendPacket(new QuestList());
  355. target.sendPacket(new ExShowQuestMark(qs.getQuest().getId()));
  356. }
  357. actor.sendMessage("");
  358. outval[0] = "name";
  359. outval[1] = val[0];
  360. showQuestMenu(target, actor, outval);
  361. }
  362. @Override
  363. public String[] getAdminCommandList()
  364. {
  365. return ADMIN_COMMANDS;
  366. }
  367. }