RequestExEnchantSkillSafe.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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) 0x32
  40. * d: skill id
  41. * d: skill lvl
  42. * @author -Wooden-
  43. *
  44. */
  45. public final class RequestExEnchantSkillSafe extends L2GameClientPacket
  46. {
  47. protected static final Logger _log = Logger.getLogger(RequestExEnchantSkillSafe.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 costMultiplier = SkillTreeTable.SAFE_ENCHANT_COST_MULTIPLIER;
  90. int reqItemId = SkillTreeTable.SAFE_ENCHANT_BOOK;
  91. L2EnchantSkillLearn s = SkillTreeTable.getInstance().getSkillEnchantmentBySkillId(_skillId);
  92. if (s == null)
  93. {
  94. return;
  95. }
  96. EnchantSkillDetail esd = s.getEnchantSkillDetail(_skillLvl);
  97. if (player.getSkillLevel(_skillId) != esd.getMinSkillLevel())
  98. {
  99. return;
  100. }
  101. int requiredSp = esd.getSpCost() * costMultiplier;
  102. int requiredExp = esd.getExp() * costMultiplier;
  103. int rate = esd.getRate(player);
  104. if (player.getSp() >= requiredSp)
  105. {
  106. long expAfter = player.getExp() - requiredExp;
  107. if (player.getExp() >= requiredExp && expAfter >= Experience.LEVEL[player.getLevel()])
  108. {
  109. // No config option for safe enchant book consume
  110. L2ItemInstance spb = player.getInventory().getItemByItemId(reqItemId);
  111. if (spb == null)// Haven't spellbook
  112. {
  113. player.sendPacket(new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ALL_OF_THE_ITEMS_NEEDED_TO_ENCHANT_THAT_SKILL));
  114. return;
  115. }
  116. boolean check = player.getStat().removeExpAndSp(requiredExp, requiredSp);
  117. check &= player.destroyItem("Consume", spb.getObjectId(), 1, trainer, true);
  118. if (!check)
  119. {
  120. player.sendPacket(new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ALL_OF_THE_ITEMS_NEEDED_TO_ENCHANT_THAT_SKILL));
  121. return;
  122. }
  123. // ok. Destroy ONE copy of the book
  124. if (Rnd.get(100) <= rate)
  125. {
  126. player.addSkill(skill, true);
  127. if (Config.DEBUG)
  128. {
  129. _log.fine("Learned skill ID: "+_skillId+" Level: "+_skillLvl+" for "+requiredSp+" SP, "+requiredExp+" EXP.");
  130. }
  131. player.sendPacket(new UserInfo(player));
  132. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_HAVE_SUCCEEDED_IN_ENCHANTING_THE_SKILL_S1);
  133. sm.addSkillName(_skillId);
  134. player.sendPacket(sm);
  135. }
  136. else
  137. {
  138. SystemMessage sm = new SystemMessage(SystemMessageId.SKILL_ENCHANT_FAILED_S1_LEVEL_WILL_REMAIN);
  139. sm.addSkillName(_skillId);
  140. player.sendPacket(sm);
  141. }
  142. trainer.showEnchantSkillList(player, true);
  143. this.updateSkillShortcuts(player);
  144. }
  145. else
  146. {
  147. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ENOUGH_EXP_TO_ENCHANT_THAT_SKILL);
  148. player.sendPacket(sm);
  149. }
  150. }
  151. else
  152. {
  153. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_DONT_HAVE_ENOUGH_SP_TO_ENCHANT_THAT_SKILL);
  154. player.sendPacket(sm);
  155. }
  156. }
  157. private void updateSkillShortcuts(L2PcInstance player)
  158. {
  159. // update all the shortcuts to this skill
  160. L2ShortCut[] allShortCuts = player.getAllShortCuts();
  161. for (L2ShortCut sc : allShortCuts)
  162. {
  163. if (sc.getId() == _skillId && sc.getType() == L2ShortCut.TYPE_SKILL)
  164. {
  165. L2ShortCut newsc = new L2ShortCut(sc.getSlot(), sc.getPage(), sc.getType(), sc.getId(), _skillLvl, 1);
  166. player.sendPacket(new ShortCutRegister(newsc));
  167. player.registerShortCut(newsc);
  168. }
  169. }
  170. }
  171. /* (non-Javadoc)
  172. * @see net.sf.l2j.gameserver.BasePacket#getType()
  173. */
  174. @Override
  175. public String getType()
  176. {
  177. return "[C] D0:32 RequestExEnchantSkillSafe";
  178. }
  179. }