RequestBuyItem.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 com.l2jserver.gameserver.network.clientpackets;
  16. import static com.l2jserver.gameserver.model.actor.L2Npc.INTERACTION_DISTANCE;
  17. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.MAX_ADENA;
  18. import java.util.List;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.TradeController;
  22. import com.l2jserver.gameserver.datatables.ItemTable;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.L2TradeList;
  25. import com.l2jserver.gameserver.model.L2TradeList.L2TradeItem;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.actor.instance.L2MerchantInstance;
  28. import com.l2jserver.gameserver.model.actor.instance.L2MerchantSummonInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  32. import com.l2jserver.gameserver.network.serverpackets.ExBuySellListPacket;
  33. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  34. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  35. import com.l2jserver.gameserver.templates.item.L2Item;
  36. import com.l2jserver.gameserver.util.Util;
  37. /**
  38. * RequestBuyItem client packet class.
  39. */
  40. public final class RequestBuyItem extends L2GameClientPacket
  41. {
  42. private static final String _C__1F_REQUESTBUYITEM = "[C] 1F RequestBuyItem";
  43. private static Logger _log = Logger.getLogger(RequestBuyItem.class.getName());
  44. private static final int BATCH_LENGTH = 12; // length of the one item
  45. private int _listId;
  46. private Item[] _items = null;
  47. @Override
  48. protected void readImpl()
  49. {
  50. _listId = readD();
  51. int count = readD();
  52. if (count <= 0 || count > Config.MAX_ITEM_IN_PACKET || count * BATCH_LENGTH != _buf.remaining())
  53. {
  54. return;
  55. }
  56. _items = new Item[count];
  57. for (int i = 0; i < count; i++)
  58. {
  59. int itemId = readD();
  60. long cnt = readQ();
  61. if (itemId < 1 || cnt < 1)
  62. {
  63. _items = null;
  64. return;
  65. }
  66. _items[i] = new Item(itemId, cnt);
  67. }
  68. }
  69. @Override
  70. protected void runImpl()
  71. {
  72. L2PcInstance player = getClient().getActiveChar();
  73. if (player == null)
  74. return;
  75. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("buy"))
  76. {
  77. player.sendMessage("You 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. if (!player.isGM() && (target == null // No target (ie GM Shop)
  93. || !(target instanceof L2MerchantInstance || target instanceof L2MerchantSummonInstance) || player.getInstanceId() != target.getInstanceId() || !player.isInsideRadius(target, INTERACTION_DISTANCE, true, false))) // Distance is too far
  94. {
  95. sendPacket(ActionFailed.STATIC_PACKET);
  96. return;
  97. }
  98. L2Character merchant = null;
  99. if (target instanceof L2MerchantInstance || target instanceof L2MerchantSummonInstance)
  100. merchant = (L2Character) target;
  101. else if (!player.isGM())
  102. {
  103. sendPacket(ActionFailed.STATIC_PACKET);
  104. return;
  105. }
  106. L2TradeList list = null;
  107. double castleTaxRate = 0;
  108. double baseTaxRate = 0;
  109. if (merchant != null)
  110. {
  111. List<L2TradeList> lists;
  112. if (merchant instanceof L2MerchantInstance)
  113. {
  114. lists = TradeController.getInstance().getBuyListByNpcId(((L2MerchantInstance) merchant).getNpcId());
  115. castleTaxRate = ((L2MerchantInstance) merchant).getMpc().getCastleTaxRate();
  116. baseTaxRate = ((L2MerchantInstance) merchant).getMpc().getBaseTaxRate();
  117. }
  118. else
  119. {
  120. lists = TradeController.getInstance().getBuyListByNpcId(((L2MerchantSummonInstance) merchant).getNpcId());
  121. baseTaxRate = 50;
  122. }
  123. if (!player.isGM())
  124. {
  125. if (lists == null)
  126. {
  127. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  128. return;
  129. }
  130. for (L2TradeList tradeList : lists)
  131. {
  132. if (tradeList.getListId() == _listId)
  133. list = tradeList;
  134. }
  135. }
  136. else
  137. list = TradeController.getInstance().getBuyList(_listId);
  138. }
  139. else
  140. list = TradeController.getInstance().getBuyList(_listId);
  141. if (list == null)
  142. {
  143. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  144. return;
  145. }
  146. _listId = list.getListId();
  147. long subTotal = 0;
  148. // Check for buylist validity and calculates summary values
  149. long slots = 0;
  150. long weight = 0;
  151. for (Item i : _items)
  152. {
  153. long price = -1;
  154. L2TradeItem tradeItem = list.getItemById(i.getItemId());
  155. if (tradeItem == null)
  156. {
  157. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId + " and item_id " + i.getItemId(), Config.DEFAULT_PUNISH);
  158. return;
  159. }
  160. L2Item template = ItemTable.getInstance().getTemplate(i.getItemId());
  161. if (template == null)
  162. continue;
  163. if (!template.isStackable() && i.getCount() > 1)
  164. {
  165. 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);
  166. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED);
  167. sendPacket(sm);
  168. sm = null;
  169. return;
  170. }
  171. price = list.getPriceForItemId(i.getItemId());
  172. if (i.getItemId() >= 3960 && i.getItemId() <= 4026)
  173. price *= Config.RATE_SIEGE_GUARDS_PRICE;
  174. if (price < 0)
  175. {
  176. _log.warning("ERROR, no price found .. wrong buylist ??");
  177. sendPacket(ActionFailed.STATIC_PACKET);
  178. return;
  179. }
  180. if (price == 0 && !player.isGM() && Config.ONLY_GM_ITEMS_FREE)
  181. {
  182. player.sendMessage("Ohh Cheat dont work? You have a problem now!");
  183. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried buy item for 0 adena.", Config.DEFAULT_PUNISH);
  184. return;
  185. }
  186. if (tradeItem.hasLimitedStock())
  187. {
  188. // trying to buy more then available
  189. if (i.getCount() > tradeItem.getCurrentCount())
  190. return;
  191. }
  192. if ((MAX_ADENA / i.getCount()) < price)
  193. {
  194. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried to purchase over " + MAX_ADENA + " adena worth of goods.", Config.DEFAULT_PUNISH);
  195. return;
  196. }
  197. // first calculate price per item with tax, then multiply by count
  198. price = (long) (price * (1 + castleTaxRate + baseTaxRate));
  199. subTotal += i.getCount() * price;
  200. if (subTotal > MAX_ADENA)
  201. {
  202. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried to purchase over " + MAX_ADENA + " adena worth of goods.", Config.DEFAULT_PUNISH);
  203. return;
  204. }
  205. weight += i.getCount() * template.getWeight();
  206. if (!template.isStackable())
  207. slots += i.getCount();
  208. else if (player.getInventory().getItemByItemId(i.getItemId()) == null)
  209. slots++;
  210. }
  211. if (weight > Integer.MAX_VALUE || weight < 0 || !player.getInventory().validateWeight((int) weight))
  212. {
  213. sendPacket(new SystemMessage(SystemMessageId.WEIGHT_LIMIT_EXCEEDED));
  214. sendPacket(ActionFailed.STATIC_PACKET);
  215. return;
  216. }
  217. if (slots > Integer.MAX_VALUE || slots < 0 || !player.getInventory().validateCapacity((int) slots))
  218. {
  219. sendPacket(new SystemMessage(SystemMessageId.SLOTS_FULL));
  220. sendPacket(ActionFailed.STATIC_PACKET);
  221. return;
  222. }
  223. // Charge buyer and add tax to castle treasury if not owned by npc clan
  224. if ((subTotal < 0) || !player.reduceAdena("Buy", subTotal, player.getLastFolkNPC(), false))
  225. {
  226. sendPacket(new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));
  227. sendPacket(ActionFailed.STATIC_PACKET);
  228. return;
  229. }
  230. // Proceed the purchase
  231. for (Item i : _items)
  232. {
  233. L2TradeItem tradeItem = list.getItemById(i.getItemId());
  234. if (tradeItem == null)
  235. {
  236. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId + " and item_id " + i.getItemId(), Config.DEFAULT_PUNISH);
  237. continue;
  238. }
  239. if (tradeItem.hasLimitedStock())
  240. {
  241. if (tradeItem.decreaseCount(i.getCount()))
  242. player.getInventory().addItem("Buy", i.getItemId(), i.getCount(), player, merchant);
  243. }
  244. else
  245. player.getInventory().addItem("Buy", i.getItemId(), i.getCount(), player, merchant);
  246. }
  247. // add to castle treasury
  248. if (merchant instanceof L2MerchantInstance)
  249. ((L2MerchantInstance) merchant).getCastle().addToTreasury((long) (subTotal / castleTaxRate));
  250. StatusUpdate su = new StatusUpdate(player);
  251. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  252. player.sendPacket(su);
  253. player.sendPacket(new ExBuySellListPacket(player, list, castleTaxRate + baseTaxRate, true));
  254. }
  255. private class Item
  256. {
  257. private final int _itemId;
  258. private final long _count;
  259. public Item(int id, long num)
  260. {
  261. _itemId = id;
  262. _count = num;
  263. }
  264. public int getItemId()
  265. {
  266. return _itemId;
  267. }
  268. public long getCount()
  269. {
  270. return _count;
  271. }
  272. }
  273. /* (non-Javadoc)
  274. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  275. */
  276. @Override
  277. public String getType()
  278. {
  279. return _C__1F_REQUESTBUYITEM;
  280. }
  281. }