RequestSellItem.java 7.4 KB

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