package l2p.gameserver.clientpackets;
import l2p.Config;
import l2p.extensions.multilang.CustomMessage;
import l2p.gameserver.cache.Msg;
import l2p.gameserver.model.L2Player;
import l2p.gameserver.model.items.L2ItemInstance;
import l2p.gameserver.modules.FloodProtector;
import l2p.gameserver.modules.FloodProtector.Action;
import l2p.util.Location;
import l2p.util.Log;
/**
* This class represents a packet that is sent when a player is dropping an item
*
* format: cdq ddd
*/
public class RequestDropItem extends L2GameClientPacket
{
private int _objectId;
private long _count;
private Location _loc;
@Override
public void readImpl()
{
_objectId = readD();
_count = readQ();
_loc = new Location(readD(), readD(), readD());
}
@Override
public void runImpl()
{
L2Player activeChar = getClient().getActiveChar();
if (activeChar == null || activeChar.isDead())
return;
if (_count < 1 || _loc.isNull())
return;
// don't allow to drop items too often
if (!FloodProtector.tryPerformAction(activeChar, Action.DROP_ITEM))
return;
// hex1r0: temp solution
if (activeChar.isBlocked())
{
activeChar.sendActionFailed();
return;
}
if (!Config.ALLOW_DISCARDITEM)
{
activeChar.sendMessage(new CustomMessage("l2p.gameserver.clientpackets.RequestDropItem.Disallowed", activeChar));
return;
}
if (activeChar.getPrivateStoreType() != L2Player.STORE_PRIVATE_NONE)
{
activeChar.sendPacket(Msg.WHILE_OPERATING_A_PRIVATE_STORE_OR_WORKSHOP_YOU_CANNOT_DISCARD_DESTROY_OR_TRADE_AN_ITEM);
return;
}
if (activeChar.isInTransaction())
{
sendPacket(Msg.NOTHING_HAPPENED);
return;
}
if (activeChar.isFishing())
{
activeChar.sendPacket(Msg.YOU_CANNOT_DO_THAT_WHILE_FISHING);
return;
}
if (activeChar.isActionsDisabled() || activeChar.isSitting())
{
activeChar.sendActionFailed();
return;
}
if (!activeChar.isInRangeSq(_loc, 22500) || Math.abs(_loc.z - activeChar.getZ()) > 50)
{
activeChar.sendPacket(Msg.THAT_IS_TOO_FAR_FROM_YOU_TO_DISCARD);
return;
}
L2ItemInstance oldItem = activeChar.getInventory().getItemByObjectId(_objectId);
if (oldItem == null)
{
activeChar.sendActionFailed();
return;
}
if (!oldItem.canBeDropped(activeChar, false))
{
activeChar.sendPacket(Msg.THAT_ITEM_CANNOT_BE_DISCARDED);
return;
}
long oldCount = oldItem.getCount();
if (oldCount < _count)
{
activeChar.sendActionFailed();
return;
}
L2ItemInstance dropedItem = activeChar.getInventory().dropItem(_objectId, _count, false);
if (dropedItem == null)
{
activeChar.sendActionFailed();
return;
}
dropedItem.dropToTheGround(activeChar, _loc);
Log.LogItem(activeChar, Log.Drop, dropedItem);
activeChar.updateStats();
}
}