RequestDestroyItem.java 7.0 KB

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