PcInventory ownerInventory = _owner.getInventory();
PcInventory playerInventory = player.getInventory();
// Prepare inventory update packet
InventoryUpdate ownerIU = new InventoryUpdate();
InventoryUpdate playerIU = new InventoryUpdate();
//we must check item are available before begining transaction, TODO: should we remove that check when transfering items as it's done here? (there might be synchro problems if player clicks fast if we remove it)
// also check if augmented items are traded. If so, cancel it...
for(ItemRequest item : items)
{
// Check if requested item is available for manipulation
L2ItemInstance oldItem = player.checkItemManipulation(item.getObjectId(), item.getCount(), "sell");
if(oldItem == null){
if(Config.DEBUG){
_log.info("[PrivateStoreSell] player.checkItemManipulation(item.getObjectId(), item.getCount(), 'sell') null, return false");
}
return false;
}
boolean found = false;
for (TradeItem ti : _items)
{
if (ti.getItem().getItemId() == item.getItemId())
{
if (ti.getPrice() != item.getPrice())
{
if (Config.DEBUG)
{
_log.info("[PrivateStoreSell] ti.getPrice() != item.getPrice(), return false");
}
return false;
}
if (ti.getEnchant() != item.getEnchant())
{
player.sendMessage("Incorect enchant level.");
return false;
}
L2Object obj = L2World.getInstance().findObject(item.getObjectId());
if ((obj == null) || (!(obj instanceof L2ItemInstance)))
{
String msgErr = "[RequestPrivateStoreSell] player " + _owner.getName() + " tried to sell null item in a private store (buy), ban this player!";
Util.handleIllegalPlayerAction(_owner, msgErr, Config.DEFAULT_PUNISH);
return false;
}
L2ItemInstance itemInstance = (L2ItemInstance) obj;
if (item.getEnchant() != itemInstance.getEnchantLevel())
{
String msgErr = "[RequestPrivateStoreSell] player " + _owner.getName() + " tried to change enchant level in a private store (buy), ban this player!";
Util.handleIllegalPlayerAction(_owner, msgErr, Config.DEFAULT_PUNISH);
return false;
}
found = true;
break;
}
}
//store is not buying that item...
if(!found){
String msg = "Requested Item is not available to sell... You are perfoming illegal operation, it has been segnalated";
_log.warning("ATTENTION: Player "+player.getName()+" has performed sell illegal operation..");
player.sendMessage(msg);
msg = null;
return false;
}
if(oldItem.getAugmentation() != null)
{
String msg = "Transaction failed. Augmented items may not be exchanged.";
_owner.sendMessage(msg);
player.sendMessage(msg);
msg = null;
return false;
}
oldItem = null;
}
// Transfer items
for(ItemRequest item : items)
{
// Check if requested item is sill on the list and adjust its count
adjustItemRequestByItemId(item);
if(item.getCount() == 0)
{
continue;
}
// Check if requested item is available for manipulation
L2ItemInstance oldItem = player.checkItemManipulation(item.getObjectId(), item.getCount(), "sell");
if(oldItem == null){
if(Config.DEBUG){
_log.info("[PrivateStoreSell] oldItem == null, return false");
}
return false;
}
// Check if requested item is correct
if (oldItem.getItemId() != item.getItemId())
{
Util.handleIllegalPlayerAction(player, player+" is cheating with sell items", Config.DEFAULT_PUNISH);
return false;
}
// Proceed with item transfer
L2ItemInstance newItem = playerInventory.transferItem("PrivateStore", item.getObjectId(), item.getCount(), ownerInventory, player, _owner);
if(newItem == null){
if(Config.DEBUG){
_log.info("[PrivateStoreSell] newItem == null, return false");
}
return false;
}
removeItem(-1, item.getItemId(), item.getCount());
// Add changes to inventory update packets
if(oldItem.getCount() > 0 && oldItem != newItem)
{
playerIU.addModifiedItem(oldItem);
}
else
{
playerIU.addRemovedItem(oldItem);
}
if(newItem.getCount() > item.getCount())
{
ownerIU.addModifiedItem(newItem);
}
else
{
ownerIU.addNewItem(newItem);
}
// Send messages about the transaction to both players
if(newItem.isStackable())
{
SystemMessage msg = new SystemMessage(SystemMessageId.PURCHASED_S3_S2_S_FROM_S1);
msg.addString(player.getName());
msg.addItemName(newItem.getItemId());
msg.addNumber(item.getCount());
_owner.sendPacket(msg);
msg = null;
msg = new SystemMessage(SystemMessageId.S1_PURCHASED_S3_S2_S);
msg.addString(_owner.getName());
msg.addItemName(newItem.getItemId());
msg.addNumber(item.getCount());
player.sendPacket(msg);
msg = null;
}
else
{
SystemMessage msg = new SystemMessage(SystemMessageId.PURCHASED_S2_FROM_S1);
msg.addString(player.getName());
msg.addItemName(newItem.getItemId());
_owner.sendPacket(msg);
msg = null;
msg = new SystemMessage(SystemMessageId.S1_PURCHASED_S2);
msg.addString(_owner.getName());
msg.addItemName(newItem.getItemId());
player.sendPacket(msg);
msg = null;
}
newItem = null;
oldItem = null;
}
if(Config.SELL_BY_ITEM){
// Transfer Item
if(price > ownerInventory.getInventoryItemCount(Config.SELL_ITEM, -1))
{
lock();
if(Config.DEBUG){
_log.info("[PrivateStoreSell] price > ownerInventory.getInventoryItemCount(Config.SELL_ITEM, -1), return false");
}
return false;
}
L2ItemInstance item = ownerInventory.getItemByItemId(Config.SELL_ITEM);
if(item==null){
if(Config.DEBUG){
_log.info("[PrivateStoreSell] item==null, return false");
}
lock();
return false;
}
// Check if requested item is available for manipulation
L2ItemInstance oldItem = _owner.checkItemManipulation(item.getObjectId(), price, "sell");
if(oldItem == null)
{
lock();
if(Config.DEBUG){
_log.info("[PrivateStoreSell] _owner.checkItemManipulation(item.getObjectId(), price, 'sell')==null, return false");
}
return false;
}
// Proceed with item transfer
L2ItemInstance newItem = ownerInventory.transferItem("PrivateStore", item.getObjectId(), price, playerInventory,_owner, player);
if(newItem == null){
if(Config.DEBUG){
_log.info("[PrivateStoreSell] newItem = ownerInventory.transferItem('PrivateStore', item.getObjectId(), price, playerInventory,_owner, player) == null, return false");
}
return false;
}
// Add changes to inventory update packets
if(oldItem.getCount() > 0 && oldItem != newItem)
{
ownerIU.addModifiedItem(oldItem);
}
else
{
ownerIU.addRemovedItem(oldItem);
}
if(newItem.getCount() > item.getCount())
{
playerIU.addModifiedItem(newItem);
}
else
{
playerIU.addNewItem(newItem);
}
// Send messages about the transaction to both players
SystemMessage msg = SystemMessage.sendString("You obtained "+price+" "+item.getItemName());
player.sendPacket(msg);
msg = null;
SystemMessage msg2 = SystemMessage.sendString("You spent "+price+" "+item.getItemName());
_owner.sendPacket(msg2);
}else{
// Transfer adena
if(price > ownerInventory.getAdena()){
if(Config.DEBUG){
_log.info("[PrivateStoreSell] price > ownerInventory.getAdena(), return false");
}
return false;
}
L2ItemInstance adenaItem = ownerInventory.getAdenaInstance();
ownerInventory.reduceAdena("PrivateStore", price, _owner, player);
ownerIU.addItem(adenaItem);
playerInventory.addAdena("PrivateStore", price, player, _owner);
playerIU.addItem(playerInventory.getAdenaInstance());
}
// Send inventory update packet