RequestExEnchantItemAttribute.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 com.l2jserver.Config;
  17. import com.l2jserver.gameserver.model.Elementals;
  18. import com.l2jserver.gameserver.model.L2ItemInstance;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  20. import com.l2jserver.gameserver.network.SystemMessageId;
  21. import com.l2jserver.gameserver.network.serverpackets.ExAttributeEnchantResult;
  22. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  23. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  24. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  25. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  26. import com.l2jserver.gameserver.templates.item.L2Item;
  27. import com.l2jserver.gameserver.templates.item.L2WeaponType;
  28. import com.l2jserver.gameserver.util.Util;
  29. import com.l2jserver.util.Rnd;
  30. public class RequestExEnchantItemAttribute extends L2GameClientPacket
  31. {
  32. private static final String D0_38_REQUEST_EX_ENCHANT_ITEM_ATTRIBUTE = "[C] D0 38 RequestExEnchantItemAttribute";
  33. private int _objectId;
  34. @Override
  35. protected void readImpl()
  36. {
  37. _objectId = readD();
  38. }
  39. @Override
  40. protected void runImpl()
  41. {
  42. L2PcInstance player = getClient().getActiveChar();
  43. if (player == null)
  44. return;
  45. if (_objectId == 0xFFFFFFFF)
  46. {
  47. // Player canceled enchant
  48. player.setActiveEnchantAttrItem(null);
  49. player.sendPacket(new SystemMessage(SystemMessageId.ELEMENTAL_ENHANCE_CANCELED));
  50. return;
  51. }
  52. if (player.isOnline() == 0)
  53. {
  54. player.setActiveEnchantAttrItem(null);
  55. return;
  56. }
  57. if (player.getPrivateStoreType() != 0)
  58. {
  59. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_ADD_ELEMENTAL_POWER_WHILE_OPERATING_PRIVATE_STORE_OR_WORKSHOP));
  60. return;
  61. }
  62. // Restrict enchant during a trade (bug if enchant fails)
  63. if (player.getActiveRequester() != null)
  64. {
  65. // Cancel trade
  66. player.cancelActiveTrade();
  67. player.sendMessage("Enchanting items is not allowed during a trade.");
  68. return;
  69. }
  70. L2ItemInstance item = player.getInventory().getItemByObjectId(_objectId);
  71. L2ItemInstance stone = player.getActiveEnchantAttrItem();
  72. if (item == null || stone == null)
  73. {
  74. player.setActiveEnchantAttrItem(null);
  75. return;
  76. }
  77. if ((item.getLocation() != L2ItemInstance.ItemLocation.INVENTORY) && (item.getLocation() != L2ItemInstance.ItemLocation.PAPERDOLL))
  78. return;
  79. if (item.isWear())
  80. {
  81. Util.handleIllegalPlayerAction(player, "Player "+player.getName()+" tried to enchant a weared Item", Config.DEFAULT_PUNISH);
  82. return;
  83. }
  84. //can't enchant rods, shadow items, adventurers', PvP items, hero items, cloaks, bracelets, underwear (e.g. shirt), belt, necklace, earring, ring
  85. if (item.getItem().getItemType() == L2WeaponType.ROD || item.isShadowItem() || item.isPvp() || item.isHeroItem() || item.isTimeLimitedItem() ||
  86. (item.getItemId() >= 7816 && item.getItemId() <= 7831) || (item.getItem().getItemType() == L2WeaponType.NONE) ||
  87. item.getItem().getItemGradeSPlus() != L2Item.CRYSTAL_S || item.getItem().getBodyPart() == L2Item.SLOT_BACK ||
  88. item.getItem().getBodyPart() == L2Item.SLOT_R_BRACELET || item.getItem().getBodyPart() == L2Item.SLOT_UNDERWEAR ||
  89. item.getItem().getBodyPart() == L2Item.SLOT_BELT || item.getItem().getBodyPart() == L2Item.SLOT_NECK ||
  90. item.getItem().getBodyPart() == L2Item.SLOT_R_EAR || item.getItem().getBodyPart() == L2Item.SLOT_R_FINGER)
  91. {
  92. player.sendPacket(new SystemMessage(SystemMessageId.ELEMENTAL_ENHANCE_REQUIREMENT_NOT_SUFFICIENT));
  93. player.setActiveEnchantAttrItem(null);
  94. return;
  95. }
  96. switch (item.getLocation())
  97. {
  98. case INVENTORY:
  99. case PAPERDOLL:
  100. {
  101. if (item.getOwnerId() != player.getObjectId())
  102. {
  103. player.setActiveEnchantAttrItem(null);
  104. return;
  105. }
  106. break;
  107. }
  108. default:
  109. {
  110. player.setActiveEnchantAttrItem(null);
  111. Util.handleIllegalPlayerAction(player, "Player "+player.getName()+" tried to use enchant Exploit!", Config.DEFAULT_PUNISH);
  112. return;
  113. }
  114. }
  115. int stoneId = stone.getItemId();
  116. Elementals oldElement = item.getElementals();
  117. int elementValue = oldElement == null ? 0 : oldElement.getValue();
  118. int limit = getLimit(stoneId, item);
  119. int powerToAdd = getPowerToAdd(stoneId, elementValue, item);
  120. byte elementToAdd = getElementFromItemId(stoneId);
  121. // Armors have the opposite element
  122. if (item.isArmor())
  123. elementToAdd = Elementals.getOppositeElement(elementToAdd);
  124. int newPower = elementValue + powerToAdd;
  125. if (newPower > limit)
  126. {
  127. newPower = limit;
  128. powerToAdd = limit - elementValue;
  129. }
  130. if (oldElement != null && oldElement.getElement() != elementToAdd && oldElement.getElement() != -2)
  131. {
  132. player.sendPacket(new SystemMessage(SystemMessageId.ANOTHER_ELEMENTAL_POWER_ALREADY_ADDED));
  133. player.setActiveEnchantAttrItem(null);
  134. return;
  135. }
  136. if (powerToAdd <= 0)
  137. {
  138. player.sendPacket(new SystemMessage(SystemMessageId.ELEMENTAL_ENHANCE_CANCELED));
  139. player.setActiveEnchantAttrItem(null);
  140. return;
  141. }
  142. if(!player.destroyItem("AttrEnchant", stone, 1, player, true))
  143. {
  144. player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
  145. Util.handleIllegalPlayerAction(player, "Player "+player.getName()+" tried to attribute enchant with a stone he doesn't have", Config.DEFAULT_PUNISH);
  146. player.setActiveEnchantAttrItem(null);
  147. return;
  148. }
  149. if (Rnd.get(100) <= Elementals.ENCHANT_CHANCE)
  150. {
  151. SystemMessage sm;
  152. if (item.getEnchantLevel() == 0)
  153. {
  154. sm = new SystemMessage(SystemMessageId.ELEMENTAL_POWER_S2_SUCCESSFULLY_ADDED_TO_S1);
  155. sm.addItemName(item);
  156. sm.addString(Elementals.getElementName(elementToAdd));
  157. }
  158. else
  159. {
  160. sm = new SystemMessage(SystemMessageId.ELEMENTAL_POWER_S3_SUCCESSFULLY_ADDED_TO_S1_S2);
  161. sm.addNumber(item.getEnchantLevel());
  162. sm.addItemName(item);
  163. sm.addString(Elementals.getElementName(elementToAdd));
  164. }
  165. player.sendPacket(sm);
  166. item.setElementAttr(elementToAdd, newPower);
  167. if (item.isEquipped())
  168. item.updateElementAttrBonus(player);
  169. // send packets
  170. InventoryUpdate iu = new InventoryUpdate();
  171. iu.addModifiedItem(item);
  172. player.sendPacket(iu);
  173. }
  174. else
  175. player.sendPacket(new SystemMessage(SystemMessageId.FAILED_ADDING_ELEMENTAL_POWER));
  176. player.sendPacket(new ExAttributeEnchantResult(powerToAdd));
  177. player.sendPacket(new UserInfo(player));
  178. player.sendPacket(new ExBrExtraUserInfo(player));
  179. player.setActiveEnchantAttrItem(null);
  180. }
  181. public byte getElementFromItemId(int itemId)
  182. {
  183. byte element = 0;
  184. for (int id : Elementals.STONES)
  185. {
  186. if (id == itemId)
  187. return element;
  188. element++;
  189. }
  190. element = 0;
  191. for (int id : Elementals.CRYSTALS)
  192. {
  193. if (id == itemId)
  194. return element;
  195. element++;
  196. }
  197. element = 0;
  198. for (int id : Elementals.JEWELS)
  199. {
  200. if (id == itemId)
  201. return element;
  202. element++;
  203. }
  204. element = 0;
  205. for (int id : Elementals.ENERGIES)
  206. {
  207. if (id == itemId)
  208. return element;
  209. element++;
  210. }
  211. element = 0;
  212. for (int id : Elementals.ROUGHORES)
  213. {
  214. if (id == itemId)
  215. return element;
  216. element++;
  217. }
  218. return -1;
  219. }
  220. public int getLimit(int itemId, L2ItemInstance item)
  221. {
  222. for (int id : Elementals.STONES)
  223. {
  224. if (id == itemId)
  225. {
  226. if (item.isWeapon())
  227. return Elementals.WEAPON_VALUES[3];
  228. return Elementals.ARMOR_VALUES[3];
  229. }
  230. }
  231. for (int id : Elementals.CRYSTALS)
  232. {
  233. if (id == itemId)
  234. {
  235. if (item.isWeapon())
  236. return Elementals.WEAPON_VALUES[6];
  237. return Elementals.ARMOR_VALUES[6];
  238. }
  239. }
  240. for (int id : Elementals.JEWELS)
  241. {
  242. if (id == itemId)
  243. {
  244. if (item.isWeapon())
  245. return Elementals.WEAPON_VALUES[9];
  246. return Elementals.ARMOR_VALUES[9];
  247. }
  248. }
  249. for (int id : Elementals.ENERGIES)
  250. {
  251. if (id == itemId)
  252. {
  253. if (item.isWeapon())
  254. return Elementals.WEAPON_VALUES[12];
  255. return Elementals.ARMOR_VALUES[12];
  256. }
  257. }
  258. for (int id : Elementals.ROUGHORES)
  259. {
  260. if (id == itemId)
  261. {
  262. if (item.isWeapon())
  263. return Elementals.WEAPON_VALUES[3];
  264. return Elementals.ARMOR_VALUES[3];
  265. }
  266. }
  267. return 0;
  268. }
  269. public int getPowerToAdd(int stoneId, int oldValue, L2ItemInstance item)
  270. {
  271. boolean found = false;
  272. for (int id : Elementals.STONES)
  273. {
  274. if (id == stoneId)
  275. {
  276. found = true;
  277. break;
  278. }
  279. }
  280. if (!found)
  281. {
  282. for (int id : Elementals.CRYSTALS)
  283. {
  284. if (id == stoneId)
  285. {
  286. found = true;
  287. break;
  288. }
  289. }
  290. if (!found)
  291. {
  292. for (int id : Elementals.JEWELS)
  293. {
  294. if (id == stoneId)
  295. {
  296. found = true;
  297. break;
  298. }
  299. }
  300. if (!found)
  301. {
  302. for (int id : Elementals.ENERGIES)
  303. {
  304. if (id == stoneId)
  305. {
  306. found = true;
  307. break;
  308. }
  309. }
  310. if (!found)
  311. {
  312. for (int id : Elementals.ROUGHORES)
  313. {
  314. if (id == stoneId)
  315. {
  316. found = true;
  317. break;
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. if (found)
  325. {
  326. if (item.isWeapon())
  327. {
  328. if (oldValue == 0)
  329. return Elementals.FIRST_WEAPON_BONUS;
  330. else
  331. return Elementals.NEXT_WEAPON_BONUS;
  332. }
  333. else if (item.isArmor())
  334. return Elementals.ARMOR_BONUS;
  335. }
  336. return 0;
  337. }
  338. @Override
  339. public String getType()
  340. {
  341. return D0_38_REQUEST_EX_ENCHANT_ITEM_ATTRIBUTE;
  342. }
  343. }