RequestEnchantItem.java 12 KB

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