RequestDestroyItem.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 (!activeChar.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(new SystemMessage(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(new SystemMessage(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(new SystemMessage(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(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
  94. return;
  95. }
  96. }
  97. int itemId = itemToRemove.getItemId();
  98. if (itemToRemove.isWear() || (!activeChar.isGM() && !itemToRemove.isDestroyable())
  99. || CursedWeaponsManager.getInstance().isCursed(itemId))
  100. {
  101. if (itemToRemove.isHeroItem())
  102. activeChar.sendPacket(new SystemMessage(SystemMessageId.HERO_WEAPONS_CANT_DESTROYED));
  103. else
  104. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
  105. return;
  106. }
  107. if (!itemToRemove.isStackable() && count > 1)
  108. {
  109. 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);
  110. return;
  111. }
  112. if (_count > itemToRemove.getCount())
  113. count = itemToRemove.getCount();
  114. if (itemToRemove.isEquipped())
  115. {
  116. L2ItemInstance[] unequiped =
  117. activeChar.getInventory().unEquipItemInSlotAndRecord(itemToRemove.getLocationSlot());
  118. InventoryUpdate iu = new InventoryUpdate();
  119. for (L2ItemInstance item: unequiped)
  120. {
  121. activeChar.checkSSMatch(null, item);
  122. iu.addModifiedItem(item);
  123. }
  124. activeChar.sendPacket(iu);
  125. activeChar.broadcastUserInfo();
  126. }
  127. if (PetDataTable.isPetItem(itemId))
  128. {
  129. Connection con = null;
  130. try
  131. {
  132. if (activeChar.getPet() != null && activeChar.getPet().getControlItemId() == _objectId)
  133. {
  134. activeChar.getPet().unSummon(activeChar);
  135. }
  136. // if it's a pet control item, delete the pet
  137. con = L2DatabaseFactory.getInstance().getConnection();
  138. PreparedStatement statement = con.prepareStatement("DELETE FROM pets WHERE item_obj_id=?");
  139. statement.setInt(1, _objectId);
  140. statement.execute();
  141. statement.close();
  142. }
  143. catch (Exception e)
  144. {
  145. _log.log(Level.WARNING, "could not delete pet objectid: ", e);
  146. }
  147. finally
  148. {
  149. L2DatabaseFactory.close(con);
  150. }
  151. }
  152. if (itemToRemove.isTimeLimitedItem())
  153. itemToRemove.endOfLife();
  154. L2ItemInstance removedItem = activeChar.getInventory().destroyItem("Destroy", _objectId, count, activeChar, null);
  155. if(removedItem == null)
  156. return;
  157. if (!Config.FORCE_INVENTORY_UPDATE)
  158. {
  159. InventoryUpdate iu = new InventoryUpdate();
  160. if (removedItem.getCount() == 0) iu.addRemovedItem(removedItem);
  161. else iu.addModifiedItem(removedItem);
  162. //client.getConnection().sendPacket(iu);
  163. activeChar.sendPacket(iu);
  164. }
  165. else sendPacket(new ItemList(activeChar, true));
  166. StatusUpdate su = new StatusUpdate(activeChar.getObjectId());
  167. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  168. activeChar.sendPacket(su);
  169. }
  170. /* (non-Javadoc)
  171. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  172. */
  173. @Override
  174. public String getType()
  175. {
  176. return _C__59_REQUESTDESTROYITEM;
  177. }
  178. }