RequestSellItem.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.clientpackets;
  20. import static com.l2jserver.gameserver.model.actor.L2Npc.INTERACTION_DISTANCE;
  21. import static com.l2jserver.gameserver.model.itemcontainer.Inventory.MAX_ADENA;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.data.xml.impl.BuyListData;
  26. import com.l2jserver.gameserver.model.L2Object;
  27. import com.l2jserver.gameserver.model.actor.L2Character;
  28. import com.l2jserver.gameserver.model.actor.instance.L2MerchantInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.buylist.L2BuyList;
  31. import com.l2jserver.gameserver.model.holders.UniqueItemHolder;
  32. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  33. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  34. import com.l2jserver.gameserver.network.serverpackets.ExBuySellList;
  35. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  36. import com.l2jserver.gameserver.util.Util;
  37. /**
  38. * RequestSellItem client packet class.
  39. */
  40. public final class RequestSellItem extends L2GameClientPacket
  41. {
  42. private static final String _C__37_REQUESTSELLITEM = "[C] 37 RequestSellItem";
  43. private static final int BATCH_LENGTH = 16;
  44. private int _listId;
  45. private List<UniqueItemHolder> _items = null;
  46. @Override
  47. protected void readImpl()
  48. {
  49. _listId = readD();
  50. int size = readD();
  51. if ((size <= 0) || (size > Config.MAX_ITEM_IN_PACKET) || ((size * BATCH_LENGTH) != _buf.remaining()))
  52. {
  53. return;
  54. }
  55. _items = new ArrayList<>(size);
  56. for (int i = 0; i < size; i++)
  57. {
  58. int objectId = readD();
  59. int itemId = readD();
  60. long count = readQ();
  61. if ((objectId < 1) || (itemId < 1) || (count < 1))
  62. {
  63. _items = null;
  64. return;
  65. }
  66. _items.add(new UniqueItemHolder(itemId, objectId, count));
  67. }
  68. }
  69. @Override
  70. protected void runImpl()
  71. {
  72. processSell();
  73. }
  74. protected void processSell()
  75. {
  76. L2PcInstance player = getClient().getActiveChar();
  77. if (player == null)
  78. {
  79. return;
  80. }
  81. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("buy"))
  82. {
  83. player.sendMessage("You are buying too fast.");
  84. return;
  85. }
  86. if (_items == null)
  87. {
  88. sendPacket(ActionFailed.STATIC_PACKET);
  89. return;
  90. }
  91. // Alt game - Karma punishment
  92. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && (player.getKarma() > 0))
  93. {
  94. sendPacket(ActionFailed.STATIC_PACKET);
  95. return;
  96. }
  97. L2Object target = player.getTarget();
  98. L2Character merchant = null;
  99. if (!player.isGM())
  100. {
  101. if ((target == null) || (!player.isInsideRadius(target, INTERACTION_DISTANCE, true, false)) || (player.getInstanceId() != target.getInstanceId()))
  102. {
  103. sendPacket(ActionFailed.STATIC_PACKET);
  104. return;
  105. }
  106. if (target instanceof L2MerchantInstance)
  107. {
  108. merchant = (L2Character) target;
  109. }
  110. else
  111. {
  112. sendPacket(ActionFailed.STATIC_PACKET);
  113. return;
  114. }
  115. }
  116. if ((merchant == null) && !player.isGM())
  117. {
  118. sendPacket(ActionFailed.STATIC_PACKET);
  119. return;
  120. }
  121. final L2BuyList buyList = BuyListData.getInstance().getBuyList(_listId);
  122. if (buyList == null)
  123. {
  124. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  125. return;
  126. }
  127. if (merchant != null)
  128. {
  129. if (!buyList.isNpcAllowed(merchant.getId()))
  130. {
  131. sendPacket(ActionFailed.STATIC_PACKET);
  132. return;
  133. }
  134. }
  135. long totalPrice = 0;
  136. // Proceed the sell
  137. for (UniqueItemHolder i : _items)
  138. {
  139. L2ItemInstance item = player.checkItemManipulation(i.getObjectId(), i.getCount(), "sell");
  140. if ((item == null) || (!item.isSellable()))
  141. {
  142. continue;
  143. }
  144. long price = item.getReferencePrice() / 2;
  145. totalPrice += price * i.getCount();
  146. if (((MAX_ADENA / i.getCount()) < price) || (totalPrice > MAX_ADENA))
  147. {
  148. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried to purchase over " + MAX_ADENA + " adena worth of goods.", Config.DEFAULT_PUNISH);
  149. return;
  150. }
  151. if (Config.ALLOW_REFUND)
  152. {
  153. item = player.getInventory().transferItem("Sell", i.getObjectId(), i.getCount(), player.getRefund(), player, merchant);
  154. }
  155. else
  156. {
  157. item = player.getInventory().destroyItem("Sell", i.getObjectId(), i.getCount(), player, merchant);
  158. }
  159. }
  160. player.addAdena("Sell", totalPrice, merchant, false);
  161. // Update current load as well
  162. StatusUpdate su = new StatusUpdate(player);
  163. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  164. player.sendPacket(su);
  165. player.sendPacket(new ExBuySellList(player, true));
  166. }
  167. @Override
  168. public String getType()
  169. {
  170. return _C__37_REQUESTSELLITEM;
  171. }
  172. }