RequestPetUseItem.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.datatables.PetDataTable;
  19. import com.l2jserver.gameserver.handler.IItemHandler;
  20. import com.l2jserver.gameserver.handler.ItemHandler;
  21. import com.l2jserver.gameserver.model.L2ItemInstance;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  24. import com.l2jserver.gameserver.network.SystemMessageId;
  25. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  26. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  27. public final class RequestPetUseItem extends L2GameClientPacket
  28. {
  29. private static Logger _log = Logger.getLogger(RequestPetUseItem.class.getName());
  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. L2PcInstance activeChar = getClient().getActiveChar();
  44. if (activeChar == null)
  45. return;
  46. L2PetInstance pet = (L2PetInstance)activeChar.getPet();
  47. if (pet == null)
  48. return;
  49. if (!getClient().getFloodProtectors().getUseItem().tryPerformAction("pet use item"))
  50. return;
  51. L2ItemInstance item = pet.getInventory().getItemByObjectId(_objectId);
  52. if (item == null)
  53. return;
  54. int itemId = item.getItemId();
  55. if (activeChar.isAlikeDead() || pet.isDead())
  56. {
  57. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_CANNOT_BE_USED);
  58. sm.addItemName(item);
  59. activeChar.sendPacket(sm);
  60. sm = null;
  61. return;
  62. }
  63. if (Config.DEBUG)
  64. _log.finest(activeChar.getObjectId()+": pet use item " + _objectId);
  65. if (!item.isEquipped())
  66. {
  67. if (!item.getItem().checkCondition(pet, pet, true))
  68. {
  69. return;
  70. }
  71. }
  72. //check if the item matches the pet
  73. if (item.isEquipable())
  74. {
  75. // all pet items have condition
  76. if (!item.getItem().isConditionAttached())
  77. {
  78. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.PET_CANNOT_USE_ITEM));
  79. return;
  80. }
  81. useItem(pet, item, activeChar);
  82. return;
  83. }
  84. else if (PetDataTable.isPetFood(itemId))
  85. {
  86. if (pet.canEatFoodId(itemId))
  87. useItem(pet, item, activeChar);
  88. else
  89. {
  90. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.PET_CANNOT_USE_ITEM));
  91. return;
  92. }
  93. }
  94. IItemHandler handler = ItemHandler.getInstance().getItemHandler(item.getEtcItem());
  95. if (handler != null)
  96. useItem(pet, item, activeChar);
  97. else
  98. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.PET_CANNOT_USE_ITEM));
  99. return;
  100. }
  101. private void useItem(L2PetInstance pet, L2ItemInstance item, L2PcInstance activeChar)
  102. {
  103. if (item.isEquipable())
  104. {
  105. if (item.isEquipped())
  106. {
  107. pet.getInventory().unEquipItemInSlot(item.getLocationSlot());
  108. }
  109. else
  110. {
  111. pet.getInventory().equipItem(item);
  112. }
  113. activeChar.sendPacket(new PetItemList(pet));
  114. pet.updateAndBroadcastStatus(1);
  115. }
  116. else
  117. {
  118. IItemHandler handler = ItemHandler.getInstance().getItemHandler(item.getEtcItem());
  119. if (handler != null)
  120. {
  121. handler.useItem(pet, item, false);
  122. pet.updateAndBroadcastStatus(1);
  123. }
  124. else
  125. _log.warning("no itemhandler registered for itemId:" + item.getItemId());
  126. }
  127. }
  128. /* (non-Javadoc)
  129. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  130. */
  131. @Override
  132. public String getType()
  133. {
  134. return _C__8A_REQUESTPETUSEITEM;
  135. }
  136. }