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