Advertisement
Guest User

SetPrivateStoreListBuy issue

a guest
Oct 2nd, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. /*
  2.  * This program is free software: you can redistribute it and/or modify it under
  3.  * the terms of the GNU General Public License as published by the Free Software
  4.  * Foundation, either version 3 of the License, or (at your option) any later
  5.  * version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT
  8.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10.  * details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License along with
  13.  * this program. If not, see <http://www.gnu.org/licenses/>.
  14.  */
  15. package net.sf.l2j.gameserver.network.clientpackets;
  16.  
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.model.TradeList;
  19. import net.sf.l2j.gameserver.model.actor.L2Character;
  20. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  21. import net.sf.l2j.gameserver.network.SystemMessageId;
  22. import net.sf.l2j.gameserver.network.serverpackets.PrivateStoreManageListBuy;
  23. import net.sf.l2j.gameserver.network.serverpackets.PrivateStoreMsgBuy;
  24. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  25. import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
  26.  
  27. public final class SetPrivateStoreListBuy extends L2GameClientPacket
  28. {
  29.     private static final int BATCH_LENGTH = 12; // length of one item
  30.    
  31.     private Item[] _items = null;
  32.  
  33.     @Override
  34.     protected void readImpl()
  35.     {
  36.         int count = readD();
  37.         if (count < 1 || count > Config.MAX_ITEM_IN_PACKET || count * BATCH_LENGTH != _buf.remaining())
  38.             return;
  39.        
  40.         _items = new Item[count];
  41.         for (int i = 0; i < count; i++)
  42.         {
  43.             int itemId = readD();
  44.             readH(); //TODO analyse this
  45.             readH(); //TODO analyse this
  46.             long cnt = readD();
  47.             int price = readD();
  48.            
  49.             if (itemId < 1 || cnt < 1 || price < 0)
  50.             {
  51.                 _items = null;
  52.                 return;
  53.             }
  54.             _items[i] = new Item(itemId, (int)cnt, price);
  55.         }
  56.     }
  57.  
  58.     @Override
  59.     protected void runImpl()
  60.     {
  61.         L2PcInstance player = getClient().getActiveChar();
  62.         if (player == null)
  63.             return;
  64.  
  65.         if (_items == null)
  66.         {
  67.             player.setPrivateStoreType(L2PcInstance.STORE_PRIVATE_NONE);
  68.             player.broadcastUserInfo();
  69.             player.sendPacket(new PrivateStoreManageListBuy(player));
  70.             return;
  71.         }
  72.  
  73.         if (!player.getAccessLevel().allowTransaction())
  74.         {
  75.             player.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_DO_THAT));
  76.             return;
  77.         }
  78.        
  79.         if (AttackStanceTaskManager.getInstance().getAttackStanceTask(player)
  80.             || (player.isCastingNow() || player.isCastingSimultaneouslyNow())
  81.             || player.isInDuel())
  82.         {
  83.             player.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_DO_THAT));
  84.             player.sendPacket(new PrivateStoreManageListBuy(player));
  85.             return;
  86.         }
  87.        
  88.         if (player.isInsideZone(L2Character.ZONE_NOSTORE))
  89.         {
  90.             player.sendPacket(new SystemMessage(SystemMessageId.NO_PRIVATE_STORE_HERE));
  91.             player.sendPacket(new PrivateStoreManageListBuy(player));
  92.             return;
  93.         }
  94.  
  95.         TradeList tradeList = player.getBuyList();
  96.         tradeList.clear();
  97.  
  98.         // Check maximum number of allowed slots for pvt shops
  99.         if (_items.length > player.getPrivateBuyStoreLimit())
  100.         {
  101.             player.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED));
  102.             player.sendPacket(new PrivateStoreManageListBuy(player));
  103.             return;
  104.         }
  105.        
  106.         int totalCost = 0;
  107.         for (Item i : _items)
  108.         {
  109.             if (!i.addToTradeList(tradeList))
  110.             {
  111.                 player.sendPacket(new SystemMessage(SystemMessageId.EXCEEDED_THE_MAXIMUM));
  112.                 player.sendPacket(new PrivateStoreManageListBuy(player));
  113.                 return;
  114.             }
  115.            
  116.             totalCost += i.getCost();
  117.             if (totalCost > Integer.MAX_VALUE)
  118.             {
  119.                 player.sendPacket(new SystemMessage(SystemMessageId.EXCEEDED_THE_MAXIMUM));
  120.                 player.sendPacket(new PrivateStoreManageListBuy(player));
  121.                 return;
  122.             }
  123.         }
  124.  
  125.         // Check for available funds
  126.         if (totalCost > player.getAdena())
  127.         {
  128.             player.sendPacket(new SystemMessage(SystemMessageId.THE_PURCHASE_PRICE_IS_HIGHER_THAN_MONEY));
  129.             player.sendPacket(new PrivateStoreManageListBuy(player));
  130.             return;
  131.         }
  132.  
  133.         player.sitDown();
  134.         player.setPrivateStoreType(L2PcInstance.STORE_PRIVATE_BUY);
  135.         player.broadcastUserInfo();
  136.         player.broadcastPacket(new PrivateStoreMsgBuy(player));
  137.     }
  138.  
  139.     private static class Item
  140.     {
  141.         private final int _itemId, _count, _price;
  142.        
  143.         public Item(int id, int num, int pri)
  144.         {
  145.             _itemId = id;
  146.             _count = num;
  147.             _price = pri;
  148.         }
  149.        
  150.         public boolean addToTradeList(TradeList list)
  151.         {
  152.             if ((Integer.MAX_VALUE / _count) < _price)
  153.                 return false;
  154.            
  155.             list.addItemByItemId(_itemId, _count, _price);
  156.             return true;
  157.         }
  158.        
  159.         public long getCost()
  160.         {
  161.             return _count * _price;
  162.         }
  163.     }
  164.  
  165.     @Override
  166.     public String getType()
  167.     {
  168.         return "[C] 91 SetPrivateStoreListBuy";
  169.     }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement