RequestEnchantItem.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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.model.Inventory;
  19. import net.sf.l2j.gameserver.model.L2ItemInstance;
  20. import net.sf.l2j.gameserver.model.L2World;
  21. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  22. import net.sf.l2j.gameserver.network.SystemMessageId;
  23. import net.sf.l2j.gameserver.serverpackets.EnchantResult;
  24. import net.sf.l2j.gameserver.serverpackets.InventoryUpdate;
  25. import net.sf.l2j.gameserver.serverpackets.ItemList;
  26. import net.sf.l2j.gameserver.serverpackets.StatusUpdate;
  27. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  28. import net.sf.l2j.gameserver.templates.L2Item;
  29. import net.sf.l2j.gameserver.templates.L2WeaponType;
  30. import net.sf.l2j.gameserver.util.IllegalPlayerAction;
  31. import net.sf.l2j.gameserver.util.Util;
  32. import net.sf.l2j.util.Rnd;
  33. public final class RequestEnchantItem extends L2GameClientPacket
  34. {
  35. protected static final Logger _log = Logger.getLogger(Inventory.class.getName());
  36. private static final String _C__58_REQUESTENCHANTITEM = "[C] 58 RequestEnchantItem";
  37. private static final int[] ENCHANT_SCROLLS = { 729, 730, 947, 948, 951, 952, 955, 956, 959, 960 };
  38. private static final int[] BLESSED_SCROLLS = { 6569, 6570, 6571, 6572, 6573, 6574, 6575, 6576, 6577, 6578 };
  39. private int _objectId;
  40. @Override
  41. protected void readImpl()
  42. {
  43. _objectId = readD();
  44. }
  45. @Override
  46. protected void runImpl()
  47. {
  48. L2PcInstance activeChar = getClient().getActiveChar();
  49. if (activeChar == null || _objectId == 0)
  50. return;
  51. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_objectId);
  52. L2ItemInstance scroll = activeChar.getActiveEnchantItem();
  53. if (item == null || scroll == null)
  54. {
  55. activeChar.setActiveEnchantItem(null);
  56. return;
  57. }
  58. // can't enchant rods, hero weapons, adventurers' and shadow items
  59. if (item.getItem().getItemType() == L2WeaponType.ROD || item.isHeroItem() || item.getItemId() >= 7816 && item.getItemId() <= 7831 || item.isShadowItem())
  60. {
  61. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  62. activeChar.setActiveEnchantItem(null);
  63. return;
  64. }
  65. if (item.isWear())
  66. {
  67. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to enchant a weared Item", IllegalPlayerAction.PUNISH_KICK);
  68. return;
  69. }
  70. switch (item.getLocation())
  71. {
  72. case INVENTORY:
  73. case PAPERDOLL:
  74. {
  75. if (item.getOwnerId() != activeChar.getObjectId())
  76. {
  77. activeChar.setActiveEnchantItem(null);
  78. return;
  79. }
  80. break;
  81. }
  82. default:
  83. {
  84. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to use enchant Exploit!", IllegalPlayerAction.PUNISH_KICKBAN);
  85. return;
  86. }
  87. }
  88. int itemType2 = item.getItem().getType2();
  89. boolean enchantItem = false;
  90. boolean blessedScroll = false;
  91. int crystalId = 0;
  92. /** pretty code ;D */
  93. switch (item.getItem().getCrystalType())
  94. {
  95. case L2Item.CRYSTAL_A:
  96. crystalId = 1461;
  97. switch (scroll.getItemId())
  98. {
  99. case 729:
  100. case 731:
  101. case 6569:
  102. if (itemType2 == L2Item.TYPE2_WEAPON)
  103. enchantItem = true;
  104. break;
  105. case 730:
  106. case 732:
  107. case 6570:
  108. if (itemType2 == L2Item.TYPE2_SHIELD_ARMOR || itemType2 == L2Item.TYPE2_ACCESSORY)
  109. enchantItem = true;
  110. break;
  111. }
  112. break;
  113. case L2Item.CRYSTAL_B:
  114. crystalId = 1460;
  115. switch (scroll.getItemId())
  116. {
  117. case 947:
  118. case 949:
  119. case 6571:
  120. if (itemType2 == L2Item.TYPE2_WEAPON)
  121. enchantItem = true;
  122. break;
  123. case 948:
  124. case 950:
  125. case 6572:
  126. if (itemType2 == L2Item.TYPE2_SHIELD_ARMOR || itemType2 == L2Item.TYPE2_ACCESSORY)
  127. enchantItem = true;
  128. break;
  129. }
  130. break;
  131. case L2Item.CRYSTAL_C:
  132. crystalId = 1459;
  133. switch (scroll.getItemId())
  134. {
  135. case 951:
  136. case 953:
  137. case 6573:
  138. if (itemType2 == L2Item.TYPE2_WEAPON)
  139. enchantItem = true;
  140. break;
  141. case 952:
  142. case 954:
  143. case 6574:
  144. if (itemType2 == L2Item.TYPE2_SHIELD_ARMOR || itemType2 == L2Item.TYPE2_ACCESSORY)
  145. enchantItem = true;
  146. break;
  147. }
  148. break;
  149. case L2Item.CRYSTAL_D:
  150. crystalId = 1458;
  151. switch (scroll.getItemId())
  152. {
  153. case 955:
  154. case 957:
  155. case 6575:
  156. if (itemType2 == L2Item.TYPE2_WEAPON)
  157. enchantItem = true;
  158. break;
  159. case 956:
  160. case 958:
  161. case 6576:
  162. if (itemType2 == L2Item.TYPE2_SHIELD_ARMOR || itemType2 == L2Item.TYPE2_ACCESSORY)
  163. enchantItem = true;
  164. break;
  165. }
  166. break;
  167. case L2Item.CRYSTAL_S:
  168. case L2Item.CRYSTAL_S80:
  169. crystalId = 1462;
  170. switch (scroll.getItemId())
  171. {
  172. case 959:
  173. case 961:
  174. case 6577:
  175. if (itemType2 == L2Item.TYPE2_WEAPON)
  176. enchantItem = true;
  177. break;
  178. case 960:
  179. case 962:
  180. case 6578:
  181. if (itemType2 == L2Item.TYPE2_SHIELD_ARMOR
  182. || itemType2 == L2Item.TYPE2_ACCESSORY)
  183. enchantItem = true;
  184. break;
  185. }
  186. break;
  187. }
  188. if (!enchantItem)
  189. {
  190. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  191. activeChar.setActiveEnchantItem(null);
  192. return;
  193. }
  194. // Check the scroll type, and if it functions like "blessed" set it to true for being "blessedScroll" - Yesod
  195. if (scroll.getItemId() >= 6569 && scroll.getItemId() <= 6578)
  196. blessedScroll = true;
  197. scroll = activeChar.getInventory().destroyItem("Enchant", scroll.getObjectId(), 1, activeChar, item);
  198. if (scroll == null)
  199. {
  200. activeChar.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
  201. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to enchant with a scroll he doesn't have", Config.DEFAULT_PUNISH);
  202. activeChar.setActiveEnchantItem(null);
  203. return;
  204. }
  205. SystemMessage sm;
  206. int chance = 0;
  207. int maxEnchantLevel = 0;
  208. if (item.getItem().getType2() == L2Item.TYPE2_WEAPON)
  209. {
  210. for (int scrollId : ENCHANT_SCROLLS)
  211. {
  212. if (scroll.getItemId() == scrollId)
  213. {
  214. chance = Config.ENCHANT_CHANCE_WEAPON;
  215. break;
  216. }
  217. }
  218. for (int scrollId : BLESSED_SCROLLS)
  219. {
  220. if (scroll.getItemId() == scrollId)
  221. {
  222. chance = Config.BLESSED_ENCHANT_CHANCE_WEAPON;
  223. break;
  224. }
  225. }
  226. maxEnchantLevel = Config.ENCHANT_MAX_WEAPON;
  227. }
  228. else if (item.getItem().getType2() == L2Item.TYPE2_SHIELD_ARMOR)
  229. {
  230. for (int scrollId : ENCHANT_SCROLLS)
  231. {
  232. if (scroll.getItemId() == scrollId)
  233. {
  234. chance = Config.ENCHANT_CHANCE_ARMOR;
  235. break;
  236. }
  237. }
  238. for (int scrollId : BLESSED_SCROLLS)
  239. {
  240. if (scroll.getItemId() == scrollId)
  241. {
  242. chance = Config.BLESSED_ENCHANT_CHANCE_ARMOR;
  243. break;
  244. }
  245. }
  246. maxEnchantLevel = Config.ENCHANT_MAX_ARMOR;
  247. }
  248. else if (item.getItem().getType2() == L2Item.TYPE2_ACCESSORY)
  249. {
  250. for (int scrollId : ENCHANT_SCROLLS)
  251. {
  252. if (scroll.getItemId() == scrollId)
  253. {
  254. chance = Config.ENCHANT_CHANCE_JEWELRY;
  255. break;
  256. }
  257. }
  258. for (int scrollId : BLESSED_SCROLLS)
  259. {
  260. if (scroll.getItemId() == scrollId)
  261. {
  262. chance = Config.BLESSED_ENCHANT_CHANCE_JEWELRY;
  263. break;
  264. }
  265. }
  266. maxEnchantLevel = Config.ENCHANT_MAX_JEWELRY;
  267. }
  268. if (item.getEnchantLevel() < Config.ENCHANT_SAFE_MAX || item.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR && item.getEnchantLevel() < Config.ENCHANT_SAFE_MAX_FULL)
  269. chance = 100;
  270. if (Rnd.get(100) < chance)
  271. {
  272. synchronized (item)
  273. {
  274. if (item.getOwnerId() != activeChar.getObjectId() // has just lost the item
  275. || item.getEnchantLevel() >= maxEnchantLevel && maxEnchantLevel != 0)
  276. {
  277. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  278. activeChar.setActiveEnchantItem(null);
  279. return;
  280. }
  281. if (item.getLocation() != L2ItemInstance.ItemLocation.INVENTORY
  282. && item.getLocation() != L2ItemInstance.ItemLocation.PAPERDOLL)
  283. {
  284. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  285. activeChar.setActiveEnchantItem(null);
  286. return;
  287. }
  288. if (item.getEnchantLevel() == 0)
  289. {
  290. sm = new SystemMessage(SystemMessageId.S1_SUCCESSFULLY_ENCHANTED);
  291. sm.addItemName(item.getItemId());
  292. activeChar.sendPacket(sm);
  293. }
  294. else
  295. {
  296. sm = new SystemMessage(SystemMessageId.S1_S2_SUCCESSFULLY_ENCHANTED);
  297. sm.addNumber(item.getEnchantLevel());
  298. sm.addItemName(item.getItemId());
  299. activeChar.sendPacket(sm);
  300. }
  301. item.setEnchantLevel(item.getEnchantLevel() + 1);
  302. item.updateDatabase();
  303. }
  304. }
  305. else
  306. {
  307. if (!blessedScroll)
  308. {
  309. if (item.getEnchantLevel() > 0)
  310. {
  311. sm = new SystemMessage(SystemMessageId.ENCHANTMENT_FAILED_S1_S2_EVAPORATED);
  312. sm.addNumber(item.getEnchantLevel());
  313. sm.addItemName(item.getItemId());
  314. activeChar.sendPacket(sm);
  315. }
  316. else
  317. {
  318. sm = new SystemMessage(SystemMessageId.ENCHANTMENT_FAILED_S1_EVAPORATED);
  319. sm.addItemName(item.getItemId());
  320. activeChar.sendPacket(sm);
  321. }
  322. }
  323. else
  324. {
  325. sm = new SystemMessage(SystemMessageId.BLESSED_ENCHANT_FAILED);
  326. activeChar.sendPacket(sm);
  327. }
  328. if (!blessedScroll)
  329. {
  330. if (item.getEnchantLevel() > 0)
  331. {
  332. sm = new SystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  333. sm.addNumber(item.getEnchantLevel());
  334. sm.addItemName(item.getItemId());
  335. activeChar.sendPacket(sm);
  336. }
  337. else
  338. {
  339. sm = new SystemMessage(SystemMessageId.S1_DISARMED);
  340. sm.addItemName(item.getItemId());
  341. activeChar.sendPacket(sm);
  342. }
  343. if (item.isEquipped())
  344. {
  345. L2ItemInstance[] unequiped = activeChar.getInventory().unEquipItemInSlotAndRecord(item.getLocationSlot());
  346. InventoryUpdate iu = new InventoryUpdate();
  347. for (int i = 0; i < unequiped.length; i++)
  348. {
  349. iu.addModifiedItem(unequiped[i]);
  350. }
  351. activeChar.sendPacket(iu);
  352. activeChar.broadcastUserInfo();
  353. }
  354. int count = item.getCrystalCount() - (item.getItem().getCrystalCount() + 1) / 2;
  355. if (count < 1)
  356. count = 1;
  357. L2ItemInstance destroyItem = activeChar.getInventory().destroyItem("Enchant", item, activeChar, null);
  358. if (destroyItem == null)
  359. {
  360. activeChar.setActiveEnchantItem(null);
  361. return;
  362. }
  363. L2ItemInstance crystals = activeChar.getInventory().addItem("Enchant", crystalId, count, activeChar, destroyItem);
  364. sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  365. sm.addItemName(crystals.getItemId());
  366. sm.addNumber(count);
  367. activeChar.sendPacket(sm);
  368. if (!Config.FORCE_INVENTORY_UPDATE)
  369. {
  370. InventoryUpdate iu = new InventoryUpdate();
  371. if (destroyItem.getCount() == 0)
  372. iu.addRemovedItem(destroyItem);
  373. else
  374. iu.addModifiedItem(destroyItem);
  375. iu.addItem(crystals);
  376. activeChar.sendPacket(iu);
  377. }
  378. else
  379. activeChar.sendPacket(new ItemList(activeChar, true));
  380. StatusUpdate su = new StatusUpdate(activeChar.getObjectId());
  381. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  382. activeChar.sendPacket(su);
  383. activeChar.broadcastUserInfo();
  384. L2World world = L2World.getInstance();
  385. world.removeObject(destroyItem);
  386. }
  387. else
  388. {
  389. item.setEnchantLevel(0);
  390. item.updateDatabase();
  391. }
  392. }
  393. sm = null;
  394. StatusUpdate su = new StatusUpdate(activeChar.getObjectId());
  395. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  396. activeChar.sendPacket(su);
  397. su = null;
  398. activeChar.sendPacket(new EnchantResult(item.getEnchantLevel()));
  399. activeChar.sendPacket(new ItemList(activeChar, false));
  400. activeChar.broadcastUserInfo();
  401. activeChar.setActiveEnchantItem(null);
  402. }
  403. /*
  404. * (non-Javadoc)
  405. *
  406. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#getType()
  407. */
  408. @Override
  409. public String getType()
  410. {
  411. return _C__58_REQUESTENCHANTITEM;
  412. }
  413. }