AdminSkill.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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.util.StringTokenizer;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.datatables.SkillTable;
  21. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  22. import com.l2jserver.gameserver.model.L2Clan;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.L2Skill;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  28. import com.l2jserver.gameserver.network.serverpackets.PledgeSkillList;
  29. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  30. import com.l2jserver.util.StringUtil;
  31. /**
  32. * This class handles following admin commands:
  33. * - show_skills
  34. * - remove_skills
  35. * - skill_list
  36. * - skill_index
  37. * - add_skill
  38. * - remove_skill
  39. * - get_skills
  40. * - reset_skills
  41. * - give_all_skills
  42. * - give_all_skills_fs
  43. * - remove_all_skills
  44. * - add_clan_skills
  45. * - admin_setskill
  46. *
  47. * @version $Revision: 1.2.4.7 $ $Date: 2005/04/11 10:06:02 $
  48. * Small fixes by Zoey76 05/03/2011
  49. */
  50. public class AdminSkill implements IAdminCommandHandler
  51. {
  52. private static Logger _log = Logger.getLogger(AdminSkill.class.getName());
  53. private static final String[] ADMIN_COMMANDS =
  54. {
  55. "admin_show_skills",
  56. "admin_remove_skills",
  57. "admin_skill_list",
  58. "admin_skill_index",
  59. "admin_add_skill",
  60. "admin_remove_skill",
  61. "admin_get_skills",
  62. "admin_reset_skills",
  63. "admin_give_all_skills",
  64. "admin_give_all_skills_fs",
  65. "admin_remove_all_skills",
  66. "admin_add_clan_skill",
  67. "admin_setskill"
  68. };
  69. private static L2Skill[] adminSkills;
  70. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  71. {
  72. if (command.equals("admin_show_skills"))
  73. showMainPage(activeChar);
  74. else if (command.startsWith("admin_remove_skills"))
  75. {
  76. try
  77. {
  78. String val = command.substring(20);
  79. removeSkillsPage(activeChar, Integer.parseInt(val));
  80. }
  81. catch (StringIndexOutOfBoundsException e)
  82. {
  83. }
  84. }
  85. else if (command.startsWith("admin_skill_list"))
  86. {
  87. AdminHelpPage.showHelpPage(activeChar, "skills.htm");
  88. }
  89. else if (command.startsWith("admin_skill_index"))
  90. {
  91. try
  92. {
  93. String val = command.substring(18);
  94. AdminHelpPage.showHelpPage(activeChar, "skills/" + val + ".htm");
  95. }
  96. catch (StringIndexOutOfBoundsException e)
  97. {
  98. }
  99. }
  100. else if (command.startsWith("admin_add_skill"))
  101. {
  102. try
  103. {
  104. String val = command.substring(15);
  105. adminAddSkill(activeChar, val);
  106. }
  107. catch (Exception e)
  108. {
  109. activeChar.sendMessage("Usage: //add_skill <skill_id> <level>");
  110. }
  111. }
  112. else if (command.startsWith("admin_remove_skill"))
  113. {
  114. try
  115. {
  116. String id = command.substring(19);
  117. int idval = Integer.parseInt(id);
  118. adminRemoveSkill(activeChar, idval);
  119. }
  120. catch (Exception e)
  121. {
  122. activeChar.sendMessage("Usage: //remove_skill <skill_id>");
  123. }
  124. }
  125. else if (command.equals("admin_get_skills"))
  126. {
  127. adminGetSkills(activeChar);
  128. }
  129. else if (command.equals("admin_reset_skills"))
  130. {
  131. adminResetSkills(activeChar);
  132. }
  133. else if (command.equals("admin_give_all_skills"))
  134. {
  135. adminGiveAllSkills(activeChar, false);
  136. }
  137. else if (command.equals("admin_give_all_skills_fs"))
  138. {
  139. adminGiveAllSkills(activeChar, true);
  140. }
  141. else if (command.equals("admin_remove_all_skills"))
  142. {
  143. if (activeChar.getTarget() instanceof L2PcInstance)
  144. {
  145. L2PcInstance player = (L2PcInstance) activeChar.getTarget();
  146. for (L2Skill skill : player.getAllSkills())
  147. player.removeSkill(skill);
  148. activeChar.sendMessage("You removed all skills from " + player.getName());
  149. player.sendMessage("Admin removed all skills from you.");
  150. player.sendSkillList();
  151. player.broadcastUserInfo();
  152. }
  153. }
  154. else if (command.startsWith("admin_add_clan_skill"))
  155. {
  156. try
  157. {
  158. String[] val = command.split(" ");
  159. adminAddClanSkill(activeChar, Integer.parseInt(val[1]), Integer.parseInt(val[2]));
  160. }
  161. catch (Exception e)
  162. {
  163. activeChar.sendMessage("Usage: //add_clan_skill <skill_id> <level>");
  164. }
  165. }
  166. else if (command.startsWith("admin_setskill"))
  167. {
  168. String[] split = command.split(" ");
  169. int id = Integer.parseInt(split[1]);
  170. int lvl = Integer.parseInt(split[2]);
  171. L2Skill skill = SkillTable.getInstance().getInfo(id, lvl);
  172. activeChar.addSkill(skill);
  173. activeChar.sendSkillList();
  174. activeChar.sendMessage("You added yourself skill "+skill.getName()+"("+id+") level "+lvl);
  175. }
  176. return true;
  177. }
  178. /**
  179. * This function will give all the skills that the target can learn at his/her level
  180. * @param activeChar: the gm char
  181. */
  182. private void adminGiveAllSkills(L2PcInstance activeChar, boolean includedByFs)
  183. {
  184. L2Object target = activeChar.getTarget();
  185. L2PcInstance player = null;
  186. if (target instanceof L2PcInstance)
  187. player = (L2PcInstance) target;
  188. else
  189. {
  190. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INCORRECT_TARGET));
  191. return;
  192. }
  193. //Notify player and admin
  194. activeChar.sendMessage("You gave " + player.giveAvailableSkills(includedByFs, true) + " skills to " + player.getName());
  195. player.sendSkillList();
  196. }
  197. public String[] getAdminCommandList()
  198. {
  199. return ADMIN_COMMANDS;
  200. }
  201. private void removeSkillsPage(L2PcInstance activeChar, int page)
  202. { //TODO: Externalize HTML
  203. L2Object target = activeChar.getTarget();
  204. L2PcInstance player = null;
  205. if (target instanceof L2PcInstance)
  206. player = (L2PcInstance) target;
  207. else
  208. {
  209. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.TARGET_IS_INCORRECT));
  210. return;
  211. }
  212. L2Skill[] skills = player.getAllSkills();
  213. int maxSkillsPerPage = 10;
  214. int maxPages = skills.length / maxSkillsPerPage;
  215. if (skills.length > maxSkillsPerPage * maxPages)
  216. maxPages++;
  217. if (page > maxPages)
  218. page = maxPages;
  219. int skillsStart = maxSkillsPerPage * page;
  220. int skillsEnd = skills.length;
  221. if (skillsEnd - skillsStart > maxSkillsPerPage)
  222. skillsEnd = skillsStart + maxSkillsPerPage;
  223. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  224. final StringBuilder replyMSG = StringUtil.startAppend(
  225. 500 + maxPages * 50 + (skillsEnd - skillsStart + 1) * 50,
  226. "<html><body>" +
  227. "<table width=260><tr>" +
  228. "<td width=40><button value=\"Main\" action=\"bypass -h admin_admin\" width=40 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>" +
  229. "<td width=180><center>Character Selection Menu</center></td>" +
  230. "<td width=40><button value=\"Back\" action=\"bypass -h admin_show_skills\" width=40 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>" +
  231. "</tr></table>" +
  232. "<br><br>" +
  233. "<center>Editing <font color=\"LEVEL\">",
  234. player.getName(),
  235. "</font></center>" +
  236. "<br><table width=270><tr><td>Lv: ",
  237. String.valueOf(player.getLevel()),
  238. " ",
  239. player.getTemplate().className,
  240. "</td></tr></table>" +
  241. "<br><table width=270><tr><td>Note: Dont forget that modifying players skills can</td></tr>" +
  242. "<tr><td>ruin the game...</td></tr></table>" +
  243. "<br><center>Click on the skill you wish to remove:</center>" +
  244. "<br>" +
  245. "<center><table width=270><tr>"
  246. );
  247. for (int x = 0; x < maxPages; x++)
  248. {
  249. int pagenr = x + 1;
  250. StringUtil.append(replyMSG,
  251. "<td><a action=\"bypass -h admin_remove_skills ",
  252. String.valueOf(x),
  253. "\">Page ",
  254. String.valueOf(pagenr),
  255. "</a></td>"
  256. );
  257. }
  258. replyMSG.append(
  259. "</tr></table></center>" +
  260. "<br><table width=270>" +
  261. "<tr><td width=80>Name:</td><td width=60>Level:</td><td width=40>Id:</td></tr>"
  262. );
  263. for (int i = skillsStart; i < skillsEnd; i++) {
  264. StringUtil.append(replyMSG,
  265. "<tr><td width=80><a action=\"bypass -h admin_remove_skill ",
  266. String.valueOf(skills[i].getId()),
  267. "\">",
  268. skills[i].getName(),
  269. "</a></td><td width=60>",
  270. String.valueOf(skills[i].getLevel()),
  271. "</td><td width=40>",
  272. String.valueOf(skills[i].getId()),
  273. "</td></tr>"
  274. );
  275. }
  276. replyMSG.append(
  277. "</table>" +
  278. "<br><center><table>" +
  279. "Remove skill by ID :" +
  280. "<tr><td>Id: </td>" +
  281. "<td><edit var=\"id_to_remove\" width=110></td></tr>" +
  282. "</table></center>" +
  283. "<center><button value=\"Remove skill\" action=\"bypass -h admin_remove_skill $id_to_remove\" width=110 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></center>" +
  284. "<br><center><button value=\"Back\" action=\"bypass -h admin_current_player\" width=40 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></center>" +
  285. "</body></html>");
  286. adminReply.setHtml(replyMSG.toString());
  287. activeChar.sendPacket(adminReply);
  288. }
  289. private void showMainPage(L2PcInstance activeChar)
  290. {
  291. L2Object target = activeChar.getTarget();
  292. L2PcInstance player = null;
  293. if (target instanceof L2PcInstance)
  294. player = (L2PcInstance) target;
  295. else
  296. {
  297. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INCORRECT_TARGET));
  298. return;
  299. }
  300. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  301. adminReply.setFile(activeChar.getHtmlPrefix(), "data/html/admin/charskills.htm");
  302. adminReply.replace("%name%", player.getName());
  303. adminReply.replace("%level%", String.valueOf(player.getLevel()));
  304. adminReply.replace("%class%", player.getTemplate().className);
  305. activeChar.sendPacket(adminReply);
  306. }
  307. private void adminGetSkills(L2PcInstance activeChar)
  308. {
  309. L2Object target = activeChar.getTarget();
  310. L2PcInstance player = null;
  311. if (target instanceof L2PcInstance)
  312. player = (L2PcInstance) target;
  313. else
  314. {
  315. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INCORRECT_TARGET));
  316. return;
  317. }
  318. if (player.getName().equals(activeChar.getName()))
  319. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.CANNOT_USE_ON_YOURSELF));
  320. else
  321. {
  322. L2Skill[] skills = player.getAllSkills();
  323. adminSkills = activeChar.getAllSkills();
  324. for (L2Skill skill: adminSkills)
  325. activeChar.removeSkill(skill);
  326. for (L2Skill skill: skills)
  327. activeChar.addSkill(skill, true);
  328. activeChar.sendMessage("You now have all the skills of " + player.getName() + ".");
  329. activeChar.sendSkillList();
  330. }
  331. showMainPage(activeChar);
  332. }
  333. private void adminResetSkills(L2PcInstance activeChar)
  334. {
  335. L2Object target = activeChar.getTarget();
  336. L2PcInstance player = null;
  337. if (target instanceof L2PcInstance)
  338. player = (L2PcInstance) target;
  339. else
  340. {
  341. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INCORRECT_TARGET));
  342. return;
  343. }
  344. if (adminSkills == null)
  345. activeChar.sendMessage("You must get the skills of someone in order to do this.");
  346. else
  347. {
  348. L2Skill[] skills = player.getAllSkills();
  349. for (L2Skill skill: skills)
  350. player.removeSkill(skill);
  351. for (L2Skill skill: activeChar.getAllSkills())
  352. player.addSkill(skill, true);
  353. for (L2Skill skill: skills)
  354. activeChar.removeSkill(skill);
  355. for (L2Skill skill: adminSkills)
  356. activeChar.addSkill(skill, true);
  357. player.sendMessage("[GM]" + activeChar.getName() + " updated your skills.");
  358. activeChar.sendMessage("You now have all your skills back.");
  359. adminSkills = null;
  360. activeChar.sendSkillList();
  361. player.sendSkillList();
  362. }
  363. showMainPage(activeChar);
  364. }
  365. private void adminAddSkill(L2PcInstance activeChar, String val)
  366. {
  367. L2Object target = activeChar.getTarget();
  368. L2PcInstance player = null;
  369. if (target instanceof L2PcInstance)
  370. player = (L2PcInstance) target;
  371. else
  372. {
  373. showMainPage(activeChar);
  374. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INCORRECT_TARGET));
  375. return;
  376. }
  377. StringTokenizer st = new StringTokenizer(val);
  378. if (st.countTokens() != 2)
  379. {
  380. showMainPage(activeChar);
  381. }
  382. else
  383. {
  384. L2Skill skill = null;
  385. try
  386. {
  387. String id = st.nextToken();
  388. String level = st.nextToken();
  389. int idval = Integer.parseInt(id);
  390. int levelval = Integer.parseInt(level);
  391. skill = SkillTable.getInstance().getInfo(idval, levelval);
  392. }
  393. catch (Exception e)
  394. {
  395. _log.log(Level.WARNING, "", e);
  396. }
  397. if (skill != null)
  398. {
  399. String name = skill.getName();
  400. // Player's info.
  401. player.sendMessage("Admin gave you the skill " + name + ".");
  402. player.addSkill(skill, true);
  403. player.sendSkillList();
  404. // Admin info.
  405. activeChar.sendMessage("You gave the skill " + name + " to " + player.getName() + ".");
  406. if (Config.DEBUG)
  407. _log.fine("[GM]" + activeChar.getName() + " gave skill " + name + " to " + player.getName() + ".");
  408. activeChar.sendSkillList();
  409. }
  410. else
  411. activeChar.sendMessage("Error: there is no such skill.");
  412. showMainPage(activeChar); //Back to start
  413. }
  414. }
  415. private void adminRemoveSkill(L2PcInstance activeChar, int idval)
  416. {
  417. L2Object target = activeChar.getTarget();
  418. L2PcInstance player = null;
  419. if (target instanceof L2PcInstance)
  420. player = (L2PcInstance) target;
  421. else
  422. {
  423. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INCORRECT_TARGET));
  424. return;
  425. }
  426. L2Skill skill = SkillTable.getInstance().getInfo(idval, player.getSkillLevel(idval));
  427. if (skill != null)
  428. {
  429. String skillname = skill.getName();
  430. player.sendMessage("Admin removed the skill " + skillname + " from your skills list.");
  431. player.removeSkill(skill);
  432. //Admin information
  433. activeChar.sendMessage("You removed the skill " + skillname + " from " + player.getName() + ".");
  434. if (Config.DEBUG)
  435. _log.fine("[GM]" + activeChar.getName() + " removed skill " + skillname + " from " + player.getName() + ".");
  436. activeChar.sendSkillList();
  437. }
  438. else
  439. activeChar.sendMessage("Error: there is no such skill.");
  440. removeSkillsPage(activeChar, 0); //Back to previous page
  441. }
  442. private void adminAddClanSkill(L2PcInstance activeChar, int id, int level)
  443. {
  444. L2Object target = activeChar.getTarget();
  445. L2PcInstance player = null;
  446. if (target instanceof L2PcInstance)
  447. player = (L2PcInstance) target;
  448. else
  449. {
  450. showMainPage(activeChar);
  451. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INCORRECT_TARGET));
  452. return;
  453. }
  454. if (!player.isClanLeader())
  455. {
  456. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.S1_IS_NOT_A_CLAN_LEADER).addString(player.getName()));
  457. showMainPage(activeChar);
  458. return;
  459. }
  460. if ((id < 370) || (id > 391) || (level < 1) || (level > 3))
  461. {
  462. activeChar.sendMessage("Usage: //add_clan_skill <skill_id> <level>");
  463. showMainPage(activeChar);
  464. return;
  465. }
  466. else
  467. {
  468. L2Skill skill = SkillTable.getInstance().getInfo(id, level);
  469. if (skill != null)
  470. {
  471. String skillname = skill.getName();
  472. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_SKILL_S1_ADDED);
  473. sm.addSkillName(skill);
  474. player.sendPacket(sm);
  475. final L2Clan clan = player.getClan();
  476. clan.broadcastToOnlineMembers(sm);
  477. clan.addNewSkill(skill);
  478. activeChar.sendMessage("You gave the Clan Skill: " + skillname + " to the clan " + clan.getName() + ".");
  479. clan.broadcastToOnlineMembers(new PledgeSkillList(clan));
  480. for (L2PcInstance member : clan.getOnlineMembers(0))
  481. {
  482. member.sendSkillList();
  483. }
  484. showMainPage(activeChar);
  485. return;
  486. }
  487. else
  488. {
  489. activeChar.sendMessage("Error: there is no such skill.");
  490. return;
  491. }
  492. }
  493. }
  494. }