UseItem.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 net.sf.l2j.gameserver.clientpackets;
  16. import java.util.Arrays;
  17. import java.util.logging.Logger;
  18. import net.sf.l2j.Config;
  19. import net.sf.l2j.gameserver.handler.IItemHandler;
  20. import net.sf.l2j.gameserver.handler.ItemHandler;
  21. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  22. import net.sf.l2j.gameserver.model.Inventory;
  23. import net.sf.l2j.gameserver.model.L2Clan;
  24. import net.sf.l2j.gameserver.model.L2ItemInstance;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26. import net.sf.l2j.gameserver.model.base.Race;
  27. import net.sf.l2j.gameserver.network.SystemMessageId;
  28. import net.sf.l2j.gameserver.serverpackets.ActionFailed;
  29. import net.sf.l2j.gameserver.serverpackets.InventoryUpdate;
  30. import net.sf.l2j.gameserver.serverpackets.ItemList;
  31. import net.sf.l2j.gameserver.serverpackets.ShowCalculator;
  32. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  33. import net.sf.l2j.gameserver.templates.L2ArmorType;
  34. import net.sf.l2j.gameserver.templates.L2Item;
  35. import net.sf.l2j.gameserver.templates.L2Weapon;
  36. import net.sf.l2j.gameserver.templates.L2WeaponType;
  37. import net.sf.l2j.gameserver.util.FloodProtector;
  38. /**
  39. * This class ...
  40. *
  41. * @version $Revision: 1.18.2.7.2.9 $ $Date: 2005/03/27 15:29:30 $
  42. */
  43. public final class UseItem extends L2GameClientPacket
  44. {
  45. private static Logger _log = Logger.getLogger(UseItem.class.getName());
  46. private static final String _C__14_USEITEM = "[C] 14 UseItem";
  47. private int _objectId;
  48. @Override
  49. protected void readImpl()
  50. {
  51. _objectId = readD();
  52. }
  53. @Override
  54. protected void runImpl()
  55. {
  56. L2PcInstance activeChar = getClient().getActiveChar();
  57. if (activeChar == null)
  58. return;
  59. // Flood protect UseItem
  60. if (!FloodProtector.getInstance().tryPerformAction(activeChar.getObjectId(), FloodProtector.PROTECTED_USEITEM))
  61. return;
  62. if (activeChar.getPrivateStoreType() != 0)
  63. {
  64. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_TRADE_DISCARD_DROP_ITEM_WHILE_IN_SHOPMODE));
  65. activeChar.sendPacket(new ActionFailed());
  66. return;
  67. }
  68. // NOTE: disabled due to deadlocks
  69. // synchronized (activeChar.getInventory())
  70. // {
  71. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_objectId);
  72. if (item == null)
  73. return;
  74. if (item.isWear())
  75. {
  76. // No unequipping wear-items
  77. return;
  78. }
  79. if (item.getItem().getType2() == L2Item.TYPE2_QUEST)
  80. {
  81. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_USE_QUEST_ITEMS);
  82. activeChar.sendPacket(sm);
  83. sm = null;
  84. return;
  85. }
  86. int itemId = item.getItemId();
  87. /*
  88. * Alt game - Karma punishment // SOE
  89. * 736 Scroll of Escape
  90. * 1538 Blessed Scroll of Escape
  91. * 1829 Scroll of Escape: Clan Hall
  92. * 1830 Scroll of Escape: Castle
  93. * 3958 L2Day - Blessed Scroll of Escape
  94. * 5858 Blessed Scroll of Escape: Clan Hall
  95. * 5859 Blessed Scroll of Escape: Castle
  96. * 6663 Scroll of Escape: Orc Village
  97. * 6664 Scroll of Escape: Silenos Village
  98. * 7117 Scroll of Escape to Talking Island
  99. * 7118 Scroll of Escape to Elven Village
  100. * 7119 Scroll of Escape to Dark Elf Village
  101. * 7120 Scroll of Escape to Orc Village
  102. * 7121 Scroll of Escape to Dwarven Village
  103. * 7122 Scroll of Escape to Gludin Village
  104. * 7123 Scroll of Escape to the Town of Gludio
  105. * 7124 Scroll of Escape to the Town of Dion
  106. * 7125 Scroll of Escape to Floran
  107. * 7126 Scroll of Escape to Giran Castle Town
  108. * 7127 Scroll of Escape to Hardin's Private Academy
  109. * 7128 Scroll of Escape to Heine
  110. * 7129 Scroll of Escape to the Town of Oren
  111. * 7130 Scroll of Escape to Ivory Tower
  112. * 7131 Scroll of Escape to Hunters Village
  113. * 7132 Scroll of Escape to Aden Castle Town
  114. * 7133 Scroll of Escape to the Town of Goddard
  115. * 7134 Scroll of Escape to the Rune Township
  116. * 7135 Scroll of Escape to the Town of Schuttgart.
  117. * 7554 Scroll of Escape to Talking Island
  118. * 7555 Scroll of Escape to Elven Village
  119. * 7556 Scroll of Escape to Dark Elf Village
  120. * 7557 Scroll of Escape to Orc Village
  121. * 7558 Scroll of Escape to Dwarven Village
  122. * 7559 Scroll of Escape to Giran Castle Town
  123. * 7618 Scroll of Escape - Ketra Orc Village
  124. * 7619 Scroll of Escape - Varka Silenos Village
  125. */
  126. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_TELEPORT && activeChar.getKarma() > 0
  127. && (itemId == 736 || itemId == 1538 || itemId == 1829 || itemId == 1830
  128. || itemId == 3958 || itemId == 5858 || itemId == 5859 || itemId == 6663
  129. || itemId == 6664 || (itemId >= 7117 && itemId <= 7135)
  130. || (itemId >= 7554 && itemId <= 7559) || itemId == 7618 || itemId == 7619))
  131. return;
  132. L2Clan cl = activeChar.getClan();
  133. //A shield that can only be used by the members of a clan that owns a castle.
  134. if ((cl == null||cl.getHasCastle() == 0) && itemId == 7015 && Config.CASTLE_SHIELD)
  135. {
  136. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION);
  137. activeChar.sendPacket(sm);
  138. sm = null;
  139. return;
  140. }
  141. //A shield that can only be used by the members of a clan that owns a clan hall.
  142. if ((cl == null|| cl.getHasHideout() == 0) && itemId == 6902 && Config.CLANHALL_SHIELD)
  143. {
  144. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION);
  145. activeChar.sendPacket(sm);
  146. sm = null;
  147. return;
  148. }
  149. //Apella armor used by clan members may be worn by a Baron or a higher level Aristocrat.
  150. if ((itemId >=7860 && itemId <=7879) && Config.APELLA_ARMORS && (cl == null||activeChar.getPledgeClass() < 5))
  151. {
  152. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION);
  153. activeChar.sendPacket(sm);
  154. sm = null;
  155. return;
  156. }
  157. //Clan Oath armor used by all clan members
  158. if ((itemId >=7850 && itemId <=7859) && Config.OATH_ARMORS && (cl == null))
  159. {
  160. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION);
  161. activeChar.sendPacket(sm);
  162. sm = null;
  163. return;
  164. }
  165. //The Lord's Crown used by castle lords only
  166. if (itemId ==6841 && Config.CASTLE_CROWN && (cl == null||(cl.getHasCastle() == 0||!activeChar.isClanLeader())))
  167. {
  168. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION);
  169. activeChar.sendPacket(sm);
  170. sm = null;
  171. return;
  172. }
  173. //Castle circlets used by the members of a clan that owns a castle, academy members are excluded.
  174. if (Config.CASTLE_CIRCLETS &&((itemId >= 6834 && itemId <= 6840)||itemId == 8182||itemId == 8183))
  175. {
  176. if (cl ==null)
  177. {
  178. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION);
  179. activeChar.sendPacket(sm);
  180. sm = null;
  181. return;
  182. }
  183. else
  184. {
  185. int circletId = CastleManager.getInstance().getCircletByCastleId(cl.getHasCastle());
  186. if (activeChar.getPledgeType() == -1||circletId != itemId)
  187. {
  188. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION);
  189. activeChar.sendPacket(sm);
  190. sm = null;
  191. return;
  192. }
  193. }
  194. }
  195. // Items that cannot be used
  196. if (itemId == 57)
  197. return;
  198. if (activeChar.isFishing() && (itemId < 6535 || itemId > 6540))
  199. {
  200. // You cannot do anything else while fishing
  201. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_DO_WHILE_FISHING_3);
  202. getClient().getActiveChar().sendPacket(sm);
  203. sm = null;
  204. return;
  205. }
  206. // Char cannot use item when dead
  207. if (activeChar.isDead())
  208. {
  209. SystemMessage sm = new SystemMessage(SystemMessageId.S1_CANNOT_BE_USED);
  210. sm.addItemName(itemId);
  211. getClient().getActiveChar().sendPacket(sm);
  212. sm = null;
  213. return;
  214. }
  215. // Char cannot use pet items
  216. if (item.getItem().isForWolf() || item.getItem().isForHatchling()
  217. || item.getItem().isForStrider() || item.getItem().isForBabyPet())
  218. {
  219. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_PET_ITEM); // You cannot equip a pet item.
  220. sm.addItemName(itemId);
  221. getClient().getActiveChar().sendPacket(sm);
  222. sm = null;
  223. return;
  224. }
  225. if (Config.DEBUG)
  226. _log.finest(activeChar.getObjectId() + ": use item " + _objectId);
  227. if (item.isEquipable())
  228. {
  229. // No unequipping/equipping while the player is in special conditions
  230. if (activeChar.isStunned() || activeChar.isSleeping() || activeChar.isParalyzed()
  231. || activeChar.isAlikeDead())
  232. {
  233. activeChar.sendMessage("Your status does not allow you to do that.");
  234. return;
  235. }
  236. switch (item.getItem().getBodyPart())
  237. {
  238. case L2Item.SLOT_LR_HAND:
  239. case L2Item.SLOT_L_HAND:
  240. case L2Item.SLOT_R_HAND:
  241. {
  242. // Prevent player to remove the weapon on special conditions
  243. if ((activeChar.isAttackingNow() || activeChar.isCastingNow() || activeChar.isMounted()))
  244. return;
  245. if (activeChar.isDisarmed())
  246. {
  247. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  248. return;
  249. }
  250. // Don't allow weapon/shield equipment if a cursed weapon is equiped
  251. if (activeChar.isCursedWeaponEquiped())
  252. {
  253. return;
  254. }
  255. // Don't allow weapon/shield hero equipment during Olympiads
  256. if (activeChar.isInOlympiadMode() && item.isHeroItem())
  257. {
  258. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  259. return;
  260. }
  261. // Don't allow other Race to Wear Kamael exclusive Weapons.
  262. if (!item.isEquipped() && item.getItem() instanceof L2Weapon && !activeChar.isGM())
  263. {
  264. L2Weapon wpn = (L2Weapon)item.getItem();
  265. switch (activeChar.getRace())
  266. {
  267. case Kamael:
  268. {
  269. switch (wpn.getItemType())
  270. {
  271. case NONE:
  272. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  273. return;
  274. }
  275. break;
  276. }
  277. case Human:
  278. case Dwarf:
  279. case Elf:
  280. case DarkElf:
  281. case Orc:
  282. {
  283. switch (wpn.getItemType())
  284. {
  285. case RAPIER:
  286. case CROSSBOW:
  287. case ANCIENT_SWORD:
  288. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  289. return;
  290. }
  291. break;
  292. }
  293. }
  294. }
  295. break;
  296. }
  297. case L2Item.SLOT_CHEST:
  298. case L2Item.SLOT_BACK:
  299. case L2Item.SLOT_GLOVES:
  300. case L2Item.SLOT_FEET:
  301. case L2Item.SLOT_HEAD:
  302. case L2Item.SLOT_FULL_ARMOR:
  303. case L2Item.SLOT_LEGS:
  304. {
  305. if (activeChar.getRace() == Race.Kamael &&
  306. (item.getItem().getItemType() == L2ArmorType.HEAVY
  307. ||item.getItem().getItemType() == L2ArmorType.MAGIC))
  308. {
  309. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  310. return;
  311. }
  312. break;
  313. }
  314. }
  315. if (activeChar.isCursedWeaponEquiped() && itemId == 6408) // Don't allow to put formal wear
  316. {
  317. return;
  318. }
  319. // Equip or unEquip
  320. L2ItemInstance[] items = null;
  321. boolean isEquiped = item.isEquipped();
  322. SystemMessage sm = null;
  323. L2ItemInstance old = activeChar.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LRHAND);
  324. if (old == null)
  325. old = activeChar.getInventory().getPaperdollItem(Inventory.PAPERDOLL_RHAND);
  326. activeChar.checkSSMatch(item, old);
  327. if (isEquiped)
  328. {
  329. if (item.getEnchantLevel() > 0)
  330. {
  331. sm = new SystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  332. sm.addNumber(item.getEnchantLevel());
  333. sm.addItemName(itemId);
  334. }
  335. else
  336. {
  337. sm = new SystemMessage(SystemMessageId.S1_DISARMED);
  338. sm.addItemName(itemId);
  339. }
  340. activeChar.sendPacket(sm);
  341. // Remove augementation boni on unequip
  342. if (item.isAugmented())
  343. item.getAugmentation().removeBoni(activeChar);
  344. int slot = activeChar.getInventory().getSlotFromItem(item);
  345. items = activeChar.getInventory().unEquipItemInBodySlotAndRecord(slot);
  346. }
  347. else
  348. {
  349. int tempBodyPart = item.getItem().getBodyPart();
  350. L2ItemInstance tempItem = activeChar.getInventory().getPaperdollItemByL2ItemId(tempBodyPart);
  351. // remove augmentation stats for replaced items
  352. // currently weapons only..
  353. if (tempItem != null && tempItem.isAugmented())
  354. tempItem.getAugmentation().removeBoni(activeChar);
  355. else if (tempBodyPart == 0x4000)
  356. {
  357. L2ItemInstance tempItem2 = activeChar.getInventory().getPaperdollItem(7);
  358. if (tempItem2 != null && tempItem2.isAugmented())
  359. tempItem2.getAugmentation().removeBoni(activeChar);
  360. tempItem2 = activeChar.getInventory().getPaperdollItem(8);
  361. if (tempItem2 != null && tempItem2.isAugmented())
  362. tempItem2.getAugmentation().removeBoni(activeChar);
  363. }
  364. //check if the item replaces a wear-item
  365. if (tempItem != null && tempItem.isWear())
  366. {
  367. // dont allow an item to replace a wear-item
  368. return;
  369. }
  370. else if (tempBodyPart == 0x4000) // left+right hand equipment
  371. {
  372. // this may not remove left OR right hand equipment
  373. tempItem = activeChar.getInventory().getPaperdollItem(7);
  374. if (tempItem != null && tempItem.isWear()) return;
  375. tempItem = activeChar.getInventory().getPaperdollItem(8);
  376. if (tempItem != null && tempItem.isWear()) return;
  377. }
  378. else if (tempBodyPart == 0x8000) // fullbody armor
  379. {
  380. // this may not remove chest or leggins
  381. tempItem = activeChar.getInventory().getPaperdollItem(10);
  382. if (tempItem != null && tempItem.isWear()) return;
  383. tempItem = activeChar.getInventory().getPaperdollItem(11);
  384. if (tempItem != null && tempItem.isWear()) return;
  385. }
  386. if (item.getEnchantLevel() > 0)
  387. {
  388. sm = new SystemMessage(SystemMessageId.S1_S2_EQUIPPED);
  389. sm.addNumber(item.getEnchantLevel());
  390. sm.addItemName(itemId);
  391. }
  392. else
  393. {
  394. sm = new SystemMessage(SystemMessageId.S1_EQUIPPED);
  395. sm.addItemName(itemId);
  396. }
  397. activeChar.sendPacket(sm);
  398. // Apply augementation boni on equip
  399. if (item.isAugmented())
  400. item.getAugmentation().applyBoni(activeChar);
  401. items = activeChar.getInventory().equipItemAndRecord(item);
  402. // Consume mana - will start a task if required; returns if item is not a shadow item
  403. item.decreaseMana(false);
  404. }
  405. sm = null;
  406. activeChar.refreshExpertisePenalty();
  407. if (item.getItem().getType2() == L2Item.TYPE2_WEAPON)
  408. activeChar.checkIfWeaponIsAllowed();
  409. InventoryUpdate iu = new InventoryUpdate();
  410. iu.addItems(Arrays.asList(items));
  411. activeChar.sendPacket(iu);
  412. activeChar.abortAttack();
  413. activeChar.broadcastUserInfo();
  414. }
  415. else
  416. {
  417. L2Weapon weaponItem = activeChar.getActiveWeaponItem();
  418. int itemid = item.getItemId();
  419. //_log.finest("item not equipable id:"+ item.getItemId());
  420. if (itemid == 4393)
  421. {
  422. activeChar.sendPacket(new ShowCalculator(4393));
  423. }
  424. else if ((weaponItem != null && weaponItem.getItemType() == L2WeaponType.ROD)
  425. && ((itemid >= 6519 && itemid <= 6527) || (itemid >= 7610 && itemid <= 7613) || (itemid >= 7807 && itemid <= 7809) || (itemid >= 8484 && itemid <= 8486) || (itemid >= 8505 && itemid <= 8513)))
  426. {
  427. activeChar.getInventory().setPaperdollItem(Inventory.PAPERDOLL_LHAND, item);
  428. activeChar.broadcastUserInfo();
  429. // Send a Server->Client packet ItemList to this L2PcINstance to update left hand equipement
  430. ItemList il = new ItemList(activeChar, false);
  431. sendPacket(il);
  432. return;
  433. }
  434. else
  435. {
  436. IItemHandler handler = ItemHandler.getInstance().getItemHandler(item.getItemId());
  437. if (handler == null)
  438. _log.warning("No item handler registered for item ID " + item.getItemId() + ".");
  439. else
  440. handler.useItem(activeChar, item);
  441. }
  442. }
  443. // }
  444. }
  445. @Override
  446. public String getType()
  447. {
  448. return _C__14_USEITEM;
  449. }
  450. }