RequestEnchantItem.java 13 KB

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