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