RequestWearItem.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.concurrent.Future;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.ThreadPoolManager;
  22. import com.l2jserver.gameserver.TradeController;
  23. import com.l2jserver.gameserver.datatables.ItemTable;
  24. import com.l2jserver.gameserver.model.L2ItemInstance;
  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.PcInventory;
  32. import com.l2jserver.gameserver.network.SystemMessageId;
  33. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  34. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  35. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  36. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  37. import com.l2jserver.gameserver.templates.item.L2Item;
  38. import com.l2jserver.gameserver.util.Util;
  39. /**
  40. * This class ...
  41. *
  42. * @version $Revision: 1.12.4.4 $ $Date: 2005/03/27 15:29:30 $
  43. */
  44. public final class RequestWearItem extends L2GameClientPacket
  45. {
  46. private static final String _C__C6_REQUESTWEARITEM = "[C] C6 RequestWearItem";
  47. protected static final Logger _log = Logger.getLogger(RequestWearItem.class.getName());
  48. protected Future<?> _removeWearItemsTask;
  49. @SuppressWarnings("unused")
  50. private int _unknow;
  51. /** List of ItemID to Wear */
  52. private int _listId;
  53. /** Number of Item to Wear */
  54. private int _count;
  55. /** Table of ItemId containing all Item to Wear */
  56. private int[] _items;
  57. /** Player that request a Try on */
  58. protected L2PcInstance _activeChar;
  59. class RemoveWearItemsTask implements Runnable
  60. {
  61. public void run()
  62. {
  63. try
  64. {
  65. _activeChar.destroyWearedItems("Wear", null, true);
  66. }
  67. catch (Exception e)
  68. {
  69. _log.log(Level.SEVERE, "", e);
  70. }
  71. }
  72. }
  73. /**
  74. * Decrypt the RequestWearItem Client->Server Packet and Create _items table containing all ItemID to Wear.<BR><BR>
  75. *
  76. */
  77. @Override
  78. protected void readImpl()
  79. {
  80. // Read and Decrypt the RequestWearItem Client->Server Packet
  81. _activeChar = getClient().getActiveChar();
  82. _unknow = readD();
  83. _listId = readD(); // List of ItemID to Wear
  84. _count = readD(); // Number of Item to Wear
  85. if (_count < 0)
  86. _count = 0;
  87. if (_count > 100)
  88. _count = 0; // prevent too long lists
  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. int itemId = readD();
  95. _items[i] = itemId;
  96. }
  97. }
  98. /**
  99. * Launch Wear action.<BR><BR>
  100. *
  101. */
  102. @Override
  103. protected void runImpl()
  104. {
  105. // Get the current player and return if null
  106. L2PcInstance player = getClient().getActiveChar();
  107. if (player == null)
  108. return;
  109. // If Alternate rule Karma punishment is set to true, forbid Wear to player with Karma
  110. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && player.getKarma() > 0)
  111. return;
  112. // Check current target of the player and the INTERACTION_DISTANCE
  113. L2Object target = player.getTarget();
  114. if (!player.isGM() && (target == null // No target (ie GM Shop)
  115. || !(target instanceof L2MerchantInstance || target instanceof L2MercManagerInstance) // Target not a merchant and not mercmanager
  116. || !player.isInsideRadius(target, L2Npc.INTERACTION_DISTANCE, false, false) // Distance is too far
  117. ))
  118. return;
  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(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id.", 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(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id.", Config.DEFAULT_PUNISH);
  138. return;
  139. }
  140. _listId = list.getListId();
  141. // Check if the quantity of Item to Wear
  142. if (_count < 1 || _listId >= 1000000)
  143. {
  144. sendPacket(ActionFailed.STATIC_PACKET);
  145. return;
  146. }
  147. // Total Price of the Try On
  148. long totalPrice = 0;
  149. // Check for buylist validity and calculates summary values
  150. int slots = 0;
  151. int weight = 0;
  152. for (int i = 0; i < _count; i++)
  153. {
  154. int itemId = _items[i];
  155. if (!list.containsItemId(itemId))
  156. {
  157. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id.", Config.DEFAULT_PUNISH);
  158. return;
  159. }
  160. L2Item template = ItemTable.getInstance().getTemplate(itemId);
  161. weight += template.getWeight();
  162. slots++;
  163. totalPrice += Config.WEAR_PRICE;
  164. if (totalPrice > PcInventory.MAX_ADENA)
  165. {
  166. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " tried to purchase over " + PcInventory.MAX_ADENA + " adena worth of goods.", Config.DEFAULT_PUNISH);
  167. return;
  168. }
  169. }
  170. // Check the weight
  171. if (!player.getInventory().validateWeight(weight))
  172. {
  173. sendPacket(new SystemMessage(SystemMessageId.WEIGHT_LIMIT_EXCEEDED));
  174. return;
  175. }
  176. // Check the inventory capacity
  177. if (!player.getInventory().validateCapacity(slots))
  178. {
  179. sendPacket(new SystemMessage(SystemMessageId.SLOTS_FULL));
  180. return;
  181. }
  182. // Charge buyer and add tax to castle treasury if not owned by npc clan because a Try On is not Free
  183. if ((totalPrice < 0) || !player.reduceAdena("Wear", totalPrice, player.getLastFolkNPC(), false))
  184. {
  185. sendPacket(new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));
  186. return;
  187. }
  188. // Proceed the wear
  189. InventoryUpdate playerIU = new InventoryUpdate();
  190. for (int i = 0; i < _count; i++)
  191. {
  192. int itemId = _items[i];
  193. if (!list.containsItemId(itemId))
  194. {
  195. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " of account " + player.getAccountName() + " sent a false BuyList list_id.", Config.DEFAULT_PUNISH);
  196. return;
  197. }
  198. // If player doesn't own this item : Add this L2ItemInstance to Inventory and set properties lastchanged to ADDED and _wear to True
  199. // If player already own this item : Return its L2ItemInstance (will not be destroy because property _wear set to False)
  200. L2ItemInstance item = player.getInventory().addWearItem("Wear", itemId, player, merchant);
  201. // Equip player with this item (set its location)
  202. player.getInventory().equipItemAndRecord(item);
  203. // Add this Item in the InventoryUpdate Server->Client Packet
  204. playerIU.addItem(item);
  205. }
  206. // Send the InventoryUpdate Server->Client Packet to the player
  207. // Add Items in player inventory and equip them
  208. player.sendPacket(playerIU);
  209. // Send the StatusUpdate Server->Client Packet to the player with new CUR_LOAD (0x0e) information
  210. StatusUpdate su = new StatusUpdate(player.getObjectId());
  211. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  212. player.sendPacket(su);
  213. // Send a Server->Client packet UserInfo to this L2PcInstance and CharInfo to all L2PcInstance in its _KnownPlayers
  214. player.broadcastUserInfo();
  215. // All weared items should be removed in ALLOW_WEAR_DELAY sec.
  216. if (_removeWearItemsTask == null)
  217. _removeWearItemsTask = ThreadPoolManager.getInstance().scheduleGeneral(new RemoveWearItemsTask(), Config.WEAR_DELAY * 1000);
  218. }
  219. /* (non-Javadoc)
  220. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  221. */
  222. @Override
  223. public String getType()
  224. {
  225. return _C__C6_REQUESTWEARITEM;
  226. }
  227. }