Advertisement
Guest User

RequestEnchantItem.java

a guest
Jun 26th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.01 KB | None | 0 0
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; either version 2, or (at your option)
  4. * any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14. * 02111-1307, USA.
  15. *
  16. * http://www.gnu.org/copyleft/gpl.html || Property of www.L2Might.eu
  17. */
  18. package com.equal.gameserver.network.clientpackets;
  19.  
  20. import java.util.logging.Logger;
  21.  
  22. import com.equal.Config;
  23. import com.equal.gameserver.model.Inventory;
  24. import com.equal.gameserver.model.L2ItemInstance;
  25. import com.equal.gameserver.model.L2World;
  26. import com.equal.gameserver.model.actor.instance.L2PcInstance;
  27. import com.equal.gameserver.model.base.Race;
  28. import com.equal.gameserver.network.SystemMessageId;
  29. import com.equal.gameserver.network.serverpackets.EnchantResult;
  30. import com.equal.gameserver.network.serverpackets.InventoryUpdate;
  31. import com.equal.gameserver.network.serverpackets.ItemList;
  32. import com.equal.gameserver.network.serverpackets.StatusUpdate;
  33. import com.equal.gameserver.network.serverpackets.SystemMessage;
  34. import com.equal.gameserver.templates.L2Item;
  35. import com.equal.gameserver.templates.L2WeaponType;
  36. import com.equal.gameserver.util.IllegalPlayerAction;
  37. import com.equal.gameserver.util.Util;
  38. import com.equal.util.Rnd;
  39.  
  40.  
  41. public final class RequestEnchantItem extends L2GameClientPacket
  42. {
  43. protected static final Logger _log = Logger.getLogger(Inventory.class.getName());
  44. private static final String _C__58_REQUESTENCHANTITEM = "[C] 58 RequestEnchantItem";
  45. private static final int[] ENCHANT_SCROLLS = { 729, 730, 947, 948, 951, 952, 955, 956, 959, 960 };
  46. private static final int[] CRYSTAL_SCROLLS = { 731, 732, 949, 950, 953, 954, 957, 958, 961, 962 };
  47. private static final int[] BLESSED_SCROLLS = { 6569, 6570, 6571, 6572, 6573, 6574, 6575, 6576, 6577, 6578 };
  48. private boolean _needsEquip = true;
  49.  
  50. private int _objectId;
  51.  
  52. @Override
  53. protected void readImpl()
  54. {
  55. _objectId = readD();
  56. }
  57.  
  58. @Override
  59. protected void runImpl()
  60. {
  61. L2PcInstance activeChar = getClient().getActiveChar();
  62. if (activeChar == null || _objectId == 0) return;
  63.  
  64. if (activeChar.isProcessingTransaction())
  65. {
  66. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  67. activeChar.setActiveEnchantItem(null);
  68. return;
  69. }
  70.  
  71. if (activeChar.isOnline() == 0)
  72. {
  73. activeChar.setActiveEnchantItem(null);
  74. return;
  75. }
  76.  
  77. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_objectId);
  78. L2ItemInstance scroll = activeChar.getActiveEnchantItem();
  79. activeChar.setActiveEnchantItem(null);
  80. if (item == null || scroll == null) return;
  81.  
  82. if (!item.isEquipped())
  83. {
  84. activeChar.getInventory().equipItemAndRecord(item);
  85. _needsEquip = false;
  86. }
  87. activeChar.getInventory().unEquipItemInSlotAndRecord(item.getEquipSlot());
  88.  
  89. // can't enchant rods, hero weapons and shadow items
  90. if(item.getItem().getItemType() == L2WeaponType.ROD
  91. || item.getItemId() >= 6611 && item.getItemId() <= 6621
  92. || item.isShadowItem())
  93. {
  94. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  95. return;
  96. }
  97. if(item.isWear())
  98. {
  99. Util.handleIllegalPlayerAction(activeChar,"Player "+activeChar.getName()+" tried to enchant a weared Item", IllegalPlayerAction.PUNISH_KICK);
  100. return;
  101. }
  102. int itemType2 = item.getItem().getType2();
  103. boolean enchantItem = false;
  104. boolean blessedScroll = false;
  105. int crystalId = 0;
  106.  
  107. /** pretty code ;D */
  108. switch (item.getItem().getCrystalType())
  109. {
  110. case L2Item.CRYSTAL_A:
  111. crystalId = 1461;
  112. switch(scroll.getItemId())
  113. {
  114. case 729: case 731: case 6569:
  115. if(itemType2 == L2Item.TYPE2_WEAPON)
  116. enchantItem = true;
  117. break;
  118. case 730: case 732: case 6570:
  119. if((itemType2 == L2Item.TYPE2_SHIELD_ARMOR) || (itemType2 == L2Item.TYPE2_ACCESSORY))
  120. enchantItem = true;
  121. break;
  122. }
  123. break;
  124. case L2Item.CRYSTAL_B:
  125. crystalId = 1460;
  126. switch(scroll.getItemId())
  127. {
  128. case 947: case 949: case 6571:
  129. if(itemType2 == L2Item.TYPE2_WEAPON)
  130. enchantItem = true;
  131. break;
  132. case 948: case 950: case 6572:
  133. if((itemType2 == L2Item.TYPE2_SHIELD_ARMOR) || (itemType2 == L2Item.TYPE2_ACCESSORY))
  134. enchantItem = true;
  135. break;
  136. }
  137. break;
  138. case L2Item.CRYSTAL_C:
  139. crystalId = 1459;
  140. switch(scroll.getItemId())
  141. {
  142. case 951: case 953: case 6573:
  143. if(itemType2 == L2Item.TYPE2_WEAPON)
  144. enchantItem = true;
  145. break;
  146. case 952: case 954: case 6574:
  147. if((itemType2 == L2Item.TYPE2_SHIELD_ARMOR) || (itemType2 == L2Item.TYPE2_ACCESSORY))
  148. enchantItem = true;
  149. break;
  150. }
  151. break;
  152. case L2Item.CRYSTAL_D:
  153. crystalId = 1458;
  154. switch(scroll.getItemId())
  155. {
  156. case 955: case 957: case 6575:
  157. if(itemType2 == L2Item.TYPE2_WEAPON)
  158. enchantItem = true;
  159. break;
  160. case 956: case 958: case 6576:
  161. if((itemType2 == L2Item.TYPE2_SHIELD_ARMOR) || (itemType2 == L2Item.TYPE2_ACCESSORY))
  162. enchantItem = true;
  163. break;
  164. }
  165. break;
  166. case L2Item.CRYSTAL_S:
  167. crystalId = 1462;
  168. switch(scroll.getItemId())
  169. {
  170. case 959: case 961: case 6577:
  171. if(itemType2 == L2Item.TYPE2_WEAPON)
  172. enchantItem = true;
  173. break;
  174. case 960: case 962: case 6578:
  175. if((itemType2 == L2Item.TYPE2_SHIELD_ARMOR) || (itemType2 == L2Item.TYPE2_ACCESSORY))
  176. enchantItem = true;
  177. break;
  178. }
  179. break;
  180. }
  181.  
  182. if (!enchantItem)
  183. {
  184. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  185. return;
  186. }
  187.  
  188. // Get the scroll type - Yesod
  189. if (scroll.getItemId() >= 6569 && scroll.getItemId() <= 6578)
  190. blessedScroll = true;
  191. else
  192. for (int crystalscroll : CRYSTAL_SCROLLS)
  193. if(scroll.getItemId() == crystalscroll)
  194. {
  195. blessedScroll = true; break;
  196. }
  197.  
  198. scroll = activeChar.getInventory().destroyItem("Enchant", scroll, activeChar, item);
  199. if(scroll == null)
  200. {
  201. activeChar.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
  202. Util.handleIllegalPlayerAction(activeChar,"Player "+activeChar.getName()+" tried to enchant with a scroll he doesnt have", Config.DEFAULT_PUNISH);
  203. return;
  204. }
  205.  
  206. // SystemMessage sm = new
  207. // SystemMessage(SystemMessageId.ENCHANT_SCROLL_CANCELLED);
  208. // activeChar.sendPacket(sm);
  209. SystemMessage sm;
  210. int chance = 0;
  211. int maxEnchantLevel = 0;
  212. if (item.getItem().getType2() == L2Item.TYPE2_WEAPON)
  213. {
  214. maxEnchantLevel = Config.ENCHANT_MAX_WEAPON;
  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 : CRYSTAL_SCROLLS)
  224. {
  225. if (scroll.getItemId() == scrollId)
  226. {
  227. chance = Config.ENCHANT_CHANCE_WEAPON_CRYSTAL;
  228. ;
  229. break;
  230. }
  231. }
  232. for (int scrollId : BLESSED_SCROLLS)
  233. {
  234. if (scroll.getItemId() == scrollId)
  235. {
  236. chance = Config.ENCHANT_CHANCE_WEAPON_BLESSED;
  237. break;
  238. }
  239. }
  240. } else if (item.getItem().getType2() == L2Item.TYPE2_SHIELD_ARMOR)
  241. {
  242. maxEnchantLevel = Config.ENCHANT_MAX_ARMOR;
  243. for (int scrollId : ENCHANT_SCROLLS)
  244. {
  245. if (scroll.getItemId() == scrollId)
  246. {
  247. chance = Config.ENCHANT_CHANCE_ARMOR;
  248. break;
  249. }
  250. }
  251. for (int scrollId : CRYSTAL_SCROLLS)
  252. {
  253. if (scroll.getItemId() == scrollId)
  254. {
  255. chance = Config.ENCHANT_CHANCE_ARMOR_CRYSTAL;
  256. break;
  257. }
  258. }
  259. for (int scrollId : BLESSED_SCROLLS)
  260. {
  261. if (scroll.getItemId() == scrollId)
  262. {
  263. chance = Config.ENCHANT_CHANCE_ARMOR_BLESSED;
  264. break;
  265. }
  266. }
  267. } else if (item.getItem().getType2() == L2Item.TYPE2_ACCESSORY)
  268. {
  269. maxEnchantLevel = Config.ENCHANT_MAX_JEWELRY;
  270. for (int scrollId : ENCHANT_SCROLLS)
  271. {
  272. if (scroll.getItemId() == scrollId)
  273. {
  274. chance = Config.ENCHANT_CHANCE_JEWELRY;
  275. break;
  276. }
  277. }
  278. for (int scrollId : CRYSTAL_SCROLLS)
  279. {
  280. if (scroll.getItemId() == scrollId)
  281. {
  282. chance = Config.ENCHANT_CHANCE_JEWELRY_CRYSTAL;
  283. break;
  284. }
  285. }
  286. for (int scrollId : BLESSED_SCROLLS)
  287. {
  288. if (scroll.getItemId() == scrollId)
  289. {
  290. chance = Config.ENCHANT_CHANCE_JEWELRY_BLESSED;
  291. break;
  292. }
  293. }
  294. }
  295.  
  296. if (item.getEnchantLevel() < Config.ENCHANT_SAFE_MAX || (item.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR && item.getEnchantLevel() < Config.ENCHANT_SAFE_MAX_FULL))
  297. chance = 100;
  298.  
  299. int rndValue = Rnd.get(100);
  300. if (Config.ENABLE_DWARF_ENCHANT_BONUS && activeChar.getRace() == Race.dwarf)
  301. if (activeChar.getLevel() >= Config.DWARF_ENCHANT_MIN_LEVEL)
  302. rndValue -= Config.DWARF_ENCHANT_BONUS;
  303.  
  304. if (Config.ENCHANT_MAX_WEAPON > 0)
  305. {
  306. if (item.getItem().getType2() == L2Item.TYPE2_WEAPON && item.getEnchantLevel() >= Config.ENCHANT_MAX_WEAPON)
  307. {
  308. activeChar.sendMessage("This is server limit for enchanting this item with scrolls.");
  309. return;
  310. }
  311. }
  312.  
  313. if (Config.ENCHANT_MAX_ARMOR > 0)
  314. {
  315. if (item.getItem().getType2() == L2Item.TYPE2_SHIELD_ARMOR && item.getEnchantLevel() >= Config.ENCHANT_MAX_ARMOR)
  316. {
  317. activeChar.sendMessage("This is server limit for enchanting this item with scrolls.");
  318. return;
  319. }
  320. }
  321.  
  322. if (Config.ENCHANT_MAX_JEWELRY > 0)
  323. {
  324. if (item.getItem().getType2() == L2Item.TYPE2_ACCESSORY && item.getEnchantLevel() >= Config.ENCHANT_MAX_JEWELRY)
  325. {
  326. activeChar.sendMessage("This is server limit for enchanting this item with scrolls.");
  327. return;
  328. }
  329. }
  330.  
  331. if (!activeChar.getFloodProtectors().getMisc().tryPerformAction("enchant"))
  332. {
  333. //_log.warning("Player "+activeChar.getName()+" has performed action too fast");
  334. activeChar.sendMessage("Nepliusink per greit !");
  335. return;
  336. }
  337.  
  338.  
  339. if (rndValue < chance)
  340. {
  341. synchronized(item)
  342. {
  343. if (item.getOwnerId() != activeChar.getObjectId() // has just lost the item
  344. || (item.getEnchantLevel() >= maxEnchantLevel && maxEnchantLevel != 0))
  345. {
  346. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  347. return;
  348. }
  349. if (item.getLocation() != L2ItemInstance.ItemLocation.INVENTORY && item.getLocation() != L2ItemInstance.ItemLocation.PAPERDOLL)
  350. {
  351. activeChar.sendPacket(new SystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  352. return;
  353. }
  354. if (item.getEnchantLevel() == 0)
  355. {
  356. if (_needsEquip)
  357. activeChar.getInventory().equipItemAndRecord(item);
  358. sm = new SystemMessage(SystemMessageId.S1_SUCCESSFULLY_ENCHANTED);
  359. sm.addItemName(item.getItemId());
  360. activeChar.sendPacket(sm);
  361. }
  362. else
  363. {
  364. if (_needsEquip)
  365. activeChar.getInventory().equipItemAndRecord(item);
  366. sm = new SystemMessage(SystemMessageId.S1_S2_SUCCESSFULLY_ENCHANTED);
  367. sm.addNumber(item.getEnchantLevel());
  368. sm.addItemName(item.getItemId());
  369. activeChar.sendPacket(sm);
  370. }
  371. item.setEnchantLevel(item.getEnchantLevel()+Config.ALTERNATIVE_ENCHANT_VALUE);
  372. item.updateDatabase();
  373. }
  374. }
  375. else
  376. {
  377. if (!blessedScroll)
  378. {
  379. if (item.getEnchantLevel() > 0)
  380. {
  381. sm = new SystemMessage(SystemMessageId.ENCHANTMENT_FAILED_S1_S2_EVAPORATED);
  382. sm.addNumber(item.getEnchantLevel());
  383. sm.addItemName(item.getItemId());
  384. activeChar.sendPacket(sm);
  385. }
  386. else
  387. {
  388. sm = new SystemMessage(SystemMessageId.ENCHANTMENT_FAILED_S1_EVAPORATED);
  389. sm.addItemName(item.getItemId());
  390. activeChar.sendPacket(sm);
  391. }
  392. }
  393. else
  394. {
  395. sm = new SystemMessage(SystemMessageId.BLESSED_ENCHANT_FAILED);
  396. activeChar.sendPacket(sm);
  397. }
  398.  
  399. if (!blessedScroll)
  400. {
  401. if (item.getEnchantLevel() > 0)
  402. {
  403. sm = new SystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  404. sm.addNumber(item.getEnchantLevel());
  405. sm.addItemName(item.getItemId());
  406. activeChar.sendPacket(sm);
  407. }
  408. else
  409. {
  410. sm = new SystemMessage(SystemMessageId.S1_DISARMED);
  411. sm.addItemName(item.getItemId());
  412. activeChar.sendPacket(sm);
  413. }
  414.  
  415. L2ItemInstance[] unequiped = activeChar.getInventory().unEquipItemInSlotAndRecord(item.getEquipSlot());
  416. if (item.isEquipped())
  417. {
  418. InventoryUpdate iu = new InventoryUpdate();
  419. for (int i = 0; i < unequiped.length; i++)
  420. {
  421. iu.addModifiedItem(unequiped[i]);
  422. }
  423. activeChar.sendPacket(iu);
  424.  
  425. activeChar.broadcastUserInfo();
  426. }
  427.  
  428. int count = item.getCrystalCount() - (item.getItem().getCrystalCount() +1) / 2;
  429. if (count < 1) count = 1;
  430.  
  431. L2ItemInstance destroyItem = activeChar.getInventory().destroyItem("Enchant", item, activeChar, null);
  432. if (destroyItem == null) return;
  433.  
  434. L2ItemInstance crystals = activeChar.getInventory().addItem("Enchant", crystalId, count, activeChar, destroyItem);
  435.  
  436. sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  437. sm.addItemName(crystals.getItemId());
  438. sm.addNumber(count);
  439. activeChar.sendPacket(sm);
  440.  
  441. if (!Config.FORCE_INVENTORY_UPDATE)
  442. {
  443. InventoryUpdate iu = new InventoryUpdate();
  444. if (destroyItem.getCount() == 0) iu.addRemovedItem(destroyItem);
  445. else iu.addModifiedItem(destroyItem);
  446. iu.addItem(crystals);
  447.  
  448. activeChar.sendPacket(iu);
  449. }
  450. else activeChar.sendPacket(new ItemList(activeChar, true));
  451.  
  452. StatusUpdate su = new StatusUpdate(activeChar.getObjectId());
  453. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  454. activeChar.sendPacket(su);
  455.  
  456. activeChar.broadcastUserInfo();
  457.  
  458. L2World world = L2World.getInstance();
  459. world.removeObject(destroyItem);
  460. }
  461. else
  462. {
  463. item.setEnchantLevel(0);
  464. item.updateDatabase();
  465. }
  466. }
  467. sm = null;
  468.  
  469. StatusUpdate su = new StatusUpdate(activeChar.getObjectId());
  470. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  471. activeChar.sendPacket(su);
  472. su = null;
  473.  
  474. activeChar.sendPacket(new EnchantResult(item.getEnchantLevel())); //FIXME i'm really not sure about this...
  475. activeChar.sendPacket(new ItemList(activeChar, false)); //TODO update only the enchanted item
  476. activeChar.broadcastUserInfo();
  477. }
  478.  
  479. /* (non-Javadoc)
  480. * @see com.equal.gameserver.clientpackets.ClientBasePacket#getType()
  481. */
  482. @Override
  483. public String getType()
  484. {
  485. return _C__58_REQUESTENCHANTITEM;
  486. }
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement