|
@@ -22,7 +22,6 @@ import net.sf.l2j.gameserver.instancemanager.CastleManorManager.CropProcure;
|
|
|
import net.sf.l2j.gameserver.model.L2ItemInstance;
|
|
|
import net.sf.l2j.gameserver.model.L2Manor;
|
|
|
import net.sf.l2j.gameserver.model.L2Object;
|
|
|
-import net.sf.l2j.gameserver.model.actor.L2Npc;
|
|
|
import net.sf.l2j.gameserver.model.actor.instance.L2ManorManagerInstance;
|
|
|
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
|
|
|
import net.sf.l2j.gameserver.network.SystemMessageId;
|
|
@@ -31,7 +30,9 @@ import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
|
|
|
import net.sf.l2j.gameserver.network.serverpackets.StatusUpdate;
|
|
|
import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
|
|
|
import net.sf.l2j.gameserver.templates.item.L2Item;
|
|
|
-import net.sf.l2j.gameserver.util.Util;
|
|
|
+
|
|
|
+import static net.sf.l2j.gameserver.model.actor.L2Npc.INTERACTION_DISTANCE;
|
|
|
+import static net.sf.l2j.gameserver.model.itemcontainer.PcInventory.MAX_ADENA;
|
|
|
|
|
|
/**
|
|
|
* Format: (ch) d [dddd]
|
|
@@ -49,106 +50,82 @@ public class RequestProcureCropList extends L2GameClientPacket
|
|
|
{
|
|
|
private static final String _C__D0_09_REQUESTPROCURECROPLIST = "[C] D0:09 RequestProcureCropList";
|
|
|
|
|
|
- private int _size;
|
|
|
+ private static final int BATCH_LENGTH = 20; // length of the one item
|
|
|
|
|
|
- private int[] _items; // count*4
|
|
|
+ private Crop[] _items = null;
|
|
|
|
|
|
@Override
|
|
|
protected void readImpl()
|
|
|
{
|
|
|
- _size = readD();
|
|
|
- if (_size * 16 > _buf.remaining() || _size > 500)
|
|
|
+ int count = readD();
|
|
|
+ if(count <= 0
|
|
|
+ || count > Config.MAX_ITEM_IN_PACKET
|
|
|
+ || count * BATCH_LENGTH != _buf.remaining())
|
|
|
{
|
|
|
- _size = 0;
|
|
|
return;
|
|
|
}
|
|
|
- _items = new int[_size * 4];
|
|
|
- for (int i = 0; i < _size; i++)
|
|
|
+
|
|
|
+ _items = new Crop[count];
|
|
|
+ for (int i = 0; i < count; i++)
|
|
|
{
|
|
|
int objId = readD();
|
|
|
- _items[i * 4 + 0] = objId;
|
|
|
int itemId = readD();
|
|
|
- _items[i * 4 + 1] = itemId;
|
|
|
int manorId = readD();
|
|
|
- _items[i * 4 + 2] = manorId;
|
|
|
- long count = readQ();
|
|
|
- if (count > Integer.MAX_VALUE) count = Integer.MAX_VALUE;
|
|
|
- _items[i * 4 + 3] = (int)count;
|
|
|
+ long cnt = readQ();
|
|
|
+ if (objId < 1 || itemId < 1 || manorId < 0 || cnt < 0)
|
|
|
+ {
|
|
|
+ _items = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ _items[i] = new Crop(objId, itemId, manorId, cnt);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void runImpl()
|
|
|
{
|
|
|
+ if (_items == null)
|
|
|
+ return;
|
|
|
+
|
|
|
L2PcInstance player = getClient().getActiveChar();
|
|
|
if (player == null)
|
|
|
return;
|
|
|
|
|
|
- L2Object target = player.getTarget();
|
|
|
+ L2Object manager = player.getTarget();
|
|
|
+
|
|
|
+ if (!(manager instanceof L2ManorManagerInstance))
|
|
|
+ manager = player.getLastFolkNPC();
|
|
|
|
|
|
- if (!(target instanceof L2ManorManagerInstance))
|
|
|
- target = player.getLastFolkNPC();
|
|
|
+ if (!(manager instanceof L2ManorManagerInstance))
|
|
|
+ return;
|
|
|
|
|
|
- if (!player.isGM()
|
|
|
- && (target == null
|
|
|
- || !(target instanceof L2ManorManagerInstance) || !player
|
|
|
- .isInsideRadius(target,
|
|
|
- L2Npc.INTERACTION_DISTANCE, false,
|
|
|
- false)))
|
|
|
+ if (!player.isInsideRadius(manager, INTERACTION_DISTANCE, false, false))
|
|
|
return;
|
|
|
|
|
|
- if (_size < 1)
|
|
|
+ if (_items == null)
|
|
|
{
|
|
|
sendPacket(ActionFailed.STATIC_PACKET);
|
|
|
return;
|
|
|
}
|
|
|
- L2ManorManagerInstance manorManager = (L2ManorManagerInstance) target;
|
|
|
|
|
|
- int currentManorId = manorManager.getCastle().getCastleId();
|
|
|
+ int castleId = ((L2ManorManagerInstance)manager).getCastle().getCastleId();
|
|
|
|
|
|
// Calculate summary values
|
|
|
int slots = 0;
|
|
|
int weight = 0;
|
|
|
|
|
|
- for (int i = 0; i < _size; i++)
|
|
|
+ for (Crop i : _items)
|
|
|
{
|
|
|
- int itemId = _items[i * 4 + 1];
|
|
|
- int manorId = _items[i * 4 + 2];
|
|
|
- int count = _items[i * 4 + 3];
|
|
|
-
|
|
|
- if (itemId == 0 || manorId == 0 || count == 0)
|
|
|
- continue;
|
|
|
- if (count < 1)
|
|
|
+ if (!i.getCrop())
|
|
|
continue;
|
|
|
- if (count > Integer.MAX_VALUE)
|
|
|
- {
|
|
|
- Util.handleIllegalPlayerAction(player, "Warning!! Character "
|
|
|
- + player.getName() + " of account "
|
|
|
- + player.getAccountName() + " tried to purchase over "
|
|
|
- + Integer.MAX_VALUE + " items at the same time.",
|
|
|
- Config.DEFAULT_PUNISH);
|
|
|
- SystemMessage sm = new SystemMessage(
|
|
|
- SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED);
|
|
|
- sendPacket(sm);
|
|
|
- return;
|
|
|
- }
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
- CropProcure crop = CastleManager.getInstance().getCastleById(manorId).getCrop(itemId, CastleManorManager.PERIOD_CURRENT);
|
|
|
- int rewardItemId = L2Manor.getInstance().getRewardItem(itemId,crop.getReward());
|
|
|
- L2Item template = ItemTable.getInstance().getTemplate(rewardItemId);
|
|
|
- weight += count * template.getWeight();
|
|
|
-
|
|
|
- if (!template.isStackable())
|
|
|
- slots += count;
|
|
|
- else if (player.getInventory().getItemByItemId(itemId) == null)
|
|
|
- slots++;
|
|
|
- }
|
|
|
- catch (NullPointerException e)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
+ L2Item template = ItemTable.getInstance().getTemplate(i.getReward());
|
|
|
+ weight += i.getCount() * template.getWeight();
|
|
|
+
|
|
|
+ if (!template.isStackable())
|
|
|
+ slots += i.getCount();
|
|
|
+ else if (player.getInventory().getItemByItemId(i.getItemId()) == null)
|
|
|
+ slots++;
|
|
|
}
|
|
|
|
|
|
if (!player.getInventory().validateWeight(weight))
|
|
@@ -166,65 +143,30 @@ public class RequestProcureCropList extends L2GameClientPacket
|
|
|
// Proceed the purchase
|
|
|
InventoryUpdate playerIU = new InventoryUpdate();
|
|
|
|
|
|
- for (int i = 0; i < _size; i++)
|
|
|
+ for (Crop i : _items)
|
|
|
{
|
|
|
- int objId = _items[i * 4 + 0];
|
|
|
- int cropId = _items[i * 4 + 1];
|
|
|
- int manorId = _items[i * 4 + 2];
|
|
|
- int count = _items[i * 4 + 3];
|
|
|
-
|
|
|
- if (objId == 0 || cropId == 0 || manorId == 0 || count == 0)
|
|
|
- continue;
|
|
|
-
|
|
|
- if (count < 1)
|
|
|
- continue;
|
|
|
-
|
|
|
- CropProcure crop = null;
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- crop = CastleManager.getInstance().getCastleById(manorId).getCrop(cropId, CastleManorManager.PERIOD_CURRENT);
|
|
|
- }
|
|
|
- catch (NullPointerException e)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (crop == null || crop.getId() == 0 || crop.getPrice() == 0)
|
|
|
- continue;
|
|
|
-
|
|
|
- long fee = 0; // fee for selling to other manors
|
|
|
-
|
|
|
- int rewardItem = L2Manor.getInstance().getRewardItem(cropId,
|
|
|
- crop.getReward());
|
|
|
-
|
|
|
- if (count > crop.getAmount())
|
|
|
- continue;
|
|
|
+ long fee = i.getFee(castleId); // fee for selling to other manors
|
|
|
|
|
|
- long sellPrice = (count * crop.getPrice());
|
|
|
- long rewardPrice = ItemTable.getInstance().getTemplate(rewardItem).getReferencePrice();
|
|
|
+ long rewardPrice = ItemTable.getInstance().getTemplate(i.getReward()).getReferencePrice();
|
|
|
|
|
|
if (rewardPrice == 0)
|
|
|
continue;
|
|
|
|
|
|
- long rewardItemCount = sellPrice / rewardPrice;
|
|
|
+ long rewardItemCount = i.getPrice() / rewardPrice;
|
|
|
if (rewardItemCount < 1)
|
|
|
{
|
|
|
SystemMessage sm = new SystemMessage(SystemMessageId.FAILED_IN_TRADING_S2_OF_CROP_S1);
|
|
|
- sm.addItemName(cropId);
|
|
|
- sm.addItemNumber(count);
|
|
|
+ sm.addItemName(i.getItemId());
|
|
|
+ sm.addItemNumber(i.getCount());
|
|
|
player.sendPacket(sm);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- if (manorId != currentManorId)
|
|
|
- fee = sellPrice * 5 / 100; // 5% fee for selling to other manor
|
|
|
-
|
|
|
if (player.getInventory().getAdena() < fee)
|
|
|
{
|
|
|
SystemMessage sm = new SystemMessage(SystemMessageId.FAILED_IN_TRADING_S2_OF_CROP_S1);
|
|
|
- sm.addItemName(cropId);
|
|
|
- sm.addItemNumber(count);
|
|
|
+ sm.addItemName(i.getItemId());
|
|
|
+ sm.addItemNumber(i.getCount());
|
|
|
player.sendPacket(sm);
|
|
|
sm = new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA);
|
|
|
player.sendPacket(sm);
|
|
@@ -234,31 +176,24 @@ public class RequestProcureCropList extends L2GameClientPacket
|
|
|
// Add item to Inventory and adjust update packet
|
|
|
L2ItemInstance itemDel = null;
|
|
|
L2ItemInstance itemAdd = null;
|
|
|
- if (player.getInventory().getItemByObjectId(objId) != null)
|
|
|
- {
|
|
|
- // check if player have correct items count
|
|
|
- L2ItemInstance item = player.getInventory().getItemByObjectId(objId);
|
|
|
- if (item.getCount() < count)
|
|
|
- continue;
|
|
|
-
|
|
|
- itemDel = player.getInventory().destroyItem("Manor", objId, count, player, manorManager);
|
|
|
- if (itemDel == null)
|
|
|
- continue;
|
|
|
- if (fee > 0)
|
|
|
- player.getInventory().reduceAdena("Manor", fee, player,manorManager);
|
|
|
- crop.setAmount(crop.getAmount() - count);
|
|
|
- if (Config.ALT_MANOR_SAVE_ALL_ACTIONS)
|
|
|
- CastleManager.getInstance().getCastleById(manorId).updateCrop(crop.getId(), crop.getAmount(), CastleManorManager.PERIOD_CURRENT);
|
|
|
- itemAdd = player.getInventory().addItem("Manor", rewardItem,rewardItemCount, player, manorManager);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+
|
|
|
+ // check if player have correct items count
|
|
|
+ L2ItemInstance item = player.getInventory().getItemByObjectId(i.getObjectId());
|
|
|
+ if (item == null || item.getCount() < i.getCount())
|
|
|
continue;
|
|
|
- }
|
|
|
|
|
|
- if (itemDel == null || itemAdd == null)
|
|
|
+ itemDel = player.getInventory().destroyItem("Manor", i.getObjectId(), i.getCount(), player, manager);
|
|
|
+ if (itemDel == null)
|
|
|
continue;
|
|
|
|
|
|
+ if (fee > 0)
|
|
|
+ player.getInventory().reduceAdena("Manor", fee, player, manager);
|
|
|
+
|
|
|
+ i.setCrop();
|
|
|
+
|
|
|
+ itemAdd = player.getInventory().addItem("Manor", i.getReward(), rewardItemCount, player, manager);
|
|
|
+ if (itemAdd == null)
|
|
|
+ continue;
|
|
|
|
|
|
playerIU.addRemovedItem(itemDel);
|
|
|
if (itemAdd.getCount() > rewardItemCount)
|
|
@@ -269,8 +204,8 @@ public class RequestProcureCropList extends L2GameClientPacket
|
|
|
// Send System Messages
|
|
|
SystemMessage sm = new SystemMessage(
|
|
|
SystemMessageId.TRADED_S2_OF_CROP_S1);
|
|
|
- sm.addItemName(cropId);
|
|
|
- sm.addItemNumber(count);
|
|
|
+ sm.addItemName(i.getItemId());
|
|
|
+ sm.addItemNumber(i.getCount());
|
|
|
player.sendPacket(sm);
|
|
|
|
|
|
if (fee > 0)
|
|
@@ -281,8 +216,8 @@ public class RequestProcureCropList extends L2GameClientPacket
|
|
|
}
|
|
|
|
|
|
sm = new SystemMessage(SystemMessageId.S2_S1_DISAPPEARED);
|
|
|
- sm.addItemName(cropId);
|
|
|
- sm.addItemNumber(count);
|
|
|
+ sm.addItemName(i.getItemId());
|
|
|
+ sm.addItemNumber(i.getCount());
|
|
|
player.sendPacket(sm);
|
|
|
|
|
|
if (fee > 0)
|
|
@@ -304,7 +239,87 @@ public class RequestProcureCropList extends L2GameClientPacket
|
|
|
StatusUpdate su = new StatusUpdate(player.getObjectId());
|
|
|
su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
|
|
|
player.sendPacket(su);
|
|
|
+ }
|
|
|
+
|
|
|
+ private class Crop
|
|
|
+ {
|
|
|
+ private final int _objectId;
|
|
|
+ private final int _itemId;
|
|
|
+ private final int _manorId;
|
|
|
+ private final long _count;
|
|
|
+ private int _reward;
|
|
|
+ private CropProcure _crop = null;
|
|
|
+
|
|
|
+ public Crop(int obj, int id, int m, long num)
|
|
|
+ {
|
|
|
+ _objectId = obj;
|
|
|
+ _itemId = id;
|
|
|
+ _manorId = m;
|
|
|
+ _count = num;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getObjectId()
|
|
|
+ {
|
|
|
+ return _objectId;
|
|
|
+ }
|
|
|
|
|
|
+ public int getItemId()
|
|
|
+ {
|
|
|
+ return _itemId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getCount()
|
|
|
+ {
|
|
|
+ return _count;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getReward()
|
|
|
+ {
|
|
|
+ return _reward;
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getPrice()
|
|
|
+ {
|
|
|
+ return _crop.getPrice() * _count;
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getFee(int castleId)
|
|
|
+ {
|
|
|
+ if (_manorId == castleId)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ return (getPrice() /100) * 5; // 5% fee for selling to other manor
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean getCrop()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ _crop = CastleManager.getInstance().getCastleById(_manorId).getCrop(_itemId, CastleManorManager.PERIOD_CURRENT);
|
|
|
+ }
|
|
|
+ catch (NullPointerException e)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (_crop == null || _crop.getId() == 0 || _crop.getPrice() == 0)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (_count > _crop.getAmount())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if ((MAX_ADENA / _count) < _crop.getPrice())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ _reward = L2Manor.getInstance().getRewardItem(_itemId, _crop.getReward());
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCrop()
|
|
|
+ {
|
|
|
+ _crop.setAmount(_crop.getAmount() - _count);
|
|
|
+ if (Config.ALT_MANOR_SAVE_ALL_ACTIONS)
|
|
|
+ CastleManager.getInstance().getCastleById(_manorId).updateCrop(_itemId, _crop.getAmount(), CastleManorManager.PERIOD_CURRENT);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|