RequestBuyItem.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.clientpackets;
  20. import static com.l2jserver.gameserver.model.actor.L2Npc.INTERACTION_DISTANCE;
  21. import static com.l2jserver.gameserver.model.itemcontainer.Inventory.MAX_ADENA;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.data.xml.impl.BuyListData;
  26. import com.l2jserver.gameserver.model.L2Object;
  27. import com.l2jserver.gameserver.model.actor.L2Character;
  28. import com.l2jserver.gameserver.model.actor.instance.L2MerchantInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.buylist.L2BuyList;
  31. import com.l2jserver.gameserver.model.buylist.Product;
  32. import com.l2jserver.gameserver.model.holders.ItemHolder;
  33. import com.l2jserver.gameserver.network.SystemMessageId;
  34. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  35. import com.l2jserver.gameserver.network.serverpackets.ExBuySellList;
  36. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  37. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  38. import com.l2jserver.gameserver.util.Util;
  39. public final class RequestBuyItem extends L2GameClientPacket
  40. {
  41. private static final String _C__40_REQUESTBUYITEM = "[C] 40 RequestBuyItem";
  42. private static final int BATCH_LENGTH = 12;
  43. private int _listId;
  44. private List<ItemHolder> _items = null;
  45. @Override
  46. protected void readImpl()
  47. {
  48. _listId = readD();
  49. int size = readD();
  50. if ((size <= 0) || (size > Config.MAX_ITEM_IN_PACKET) || ((size * BATCH_LENGTH) != _buf.remaining()))
  51. {
  52. return;
  53. }
  54. _items = new ArrayList<>(size);
  55. for (int i = 0; i < size; i++)
  56. {
  57. int itemId = readD();
  58. long count = readQ();
  59. if ((itemId < 1) || (count < 1))
  60. {
  61. _items = null;
  62. return;
  63. }
  64. _items.add(new ItemHolder(itemId, count));
  65. }
  66. }
  67. @Override
  68. protected void runImpl()
  69. {
  70. L2PcInstance player = getClient().getActiveChar();
  71. if (player == null)
  72. {
  73. return;
  74. }
  75. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("buy"))
  76. {
  77. player.sendMessage("You are buying too fast.");
  78. return;
  79. }
  80. if (_items == null)
  81. {
  82. sendPacket(ActionFailed.STATIC_PACKET);
  83. return;
  84. }
  85. // Alt game - Karma punishment
  86. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && (player.getKarma() > 0))
  87. {
  88. sendPacket(ActionFailed.STATIC_PACKET);
  89. return;
  90. }
  91. L2Object target = player.getTarget();
  92. L2Character merchant = null;
  93. if (!player.isGM())
  94. {
  95. if (!(target instanceof L2MerchantInstance) || (!player.isInsideRadius(target, INTERACTION_DISTANCE, true, false)) || (player.getInstanceId() != target.getInstanceId()))
  96. {
  97. sendPacket(ActionFailed.STATIC_PACKET);
  98. return;
  99. }
  100. merchant = (L2Character) target;
  101. }
  102. double castleTaxRate = 0;
  103. double baseTaxRate = 0;
  104. if ((merchant == null) && !player.isGM())
  105. {
  106. sendPacket(ActionFailed.STATIC_PACKET);
  107. return;
  108. }
  109. final L2BuyList buyList = BuyListData.getInstance().getBuyList(_listId);
  110. if (buyList == null)
  111. {
  112. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  113. return;
  114. }
  115. if (merchant != null)
  116. {
  117. if (!buyList.isNpcAllowed(merchant.getId()))
  118. {
  119. sendPacket(ActionFailed.STATIC_PACKET);
  120. return;
  121. }
  122. if (merchant instanceof L2MerchantInstance)
  123. {
  124. castleTaxRate = ((L2MerchantInstance) merchant).getMpc().getCastleTaxRate();
  125. baseTaxRate = ((L2MerchantInstance) merchant).getMpc().getBaseTaxRate();
  126. }
  127. else
  128. {
  129. baseTaxRate = 0.5;
  130. }
  131. }
  132. long subTotal = 0;
  133. // Check for buylist validity and calculates summary values
  134. long slots = 0;
  135. long weight = 0;
  136. for (ItemHolder i : _items)
  137. {
  138. long price = -1;
  139. final Product product = buyList.getProductByItemId(i.getId());
  140. if (product == null)
  141. {
  142. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId + " and item_id " + i.getId(), Config.DEFAULT_PUNISH);
  143. return;
  144. }
  145. if (!product.getItem().isStackable() && (i.getCount() > 1))
  146. {
  147. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried to purchase invalid quantity of items at the same time.", Config.DEFAULT_PUNISH);
  148. sendPacket(SystemMessage.getSystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED));
  149. return;
  150. }
  151. price = product.getPrice();
  152. if ((product.getItemId() >= 3960) && (product.getItemId() <= 4026))
  153. {
  154. price *= Config.RATE_SIEGE_GUARDS_PRICE;
  155. }
  156. if (price < 0)
  157. {
  158. _log.warning("ERROR, no price found .. wrong buylist ??");
  159. sendPacket(ActionFailed.STATIC_PACKET);
  160. return;
  161. }
  162. if ((price == 0) && !player.isGM() && Config.ONLY_GM_ITEMS_FREE)
  163. {
  164. player.sendMessage("Ohh Cheat dont work? You have a problem now!");
  165. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried buy item for 0 adena.", Config.DEFAULT_PUNISH);
  166. return;
  167. }
  168. if (product.hasLimitedStock())
  169. {
  170. // trying to buy more then available
  171. if (i.getCount() > product.getCount())
  172. {
  173. sendPacket(ActionFailed.STATIC_PACKET);
  174. return;
  175. }
  176. }
  177. if ((MAX_ADENA / i.getCount()) < price)
  178. {
  179. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried to purchase over " + MAX_ADENA + " adena worth of goods.", Config.DEFAULT_PUNISH);
  180. return;
  181. }
  182. // first calculate price per item with tax, then multiply by count
  183. price = (long) (price * (1 + castleTaxRate + baseTaxRate));
  184. subTotal += i.getCount() * price;
  185. if (subTotal > MAX_ADENA)
  186. {
  187. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried to purchase over " + MAX_ADENA + " adena worth of goods.", Config.DEFAULT_PUNISH);
  188. return;
  189. }
  190. weight += i.getCount() * product.getItem().getWeight();
  191. if (player.getInventory().getItemByItemId(product.getItemId()) == null)
  192. {
  193. slots++;
  194. }
  195. }
  196. if (!player.isGM() && ((weight > Integer.MAX_VALUE) || (weight < 0) || !player.getInventory().validateWeight((int) weight)))
  197. {
  198. player.sendPacket(SystemMessageId.WEIGHT_LIMIT_EXCEEDED);
  199. sendPacket(ActionFailed.STATIC_PACKET);
  200. return;
  201. }
  202. if (!player.isGM() && ((slots > Integer.MAX_VALUE) || (slots < 0) || !player.getInventory().validateCapacity((int) slots)))
  203. {
  204. player.sendPacket(SystemMessageId.SLOTS_FULL);
  205. sendPacket(ActionFailed.STATIC_PACKET);
  206. return;
  207. }
  208. // Charge buyer and add tax to castle treasury if not owned by npc clan
  209. if ((subTotal < 0) || !player.reduceAdena("Buy", subTotal, player.getLastFolkNPC(), false))
  210. {
  211. player.sendPacket(SystemMessageId.YOU_NOT_ENOUGH_ADENA);
  212. sendPacket(ActionFailed.STATIC_PACKET);
  213. return;
  214. }
  215. // Proceed the purchase
  216. for (ItemHolder i : _items)
  217. {
  218. Product product = buyList.getProductByItemId(i.getId());
  219. if (product == null)
  220. {
  221. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId + " and item_id " + i.getId(), Config.DEFAULT_PUNISH);
  222. continue;
  223. }
  224. if (product.hasLimitedStock())
  225. {
  226. if (product.decreaseCount(i.getCount()))
  227. {
  228. player.getInventory().addItem("Buy", i.getId(), i.getCount(), player, merchant);
  229. }
  230. }
  231. else
  232. {
  233. player.getInventory().addItem("Buy", i.getId(), i.getCount(), player, merchant);
  234. }
  235. }
  236. // add to castle treasury
  237. if (merchant instanceof L2MerchantInstance)
  238. {
  239. ((L2MerchantInstance) merchant).getCastle().addToTreasury((long) (subTotal * castleTaxRate));
  240. }
  241. StatusUpdate su = new StatusUpdate(player);
  242. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  243. player.sendPacket(su);
  244. player.sendPacket(new ExBuySellList(player, true));
  245. }
  246. @Override
  247. public String getType()
  248. {
  249. return _C__40_REQUESTBUYITEM;
  250. }
  251. }