RequestBuyItem.java 12 KB

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