RequestSellItem.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 com.l2jserver.Config;
  20. import com.l2jserver.gameserver.TradeController;
  21. import com.l2jserver.gameserver.model.L2ItemInstance;
  22. import com.l2jserver.gameserver.model.L2Object;
  23. import com.l2jserver.gameserver.model.L2TradeList;
  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.serverpackets.ActionFailed;
  29. import com.l2jserver.gameserver.network.serverpackets.ExBuySellListPacket;
  30. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  31. import com.l2jserver.gameserver.util.Util;
  32. /**
  33. * RequestSellItem client packet class.
  34. */
  35. public final class RequestSellItem extends L2GameClientPacket
  36. {
  37. private static final String _C__1E_REQUESTSELLITEM = "[C] 1E RequestSellItem";
  38. //private static Logger _log = Logger.getLogger(RequestSellItem.class.getName());
  39. private static final int BATCH_LENGTH = 16; // length of the one item
  40. private int _listId;
  41. private Item[] _items = null;
  42. /**
  43. * packet type id 0x1e
  44. *
  45. * sample
  46. *
  47. * 1e
  48. * 00 00 00 00 // list id
  49. * 02 00 00 00 // number of items
  50. *
  51. * 71 72 00 10 // object id
  52. * ea 05 00 00 // item id
  53. * 01 00 00 00 // item count
  54. *
  55. * 76 4b 00 10 // object id
  56. * 2e 0a 00 00 // item id
  57. * 01 00 00 00 // item count
  58. *
  59. * format: cdd (ddd)
  60. * @param decrypt
  61. */
  62. @Override
  63. protected void readImpl()
  64. {
  65. _listId = readD();
  66. int count = readD();
  67. if (count <= 0 || count > Config.MAX_ITEM_IN_PACKET || count * BATCH_LENGTH != _buf.remaining())
  68. {
  69. return;
  70. }
  71. _items = new Item[count];
  72. for (int i = 0; i < count; i++)
  73. {
  74. int objectId = readD();
  75. int itemId = readD();
  76. long cnt = readQ();
  77. if (objectId < 1 || itemId < 1 || cnt < 1)
  78. {
  79. _items = null;
  80. return;
  81. }
  82. _items[i] = new Item(objectId, itemId, cnt);
  83. }
  84. }
  85. @Override
  86. protected void runImpl()
  87. {
  88. this.processSell();
  89. }
  90. protected void processSell()
  91. {
  92. L2PcInstance player = getClient().getActiveChar();
  93. if (player == null)
  94. return;
  95. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("buy"))
  96. {
  97. player.sendMessage("You buying too fast.");
  98. return;
  99. }
  100. if (_items == null)
  101. {
  102. sendPacket(ActionFailed.STATIC_PACKET);
  103. return;
  104. }
  105. // Alt game - Karma punishment
  106. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && player.getKarma() > 0)
  107. {
  108. sendPacket(ActionFailed.STATIC_PACKET);
  109. return;
  110. }
  111. L2Object target = player.getTarget();
  112. if (!player.isGM() && (target == null // No target (ie GM Shop)
  113. || !(target instanceof L2MerchantInstance || target instanceof L2MerchantSummonInstance) // Target not a merchant
  114. || target.getInstanceId() != player.getInstanceId() || !player.isInsideRadius(target, INTERACTION_DISTANCE, true, false))) // Distance is too far
  115. {
  116. sendPacket(ActionFailed.STATIC_PACKET);
  117. return;
  118. }
  119. L2Character merchant = null;
  120. double taxRate = 0;
  121. if (target instanceof L2MerchantInstance || target instanceof L2MerchantSummonInstance)
  122. merchant = (L2Character) target;
  123. else if (!player.isGM())
  124. {
  125. sendPacket(ActionFailed.STATIC_PACKET);
  126. return;
  127. }
  128. L2TradeList list = null;
  129. if (merchant != null)
  130. {
  131. List<L2TradeList> lists;
  132. if (merchant instanceof L2MerchantInstance)
  133. {
  134. lists = TradeController.getInstance().getBuyListByNpcId(((L2MerchantInstance) merchant).getNpcId());
  135. taxRate = ((L2MerchantInstance) merchant).getMpc().getTotalTaxRate();
  136. }
  137. else
  138. {
  139. lists = TradeController.getInstance().getBuyListByNpcId(((L2MerchantSummonInstance) merchant).getNpcId());
  140. taxRate = 50;
  141. }
  142. if (!player.isGM())
  143. {
  144. if (lists == null)
  145. {
  146. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  147. return;
  148. }
  149. for (L2TradeList tradeList : lists)
  150. {
  151. if (tradeList.getListId() == _listId)
  152. list = tradeList;
  153. }
  154. }
  155. else
  156. list = TradeController.getInstance().getBuyList(_listId);
  157. }
  158. else
  159. list = TradeController.getInstance().getBuyList(_listId);
  160. if (list == null)
  161. {
  162. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  163. return;
  164. }
  165. long totalPrice = 0;
  166. // Proceed the sell
  167. for (Item i : _items)
  168. {
  169. L2ItemInstance item = player.checkItemManipulation(i.getObjectId(), i.getCount(), "sell");
  170. if (item == null || (!item.isSellable()))
  171. continue;
  172. long price = item.getReferencePrice() / 2;
  173. totalPrice += price * i.getCount();
  174. if ((MAX_ADENA / i.getCount()) < price || totalPrice > MAX_ADENA)
  175. {
  176. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried to purchase over " + MAX_ADENA + " adena worth of goods.", Config.DEFAULT_PUNISH);
  177. return;
  178. }
  179. if (Config.ALLOW_REFUND)
  180. item = player.getInventory().transferItem("Sell", i.getObjectId(), i.getCount(), player.getRefund(), player, merchant);
  181. else
  182. item = player.getInventory().destroyItem("Sell", i.getObjectId(), i.getCount(), player, merchant);
  183. }
  184. player.addAdena("Sell", totalPrice, merchant, false);
  185. // Update current load as well
  186. StatusUpdate su = new StatusUpdate(player.getObjectId());
  187. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  188. player.sendPacket(su);
  189. player.sendPacket(new ExBuySellListPacket(player, list, taxRate, true));
  190. }
  191. private class Item
  192. {
  193. private final int _objectId;
  194. private final int _itemId;
  195. private final long _count;
  196. public Item(int objId, int id, long num)
  197. {
  198. _objectId = objId;
  199. _itemId = id;
  200. _count = num;
  201. }
  202. public int getObjectId()
  203. {
  204. return _objectId;
  205. }
  206. @SuppressWarnings("unused")
  207. public int getItemId()
  208. {
  209. return _itemId;
  210. }
  211. public long getCount()
  212. {
  213. return _count;
  214. }
  215. }
  216. /* (non-Javadoc)
  217. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  218. */
  219. @Override
  220. public String getType()
  221. {
  222. return _C__1E_REQUESTSELLITEM;
  223. }
  224. }