{
player.sendMessage("Invalid Target: Seller must be targetted");
return;
}
L2TradeList list = null;
if (merchant != null)
{
List<L2TradeList> lists = TradeController.getInstance().getBuyListByNpcId(merchant.getNpcId());
if(!player.isGM() )
{
if (lists == null)
{
Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" sent a false BuyList list_id.",Config.DEFAULT_PUNISH);
return;
}
for (L2TradeList tradeList : lists)
{
if (tradeList.getListId() == _listId)
{
list = tradeList;
}
}
}
else
{
list = TradeController.getInstance().getBuyList(_listId);
}
}
else
list = TradeController.getInstance().getBuyList(_listId);
if (list == null)
{
Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" sent a false BuyList list_id.",Config.DEFAULT_PUNISH);
return;
}
_listId = list.getListId();
if (_listId > 1000000) // lease
{
if (merchant != null && merchant.getTemplate().npcId != _listId-1000000)
{
sendPacket(new ActionFailed());
return;
}
}
if(_count < 1)
{
sendPacket(new ActionFailed());
return;
}
double taxRate = 0;
if (merchant != null && merchant.getIsInTown()) taxRate = merchant.getCastle().getTaxRate();
long subTotal = 0;
int tax = 0;
// Check for buylist validity and calculates summary values
long slots = 0;
long weight = 0;
for (int i = 0; i < _count; i++)
{
int itemId = _items[i * 2 + 0];
int count = _items[i * 2 + 1];
int price = -1;
if (!list.containsItemId(itemId))
{
Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" sent a false BuyList list_id.",Config.DEFAULT_PUNISH);
return;
}
L2Item template = ItemTable.getInstance().getTemplate(itemId);
if (template == null) continue;
if (count > Integer.MAX_VALUE || (!template.isStackable() && count > 1))
{
Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" tried to purchase invalid quantity of items at the same time.",Config.DEFAULT_PUNISH);
SystemMessage sm = new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED);
sendPacket(sm);
sm = null;
return;
}
if (_listId < 1000000)
{
//list = TradeController.getInstance().getBuyList(_listId);
price = list.getPriceForItemId(itemId);
if (itemId >= 3960 && itemId <= 4026) price *= Config.RATE_SIEGE_GUARDS_PRICE;
}
/* TODO: Disabled until Leaseholders are rewritten ;-)
} else {
L2ItemInstance li = merchant.findLeaseItem(itemId, 0);
if (li == null || li.getCount() < cnt) {
cnt = li.getCount();
if (cnt <= 0) {
items.remove(i);
continue;
}
items.get(i).setCount((int)cnt);
}
price = li.getPriceToSell(); // lease holder sells the item
weight = li.getItem().getWeight();
}
*/
if (price < 0)
{
_log.warning("ERROR, no price found .. wrong buylist ??");
sendPacket(new ActionFailed());
return;
}
if(price == 0 && !player.isGM() && Config.ONLY_GM_ITEMS_FREE)
{
player.sendMessage("Ohh Cheat dont work? You have a problem now!");
Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" tried buy item for 0 adena.", Config.DEFAULT_PUNISH);
return;
}
subTotal += (long)count * price; // Before tax
tax = (int)(subTotal * taxRate);
if (subTotal + tax > Integer.MAX_VALUE)
{
Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" tried to purchase over "+Integer.MAX_VALUE+" adena worth of goods.", Config.DEFAULT_PUNISH);
return;
}
weight += (long)count * template.getWeight();
if (!template.isStackable()) slots += count;
else if (player.getInventory().getItemByItemId(itemId) == null) slots++;
}
if (weight > Integer.MAX_VALUE || weight < 0 || !player.getInventory().validateWeight((int)weight))
{
sendPacket(new SystemMessage(SystemMessageId.WEIGHT_LIMIT_EXCEEDED));
return;
}
if (slots > Integer.MAX_VALUE || slots < 0 || !player.getInventory().validateCapacity((int)slots))
{
sendPacket(new SystemMessage(SystemMessageId.SLOTS_FULL));
return;
}
// Charge buyer and add tax to castle treasury if not owned by npc clan
if ((subTotal < 0) || !player.reduceAdena("Buy", (int)(subTotal + tax), player.getLastFolkNPC(), false))
{
sendPacket(new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));
return;
}
if (merchant != null && merchant.getIsInTown() && merchant.getCastle().getOwnerId() > 0)
merchant.getCastle().addToTreasury(tax);
// Proceed the purchase
for (int i=0; i < _count; i++)
{
int itemId = _items[i * 2 + 0];
int count = _items[i * 2 + 1];
if (count < 0) count = 0;
if (!list.containsItemId(itemId))
{
Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" sent a false BuyList list_id.",Config.DEFAULT_PUNISH);
return;
}
if(list.countDecrease(itemId))
list.decreaseCount(itemId,count);
// Add item to Inventory and adjust update packet
player.getInventory().addItem("Buy", itemId, count, player, merchant);
/* TODO: Disabled until Leaseholders are rewritten ;-)
// Update Leaseholder list
if (_listId >= 1000000)