UseItem.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.Logger;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.GameTimeController;
  19. import com.l2jserver.gameserver.ThreadPoolManager;
  20. import com.l2jserver.gameserver.handler.IItemHandler;
  21. import com.l2jserver.gameserver.handler.ItemHandler;
  22. import com.l2jserver.gameserver.instancemanager.FortSiegeManager;
  23. import com.l2jserver.gameserver.model.L2ItemInstance;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.base.Race;
  26. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  27. import com.l2jserver.gameserver.network.SystemMessageId;
  28. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  29. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  30. import com.l2jserver.gameserver.network.serverpackets.ShowCalculator;
  31. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  32. import com.l2jserver.gameserver.templates.item.L2Armor;
  33. import com.l2jserver.gameserver.templates.item.L2ArmorType;
  34. import com.l2jserver.gameserver.templates.item.L2Item;
  35. import com.l2jserver.gameserver.templates.item.L2Weapon;
  36. import com.l2jserver.gameserver.templates.item.L2WeaponType;
  37. /**
  38. * This class ...
  39. *
  40. * @version $Revision: 1.18.2.7.2.9 $ $Date: 2005/03/27 15:29:30 $
  41. */
  42. public final class UseItem extends L2GameClientPacket
  43. {
  44. private static Logger _log = Logger.getLogger(UseItem.class.getName());
  45. private static final String _C__14_USEITEM = "[C] 14 UseItem";
  46. private int _objectId;
  47. private int _itemId;
  48. /** Weapon Equip Task */
  49. public class WeaponEquipTask implements Runnable
  50. {
  51. L2ItemInstance item;
  52. L2PcInstance activeChar;
  53. public WeaponEquipTask(L2ItemInstance it, L2PcInstance character){
  54. item = it;
  55. activeChar = character;
  56. }
  57. public void run()
  58. {
  59. //If character is still engaged in strike we should not change weapon
  60. if (activeChar.isAttackingNow())
  61. return;
  62. // Equip or unEquip
  63. activeChar.useEquippableItem(item, false);
  64. }
  65. }
  66. @Override
  67. protected void readImpl()
  68. {
  69. _objectId = readD();
  70. // TODO:
  71. //readD();
  72. }
  73. @Override
  74. protected void runImpl()
  75. {
  76. L2PcInstance activeChar = getClient().getActiveChar();
  77. if (activeChar == null)
  78. return;
  79. // Flood protect UseItem
  80. if (!activeChar.getFloodProtectors().getUseItem().tryPerformAction("use item"))
  81. return;
  82. if (activeChar.getPrivateStoreType() != 0)
  83. {
  84. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_TRADE_DISCARD_DROP_ITEM_WHILE_IN_SHOPMODE));
  85. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  86. return;
  87. }
  88. if (activeChar.getActiveTradeList() != null)
  89. activeChar.cancelActiveTrade();
  90. // cannot use items during Fear (possible more abnormal states?)
  91. if (activeChar.isAfraid())
  92. {
  93. // no sysmsg
  94. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  95. return;
  96. }
  97. // NOTE: disabled due to deadlocks
  98. // synchronized (activeChar.getInventory())
  99. // {
  100. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_objectId);
  101. if (item == null)
  102. return;
  103. if (item.isWear())
  104. {
  105. // No unequipping wear-items
  106. return;
  107. }
  108. if (item.getItem().getType2() == L2Item.TYPE2_QUEST)
  109. {
  110. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_USE_QUEST_ITEMS);
  111. activeChar.sendPacket(sm);
  112. sm = null;
  113. return;
  114. }
  115. _itemId = item.getItemId();
  116. /*
  117. * Alt game - Karma punishment // SOE
  118. * 736 Scroll of Escape
  119. * 1538 Blessed Scroll of Escape
  120. * 1829 Scroll of Escape: Clan Hall
  121. * 1830 Scroll of Escape: Castle
  122. * 3958 L2Day - Blessed Scroll of Escape
  123. * 5858 Blessed Scroll of Escape: Clan Hall
  124. * 5859 Blessed Scroll of Escape: Castle
  125. * 6663 Scroll of Escape: Orc Village
  126. * 6664 Scroll of Escape: Silenos Village
  127. * 7117 Scroll of Escape to Talking Island
  128. * 7118 Scroll of Escape to Elven Village
  129. * 7119 Scroll of Escape to Dark Elf Village
  130. * 7120 Scroll of Escape to Orc Village
  131. * 7121 Scroll of Escape to Dwarven Village
  132. * 7122 Scroll of Escape to Gludin Village
  133. * 7123 Scroll of Escape to the Town of Gludio
  134. * 7124 Scroll of Escape to the Town of Dion
  135. * 7125 Scroll of Escape to Floran
  136. * 7126 Scroll of Escape to Giran Castle Town
  137. * 7127 Scroll of Escape to Hardin's Private Academy
  138. * 7128 Scroll of Escape to Heine
  139. * 7129 Scroll of Escape to the Town of Oren
  140. * 7130 Scroll of Escape to Ivory Tower
  141. * 7131 Scroll of Escape to Hunters Village
  142. * 7132 Scroll of Escape to Aden Castle Town
  143. * 7133 Scroll of Escape to the Town of Goddard
  144. * 7134 Scroll of Escape to the Rune Township
  145. * 7135 Scroll of Escape to the Town of Schuttgart.
  146. * 7554 Scroll of Escape to Talking Island
  147. * 7555 Scroll of Escape to Elven Village
  148. * 7556 Scroll of Escape to Dark Elf Village
  149. * 7557 Scroll of Escape to Orc Village
  150. * 7558 Scroll of Escape to Dwarven Village
  151. * 7559 Scroll of Escape to Giran Castle Town
  152. * 7618 Scroll of Escape - Ketra Orc Village
  153. * 7619 Scroll of Escape - Varka Silenos Village
  154. * 10129 Scroll of Escape : Fortress
  155. * 10130 Blessed Scroll of Escape : Fortress
  156. */
  157. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_TELEPORT && activeChar.getKarma() > 0)
  158. {
  159. switch (_itemId)
  160. {
  161. case 736: case 1538: case 1829: case 1830: case 3958: case 5858:
  162. case 5859: case 6663: case 6664: case 7554: case 7555: case 7556:
  163. case 7557: case 7558: case 7559: case 7618: case 7619: case 10129:
  164. case 10130:
  165. return;
  166. }
  167. if (_itemId >= 7117 && _itemId <= 7135)
  168. return;
  169. }
  170. // Items that cannot be used
  171. if (_itemId == 57)
  172. return;
  173. if (activeChar.isFishing() && (_itemId < 6535 || _itemId > 6540))
  174. {
  175. // You cannot do anything else while fishing
  176. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_DO_WHILE_FISHING_3);
  177. getClient().getActiveChar().sendPacket(sm);
  178. sm = null;
  179. return;
  180. }
  181. // Char cannot use item when dead
  182. if (activeChar.isDead())
  183. {
  184. SystemMessage sm = new SystemMessage(SystemMessageId.S1_CANNOT_BE_USED);
  185. sm.addItemName(item);
  186. getClient().getActiveChar().sendPacket(sm);
  187. sm = null;
  188. return;
  189. }
  190. // Char cannot use pet items
  191. if ((item.getItem() instanceof L2Armor && item.getItem().getItemType() == L2ArmorType.PET)
  192. || (item.getItem() instanceof L2Weapon && item.getItem().getItemType() == L2WeaponType.PET) )
  193. {
  194. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_PET_ITEM); // You cannot equip a pet item.
  195. sm.addItemName(item);
  196. getClient().getActiveChar().sendPacket(sm);
  197. sm = null;
  198. return;
  199. }
  200. if (Config.DEBUG)
  201. _log.finest(activeChar.getObjectId() + ": use item " + _objectId);
  202. if (!item.isEquipped())
  203. {
  204. if (!item.getItem().checkCondition(activeChar, activeChar, true))
  205. return;
  206. }
  207. if (item.isEquipable())
  208. {
  209. // No unequipping/equipping while the player is in special conditions
  210. if (activeChar.isStunned()
  211. || activeChar.isSleeping()
  212. || activeChar.isParalyzed()
  213. || activeChar.isAlikeDead()
  214. || activeChar.isCastingNow())
  215. {
  216. return;
  217. }
  218. // Don't allow hero equipment and restricted items during Olympiad
  219. if (activeChar.isInOlympiadMode() && (item.isHeroItem() || item.isOlyRestrictedItem()))
  220. {
  221. activeChar.sendPacket(new SystemMessage(SystemMessageId.THIS_ITEM_CANT_BE_EQUIPPED_FOR_THE_OLYMPIAD_EVENT));
  222. return;
  223. }
  224. switch (item.getItem().getBodyPart())
  225. {
  226. case L2Item.SLOT_LR_HAND:
  227. case L2Item.SLOT_L_HAND:
  228. case L2Item.SLOT_R_HAND:
  229. {
  230. // prevent players to equip weapon while wearing combat flag
  231. if (activeChar.getActiveWeaponItem() != null && activeChar.getActiveWeaponItem().getItemId() == 9819)
  232. {
  233. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  234. return;
  235. }
  236. // Prevent player to remove the weapon on special conditions
  237. if (activeChar.isCastingNow() || activeChar.isCastingSimultaneouslyNow())
  238. {
  239. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_USE_ITEM_WHILE_USING_MAGIC));
  240. return;
  241. }
  242. if (activeChar.isMounted())
  243. {
  244. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  245. return;
  246. }
  247. if (activeChar.isDisarmed())
  248. {
  249. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  250. return;
  251. }
  252. // Don't allow weapon/shield equipment if a cursed weapon is equiped
  253. if (activeChar.isCursedWeaponEquipped())
  254. return;
  255. // Don't allow other Race to Wear Kamael exclusive Weapons.
  256. if (!item.isEquipped() && item.getItem() instanceof L2Weapon && !activeChar.isGM())
  257. {
  258. L2Weapon wpn = (L2Weapon)item.getItem();
  259. switch (activeChar.getRace())
  260. {
  261. case Kamael:
  262. {
  263. switch (wpn.getItemType())
  264. {
  265. case NONE:
  266. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  267. return;
  268. }
  269. break;
  270. }
  271. case Human:
  272. case Dwarf:
  273. case Elf:
  274. case DarkElf:
  275. case Orc:
  276. {
  277. switch (wpn.getItemType())
  278. {
  279. case RAPIER:
  280. case CROSSBOW:
  281. case ANCIENT_SWORD:
  282. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  283. return;
  284. }
  285. break;
  286. }
  287. }
  288. }
  289. break;
  290. }
  291. case L2Item.SLOT_CHEST:
  292. case L2Item.SLOT_BACK:
  293. case L2Item.SLOT_GLOVES:
  294. case L2Item.SLOT_FEET:
  295. case L2Item.SLOT_HEAD:
  296. case L2Item.SLOT_FULL_ARMOR:
  297. case L2Item.SLOT_LEGS:
  298. {
  299. if (activeChar.getRace() == Race.Kamael &&
  300. (item.getItem().getItemType() == L2ArmorType.HEAVY
  301. ||item.getItem().getItemType() == L2ArmorType.MAGIC))
  302. {
  303. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  304. return;
  305. }
  306. break;
  307. }
  308. case L2Item.SLOT_DECO:
  309. {
  310. if (!item.isEquipped() && activeChar.getInventory().getMaxTalismanCount() == 0)
  311. {
  312. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  313. return;
  314. }
  315. }
  316. }
  317. if (activeChar.isCursedWeaponEquipped() && _itemId == 6408) // Don't allow to put formal wear
  318. return;
  319. if (activeChar.isAttackingNow())
  320. {
  321. ThreadPoolManager.getInstance().scheduleGeneral( new WeaponEquipTask(item,activeChar), (activeChar.getAttackEndTime()-GameTimeController.getGameTicks())*GameTimeController.MILLIS_IN_TICK);
  322. return;
  323. }
  324. // Equip or unEquip
  325. if (FortSiegeManager.getInstance().isCombat(item.getItemId()))
  326. return; //no message
  327. else if (activeChar.isCombatFlagEquipped())
  328. return;
  329. activeChar.useEquippableItem(item, true);
  330. }
  331. else
  332. {
  333. L2Weapon weaponItem = activeChar.getActiveWeaponItem();
  334. int itemid = item.getItemId();
  335. if (itemid == 4393)
  336. {
  337. activeChar.sendPacket(new ShowCalculator(4393));
  338. }
  339. else if ((weaponItem != null && weaponItem.getItemType() == L2WeaponType.ROD)
  340. && ((itemid >= 6519 && itemid <= 6527) || (itemid >= 7610 && itemid <= 7613) || (itemid >= 7807 && itemid <= 7809) || (itemid >= 8484 && itemid <= 8486) || (itemid >= 8505 && itemid <= 8513)))
  341. {
  342. activeChar.getInventory().setPaperdollItem(Inventory.PAPERDOLL_LHAND, item);
  343. activeChar.broadcastUserInfo();
  344. // Send a Server->Client packet ItemList to this L2PcINstance to update left hand equipement
  345. ItemList il = new ItemList(activeChar, false);
  346. sendPacket(il);
  347. return;
  348. }
  349. else
  350. {
  351. IItemHandler handler = ItemHandler.getInstance().getItemHandler(item.getEtcItem());
  352. if (handler == null)
  353. {
  354. if (Config.DEBUG)
  355. _log.warning("No item handler registered for item ID " + item.getItemId() + ".");
  356. }
  357. else
  358. handler.useItem(activeChar, item);
  359. }
  360. }
  361. // }
  362. }
  363. @Override
  364. public String getType()
  365. {
  366. return _C__14_USEITEM;
  367. }
  368. @Override
  369. protected boolean triggersOnActionRequest()
  370. {
  371. return !Config.SPAWN_PROTECTION_ALLOWED_ITEMS.contains(_itemId);
  372. }
  373. }