RequestExEnchantSkill.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.clientpackets;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.datatables.SkillTable;
  19. import net.sf.l2j.gameserver.datatables.SkillTreeTable;
  20. import net.sf.l2j.gameserver.model.L2EnchantSkillLearn;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.L2ShortCut;
  23. import net.sf.l2j.gameserver.model.L2Skill;
  24. import net.sf.l2j.gameserver.model.L2EnchantSkillLearn.EnchantSkillDetail;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  28. import net.sf.l2j.gameserver.model.base.Experience;
  29. import net.sf.l2j.gameserver.network.SystemMessageId;
  30. import net.sf.l2j.gameserver.serverpackets.ShortCutRegister;
  31. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  32. import net.sf.l2j.gameserver.serverpackets.UserInfo;
  33. import net.sf.l2j.gameserver.util.IllegalPlayerAction;
  34. import net.sf.l2j.gameserver.util.Util;
  35. import net.sf.l2j.util.Rnd;
  36. /**
  37. * Format (ch) dd
  38. * c: (id) 0xD0
  39. * h: (subid) 0x06
  40. * d: skill id
  41. * d: skill lvl
  42. * @author -Wooden-
  43. *
  44. */
  45. public final class RequestExEnchantSkill extends L2GameClientPacket
  46. {
  47. private static final String _C__D0_07_REQUESTEXENCHANTSKILL = "[C] D0:07 RequestExEnchantSkill";
  48. private static Logger _log = Logger.getLogger(RequestAquireSkill.class.getName());
  49. private int _skillId;
  50. private int _skillLvl;
  51. @Override
  52. protected void readImpl()
  53. {
  54. _skillId = readD();
  55. _skillLvl = readD();
  56. }
  57. /* (non-Javadoc)
  58. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#runImpl()
  59. */
  60. @Override
  61. protected void runImpl()
  62. {
  63. L2PcInstance player = getClient().getActiveChar();
  64. if (player == null)
  65. return;
  66. L2FolkInstance trainer = player.getLastFolkNPC();
  67. if (trainer == null)
  68. return;
  69. int npcid = trainer.getNpcId();
  70. if ((trainer == null || !player.isInsideRadius(trainer, L2NpcInstance.INTERACTION_DISTANCE, false, false)) && !player.isGM())
  71. return;
  72. if (player.getClassId().getId() < 88 ||(player.getClassId().getId() >= 123 && player.getClassId().getId() < 132 )||player.getClassId().getId() == 135) // requires to have 3rd class quest completed
  73. return;
  74. if (player.getLevel() < 76)
  75. return;
  76. L2Skill skill = SkillTable.getInstance().getInfo(_skillId, _skillLvl);
  77. if (skill == null)
  78. {
  79. return;
  80. }
  81. if (!skill.canTeachBy(npcid) || !skill.getCanLearn(player.getClassId()))
  82. {
  83. if (!Config.ALT_GAME_SKILL_LEARN)
  84. {
  85. player.sendMessage("You are trying to learn skill that u can't..");
  86. Util.handleIllegalPlayerAction(player, "Client "+this.getClient()+" tried to learn skill that he can't!!!", IllegalPlayerAction.PUNISH_KICK);
  87. return;
  88. }
  89. }
  90. int costMultiplier = SkillTreeTable.NORMAL_ENCHANT_COST_MULTIPLIER;
  91. int reqItemId = SkillTreeTable.NORMAL_ENCHANT_BOOK;
  92. L2EnchantSkillLearn s = SkillTreeTable.getInstance().getSkillEnchantmentBySkillId(_skillId);
  93. if (s == null)
  94. {
  95. return;
  96. }
  97. EnchantSkillDetail esd = s.getEnchantSkillDetail(_skillLvl);
  98. if (player.getSkillLevel(_skillId) != esd.getMinSkillLevel())
  99. {
  100. return;
  101. }
  102. int requiredSp = esd.getSpCost() * costMultiplier;
  103. int requiredExp = esd.getExp() * costMultiplier;
  104. int rate = esd.getRate(player);
  105. if (player.getSp() >= requiredSp)
  106. {
  107. long expAfter = player.getExp() - requiredExp;
  108. if (player.getExp() >= requiredExp && expAfter >= Experience.LEVEL[player.getLevel()])
  109. {
  110. // only first lvl requires book
  111. boolean usesBook = _skillLvl % 100 == 1; // 101, 201, 301 ...
  112. L2ItemInstance spb = player.getInventory().getItemByItemId(reqItemId);
  113. if (Config.ES_SP_BOOK_NEEDED && usesBook)
  114. {
  115. if (spb == null)// Haven't spellbook
  116. {
  117. player.sendPacket(new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ALL_OF_THE_ITEMS_NEEDED_TO_ENCHANT_THAT_SKILL));
  118. return;
  119. }
  120. }
  121. boolean check;
  122. check = player.getStat().removeExpAndSp(requiredExp, requiredSp);
  123. if (Config.ES_SP_BOOK_NEEDED && usesBook)
  124. {
  125. check &= player.destroyItem("Consume", spb.getObjectId(), 1, trainer, true);
  126. }
  127. if (!check)
  128. {
  129. player.sendPacket(new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ALL_OF_THE_ITEMS_NEEDED_TO_ENCHANT_THAT_SKILL));
  130. return;
  131. }
  132. // ok. Destroy ONE copy of the book
  133. if (Rnd.get(100) <= rate)
  134. {
  135. player.addSkill(skill, true);
  136. if (Config.DEBUG)
  137. {
  138. _log.fine("Learned skill ID: "+_skillId+" Level: "+_skillLvl+" for "+requiredSp+" SP, "+requiredExp+" EXP.");
  139. }
  140. player.sendPacket(new UserInfo(player));
  141. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_HAVE_SUCCEEDED_IN_ENCHANTING_THE_SKILL_S1);
  142. sm.addSkillName(_skillId);
  143. player.sendPacket(sm);
  144. }
  145. else
  146. {
  147. player.addSkill(SkillTable.getInstance().getInfo(_skillId, s.getBaseLevel()), true);
  148. player.sendSkillList();
  149. player.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_FAILED_TO_ENCHANT_THE_SKILL_S1));
  150. }
  151. trainer.showEnchantSkillList(player, false);
  152. this.updateSkillShortcuts(player);
  153. }
  154. else
  155. {
  156. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ENOUGH_EXP_TO_ENCHANT_THAT_SKILL);
  157. player.sendPacket(sm);
  158. }
  159. }
  160. else
  161. {
  162. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ENOUGH_SP_TO_ENCHANT_THAT_SKILL);
  163. player.sendPacket(sm);
  164. }
  165. }
  166. private void updateSkillShortcuts(L2PcInstance player)
  167. {
  168. // update all the shortcuts to this skill
  169. L2ShortCut[] allShortCuts = player.getAllShortCuts();
  170. for (L2ShortCut sc : allShortCuts)
  171. {
  172. if (sc.getId() == _skillId && sc.getType() == L2ShortCut.TYPE_SKILL)
  173. {
  174. L2ShortCut newsc = new L2ShortCut(sc.getSlot(), sc.getPage(), sc.getType(), sc.getId(), _skillLvl, 1);
  175. player.sendPacket(new ShortCutRegister(newsc));
  176. player.registerShortCut(newsc);
  177. }
  178. }
  179. }
  180. /* (non-Javadoc)
  181. * @see net.sf.l2j.gameserver.BasePacket#getType()
  182. */
  183. @Override
  184. public String getType()
  185. {
  186. return _C__D0_07_REQUESTEXENCHANTSKILL;
  187. }
  188. }