if (!activeChar.isGM() && ((merchant == null) || !isValidMerchant || !activeChar.isInRange(merchant, Creature.INTERACTION_DISTANCE)))
{
activeChar.sendActionFailed();
return;
}
NpcTradeList list = BuyListHolder.getInstance().getBuyList(_listId);
if (list == null)
{
activeChar.sendActionFailed();
return;
}
int slots = 0;
long weight = 0;
long totalPrice = 0;
long tax = 0;
double taxRate = 0;
Castle castle = null;
if (merchant != null)
{
castle = merchant.getCastle(activeChar);
if (castle != null)
{
taxRate = castle.getTaxRate();
}
}
List<TradeItem> buyList = new ArrayList<TradeItem>(_count);
List<TradeItem> tradeList = list.getItems();
try
{
loop:
for (int i = 0; i < _count; i++)
{
int itemId = _items[i];
long count = _itemQ[i];
long price = 0;
for (TradeItem ti : tradeList)
{
if (ti.getItemId() == itemId)
{
if (ti.isCountLimited() && (ti.getCurrentValue() < count))
{
continue loop;
}
price = ti.getOwnersPrice();
}
}
if ((price == 0) && (!activeChar.isGM() || !activeChar.getPlayerAccess().UseGMShop))
{
activeChar.sendActionFailed();
return;
}
totalPrice = SafeMath.addAndCheck(totalPrice, SafeMath.mulAndCheck(count, price));
TradeItem ti = new TradeItem();
ti.setItemId(itemId);
ti.setCount(count);
ti.setOwnersPrice(price);
weight = SafeMath.addAndCheck(weight, SafeMath.mulAndCheck(count, ti.getItem().getWeight()));
if (!ti.getItem().isStackable() || (activeChar.getInventory().getItemByItemId(itemId) == null))
{
slots++;
}
buyList.add(ti);
}
tax = (long) (totalPrice * taxRate);
totalPrice = SafeMath.addAndCheck(totalPrice, tax);
if (!activeChar.getInventory().validateWeight(weight))
{
sendPacket(Msg.YOU_HAVE_EXCEEDED_THE_WEIGHT_LIMIT);
return;
}
if (!activeChar.getInventory().validateCapacity(slots))
{
sendPacket(Msg.YOUR_INVENTORY_IS_FULL);
return;
}
if (!activeChar.reduceAdena(totalPrice))
{
activeChar.sendPacket(Msg.YOU_DO_NOT_HAVE_ENOUGH_ADENA);
return;
}
for (TradeItem ti : buyList)
{
activeChar.getInventory().addItem(ti.getItemId(), ti.getCount());
}
list.updateItems(buyList);
if (castle != null)
{
if ((tax > 0) && (castle.getOwnerId() > 0) && (activeChar.getReflection() == ReflectionManager.DEFAULT))
{
castle.addToTreasury(tax, true, false);