if(activeChar.getInventory().getItemByItemId(itemId) != null)
{
needsSpace = 0;
}
}
NpcTradeList list = TradeController.getInstance().getBuyList(_listId);
if(list == null)
{
Log.add("tried to buy from non-exist list " + _listId, "errors", activeChar);
activeChar.sendActionFailed();
return;
}
TradeItem ti = getItemByItemId(itemId, list);
price = ti == null ? 0 : ti.getOwnersPrice();
if(itemId >= 3960 && itemId <= 4921)
{
price *= Config.RATE_SIEGE_GUARDS_PRICE;
}
if(price == 0 && !activeChar.getPlayerAccess().UseGMShop)
{
Util.handleIllegalPlayerAction(activeChar, "RequestBuyItem[191]", "Tried to buy zero price item, list " + _listId + " item " + itemId, 0);
for(L2ItemInstance item : items)
{
item.deleteMe();
}
activeChar.sendMessage("Error: zero-price item! Please notify GM.");
activeChar.sendActionFailed();
return;
}
weight = items.get(i).getItem().getWeight();
if(price < 0)
{
_log.warning("ERROR, no price found. Wrong buylist?");
for(L2ItemInstance item : items)
{
item.deleteMe();
}
activeChar.sendActionFailed();
return;
}
try
{
if(cnt < 0)
{
throw new ArithmeticException("cnt < 0");
}
subTotal = SafeMath.safeAddLong(subTotal, SafeMath.safeMulLong(cnt, price)); // Before tax
tax = SafeMath.safeMulLong(subTotal, taxRate);
totalCost = SafeMath.safeAddLong(subTotal, tax);
if(totalCost < 0)
{
throw new ArithmeticException("213: Tried to purchase negative " + totalCost + " adena worth of goods.");
}
finalLoad = SafeMath.safeAddLong(finalLoad, SafeMath.safeMulLong(cnt, weight));
if(finalLoad < 0)
{
throw new ArithmeticException("254: Tried to purchase negative " + finalLoad + " adena worth of goods.");
}
}
catch(ArithmeticException e)
{
Util.handleIllegalPlayerAction(activeChar, "RequestBuyItem[157]", "merchant: " + merchant + ": " + e.getMessage(), 1);
for(L2ItemInstance item : items)
{
item.deleteMe();
}
sendPacket(Msg.YOU_HAVE_EXCEEDED_THE_QUANTITY_THAT_CAN_BE_INPUTTED, Msg.ActionFail);
return;
}
if(needsSpace == 2)
{
finalCount += cnt;
}
else if(needsSpace == 1)
{
finalCount += 1;
}
}
if(totalCost > currentMoney || subTotal < 0 || currentMoney <= 0)
{
for(L2ItemInstance item : items)
{
item.deleteMe();
}
sendPacket(Msg.YOU_DO_NOT_HAVE_ENOUGH_ADENA, Msg.ActionFail);
return;
}
if(!activeChar.getInventory().validateWeight(finalLoad))
{
for(L2ItemInstance item : items)
{
item.deleteMe();
}
sendPacket(Msg.YOU_HAVE_EXCEEDED_THE_WEIGHT_LIMIT, Msg.ActionFail);
return;
}
if(!activeChar.getInventory().validateCapacity(finalCount))
{
for(L2ItemInstance item : items)
{
item.deleteMe();
}
sendPacket(Msg.YOUR_INVENTORY_IS_FULL, Msg.ActionFail);
return;
}
// Для магазинов с ограниченным количеством товара число продаваемых предметов уменьшаем после всех проверок
NpcTradeList list = TradeController.getInstance().getBuyList(_listId);
for(int i = 0; i < items.size(); i++)
{
itemId = items.get(i).getItemId();
cnt = items.get(i).getCount();
TradeItem ic = getItemByItemId(itemId, list);