AdminSkill.java 16 KB

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