Castle castle = ResidenceHolder.getInstance().getResidence(Castle.class, manorId);
if (castle == null)
{
return;
}
CropProcure crop = castle.getCrop(cropId, CastleManorManager.PERIOD_CURRENT);
if ((crop == null) || (crop.getId() == 0) || (crop.getPrice() == 0))
{
return;
}
if (count > crop.getAmount())
{
return;
}
long price = SafeMath.mulAndCheck(count, crop.getPrice());
long fee = 0;
if ((currentManorId != 0) && (manorId != currentManorId))
{
fee = (price * 5) / 100;
}
totalFee = SafeMath.addAndCheck(totalFee, fee);
int rewardItemId = Manor.getInstance().getRewardItem(cropId, crop.getReward());
ItemTemplate template = ItemHolder.getInstance().getTemplate(rewardItemId);
if (template == null)
{
return;
}
weight = SafeMath.addAndCheck(weight, SafeMath.mulAndCheck(count, template.getWeight()));
if (!template.isStackable() || (activeChar.getInventory().getItemByItemId(cropId) == null))
{
slots++;
}
}
}
catch (ArithmeticException ae)
{
sendPacket(Msg.YOU_HAVE_EXCEEDED_THE_QUANTITY_THAT_CAN_BE_INPUTTED);
return;
}
activeChar.getInventory().writeLock();
try
{
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.getInventory().getAdena() < totalFee)
{
activeChar.sendPacket(Msg.YOU_DO_NOT_HAVE_ENOUGH_ADENA);
return;
}
for (int i = 0; i < _count; i++)
{
int objId = _items[i];
int cropId = _crop[i];
int manorId = _manor[i];
long count = _itemQ[i];
ItemInstance item = activeChar.getInventory().getItemByObjectId(objId);
if ((item == null) || (item.getCount() < count) || (item.getItemId() != cropId))
{
continue;
}
Castle castle = ResidenceHolder.getInstance().getResidence(Castle.class, manorId);
if (castle == null)
{
continue;
}
CropProcure crop = castle.getCrop(cropId, CastleManorManager.PERIOD_CURRENT);
if ((crop == null) || (crop.getId() == 0) || (crop.getPrice() == 0))
{
continue;
}
if (count > crop.getAmount())
{
continue;
}
int rewardItemId = Manor.getInstance().getRewardItem(cropId, crop.getReward());
long sellPrice = count * crop.getPrice();
long rewardPrice = ItemHolder.getInstance().getTemplate(rewardItemId).getReferencePrice();
if (rewardPrice == 0)
{
continue;
}
double reward = (double) sellPrice / rewardPrice;
long rewardItemCount = (long) reward + (Rnd.nextDouble() <= (reward % 1) ? 1 : 0);
if (rewardItemCount < 1)
{
SystemMessage sm = new SystemMessage(SystemMessage.FAILED_IN_TRADING_S2_OF_CROP_S1);
sm.addItemName(cropId);
sm.addNumber(count);
activeChar.sendPacket(sm);
continue;
}
long fee = 0;
if ((currentManorId != 0) && (manorId != currentManorId))
{
fee = (sellPrice * 5) / 100;
}
if (!activeChar.getInventory().destroyItemByObjectId(objId, count))
{
continue;
}
if (!activeChar.reduceAdena(fee, false))
{
SystemMessage sm = new SystemMessage(SystemMessage.FAILED_IN_TRADING_S2_OF_CROP_S1);
sm.addItemName(cropId);
sm.addNumber(count);
activeChar.sendPacket(sm, Msg.YOU_DO_NOT_HAVE_ENOUGH_ADENA);
continue;
}
crop.setAmount(crop.getAmount() - count);
castle.updateCrop(crop.getId(), crop.getAmount(), CastleManorManager.PERIOD_CURRENT);
castle.addToTreasuryNoTax(fee, false, false);
if (activeChar.getInventory().addItem(rewardItemId, rewardItemCount) == null)
{
continue;
}