RequestEnchantItem.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 com.l2jserver.gameserver.network.clientpackets;
  16. import java.util.logging.Level;
  17. import java.util.logging.LogRecord;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.datatables.SkillTable;
  21. import com.l2jserver.gameserver.model.L2ItemInstance;
  22. import com.l2jserver.gameserver.model.L2Skill;
  23. import com.l2jserver.gameserver.model.L2World;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.network.SystemMessageId;
  26. import com.l2jserver.gameserver.network.serverpackets.EnchantResult;
  27. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  28. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  29. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  30. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  31. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  32. import com.l2jserver.gameserver.templates.item.L2Armor;
  33. import com.l2jserver.gameserver.templates.item.L2Item;
  34. import com.l2jserver.gameserver.util.Util;
  35. import com.l2jserver.util.Rnd;
  36. public final class RequestEnchantItem extends AbstractEnchantPacket
  37. {
  38. protected static final Logger _log = Logger.getLogger(RequestEnchantItem.class.getName());
  39. protected static final Logger _logEnchant = Logger.getLogger("enchant");
  40. private static final String _C__58_REQUESTENCHANTITEM = "[C] 58 RequestEnchantItem";
  41. private int _objectId = 0;
  42. private int _supportId;
  43. @Override
  44. protected void readImpl()
  45. {
  46. _objectId = readD();
  47. _supportId = readD();
  48. }
  49. @Override
  50. protected void runImpl()
  51. {
  52. L2PcInstance activeChar = getClient().getActiveChar();
  53. if (activeChar == null || _objectId == 0)
  54. return;
  55. if (!activeChar.isOnline() || getClient().isDetached())
  56. {
  57. activeChar.setActiveEnchantItem(null);
  58. return;
  59. }
  60. if (activeChar.isProcessingTransaction() || activeChar.isInStoreMode())
  61. {
  62. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.CANNOT_ENCHANT_WHILE_STORE));
  63. activeChar.setActiveEnchantItem(null);
  64. return;
  65. }
  66. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_objectId);
  67. L2ItemInstance scroll = activeChar.getActiveEnchantItem();
  68. L2ItemInstance support = activeChar.getActiveEnchantSupportItem();
  69. if (item == null || scroll == null)
  70. {
  71. activeChar.setActiveEnchantItem(null);
  72. return;
  73. }
  74. // template for scroll
  75. EnchantScroll scrollTemplate = getEnchantScroll(scroll);
  76. // scroll not found in list
  77. if (scrollTemplate == null)
  78. return;
  79. // template for support item, if exist
  80. EnchantItem supportTemplate = null;
  81. if (support != null)
  82. {
  83. if (support.getObjectId() != _supportId)
  84. {
  85. activeChar.setActiveEnchantItem(null);
  86. return;
  87. }
  88. supportTemplate = getSupportItem(support);
  89. }
  90. // first validation check
  91. if (!scrollTemplate.isValid(item, supportTemplate) || !isEnchantable(item))
  92. {
  93. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  94. activeChar.setActiveEnchantItem(null);
  95. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  96. return;
  97. }
  98. // fast auto-enchant cheat check
  99. if (activeChar.getActiveEnchantTimestamp() == 0 || System.currentTimeMillis() - activeChar.getActiveEnchantTimestamp() < 2000)
  100. {
  101. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " use autoenchant program ", Config.DEFAULT_PUNISH);
  102. activeChar.setActiveEnchantItem(null);
  103. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  104. return;
  105. }
  106. // attempting to destroy scroll
  107. scroll = activeChar.getInventory().destroyItem("Enchant", scroll.getObjectId(), 1, activeChar, item);
  108. if (scroll == null)
  109. {
  110. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
  111. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to enchant with a scroll he doesn't have", Config.DEFAULT_PUNISH);
  112. activeChar.setActiveEnchantItem(null);
  113. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  114. return;
  115. }
  116. // attempting to destroy support if exist
  117. if (support != null)
  118. {
  119. support = activeChar.getInventory().destroyItem("Enchant", support.getObjectId(), 1, activeChar, item);
  120. if (support == null)
  121. {
  122. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
  123. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to enchant with a support item he doesn't have", Config.DEFAULT_PUNISH);
  124. activeChar.setActiveEnchantItem(null);
  125. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  126. return;
  127. }
  128. }
  129. synchronized (item)
  130. {
  131. int chance = scrollTemplate.getChance(item, supportTemplate);
  132. L2Skill enchant4Skill = null;
  133. L2Item it = item.getItem();
  134. // last validation check
  135. if (item.getOwnerId() != activeChar.getObjectId()
  136. || !isEnchantable(item)
  137. || chance < 0)
  138. {
  139. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.INAPPROPRIATE_ENCHANT_CONDITION));
  140. activeChar.setActiveEnchantItem(null);
  141. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  142. return;
  143. }
  144. if (Rnd.get(100) < chance)
  145. {
  146. // success
  147. item.setEnchantLevel(item.getEnchantLevel() + 1);
  148. item.updateDatabase();
  149. activeChar.sendPacket(new EnchantResult(0, 0, 0));
  150. if (Config.LOG_ITEM_ENCHANTS)
  151. {
  152. LogRecord record = new LogRecord(Level.INFO, "Success");
  153. record.setParameters(new Object[]{activeChar, item, scroll, support, chance});
  154. record.setLoggerName("item");
  155. _logEnchant.log(record);
  156. }
  157. // announce the success
  158. int minEnchantAnnounce = item.isArmor() ? 6 : 7;
  159. int maxEnchantAnnounce = item.isArmor() ? 0 : 15;
  160. if (item.getEnchantLevel() == minEnchantAnnounce || item.getEnchantLevel() == maxEnchantAnnounce)
  161. {
  162. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_SUCCESSFULY_ENCHANTED_A_S2_S3);
  163. sm.addCharName(activeChar);
  164. sm.addNumber(item.getEnchantLevel());
  165. sm.addItemName(item);
  166. activeChar.broadcastPacket(sm);
  167. L2Skill skill = SkillTable.FrequentSkill.FIREWORK.getSkill();
  168. if (skill != null)
  169. activeChar.broadcastPacket(new MagicSkillUse(activeChar, activeChar, skill.getId(), skill.getLevel(), skill.getHitTime(), skill.getReuseDelay()));
  170. }
  171. if (it instanceof L2Armor && item.getEnchantLevel() == 4 && activeChar.getInventory().getItemByObjectId(item.getObjectId()).isEquipped())
  172. {
  173. enchant4Skill = ((L2Armor)it).getEnchant4Skill();
  174. if (enchant4Skill != null)
  175. {
  176. // add skills bestowed from +4 armor
  177. activeChar.addSkill(enchant4Skill, false);
  178. activeChar.sendSkillList();
  179. }
  180. }
  181. }
  182. else
  183. {
  184. if (scrollTemplate.isSafe())
  185. {
  186. // safe enchant - remain old value
  187. // need retail message
  188. activeChar.sendPacket(new EnchantResult(5, 0, 0));
  189. if (Config.LOG_ITEM_ENCHANTS)
  190. {
  191. LogRecord record = new LogRecord(Level.INFO, "Safe Fail");
  192. record.setParameters(new Object[]{activeChar, item, scroll, support, chance});
  193. record.setLoggerName("item");
  194. _logEnchant.log(record);
  195. }
  196. }
  197. else
  198. {
  199. // unequip item on enchant failure to avoid item skills stack
  200. if (item.isEquipped())
  201. {
  202. if (item.getEnchantLevel() > 0)
  203. {
  204. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  205. sm.addNumber(item.getEnchantLevel());
  206. sm.addItemName(item);
  207. activeChar.sendPacket(sm);
  208. }
  209. else
  210. {
  211. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DISARMED);
  212. sm.addItemName(item);
  213. activeChar.sendPacket(sm);
  214. }
  215. L2ItemInstance[] unequiped = activeChar.getInventory().unEquipItemInSlotAndRecord(item.getLocationSlot());
  216. InventoryUpdate iu = new InventoryUpdate();
  217. for (L2ItemInstance itm : unequiped)
  218. iu.addModifiedItem(itm);
  219. activeChar.sendPacket(iu);
  220. activeChar.broadcastUserInfo();
  221. }
  222. if (scrollTemplate.isBlessed())
  223. {
  224. // blessed enchant - clear enchant value
  225. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.BLESSED_ENCHANT_FAILED));
  226. item.setEnchantLevel(0);
  227. item.updateDatabase();
  228. activeChar.sendPacket(new EnchantResult(3, 0, 0));
  229. if (Config.LOG_ITEM_ENCHANTS)
  230. {
  231. LogRecord record = new LogRecord(Level.INFO, "Blessed Fail");
  232. record.setParameters(new Object[]{activeChar, item, scroll, support, chance});
  233. record.setLoggerName("item");
  234. _logEnchant.log(record);
  235. }
  236. }
  237. else
  238. {
  239. // enchant failed, destroy item
  240. int crystalId = item.getItem().getCrystalItemId();
  241. int count = item.getCrystalCount() - (item.getItem().getCrystalCount() + 1) / 2;
  242. if (count < 1)
  243. count = 1;
  244. L2ItemInstance destroyItem = activeChar.getInventory().destroyItem("Enchant", item, activeChar, null);
  245. if (destroyItem == null)
  246. {
  247. // unable to destroy item, cheater ?
  248. Util.handleIllegalPlayerAction(activeChar, "Unable to delete item on enchant failure from player " + activeChar.getName() + ", possible cheater !", Config.DEFAULT_PUNISH);
  249. activeChar.setActiveEnchantItem(null);
  250. activeChar.sendPacket(new EnchantResult(2, 0, 0));
  251. if (Config.LOG_ITEM_ENCHANTS)
  252. {
  253. LogRecord record = new LogRecord(Level.INFO, "Unable to destroy");
  254. record.setParameters(new Object[]{activeChar, item, scroll, support, chance});
  255. record.setLoggerName("item");
  256. _logEnchant.log(record);
  257. }
  258. return;
  259. }
  260. L2ItemInstance crystals = null;
  261. if (crystalId != 0)
  262. {
  263. crystals = activeChar.getInventory().addItem("Enchant", crystalId, count, activeChar, destroyItem);
  264. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  265. sm.addItemName(crystals);
  266. sm.addItemNumber(count);
  267. activeChar.sendPacket(sm);
  268. }
  269. if (!Config.FORCE_INVENTORY_UPDATE)
  270. {
  271. InventoryUpdate iu = new InventoryUpdate();
  272. if (destroyItem.getCount() == 0)
  273. iu.addRemovedItem(destroyItem);
  274. else
  275. iu.addModifiedItem(destroyItem);
  276. if (crystals != null)
  277. iu.addItem(crystals);
  278. activeChar.sendPacket(iu);
  279. }
  280. else
  281. activeChar.sendPacket(new ItemList(activeChar, true));
  282. L2World world = L2World.getInstance();
  283. world.removeObject(destroyItem);
  284. if (crystalId == 0)
  285. activeChar.sendPacket(new EnchantResult(4, 0, 0));
  286. else
  287. activeChar.sendPacket(new EnchantResult(1, crystalId, count));
  288. if (Config.LOG_ITEM_ENCHANTS)
  289. {
  290. LogRecord record = new LogRecord(Level.INFO, "Fail");
  291. record.setParameters(new Object[]{activeChar, item, scroll, support, chance});
  292. record.setLoggerName("item");
  293. _logEnchant.log(record);
  294. }
  295. }
  296. }
  297. }
  298. StatusUpdate su = new StatusUpdate(activeChar);
  299. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  300. activeChar.sendPacket(su);
  301. activeChar.sendPacket(new ItemList(activeChar, false));
  302. activeChar.broadcastUserInfo();
  303. activeChar.setActiveEnchantItem(null);
  304. }
  305. }
  306. /*
  307. * (non-Javadoc)
  308. *
  309. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  310. */
  311. @Override
  312. public String getType()
  313. {
  314. return _C__58_REQUESTENCHANTITEM;
  315. }
  316. }