RequestEnchantItem.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.clientpackets;
  20. import java.util.logging.Level;
  21. import java.util.logging.LogRecord;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.data.xml.impl.EnchantItemData;
  25. import com.l2jserver.gameserver.model.L2World;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.model.items.L2Item;
  28. import com.l2jserver.gameserver.model.items.enchant.EnchantResultType;
  29. import com.l2jserver.gameserver.model.items.enchant.EnchantScroll;
  30. import com.l2jserver.gameserver.model.items.enchant.EnchantSupportItem;
  31. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  32. import com.l2jserver.gameserver.model.skills.CommonSkill;
  33. import com.l2jserver.gameserver.model.skills.Skill;
  34. import com.l2jserver.gameserver.network.SystemMessageId;
  35. import com.l2jserver.gameserver.network.serverpackets.EnchantResult;
  36. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  37. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  38. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  39. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  40. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  41. import com.l2jserver.gameserver.util.Util;
  42. public final class RequestEnchantItem extends L2GameClientPacket
  43. {
  44. protected static final Logger _logEnchant = Logger.getLogger("enchant");
  45. private static final String _C__5F_REQUESTENCHANTITEM = "[C] 5F RequestEnchantItem";
  46. private int _objectId;
  47. private int _supportId;
  48. @Override
  49. protected void readImpl()
  50. {
  51. _objectId = readD();
  52. _supportId = readD();
  53. }
  54. @Override
  55. protected void runImpl()
  56. {
  57. final L2PcInstance activeChar = getClient().getActiveChar();
  58. if ((activeChar == null) || (_objectId == 0))
  59. {
  60. return;
  61. }
  62. if (!activeChar.isOnline() || getClient().isDetached())
  63. {
  64. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  65. return;
  66. }
  67. if (activeChar.isProcessingTransaction() || activeChar.isInStoreMode())
  68. {
  69. activeChar.sendPacket(SystemMessageId.CANNOT_ENCHANT_WHILE_STORE);
  70. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  71. return;
  72. }
  73. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_objectId);
  74. L2ItemInstance scroll = activeChar.getInventory().getItemByObjectId(activeChar.getActiveEnchantItemId());
  75. L2ItemInstance support = activeChar.getInventory().getItemByObjectId(activeChar.getActiveEnchantSupportItemId());
  76. if ((item == null) || (scroll == null))
  77. {
  78. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  79. return;
  80. }
  81. // template for scroll
  82. final EnchantScroll scrollTemplate = EnchantItemData.getInstance().getEnchantScroll(scroll);
  83. // scroll not found in list
  84. if (scrollTemplate == null)
  85. {
  86. return;
  87. }
  88. // template for support item, if exist
  89. EnchantSupportItem supportTemplate = null;
  90. if (support != null)
  91. {
  92. if (support.getObjectId() != _supportId)
  93. {
  94. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  95. return;
  96. }
  97. supportTemplate = EnchantItemData.getInstance().getSupportItem(support);
  98. }
  99. // first validation check
  100. if (!scrollTemplate.isValid(item, supportTemplate))
  101. {
  102. activeChar.sendPacket(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION);
  103. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  104. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  105. return;
  106. }
  107. // fast auto-enchant cheat check
  108. if ((activeChar.getActiveEnchantTimestamp() == 0) || ((System.currentTimeMillis() - activeChar.getActiveEnchantTimestamp()) < 2000))
  109. {
  110. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " use autoenchant program ", Config.DEFAULT_PUNISH);
  111. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  112. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  113. return;
  114. }
  115. // attempting to destroy scroll
  116. scroll = activeChar.getInventory().destroyItem("Enchant", scroll.getObjectId(), 1, activeChar, item);
  117. if (scroll == null)
  118. {
  119. activeChar.sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
  120. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to enchant with a scroll he doesn't have", Config.DEFAULT_PUNISH);
  121. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  122. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  123. return;
  124. }
  125. // attempting to destroy support if exist
  126. if (support != null)
  127. {
  128. support = activeChar.getInventory().destroyItem("Enchant", support.getObjectId(), 1, activeChar, item);
  129. if (support == null)
  130. {
  131. activeChar.sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
  132. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to enchant with a support item he doesn't have", Config.DEFAULT_PUNISH);
  133. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  134. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  135. return;
  136. }
  137. }
  138. final InventoryUpdate iu = new InventoryUpdate();
  139. synchronized (item)
  140. {
  141. // last validation check
  142. if ((item.getOwnerId() != activeChar.getObjectId()) || (item.isEnchantable() == 0))
  143. {
  144. activeChar.sendPacket(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION);
  145. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  146. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  147. return;
  148. }
  149. final EnchantResultType resultType = scrollTemplate.calculateSuccess(activeChar, item, supportTemplate);
  150. switch (resultType)
  151. {
  152. case ERROR:
  153. {
  154. activeChar.sendPacket(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION);
  155. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  156. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  157. break;
  158. }
  159. case SUCCESS:
  160. {
  161. Skill enchant4Skill = null;
  162. L2Item it = item.getItem();
  163. // Increase enchant level only if scroll's base template has chance, some armors can success over +20 but they shouldn't have increased.
  164. if (scrollTemplate.getChance(activeChar, item) > 0)
  165. {
  166. item.setEnchantLevel(item.getEnchantLevel() + 1);
  167. item.updateDatabase();
  168. }
  169. activeChar.sendPacket(new EnchantResult(0, 0, 0));
  170. if (Config.LOG_ITEM_ENCHANTS)
  171. {
  172. LogRecord record = new LogRecord(Level.INFO, "Success");
  173. record.setParameters(new Object[]
  174. {
  175. activeChar,
  176. item,
  177. scroll,
  178. support,
  179. });
  180. record.setLoggerName("item");
  181. _logEnchant.log(record);
  182. }
  183. // announce the success
  184. int minEnchantAnnounce = item.isArmor() ? 6 : 7;
  185. int maxEnchantAnnounce = item.isArmor() ? 0 : 15;
  186. if ((item.getEnchantLevel() == minEnchantAnnounce) || (item.getEnchantLevel() == maxEnchantAnnounce))
  187. {
  188. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_SUCCESSFULY_ENCHANTED_A_S2_S3);
  189. sm.addCharName(activeChar);
  190. sm.addInt(item.getEnchantLevel());
  191. sm.addItemName(item);
  192. activeChar.broadcastPacket(sm);
  193. Skill skill = CommonSkill.FIREWORK.getSkill();
  194. if (skill != null)
  195. {
  196. activeChar.broadcastPacket(new MagicSkillUse(activeChar, activeChar, skill.getId(), skill.getLevel(), skill.getHitTime(), skill.getReuseDelay()));
  197. }
  198. }
  199. if ((item.isArmor()) && (item.getEnchantLevel() == 4) && item.isEquipped())
  200. {
  201. enchant4Skill = it.getEnchant4Skill();
  202. if (enchant4Skill != null)
  203. {
  204. // add skills bestowed from +4 armor
  205. activeChar.addSkill(enchant4Skill, false);
  206. activeChar.sendSkillList();
  207. }
  208. }
  209. break;
  210. }
  211. case FAILURE:
  212. {
  213. if (scrollTemplate.isSafe())
  214. {
  215. // safe enchant - remain old value
  216. activeChar.sendPacket(SystemMessageId.SAFE_ENCHANT_FAILED);
  217. activeChar.sendPacket(new EnchantResult(5, 0, 0));
  218. if (Config.LOG_ITEM_ENCHANTS)
  219. {
  220. LogRecord record = new LogRecord(Level.INFO, "Safe Fail");
  221. record.setParameters(new Object[]
  222. {
  223. activeChar,
  224. item,
  225. scroll,
  226. support,
  227. });
  228. record.setLoggerName("item");
  229. _logEnchant.log(record);
  230. }
  231. }
  232. else
  233. {
  234. // unequip item on enchant failure to avoid item skills stack
  235. if (item.isEquipped())
  236. {
  237. if (item.getEnchantLevel() > 0)
  238. {
  239. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  240. sm.addInt(item.getEnchantLevel());
  241. sm.addItemName(item);
  242. activeChar.sendPacket(sm);
  243. }
  244. else
  245. {
  246. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DISARMED);
  247. sm.addItemName(item);
  248. activeChar.sendPacket(sm);
  249. }
  250. L2ItemInstance[] unequiped = activeChar.getInventory().unEquipItemInSlotAndRecord(item.getLocationSlot());
  251. for (L2ItemInstance itm : unequiped)
  252. {
  253. iu.addModifiedItem(itm);
  254. }
  255. activeChar.sendPacket(iu);
  256. activeChar.broadcastUserInfo();
  257. }
  258. if (scrollTemplate.isBlessed())
  259. {
  260. // blessed enchant - clear enchant value
  261. activeChar.sendPacket(SystemMessageId.BLESSED_ENCHANT_FAILED);
  262. item.setEnchantLevel(0);
  263. item.updateDatabase();
  264. activeChar.sendPacket(new EnchantResult(3, 0, 0));
  265. if (Config.LOG_ITEM_ENCHANTS)
  266. {
  267. LogRecord record = new LogRecord(Level.INFO, "Blessed Fail");
  268. record.setParameters(new Object[]
  269. {
  270. activeChar,
  271. item,
  272. scroll,
  273. support,
  274. });
  275. record.setLoggerName("item");
  276. _logEnchant.log(record);
  277. }
  278. }
  279. else
  280. {
  281. // enchant failed, destroy item
  282. int crystalId = item.getItem().getCrystalItemId();
  283. int count = item.getCrystalCount() - ((item.getItem().getCrystalCount() + 1) / 2);
  284. if (count < 1)
  285. {
  286. count = 1;
  287. }
  288. item = activeChar.getInventory().destroyItem("Enchant", item, activeChar, null);
  289. if (item == null)
  290. {
  291. // unable to destroy item, cheater ?
  292. Util.handleIllegalPlayerAction(activeChar, "Unable to delete item on enchant failure from player " + activeChar.getName() + ", possible cheater !", Config.DEFAULT_PUNISH);
  293. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  294. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  295. if (Config.LOG_ITEM_ENCHANTS)
  296. {
  297. LogRecord record = new LogRecord(Level.INFO, "Unable to destroy");
  298. record.setParameters(new Object[]
  299. {
  300. activeChar,
  301. item,
  302. scroll,
  303. support,
  304. });
  305. record.setLoggerName("item");
  306. _logEnchant.log(record);
  307. }
  308. return;
  309. }
  310. L2World.getInstance().removeObject(item);
  311. L2ItemInstance crystals = null;
  312. if (crystalId != 0)
  313. {
  314. crystals = activeChar.getInventory().addItem("Enchant", crystalId, count, activeChar, item);
  315. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  316. sm.addItemName(crystals);
  317. sm.addLong(count);
  318. activeChar.sendPacket(sm);
  319. }
  320. if (crystalId == 0)
  321. {
  322. activeChar.sendPacket(new EnchantResult(4, 0, 0));
  323. }
  324. else
  325. {
  326. activeChar.sendPacket(new EnchantResult(1, crystalId, count));
  327. }
  328. if (Config.LOG_ITEM_ENCHANTS)
  329. {
  330. LogRecord record = new LogRecord(Level.INFO, "Fail");
  331. record.setParameters(new Object[]
  332. {
  333. activeChar,
  334. item,
  335. scroll,
  336. support,
  337. });
  338. record.setLoggerName("item");
  339. _logEnchant.log(record);
  340. }
  341. }
  342. }
  343. break;
  344. }
  345. }
  346. final StatusUpdate su = new StatusUpdate(activeChar);
  347. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  348. activeChar.sendPacket(su);
  349. if (!Config.FORCE_INVENTORY_UPDATE)
  350. {
  351. if (scroll.getCount() == 0)
  352. {
  353. iu.addRemovedItem(scroll);
  354. }
  355. else
  356. {
  357. iu.addModifiedItem(scroll);
  358. }
  359. if (item.getCount() == 0)
  360. {
  361. iu.addRemovedItem(item);
  362. }
  363. else
  364. {
  365. iu.addModifiedItem(item);
  366. }
  367. if (support != null)
  368. {
  369. if (support.getCount() == 0)
  370. {
  371. iu.addRemovedItem(support);
  372. }
  373. else
  374. {
  375. iu.addModifiedItem(support);
  376. }
  377. }
  378. activeChar.sendPacket(iu);
  379. }
  380. else
  381. {
  382. activeChar.sendPacket(new ItemList(activeChar, true));
  383. }
  384. activeChar.broadcastUserInfo();
  385. activeChar.setActiveEnchantItemId(L2PcInstance.ID_NONE);
  386. }
  387. }
  388. @Override
  389. public String getType()
  390. {
  391. return _C__5F_REQUESTENCHANTITEM;
  392. }
  393. }