RequestDestroyItem.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.L2DatabaseFactory;
  22. import com.l2jserver.gameserver.datatables.PetDataTable;
  23. import com.l2jserver.gameserver.instancemanager.CursedWeaponsManager;
  24. import com.l2jserver.gameserver.model.L2ItemInstance;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  28. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  29. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  30. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  31. import com.l2jserver.gameserver.util.Util;
  32. /**
  33. * This class ...
  34. *
  35. * @version $Revision: 1.7.2.4.2.6 $ $Date: 2005/03/27 15:29:30 $
  36. */
  37. public final class RequestDestroyItem extends L2GameClientPacket
  38. {
  39. private static final String _C__59_REQUESTDESTROYITEM = "[C] 59 RequestDestroyItem";
  40. private static Logger _log = Logger.getLogger(RequestDestroyItem.class.getName());
  41. private int _objectId;
  42. private long _count;
  43. @Override
  44. protected void readImpl()
  45. {
  46. _objectId = readD();
  47. _count = readQ();
  48. }
  49. @Override
  50. protected void runImpl()
  51. {
  52. L2PcInstance activeChar = getClient().getActiveChar();
  53. if (activeChar == null)
  54. return;
  55. if(_count <= 0)
  56. {
  57. if (_count < 0)
  58. Util.handleIllegalPlayerAction(activeChar,"[RequestDestroyItem] Character " + activeChar.getName() + " of account " + activeChar.getAccountName() + " tried to destroy item with oid " + _objectId + " but has count < 0!", Config.DEFAULT_PUNISH);
  59. return;
  60. }
  61. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("destroy"))
  62. {
  63. activeChar.sendMessage("You destroying items too fast.");
  64. return;
  65. }
  66. long count = _count;
  67. if (activeChar.isProcessingTransaction() || activeChar.getPrivateStoreType() != 0)
  68. {
  69. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.CANNOT_TRADE_DISCARD_DROP_ITEM_WHILE_IN_SHOPMODE));
  70. return;
  71. }
  72. L2ItemInstance itemToRemove = activeChar.getInventory().getItemByObjectId(_objectId);
  73. // if we can't find the requested item, its actually a cheat
  74. if (itemToRemove == null)
  75. {
  76. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
  77. return;
  78. }
  79. // Cannot discard item that the skill is consuming
  80. if (activeChar.isCastingNow())
  81. {
  82. if (activeChar.getCurrentSkill() != null && activeChar.getCurrentSkill().getSkill().getItemConsumeId() == itemToRemove.getItemId())
  83. {
  84. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
  85. return;
  86. }
  87. }
  88. // Cannot discard item that the skill is consuming
  89. if (activeChar.isCastingSimultaneouslyNow())
  90. {
  91. if (activeChar.getLastSimultaneousSkillCast() != null && activeChar.getLastSimultaneousSkillCast().getItemConsumeId() == itemToRemove.getItemId())
  92. {
  93. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
  94. return;
  95. }
  96. }
  97. int itemId = itemToRemove.getItemId();
  98. if ((!activeChar.isGM() && !itemToRemove.isDestroyable()) || CursedWeaponsManager.getInstance().isCursed(itemId))
  99. {
  100. if (itemToRemove.isHeroItem())
  101. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.HERO_WEAPONS_CANT_DESTROYED));
  102. else
  103. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
  104. return;
  105. }
  106. if (!itemToRemove.isStackable() && count > 1)
  107. {
  108. Util.handleIllegalPlayerAction(activeChar, "[RequestDestroyItem] Character " + activeChar.getName() + " of account " + activeChar.getAccountName() + " tried to destroy a non-stackable item with oid " + _objectId + " but has count > 1!", Config.DEFAULT_PUNISH);
  109. return;
  110. }
  111. if (!activeChar.getInventory().canManipulateWithItemId(itemToRemove.getItemId()))
  112. {
  113. activeChar.sendMessage("Cannot use this item.");
  114. return;
  115. }
  116. if (_count > itemToRemove.getCount())
  117. count = itemToRemove.getCount();
  118. if (itemToRemove.isEquipped())
  119. {
  120. L2ItemInstance[] unequiped =
  121. activeChar.getInventory().unEquipItemInSlotAndRecord(itemToRemove.getLocationSlot());
  122. InventoryUpdate iu = new InventoryUpdate();
  123. for (L2ItemInstance item: unequiped)
  124. {
  125. activeChar.checkSShotsMatch(null, item);
  126. iu.addModifiedItem(item);
  127. }
  128. activeChar.sendPacket(iu);
  129. activeChar.broadcastUserInfo();
  130. }
  131. if (PetDataTable.isPetItem(itemId))
  132. {
  133. Connection con = null;
  134. try
  135. {
  136. if (activeChar.getPet() != null && activeChar.getPet().getControlObjectId() == _objectId)
  137. {
  138. activeChar.getPet().unSummon(activeChar);
  139. }
  140. // if it's a pet control item, delete the pet
  141. con = L2DatabaseFactory.getInstance().getConnection();
  142. PreparedStatement statement = con.prepareStatement("DELETE FROM pets WHERE item_obj_id=?");
  143. statement.setInt(1, _objectId);
  144. statement.execute();
  145. statement.close();
  146. }
  147. catch (Exception e)
  148. {
  149. _log.log(Level.WARNING, "could not delete pet objectid: ", e);
  150. }
  151. finally
  152. {
  153. L2DatabaseFactory.close(con);
  154. }
  155. }
  156. if (itemToRemove.isTimeLimitedItem())
  157. itemToRemove.endOfLife();
  158. L2ItemInstance removedItem = activeChar.getInventory().destroyItem("Destroy", _objectId, count, activeChar, null);
  159. if(removedItem == null)
  160. return;
  161. if (!Config.FORCE_INVENTORY_UPDATE)
  162. {
  163. InventoryUpdate iu = new InventoryUpdate();
  164. if (removedItem.getCount() == 0) iu.addRemovedItem(removedItem);
  165. else iu.addModifiedItem(removedItem);
  166. //client.getConnection().sendPacket(iu);
  167. activeChar.sendPacket(iu);
  168. }
  169. else sendPacket(new ItemList(activeChar, true));
  170. StatusUpdate su = new StatusUpdate(activeChar);
  171. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  172. activeChar.sendPacket(su);
  173. }
  174. /* (non-Javadoc)
  175. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  176. */
  177. @Override
  178. public String getType()
  179. {
  180. return _C__59_REQUESTDESTROYITEM;
  181. }
  182. }