AdminSkill.java 16 KB

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