public synchronized boolean buySellItems(L2Player buyer, ConcurrentLinkedQueue<TradeItem> listToBuy, L2Player seller, ConcurrentLinkedQueue<TradeItem> listToSell)
{
Inventory sellerInv = seller.getInventory();
Inventory buyerInv = buyer.getInventory();
TradeItem sellerTradeItem = null;
L2ItemInstance sellerInventoryItem = null;
L2ItemInstance TransferItem = null;
L2ItemInstance temp;
ConcurrentLinkedQueue<TradeItem> unsold = new ConcurrentLinkedQueue<TradeItem>();
unsold.addAll(listToSell);
long cost = 0, amount = 0;
for(TradeItem buyerTradeItem : listToBuy)
{
sellerTradeItem = null;
for(TradeItem unsoldItem : unsold)
{
if(unsoldItem.getItemId() == buyerTradeItem.getItemId() && unsoldItem.getOwnersPrice() == buyerTradeItem.getOwnersPrice())
{
sellerTradeItem = unsoldItem;
break;
}
}
if(sellerTradeItem == null)
{
continue;
}
sellerInventoryItem = sellerInv.getItemByObjectId(sellerTradeItem.getObjectId());
unsold.remove(sellerTradeItem);
if(sellerInventoryItem == null)
{
continue;
}
long buyerItemCount = buyerTradeItem.getCount();
long sellerItemCount = sellerTradeItem.getCount();
if(sellerItemCount > sellerInventoryItem.getCount())
{
sellerItemCount = sellerInventoryItem.getCount();
}
if(seller.getPrivateStoreType() == L2Player.STORE_PRIVATE_SELL || seller.getPrivateStoreType() == L2Player.STORE_PRIVATE_SELL_PACKAGE)
{
if(buyerItemCount > sellerItemCount)
{
buyerTradeItem.setCount(sellerItemCount);
}
if(buyerItemCount > sellerInventoryItem.getCount())
{
buyerTradeItem.setCount(sellerInventoryItem.getCount());
}
buyerItemCount = buyerTradeItem.getCount();
amount = buyerItemCount;
cost = amount * sellerTradeItem.getOwnersPrice();
}
if(buyer.getPrivateStoreType() == L2Player.STORE_PRIVATE_BUY)
{
if(sellerItemCount > buyerItemCount)
{
sellerTradeItem.setCount(buyerItemCount);
}
if(sellerItemCount > sellerInventoryItem.getCount())
{
sellerTradeItem.setCount(sellerInventoryItem.getCount());
}
sellerItemCount = sellerTradeItem.getCount();
amount = sellerItemCount;
cost = amount * buyerTradeItem.getOwnersPrice();
}
if(buyer.getAdena() < cost)
{
return false;
}
try
{
SafeMath.safeMulLong(buyerItemCount, buyerTradeItem.getOwnersPrice());
SafeMath.safeMulLong(sellerItemCount, sellerTradeItem.getOwnersPrice());
}
catch(ArithmeticException e)
{
return false;
}
TransferItem = sellerInv.dropItem(sellerInventoryItem.getObjectId(), amount, false);
Log.LogItem(seller, buyer, Log.PrivateStoreSell, TransferItem, amount);
buyer.reduceAdena(cost, false);
seller.addAdena(cost);
int tax = (int) (cost * Config.SERVICES_TRADE_TAX / 100);
if(seller.isInZone(L2Zone.ZoneType.offshore))
{
tax = (int) (cost * Config.SERVICES_OFFSHORE_TRADE_TAX / 100);
}
if(Config.SERVICES_TRADE_TAX_ONLY_OFFLINE && !seller.isInOfflineMode())
{
tax = 0;
}
if(seller.getReflection().getId() == -1) // Особая зона в Parnassus
{
tax = 0;
}
if(tax > 0)
{
seller.reduceAdena(tax, true);
Stat.addTax(tax);
seller.sendMessage(new CustomMessage("trade.HavePaidTax", seller).addNumber(tax));
}
temp = buyerInv.addItem(TransferItem);
Log.LogItem(buyer, seller, Log.PrivateStoreBuy, TransferItem, amount);
if(!temp.isStackable())
{
if(temp.getEnchantLevel() > 0)
{
seller.sendPacket(new SystemMessage(SystemMessage._S2S3_HAS_BEEN_SOLD_TO_S1_AT_THE_PRICE_OF_S4_ADENA).addString(buyer.getName()).addNumber(temp.getEnchantLevel()).addItemName(sellerInventoryItem.getItemId()).addNumber(cost));
buyer.sendPacket(new SystemMessage(SystemMessage._S2S3_HAS_BEEN_PURCHASED_FROM_S1_AT_THE_PRICE_OF_S4_ADENA).addString(seller.getName()).addNumber(temp.getEnchantLevel()).addItemName(sellerInventoryItem.getItemId()).addNumber(cost));
}
else
{
seller.sendPacket(new SystemMessage(SystemMessage.S2_IS_SOLD_TO_S1_AT_THE_PRICE_OF_S3_ADENA).addString(buyer.getName()).addItemName(sellerInventoryItem.getItemId()).addNumber(cost));
buyer.sendPacket(new SystemMessage(SystemMessage.S2_HAS_BEEN_PURCHASED_FROM_S1_AT_THE_PRICE_OF_S3_ADENA).addString(seller.getName()).addItemName(sellerInventoryItem.getItemId()).addNumber(cost));