RequestPetUseItem.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.clientpackets;
  20. import com.l2jserver.gameserver.handler.IItemHandler;
  21. import com.l2jserver.gameserver.handler.ItemHandler;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  24. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  25. import com.l2jserver.gameserver.network.SystemMessageId;
  26. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  27. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  28. public final class RequestPetUseItem extends L2GameClientPacket
  29. {
  30. private static final String _C__8A_REQUESTPETUSEITEM = "[C] 8A RequestPetUseItem";
  31. private int _objectId;
  32. @Override
  33. protected void readImpl()
  34. {
  35. _objectId = readD();
  36. // TODO: implement me properly
  37. // readQ();
  38. // readD();
  39. }
  40. @Override
  41. protected void runImpl()
  42. {
  43. final L2PcInstance activeChar = getClient().getActiveChar();
  44. if ((activeChar == null) || !activeChar.hasPet())
  45. {
  46. return;
  47. }
  48. if (!getClient().getFloodProtectors().getUseItem().tryPerformAction("pet use item"))
  49. {
  50. return;
  51. }
  52. final L2PetInstance pet = (L2PetInstance) activeChar.getSummon();
  53. final L2ItemInstance item = pet.getInventory().getItemByObjectId(_objectId);
  54. if (item == null)
  55. {
  56. return;
  57. }
  58. if (!item.getItem().isForNpc())
  59. {
  60. activeChar.sendPacket(SystemMessageId.PET_CANNOT_USE_ITEM);
  61. return;
  62. }
  63. if (activeChar.isAlikeDead() || pet.isDead())
  64. {
  65. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_CANNOT_BE_USED);
  66. sm.addItemName(item);
  67. activeChar.sendPacket(sm);
  68. return;
  69. }
  70. // If the item has reuse time and it has not passed.
  71. // Message from reuse delay must come from item.
  72. final int reuseDelay = item.getReuseDelay();
  73. if (reuseDelay > 0)
  74. {
  75. final long reuse = pet.getItemRemainingReuseTime(item.getObjectId());
  76. if (reuse > 0)
  77. {
  78. return;
  79. }
  80. }
  81. if (!item.isEquipped() && !item.getItem().checkCondition(pet, pet, true))
  82. {
  83. return;
  84. }
  85. useItem(pet, item, activeChar);
  86. }
  87. private void useItem(L2PetInstance pet, L2ItemInstance item, L2PcInstance activeChar)
  88. {
  89. if (item.isEquipable())
  90. {
  91. if (!item.getItem().isConditionAttached())
  92. {
  93. activeChar.sendPacket(SystemMessageId.PET_CANNOT_USE_ITEM);
  94. return;
  95. }
  96. if (item.isEquipped())
  97. {
  98. pet.getInventory().unEquipItemInSlot(item.getLocationSlot());
  99. }
  100. else
  101. {
  102. pet.getInventory().equipItem(item);
  103. }
  104. activeChar.sendPacket(new PetItemList(pet.getInventory().getItems()));
  105. pet.updateAndBroadcastStatus(1);
  106. }
  107. else
  108. {
  109. final IItemHandler handler = ItemHandler.getInstance().getHandler(item.getEtcItem());
  110. if (handler != null)
  111. {
  112. if (handler.useItem(pet, item, false))
  113. {
  114. final int reuseDelay = item.getReuseDelay();
  115. if (reuseDelay > 0)
  116. {
  117. activeChar.addTimeStampItem(item, reuseDelay);
  118. }
  119. pet.updateAndBroadcastStatus(1);
  120. }
  121. }
  122. else
  123. {
  124. activeChar.sendPacket(SystemMessageId.PET_CANNOT_USE_ITEM);
  125. _log.warning("No item handler registered for itemId: " + item.getId());
  126. }
  127. }
  128. }
  129. @Override
  130. public String getType()
  131. {
  132. return _C__8A_REQUESTPETUSEITEM;
  133. }
  134. }