2
0

RequestExEnchantSkillRouteChange.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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) 0x34
  40. * d: skill id
  41. * d: skill lvl
  42. * @author -Wooden-
  43. *
  44. */
  45. public final class RequestExEnchantSkillRouteChange extends L2GameClientPacket
  46. {
  47. protected static final Logger _log = Logger.getLogger(RequestExEnchantSkillRouteChange.class.getName());
  48. private int _skillId;
  49. private int _skillLvl;
  50. @Override
  51. protected void readImpl()
  52. {
  53. _skillId = readD();
  54. _skillLvl = readD();
  55. }
  56. /* (non-Javadoc)
  57. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#runImpl()
  58. */
  59. @Override
  60. protected void runImpl()
  61. {
  62. L2PcInstance player = getClient().getActiveChar();
  63. if (player == null)
  64. return;
  65. L2FolkInstance trainer = player.getLastFolkNPC();
  66. if (trainer == null)
  67. return;
  68. int npcid = trainer.getNpcId();
  69. if ((trainer == null || !player.isInsideRadius(trainer, L2NpcInstance.INTERACTION_DISTANCE, false, false)) && !player.isGM())
  70. return;
  71. if (player.getClassId().getId() < 88 ||(player.getClassId().getId() >= 123 && player.getClassId().getId() < 132 )||player.getClassId().getId() == 135) // requires to have 3rd class quest completed
  72. return;
  73. if (player.getLevel() < 76)
  74. return;
  75. L2Skill skill = SkillTable.getInstance().getInfo(_skillId, _skillLvl);
  76. if (skill == null)
  77. {
  78. return;
  79. }
  80. if (!skill.canTeachBy(npcid) || !skill.getCanLearn(player.getClassId()))
  81. {
  82. if (!Config.ALT_GAME_SKILL_LEARN)
  83. {
  84. player.sendMessage("You are trying to learn skill that u can't..");
  85. Util.handleIllegalPlayerAction(player, "Client "+this.getClient()+" tried to learn skill that he can't!!!", IllegalPlayerAction.PUNISH_KICK);
  86. return;
  87. }
  88. }
  89. int reqItemId = SkillTreeTable.CHANGE_ENCHANT_BOOK;
  90. L2EnchantSkillLearn s = SkillTreeTable.getInstance().getSkillEnchantmentBySkillId(_skillId);
  91. if (s == null)
  92. {
  93. return;
  94. }
  95. int currentLevel = player.getSkillLevel(_skillId);
  96. // do u have this skill enchanted?
  97. if (currentLevel <= 100)
  98. {
  99. return;
  100. }
  101. int currentEnchantLevel = currentLevel%100;
  102. // is the requested level valid?
  103. if (currentEnchantLevel != _skillLvl%100)
  104. {
  105. return;
  106. }
  107. EnchantSkillDetail esd = s.getEnchantSkillDetail(_skillLvl);
  108. int requiredSp = esd.getSpCost();
  109. int requiredExp = esd.getExp();
  110. if (player.getSp() >= requiredSp)
  111. {
  112. long expAfter = player.getExp() - requiredExp;
  113. if (player.getExp() >= requiredExp && expAfter >= Experience.LEVEL[player.getLevel()])
  114. {
  115. // only first lvl requires book
  116. L2ItemInstance spb = player.getInventory().getItemByItemId(reqItemId);
  117. if (Config.ES_SP_BOOK_NEEDED)
  118. {
  119. if (spb == null)// Haven't spellbook
  120. {
  121. player.sendPacket(new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ALL_ITENS_NEEDED_TO_CHANGE_SKILL_ENCHANT_ROUTE));
  122. return;
  123. }
  124. }
  125. boolean check;
  126. check = player.getStat().removeExpAndSp(requiredExp, requiredSp);
  127. if (Config.ES_SP_BOOK_NEEDED)
  128. {
  129. check &= player.destroyItem("Consume", spb.getObjectId(), 1, trainer, true);
  130. }
  131. if (!check)
  132. {
  133. player.sendPacket(new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ALL_OF_THE_ITEMS_NEEDED_TO_ENCHANT_THAT_SKILL));
  134. return;
  135. }
  136. int levelPenalty = Rnd.get(Math.min(4, currentEnchantLevel));
  137. _skillLvl -= levelPenalty;
  138. if (_skillLvl%100 == 0)
  139. {
  140. _skillLvl = s.getBaseLevel();
  141. }
  142. skill = SkillTable.getInstance().getInfo(_skillId, _skillLvl);
  143. if (skill != null)
  144. {
  145. player.addSkill(skill, true);
  146. }
  147. if (Config.DEBUG)
  148. {
  149. _log.fine("Learned skill ID: "+_skillId+" Level: "+_skillLvl+" for "+requiredSp+" SP, "+requiredExp+" EXP.");
  150. }
  151. player.sendPacket(new UserInfo(player));
  152. if (levelPenalty == 0)
  153. {
  154. SystemMessage sm = new SystemMessage(SystemMessageId.SKILL_ENCHANT_CHANGE_SUCCESSFUL_S1_LEVEL_WILL_REMAIN);
  155. sm.addSkillName(_skillId);
  156. player.sendPacket(sm);
  157. }
  158. else
  159. {
  160. SystemMessage sm = new SystemMessage(SystemMessageId.SKILL_ENCHANT_CHANGE_SUCCESSFUL_S1_LEVEL_WAS_DECREASED_BY_S2);
  161. sm.addSkillName(_skillId);
  162. sm.addNumber(levelPenalty);
  163. player.sendPacket(sm);
  164. }
  165. trainer.showEnchantChangeSkillList(player);
  166. this.updateSkillShortcuts(player);
  167. }
  168. else
  169. {
  170. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ENOUGH_EXP_TO_ENCHANT_THAT_SKILL);
  171. player.sendPacket(sm);
  172. }
  173. }
  174. else
  175. {
  176. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ENOUGH_SP_TO_ENCHANT_THAT_SKILL);
  177. player.sendPacket(sm);
  178. }
  179. }
  180. private void updateSkillShortcuts(L2PcInstance player)
  181. {
  182. // update all the shortcuts to this skill
  183. L2ShortCut[] allShortCuts = player.getAllShortCuts();
  184. for (L2ShortCut sc : allShortCuts)
  185. {
  186. if (sc.getId() == _skillId && sc.getType() == L2ShortCut.TYPE_SKILL)
  187. {
  188. L2ShortCut newsc = new L2ShortCut(sc.getSlot(), sc.getPage(), sc.getType(), sc.getId(), _skillLvl, 1);
  189. player.sendPacket(new ShortCutRegister(newsc));
  190. player.registerShortCut(newsc);
  191. }
  192. }
  193. }
  194. /* (non-Javadoc)
  195. * @see net.sf.l2j.gameserver.BasePacket#getType()
  196. */
  197. @Override
  198. public String getType()
  199. {
  200. return "[C] D0:34 RequestExEnchantSkillRouteChange";
  201. }
  202. }