/*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/>.
*/
package silentium.gameserver.network.clientpackets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import silentium.commons.database.DatabaseFactory;
import silentium.gameserver.configs.MainConfig;
import silentium.gameserver.instancemanager.CursedWeaponsManager;
import silentium.gameserver.model.L2ItemInstance;
import silentium.gameserver.model.actor.instance.L2PcInstance;
import silentium.gameserver.network.SystemMessageId;
import silentium.gameserver.network.serverpackets.InventoryUpdate;
import silentium.gameserver.network.serverpackets.ItemList;
import silentium.gameserver.network.serverpackets.StatusUpdate;
import silentium.gameserver.tables.PetDataTable;
import silentium.gameserver.utils.Util;
public final class RequestDestroyItem extends L2GameClientPacket
{
private int _objectId;
private int _count;
@Override
protected void readImpl()
{
_objectId = readD();
_count = readD();
}
@Override
protected void runImpl()
{
final L2PcInstance activeChar = getClient().getActiveChar();
if (activeChar == null)
return;
int count = _count;
if (count <= 0)
{
if (count < 0)
Util.handleIllegalPlayerAction(activeChar, "[RequestDestroyItem] " + activeChar.getName() + " of account " + activeChar.getAccountName() + " tried to destroy item with oid " + _objectId + " but has count < 0.", MainConfig.DEFAULT_PUNISH);
return;
}
if (activeChar.isProcessingTransaction() || activeChar.getPrivateStoreType() != 0)
{
activeChar.sendPacket(SystemMessageId.CANNOT_TRADE_DISCARD_DROP_ITEM_WHILE_IN_SHOPMODE);
return;
}
final L2ItemInstance itemToRemove = activeChar.getInventory().getItemByObjectId(_objectId);
if (itemToRemove == null)
return;
final int itemId = itemToRemove.getItemId();
// Cannot discard item that the skill is consumming
if (activeChar.isCastingNow())
{
if (activeChar.getCurrentSkill() != null && activeChar.getCurrentSkill().getSkill().getItemConsumeId() == itemId)
{
activeChar.sendPacket(SystemMessageId.CANNOT_DISCARD_THIS_ITEM);
return;
}
}
// Cannot discard item that the skill is consuming
if (activeChar.isCastingSimultaneouslyNow())
{
if (activeChar.getLastSimultaneousSkillCast() != null && activeChar.getLastSimultaneousSkillCast().getItemConsumeId() == itemId)
{
activeChar.sendPacket(SystemMessageId.CANNOT_DISCARD_THIS_ITEM);
return;
}
}
if (!itemToRemove.isDestroyable() || CursedWeaponsManager.getInstance().isCursed(itemId))
{
if (itemToRemove.isHeroItem())
activeChar.sendPacket(SystemMessageId.HERO_WEAPONS_CANT_DESTROYED);
else
activeChar.sendPacket(SystemMessageId.CANNOT_DISCARD_THIS_ITEM);
return;
}
if (!itemToRemove.isStackable() && count > 1)
{
Util.handleIllegalPlayerAction(activeChar, "[RequestDestroyItem] " + activeChar.getName() + " of account " + activeChar.getAccountName() + " tried to destroy a non-stackable item with oid " + _objectId + " but has count > 1.", MainConfig.DEFAULT_PUNISH);
return;
}
if (!activeChar.getInventory().canManipulateWithItemId(itemId))
{
activeChar.sendPacket(SystemMessageId.CANNOT_USE_ON_YOURSELF);
return;
}
if (count > itemToRemove.getCount())
count = itemToRemove.getCount();
if (itemToRemove.isEquipped())
{
L2ItemInstance[] unequipped = activeChar.getInventory().unEquipItemInSlotAndRecord(itemToRemove.getLocationSlot());
InventoryUpdate iu = new InventoryUpdate();
for (L2ItemInstance item : unequipped)
{
L2PcInstance.cleanWeaponShots(item);
iu.addModifiedItem(item);
}
activeChar.sendPacket(iu);
activeChar.broadcastUserInfo();
}
// if it's a pet control item.
if (PetDataTable.isPetCollar(itemId))
{
// See if pet or mount is active ; can't destroy item linked to that pet.
if ((activeChar.getPet() != null && activeChar.getPet().getControlItemId() == _objectId) || (activeChar.isMounted() && activeChar.getMountObjectID() == _objectId))
{
activeChar.sendPacket(SystemMessageId.PET_SUMMONED_MAY_NOT_DESTROYED);
return;
}
try (Connection con = DatabaseFactory.getConnection())
{
PreparedStatement statement = con.prepareStatement("DELETE FROM pets WHERE item_obj_id=?");
statement.setInt(1, _objectId);
statement.execute();
statement.close();
}
catch (Exception e)
{
log.warn("could not delete pet objectid: ", e);
}
}
L2ItemInstance removedItem = activeChar.getInventory().destroyItem("Destroy", _objectId, count, activeChar, null);
if (removedItem == null)
return;
if (!MainConfig.FORCE_INVENTORY_UPDATE)
{
InventoryUpdate iu = new InventoryUpdate();
if (removedItem.getCount() == 0)
iu.addRemovedItem(removedItem);
else
iu.addModifiedItem(removedItem);
activeChar.sendPacket(iu);
}
else
sendPacket(new ItemList(activeChar, true));
StatusUpdate su = new StatusUpdate(activeChar);
su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
activeChar.sendPacket(su);
}
}