RequestPreviewItem.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.ThreadPoolManager;
  25. import com.l2jserver.gameserver.data.xml.impl.BuyListData;
  26. import com.l2jserver.gameserver.model.L2Object;
  27. import com.l2jserver.gameserver.model.actor.L2Npc;
  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.buylist.Product;
  32. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  33. import com.l2jserver.gameserver.model.items.L2Armor;
  34. import com.l2jserver.gameserver.model.items.L2Item;
  35. import com.l2jserver.gameserver.model.items.L2Weapon;
  36. import com.l2jserver.gameserver.model.items.type.ArmorType;
  37. import com.l2jserver.gameserver.model.items.type.WeaponType;
  38. import com.l2jserver.gameserver.network.SystemMessageId;
  39. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  40. import com.l2jserver.gameserver.network.serverpackets.ShopPreviewInfo;
  41. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  42. import com.l2jserver.gameserver.util.Util;
  43. /**
  44. ** @author Gnacik
  45. */
  46. public final class RequestPreviewItem extends L2GameClientPacket
  47. {
  48. private static final String _C__C7_REQUESTPREVIEWITEM = "[C] C7 RequestPreviewItem";
  49. @SuppressWarnings("unused")
  50. private int _unk;
  51. private int _listId;
  52. private int _count;
  53. private int[] _items;
  54. private class RemoveWearItemsTask implements Runnable
  55. {
  56. private final L2PcInstance activeChar;
  57. protected RemoveWearItemsTask(L2PcInstance player)
  58. {
  59. activeChar = player;
  60. }
  61. @Override
  62. public void run()
  63. {
  64. try
  65. {
  66. activeChar.sendPacket(SystemMessageId.NO_LONGER_TRYING_ON);
  67. activeChar.sendPacket(new UserInfo(activeChar));
  68. }
  69. catch (Exception e)
  70. {
  71. _log.log(Level.SEVERE, "", e);
  72. }
  73. }
  74. }
  75. @Override
  76. protected void readImpl()
  77. {
  78. _unk = readD();
  79. _listId = readD();
  80. _count = readD();
  81. if (_count < 0)
  82. {
  83. _count = 0;
  84. }
  85. if (_count > 100)
  86. {
  87. return; // prevent too long lists
  88. }
  89. // Create _items table that will contain all ItemID to Wear
  90. _items = new int[_count];
  91. // Fill _items table with all ItemID to Wear
  92. for (int i = 0; i < _count; i++)
  93. {
  94. _items[i] = readD();
  95. }
  96. }
  97. @Override
  98. protected void runImpl()
  99. {
  100. if (_items == null)
  101. {
  102. return;
  103. }
  104. // Get the current player and return if null
  105. L2PcInstance activeChar = getClient().getActiveChar();
  106. if (activeChar == null)
  107. {
  108. return;
  109. }
  110. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("buy"))
  111. {
  112. activeChar.sendMessage("You are buying too fast.");
  113. return;
  114. }
  115. // If Alternate rule Karma punishment is set to true, forbid Wear to player with Karma
  116. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && (activeChar.getKarma() > 0))
  117. {
  118. return;
  119. }
  120. // Check current target of the player and the INTERACTION_DISTANCE
  121. L2Object target = activeChar.getTarget();
  122. if (!activeChar.isGM() && ((target == null // No target (i.e. GM Shop)
  123. ) || !((target instanceof L2MerchantInstance)) // Target not a merchant
  124. || !activeChar.isInsideRadius(target, L2Npc.INTERACTION_DISTANCE, false, false) // Distance is too far
  125. ))
  126. {
  127. return;
  128. }
  129. if ((_count < 1) || (_listId >= 4000000))
  130. {
  131. sendPacket(ActionFailed.STATIC_PACKET);
  132. return;
  133. }
  134. // Get the current merchant targeted by the player
  135. final L2MerchantInstance merchant = (target instanceof L2MerchantInstance) ? (L2MerchantInstance) target : null;
  136. if (merchant == null)
  137. {
  138. _log.warning(getClass().getName() + " Null merchant!");
  139. return;
  140. }
  141. final L2BuyList buyList = BuyListData.getInstance().getBuyList(_listId);
  142. if (buyList == null)
  143. {
  144. Util.handleIllegalPlayerAction(activeChar, "Warning!! Character " + activeChar.getName() + " of account " + activeChar.getAccountName() + " sent a false BuyList list_id " + _listId, Config.DEFAULT_PUNISH);
  145. return;
  146. }
  147. long totalPrice = 0;
  148. Map<Integer, Integer> itemList = new HashMap<>();
  149. for (int i = 0; i < _count; i++)
  150. {
  151. int itemId = _items[i];
  152. final Product product = buyList.getProductByItemId(itemId);
  153. if (product == null)
  154. {
  155. Util.handleIllegalPlayerAction(activeChar, "Warning!! Character " + activeChar.getName() + " of account " + activeChar.getAccountName() + " sent a false BuyList list_id " + _listId + " and item_id " + itemId, Config.DEFAULT_PUNISH);
  156. return;
  157. }
  158. L2Item template = product.getItem();
  159. if (template == null)
  160. {
  161. continue;
  162. }
  163. int slot = Inventory.getPaperdollIndex(template.getBodyPart());
  164. if (slot < 0)
  165. {
  166. continue;
  167. }
  168. if (template instanceof L2Weapon)
  169. {
  170. if (activeChar.getRace().ordinal() == 5)
  171. {
  172. if (template.getItemType() == WeaponType.NONE)
  173. {
  174. continue;
  175. }
  176. else if ((template.getItemType() == WeaponType.RAPIER) || (template.getItemType() == WeaponType.CROSSBOW) || (template.getItemType() == WeaponType.ANCIENTSWORD))
  177. {
  178. continue;
  179. }
  180. }
  181. }
  182. else if (template instanceof L2Armor)
  183. {
  184. if (activeChar.getRace().ordinal() == 5)
  185. {
  186. if ((template.getItemType() == ArmorType.HEAVY) || (template.getItemType() == ArmorType.MAGIC))
  187. {
  188. continue;
  189. }
  190. }
  191. }
  192. if (itemList.containsKey(slot))
  193. {
  194. activeChar.sendPacket(SystemMessageId.YOU_CAN_NOT_TRY_THOSE_ITEMS_ON_AT_THE_SAME_TIME);
  195. return;
  196. }
  197. itemList.put(slot, itemId);
  198. totalPrice += Config.WEAR_PRICE;
  199. if (totalPrice > Inventory.MAX_ADENA)
  200. {
  201. Util.handleIllegalPlayerAction(activeChar, "Warning!! Character " + activeChar.getName() + " of account " + activeChar.getAccountName() + " tried to purchase over " + Inventory.MAX_ADENA + " adena worth of goods.", Config.DEFAULT_PUNISH);
  202. return;
  203. }
  204. }
  205. // Charge buyer and add tax to castle treasury if not owned by npc clan because a Try On is not Free
  206. if ((totalPrice < 0) || !activeChar.reduceAdena("Wear", totalPrice, activeChar.getLastFolkNPC(), true))
  207. {
  208. activeChar.sendPacket(SystemMessageId.YOU_NOT_ENOUGH_ADENA);
  209. return;
  210. }
  211. if (!itemList.isEmpty())
  212. {
  213. activeChar.sendPacket(new ShopPreviewInfo(itemList));
  214. // Schedule task
  215. ThreadPoolManager.getInstance().scheduleGeneral(new RemoveWearItemsTask(activeChar), Config.WEAR_DELAY * 1000);
  216. }
  217. }
  218. @Override
  219. public String getType()
  220. {
  221. return _C__C7_REQUESTPREVIEWITEM;
  222. }
  223. }