2
0

RequestRefundItem.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 java.util.List;
  18. import java.util.logging.Logger;
  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.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. /**
  36. * RequestRefundItem client packet class.
  37. */
  38. public final class RequestRefundItem extends L2GameClientPacket
  39. {
  40. private static final String _C__D0_75_REQUESTREFUNDITEM = "[C] D0:75 RequestRefundItem";
  41. private static final Logger _log = Logger.getLogger(RequestRefundItem.class.getName());
  42. private static final int BATCH_LENGTH = 4; // length of the one item
  43. private int _listId;
  44. private int[] _items = null;
  45. @Override
  46. protected void readImpl()
  47. {
  48. _listId = readD();
  49. final int count = readD();
  50. if (count <= 0 || count > Config.MAX_ITEM_IN_PACKET || count * BATCH_LENGTH != _buf.remaining())
  51. return;
  52. _items = new int[count];
  53. for (int i = 0; i < count; i++)
  54. _items[i] = readD();
  55. }
  56. @Override
  57. protected void runImpl()
  58. {
  59. final L2PcInstance player = getClient().getActiveChar();
  60. if (player == null)
  61. return;
  62. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("refund"))
  63. {
  64. player.sendMessage("You using refund too fast.");
  65. return;
  66. }
  67. if (_items == null)
  68. {
  69. sendPacket(ActionFailed.STATIC_PACKET);
  70. return;
  71. }
  72. if (!player.hasRefund())
  73. {
  74. sendPacket(ActionFailed.STATIC_PACKET);
  75. return;
  76. }
  77. L2Object target = player.getTarget();
  78. if (!player.isGM() && (target == null // No target (ie GM Shop)
  79. || !(target instanceof L2MerchantInstance || target instanceof L2MerchantSummonInstance) || player.getInstanceId() != target.getInstanceId() || !player.isInsideRadius(target, INTERACTION_DISTANCE, true, false))) // Distance is too far
  80. {
  81. sendPacket(ActionFailed.STATIC_PACKET);
  82. return;
  83. }
  84. L2Character merchant = null;
  85. if (target instanceof L2MerchantInstance || target instanceof L2MerchantSummonInstance)
  86. merchant = (L2Character) target;
  87. else if (!player.isGM())
  88. {
  89. sendPacket(ActionFailed.STATIC_PACKET);
  90. return;
  91. }
  92. L2TradeList list = null;
  93. double taxRate = 0;
  94. if (merchant != null)
  95. {
  96. List<L2TradeList> lists;
  97. if (merchant instanceof L2MerchantInstance)
  98. {
  99. lists = TradeController.getInstance().getBuyListByNpcId(((L2MerchantInstance) merchant).getNpcId());
  100. taxRate = ((L2MerchantInstance) merchant).getMpc().getTotalTaxRate();
  101. }
  102. else
  103. {
  104. lists = TradeController.getInstance().getBuyListByNpcId(((L2MerchantSummonInstance) merchant).getNpcId());
  105. taxRate = 50;
  106. }
  107. if (!player.isGM())
  108. {
  109. if (lists == null)
  110. {
  111. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  112. return;
  113. }
  114. for (L2TradeList tradeList : lists)
  115. {
  116. if (tradeList.getListId() == _listId)
  117. list = tradeList;
  118. }
  119. }
  120. else
  121. list = TradeController.getInstance().getBuyList(_listId);
  122. }
  123. else
  124. list = TradeController.getInstance().getBuyList(_listId);
  125. if (list == null)
  126. {
  127. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  128. return;
  129. }
  130. long weight = 0;
  131. long adena = 0;
  132. long slots = 0;
  133. L2ItemInstance[] refund = player.getRefund().getItems();
  134. int[] objectIds = new int[_items.length];
  135. for (int i = 0; i < _items.length; i++)
  136. {
  137. int idx = _items[i];
  138. if (idx < 0 || idx >= refund.length)
  139. {
  140. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent invalid refund index", Config.DEFAULT_PUNISH);
  141. return;
  142. }
  143. // check for duplicates - indexes
  144. for (int j = i + 1; j < _items.length; j++)
  145. if (idx == _items[j])
  146. {
  147. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent duplicate refund index", Config.DEFAULT_PUNISH);
  148. return;
  149. }
  150. final L2ItemInstance item = refund[idx];
  151. final L2Item template = item.getItem();
  152. objectIds[i] = item.getObjectId();
  153. // second check for duplicates - object ids
  154. for (int j = 0; j < i; j++)
  155. if (objectIds[i] == objectIds[j])
  156. {
  157. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " has duplicate items in refund list", Config.DEFAULT_PUNISH);
  158. return;
  159. }
  160. long count = item.getCount();
  161. weight += count * template.getWeight();
  162. adena += count * template.getReferencePrice() / 2;
  163. if (!template.isStackable())
  164. slots += count;
  165. else if (player.getInventory().getItemByItemId(template.getItemId()) == null)
  166. slots++;
  167. }
  168. if (weight > Integer.MAX_VALUE || weight < 0 || !player.getInventory().validateWeight((int) weight))
  169. {
  170. sendPacket(new SystemMessage(SystemMessageId.WEIGHT_LIMIT_EXCEEDED));
  171. sendPacket(ActionFailed.STATIC_PACKET);
  172. return;
  173. }
  174. if (slots > Integer.MAX_VALUE || slots < 0 || !player.getInventory().validateCapacity((int) slots))
  175. {
  176. sendPacket(new SystemMessage(SystemMessageId.SLOTS_FULL));
  177. sendPacket(ActionFailed.STATIC_PACKET);
  178. return;
  179. }
  180. if ((adena < 0) || !player.reduceAdena("Refund", adena, player.getLastFolkNPC(), false))
  181. {
  182. sendPacket(new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));
  183. sendPacket(ActionFailed.STATIC_PACKET);
  184. return;
  185. }
  186. for (int i = 0; i < _items.length; i++)
  187. {
  188. L2ItemInstance item = player.getRefund().transferItem("Refund", objectIds[i], Long.MAX_VALUE, player.getInventory(), player, player.getLastFolkNPC());
  189. if (item == null)
  190. {
  191. _log.warning("Error refunding object for char " + player.getName() + " (newitem == null)");
  192. continue;
  193. }
  194. }
  195. // Update current load status on player
  196. StatusUpdate su = new StatusUpdate(player);
  197. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  198. player.sendPacket(su);
  199. player.sendPacket(new ExBuySellListPacket(player, list, taxRate, true));
  200. }
  201. /* (non-Javadoc)
  202. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  203. */
  204. @Override
  205. public String getType()
  206. {
  207. return _C__D0_75_REQUESTREFUNDITEM;
  208. }
  209. }