UseItem.java 16 KB

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