RequestBuyItem.java 11 KB

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