RequestDestroyItem.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 net.sf.l2j.gameserver.clientpackets;
  16. import java.sql.PreparedStatement;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import net.sf.l2j.Config;
  20. import net.sf.l2j.L2DatabaseFactory;
  21. import net.sf.l2j.gameserver.instancemanager.CursedWeaponsManager;
  22. import net.sf.l2j.gameserver.model.L2ItemInstance;
  23. import net.sf.l2j.gameserver.model.L2PetDataTable;
  24. import net.sf.l2j.gameserver.model.L2World;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26. import net.sf.l2j.gameserver.network.SystemMessageId;
  27. import net.sf.l2j.gameserver.serverpackets.InventoryUpdate;
  28. import net.sf.l2j.gameserver.serverpackets.ItemList;
  29. import net.sf.l2j.gameserver.serverpackets.StatusUpdate;
  30. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  31. import net.sf.l2j.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 int _count;
  43. @Override
  44. protected void readImpl()
  45. {
  46. _objectId = readD();
  47. _count = readD();
  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) Util.handleIllegalPlayerAction(activeChar,"[RequestDestroyItem] count < 0! ban! oid: "+_objectId+" owner: "+activeChar.getName(),Config.DEFAULT_PUNISH);
  58. return;
  59. }
  60. int count = _count;
  61. if (activeChar.getPrivateStoreType() != 0)
  62. {
  63. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_TRADE_DISCARD_DROP_ITEM_WHILE_IN_SHOPMODE));
  64. return;
  65. }
  66. L2ItemInstance itemToRemove = activeChar.getInventory().getItemByObjectId(_objectId);
  67. // if we can't find requested item, its actually a cheat!
  68. if (itemToRemove == null) return;
  69. // Cannot discard item that the skill is consuming
  70. if (activeChar.isCastingNow())
  71. {
  72. if (activeChar.getCurrentSkill() != null && activeChar.getCurrentSkill().getSkill().getItemConsumeId() == itemToRemove.getItemId())
  73. {
  74. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
  75. return;
  76. }
  77. }
  78. int itemId = itemToRemove.getItemId();
  79. if (itemToRemove == null || itemToRemove.isWear() || (!activeChar.isGM() && !itemToRemove.isDestroyable()) || CursedWeaponsManager.getInstance().isCursed(itemId))
  80. {
  81. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
  82. return;
  83. }
  84. if(!itemToRemove.isStackable() && count > 1)
  85. {
  86. Util.handleIllegalPlayerAction(activeChar,"[RequestDestroyItem] count > 1 but item is not stackable! oid: "+_objectId+" owner: "+activeChar.getName(),Config.DEFAULT_PUNISH);
  87. return;
  88. }
  89. if (_count > itemToRemove.getCount())
  90. count = itemToRemove.getCount();
  91. if (itemToRemove.isEquipped())
  92. {
  93. L2ItemInstance[] unequiped =
  94. activeChar.getInventory().unEquipItemInSlotAndRecord(itemToRemove.getLocationSlot());
  95. InventoryUpdate iu = new InventoryUpdate();
  96. for (int i = 0; i < unequiped.length; i++)
  97. {
  98. activeChar.checkSSMatch(null, unequiped[i]);
  99. iu.addModifiedItem(unequiped[i]);
  100. }
  101. activeChar.sendPacket(iu);
  102. activeChar.broadcastUserInfo();
  103. }
  104. if (L2PetDataTable.isPetItem(itemId))
  105. {
  106. java.sql.Connection con = null;
  107. try
  108. {
  109. if (activeChar.getPet() != null && activeChar.getPet().getControlItemId() == _objectId)
  110. {
  111. activeChar.getPet().unSummon(activeChar);
  112. }
  113. // if it's a pet control item, delete the pet
  114. con = L2DatabaseFactory.getInstance().getConnection();
  115. PreparedStatement statement = con.prepareStatement("DELETE FROM pets WHERE item_obj_id=?");
  116. statement.setInt(1, _objectId);
  117. statement.execute();
  118. statement.close();
  119. }
  120. catch (Exception e)
  121. {
  122. _log.log(Level.WARNING, "could not delete pet objectid: ", e);
  123. }
  124. finally
  125. {
  126. try { con.close(); } catch (Exception e) {}
  127. }
  128. }
  129. L2ItemInstance removedItem = activeChar.getInventory().destroyItem("Destroy", _objectId, count, activeChar, null);
  130. if(removedItem == null)
  131. return;
  132. if (!Config.FORCE_INVENTORY_UPDATE)
  133. {
  134. InventoryUpdate iu = new InventoryUpdate();
  135. if (removedItem.getCount() == 0) iu.addRemovedItem(removedItem);
  136. else iu.addModifiedItem(removedItem);
  137. //client.getConnection().sendPacket(iu);
  138. activeChar.sendPacket(iu);
  139. }
  140. else sendPacket(new ItemList(activeChar, true));
  141. StatusUpdate su = new StatusUpdate(activeChar.getObjectId());
  142. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  143. activeChar.sendPacket(su);
  144. L2World world = L2World.getInstance();
  145. world.removeObject(removedItem);
  146. }
  147. /* (non-Javadoc)
  148. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#getType()
  149. */
  150. @Override
  151. public String getType()
  152. {
  153. return _C__59_REQUESTDESTROYITEM;
  154. }
  155. }