RequestPrivateStoreBuy.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.clientpackets;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.model.ItemRequest;
  19. import net.sf.l2j.gameserver.model.L2Object;
  20. import net.sf.l2j.gameserver.model.L2World;
  21. import net.sf.l2j.gameserver.model.TradeList;
  22. import net.sf.l2j.gameserver.model.TradeList.TradeItem;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  24. import net.sf.l2j.gameserver.network.SystemMessageId;
  25. import net.sf.l2j.gameserver.serverpackets.ActionFailed;
  26. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  27. import net.sf.l2j.gameserver.util.Util;
  28. /**
  29. * This class ...
  30. *
  31. * @version $Revision: 1.2.2.1.2.5 $ $Date: 2005/03/27 15:29:30 $
  32. */
  33. public final class RequestPrivateStoreBuy extends L2GameClientPacket
  34. {
  35. // private static final String _C__79_SENDPRIVATESTOREBUYLIST = "[C] 79 SendPrivateStoreBuyList";
  36. private static final String _C__79_REQUESTPRIVATESTOREBUY = "[C] 79 RequestPrivateStoreBuy";
  37. private static Logger _log = Logger.getLogger(RequestPrivateStoreBuy.class.getName());
  38. private int _storePlayerId;
  39. private int _count;
  40. private ItemRequest[] _items;
  41. @Override
  42. protected void readImpl()
  43. {
  44. _storePlayerId = readD();
  45. _count = readD();
  46. // count*12 is the size of a for iteration of each item
  47. if (_count < 0 || _count * 12 > _buf.remaining() || _count > Config.MAX_ITEM_IN_PACKET)
  48. {
  49. _count = 0;
  50. }
  51. _items = new ItemRequest[_count];
  52. for (int i = 0; i < _count ; i++)
  53. {
  54. int objectId = readD();
  55. long count = readD();
  56. if (count > Integer.MAX_VALUE) count = Integer.MAX_VALUE;
  57. int price = readD();
  58. _items[i] = new ItemRequest(objectId, (int)count, price);
  59. }
  60. }
  61. @Override
  62. protected void runImpl()
  63. {
  64. L2PcInstance player = getClient().getActiveChar();
  65. if (player == null) return;
  66. L2Object object = L2World.getInstance().findObject(_storePlayerId);
  67. if (!(object instanceof L2PcInstance)) return;
  68. if(player.isCursedWeaponEquipped())
  69. return;
  70. L2PcInstance storePlayer = (L2PcInstance)object;
  71. if (!(storePlayer.getPrivateStoreType() == L2PcInstance.STORE_PRIVATE_SELL || storePlayer.getPrivateStoreType() == L2PcInstance.STORE_PRIVATE_PACKAGE_SELL)) return;
  72. TradeList storeList = storePlayer.getSellList();
  73. if (storeList == null) return;
  74. if (!player.getAccessLevel().allowTransaction())
  75. {
  76. player.sendMessage("Transactions are disable for your Access Level");
  77. sendPacket(ActionFailed.STATIC_PACKET);
  78. return;
  79. }
  80. // FIXME: this check should be (and most probably is) done in the TradeList mechanics
  81. long priceTotal = 0;
  82. for(ItemRequest ir : _items)
  83. {
  84. if (ir.getCount() > Integer.MAX_VALUE || ir.getCount() < 0)
  85. {
  86. String msgErr = "[RequestPrivateStoreBuy] player "+getClient().getActiveChar().getName()+" tried an overflow exploit, ban this player!";
  87. Util.handleIllegalPlayerAction(getClient().getActiveChar(),msgErr,Config.DEFAULT_PUNISH);
  88. return;
  89. }
  90. TradeItem sellersItem = storeList.getItem(ir.getObjectId());
  91. if(sellersItem == null)
  92. {
  93. String msgErr = "[RequestPrivateStoreBuy] player "+getClient().getActiveChar().getName()+" tried to buy an item not sold in a private store (buy), ban this player!";
  94. Util.handleIllegalPlayerAction(getClient().getActiveChar(),msgErr,Config.DEFAULT_PUNISH);
  95. return;
  96. }
  97. if(ir.getPrice() != sellersItem.getPrice())
  98. {
  99. String msgErr = "[RequestPrivateStoreBuy] player "+getClient().getActiveChar().getName()+" tried to change the seller's price in a private store (buy), ban this player!";
  100. Util.handleIllegalPlayerAction(getClient().getActiveChar(),msgErr,Config.DEFAULT_PUNISH);
  101. return;
  102. }
  103. priceTotal += ir.getPrice() * ir.getCount();
  104. }
  105. // FIXME: this check should be (and most probably is) done in the TradeList mechanics
  106. if(priceTotal < 0 || priceTotal > Integer.MAX_VALUE)
  107. {
  108. String msgErr = "[RequestPrivateStoreBuy] player "+getClient().getActiveChar().getName()+" tried an overflow exploit, ban this player!";
  109. Util.handleIllegalPlayerAction(getClient().getActiveChar(),msgErr,Config.DEFAULT_PUNISH);
  110. return;
  111. }
  112. if (player.getAdena() < priceTotal)
  113. {
  114. sendPacket(new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));
  115. sendPacket(ActionFailed.STATIC_PACKET);
  116. return;
  117. }
  118. if (storePlayer.getPrivateStoreType() == L2PcInstance.STORE_PRIVATE_PACKAGE_SELL)
  119. {
  120. if (storeList.getItemCount() > _count)
  121. {
  122. String msgErr = "[RequestPrivateStoreBuy] player "+getClient().getActiveChar().getName()+" tried to buy less items then sold by package-sell, ban this player for bot-usage!";
  123. Util.handleIllegalPlayerAction(getClient().getActiveChar(),msgErr,Config.DEFAULT_PUNISH);
  124. return;
  125. }
  126. }
  127. if (!storeList.privateStoreBuy(player, _items, (int) priceTotal))
  128. {
  129. sendPacket(ActionFailed.STATIC_PACKET);
  130. _log.warning("PrivateStore buy has failed due to invalid list or request. Player: " + player.getName() + ", Private store of: " + storePlayer.getName());
  131. return;
  132. }
  133. if (storeList.getItemCount() == 0)
  134. {
  135. storePlayer.setPrivateStoreType(L2PcInstance.STORE_PRIVATE_NONE);
  136. storePlayer.broadcastUserInfo();
  137. }
  138. /* Lease holders are currently not implemented
  139. else if (_seller != null)
  140. {
  141. // lease shop sell
  142. L2MerchantInstance seller = (L2MerchantInstance)_seller;
  143. L2ItemInstance ladena = seller.getLeaseAdena();
  144. for (TradeItem ti : buyerlist) {
  145. L2ItemInstance li = seller.getLeaseItemByObjectId(ti.getObjectId());
  146. if (li == null) {
  147. if (ti.getObjectId() == ladena.getObjectId())
  148. {
  149. buyer.addAdena(ti.getCount());
  150. ladena.setCount(ladena.getCount()-ti.getCount());
  151. ladena.updateDatabase();
  152. }
  153. continue;
  154. }
  155. int cnt = li.getCount();
  156. if (cnt < ti.getCount())
  157. ti.setCount(cnt);
  158. if (ti.getCount() <= 0)
  159. continue;
  160. L2ItemInstance inst = ItemTable.getInstance().createItem(li.getItemId());
  161. inst.setCount(ti.getCount());
  162. inst.setEnchantLevel(li.getEnchantLevel());
  163. buyer.getInventory().addItem(inst);
  164. li.setCount(li.getCount()-ti.getCount());
  165. li.updateDatabase();
  166. ladena.setCount(ladena.getCount()+ti.getCount()*ti.getOwnersPrice());
  167. ladena.updateDatabase();
  168. }
  169. }*/
  170. }
  171. @Override
  172. public String getType()
  173. {
  174. return _C__79_REQUESTPRIVATESTOREBUY;
  175. }
  176. }