RequestExEnchantItemAttribute.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_35_REQUEST_EX_ENCHANT_ITEM_ATTRIBUTE = "[C] D0:35 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. item.getItem().getElementals() != null)
  92. {
  93. player.sendPacket(new SystemMessage(SystemMessageId.ELEMENTAL_ENHANCE_REQUIREMENT_NOT_SUFFICIENT));
  94. player.setActiveEnchantAttrItem(null);
  95. return;
  96. }
  97. switch (item.getLocation())
  98. {
  99. case INVENTORY:
  100. case PAPERDOLL:
  101. {
  102. if (item.getOwnerId() != player.getObjectId())
  103. {
  104. player.setActiveEnchantAttrItem(null);
  105. return;
  106. }
  107. break;
  108. }
  109. default:
  110. {
  111. player.setActiveEnchantAttrItem(null);
  112. Util.handleIllegalPlayerAction(player, "Player "+player.getName()+" tried to use enchant Exploit!", Config.DEFAULT_PUNISH);
  113. return;
  114. }
  115. }
  116. int stoneId = stone.getItemId();
  117. Elementals oldElement = item.getElementals();
  118. int elementValue = oldElement == null ? 0 : oldElement.getValue();
  119. int limit = getLimit(stoneId, item);
  120. int powerToAdd = getPowerToAdd(stoneId, elementValue, item);
  121. byte elementToAdd = getElementFromItemId(stoneId);
  122. // Armors have the opposite element
  123. if (item.isArmor())
  124. elementToAdd = Elementals.getOppositeElement(elementToAdd);
  125. int newPower = elementValue + powerToAdd;
  126. if (newPower > limit)
  127. {
  128. newPower = limit;
  129. powerToAdd = limit - elementValue;
  130. }
  131. if (oldElement != null && oldElement.getElement() != elementToAdd && oldElement.getElement() != -2)
  132. {
  133. player.sendPacket(new SystemMessage(SystemMessageId.ANOTHER_ELEMENTAL_POWER_ALREADY_ADDED));
  134. player.setActiveEnchantAttrItem(null);
  135. return;
  136. }
  137. if (powerToAdd <= 0)
  138. {
  139. player.sendPacket(new SystemMessage(SystemMessageId.ELEMENTAL_ENHANCE_CANCELED));
  140. player.setActiveEnchantAttrItem(null);
  141. return;
  142. }
  143. if(!player.destroyItem("AttrEnchant", stone, 1, player, true))
  144. {
  145. player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
  146. Util.handleIllegalPlayerAction(player, "Player "+player.getName()+" tried to attribute enchant with a stone he doesn't have", Config.DEFAULT_PUNISH);
  147. player.setActiveEnchantAttrItem(null);
  148. return;
  149. }
  150. if (Rnd.get(100) <= Elementals.ENCHANT_CHANCE)
  151. {
  152. SystemMessage sm;
  153. if (item.getEnchantLevel() == 0)
  154. {
  155. sm = new SystemMessage(SystemMessageId.ELEMENTAL_POWER_S2_SUCCESSFULLY_ADDED_TO_S1);
  156. sm.addItemName(item);
  157. sm.addString(Elementals.getElementName(elementToAdd));
  158. }
  159. else
  160. {
  161. sm = new SystemMessage(SystemMessageId.ELEMENTAL_POWER_S3_SUCCESSFULLY_ADDED_TO_S1_S2);
  162. sm.addNumber(item.getEnchantLevel());
  163. sm.addItemName(item);
  164. sm.addString(Elementals.getElementName(elementToAdd));
  165. }
  166. player.sendPacket(sm);
  167. item.setElementAttr(elementToAdd, newPower);
  168. if (item.isEquipped())
  169. item.updateElementAttrBonus(player);
  170. // send packets
  171. InventoryUpdate iu = new InventoryUpdate();
  172. iu.addModifiedItem(item);
  173. player.sendPacket(iu);
  174. }
  175. else
  176. player.sendPacket(new SystemMessage(SystemMessageId.FAILED_ADDING_ELEMENTAL_POWER));
  177. player.sendPacket(new ExAttributeEnchantResult(powerToAdd));
  178. player.sendPacket(new UserInfo(player));
  179. player.sendPacket(new ExBrExtraUserInfo(player));
  180. player.setActiveEnchantAttrItem(null);
  181. }
  182. public byte getElementFromItemId(int itemId)
  183. {
  184. byte element = 0;
  185. for (int id : Elementals.STONES)
  186. {
  187. if (id == itemId)
  188. return element;
  189. element++;
  190. }
  191. element = 0;
  192. for (int id : Elementals.CRYSTALS)
  193. {
  194. if (id == itemId)
  195. return element;
  196. element++;
  197. }
  198. element = 0;
  199. for (int id : Elementals.JEWELS)
  200. {
  201. if (id == itemId)
  202. return element;
  203. element++;
  204. }
  205. element = 0;
  206. for (int id : Elementals.ENERGIES)
  207. {
  208. if (id == itemId)
  209. return element;
  210. element++;
  211. }
  212. element = 0;
  213. for (int id : Elementals.ROUGHORES)
  214. {
  215. if (id == itemId)
  216. return element;
  217. element++;
  218. }
  219. return -1;
  220. }
  221. public int getLimit(int itemId, L2ItemInstance item)
  222. {
  223. for (int id : Elementals.STONES)
  224. {
  225. if (id == itemId)
  226. {
  227. if (item.isWeapon())
  228. return Elementals.WEAPON_VALUES[3];
  229. return Elementals.ARMOR_VALUES[3];
  230. }
  231. }
  232. for (int id : Elementals.CRYSTALS)
  233. {
  234. if (id == itemId)
  235. {
  236. if (item.isWeapon())
  237. return Elementals.WEAPON_VALUES[6];
  238. return Elementals.ARMOR_VALUES[6];
  239. }
  240. }
  241. for (int id : Elementals.JEWELS)
  242. {
  243. if (id == itemId)
  244. {
  245. if (item.isWeapon())
  246. return Elementals.WEAPON_VALUES[9];
  247. return Elementals.ARMOR_VALUES[9];
  248. }
  249. }
  250. for (int id : Elementals.ENERGIES)
  251. {
  252. if (id == itemId)
  253. {
  254. if (item.isWeapon())
  255. return Elementals.WEAPON_VALUES[12];
  256. return Elementals.ARMOR_VALUES[12];
  257. }
  258. }
  259. for (int id : Elementals.ROUGHORES)
  260. {
  261. if (id == itemId)
  262. {
  263. if (item.isWeapon())
  264. return Elementals.WEAPON_VALUES[3];
  265. return Elementals.ARMOR_VALUES[3];
  266. }
  267. }
  268. return 0;
  269. }
  270. public int getPowerToAdd(int stoneId, int oldValue, L2ItemInstance item)
  271. {
  272. boolean found = false;
  273. for (int id : Elementals.STONES)
  274. {
  275. if (id == stoneId)
  276. {
  277. found = true;
  278. break;
  279. }
  280. }
  281. if (!found)
  282. {
  283. for (int id : Elementals.CRYSTALS)
  284. {
  285. if (id == stoneId)
  286. {
  287. found = true;
  288. break;
  289. }
  290. }
  291. if (!found)
  292. {
  293. for (int id : Elementals.JEWELS)
  294. {
  295. if (id == stoneId)
  296. {
  297. found = true;
  298. break;
  299. }
  300. }
  301. if (!found)
  302. {
  303. for (int id : Elementals.ENERGIES)
  304. {
  305. if (id == stoneId)
  306. {
  307. found = true;
  308. break;
  309. }
  310. }
  311. if (!found)
  312. {
  313. for (int id : Elementals.ROUGHORES)
  314. {
  315. if (id == stoneId)
  316. {
  317. found = true;
  318. break;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. if (found)
  326. {
  327. if (item.isWeapon())
  328. {
  329. if (oldValue == 0)
  330. return Elementals.FIRST_WEAPON_BONUS;
  331. else
  332. return Elementals.NEXT_WEAPON_BONUS;
  333. }
  334. else if (item.isArmor())
  335. return Elementals.ARMOR_BONUS;
  336. }
  337. return 0;
  338. }
  339. @Override
  340. public String getType()
  341. {
  342. return D0_35_REQUEST_EX_ENCHANT_ITEM_ATTRIBUTE;
  343. }
  344. }