Guest User

Untitled

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