RequestPackageSend.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 net.sf.l2j.gameserver.clientpackets;
  16. import java.util.List;
  17. import java.util.logging.Logger;
  18. import javolution.util.FastList;
  19. import net.sf.l2j.Config;
  20. import net.sf.l2j.gameserver.model.ItemContainer;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.PcFreight;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2FolkInstance;
  24. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26. import net.sf.l2j.gameserver.network.SystemMessageId;
  27. import net.sf.l2j.gameserver.serverpackets.InventoryUpdate;
  28. import net.sf.l2j.gameserver.serverpackets.ItemList;
  29. import net.sf.l2j.gameserver.serverpackets.StatusUpdate;
  30. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  31. import net.sf.l2j.gameserver.templates.L2EtcItemType;
  32. /**
  33. *
  34. * @author -Wooden-
  35. */
  36. public final class RequestPackageSend extends L2GameClientPacket
  37. {
  38. private static final String _C_9F_REQUESTPACKAGESEND = "[C] 9F RequestPackageSend";
  39. private static Logger _log = Logger.getLogger(RequestPackageSend.class.getName());
  40. private List<Item> _items = new FastList<Item>();
  41. private int _objectID;
  42. private int _count;
  43. @Override
  44. protected void readImpl()
  45. {
  46. _objectID = readD();
  47. _count = readD();
  48. if (_count < 0 || _count > 500)
  49. {
  50. _count = -1;
  51. return;
  52. }
  53. for(int i = 0; i<_count; i++)
  54. {
  55. int id = readD(); //this is some id sent in PackageSendableList
  56. int count = readD();
  57. _items.add(new Item(id, count));
  58. }
  59. }
  60. /**
  61. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#runImpl()
  62. */
  63. @Override
  64. protected
  65. void runImpl()
  66. {
  67. if (_count == -1) return;
  68. L2PcInstance player = getClient().getActiveChar();
  69. if (player == null) return;
  70. L2PcInstance target = L2PcInstance.load(_objectID);
  71. try {
  72. PcFreight freight = target.getFreight();
  73. getClient().getActiveChar().setActiveWarehouse(freight);
  74. ItemContainer warehouse = player.getActiveWarehouse();
  75. if (warehouse == null) return;
  76. L2FolkInstance manager = player.getLastFolkNPC();
  77. if ((manager == null || !player.isInsideRadius(manager, L2NpcInstance.INTERACTION_DISTANCE, false, false)) && !player.isGM()) return;
  78. if (warehouse instanceof PcFreight && !player.getAccessLevel().allowTransaction())
  79. {
  80. player.sendMessage("Transactions are disable for your Access Level");
  81. return;
  82. }
  83. // Alt game - Karma punishment
  84. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_USE_WAREHOUSE && player.getKarma() > 0) return;
  85. // Freight price from config or normal price per item slot (30)
  86. int fee = _count * Config.ALT_GAME_FREIGHT_PRICE;
  87. int currentAdena = player.getAdena();
  88. int slots = 0;
  89. for (Item i : _items)
  90. {
  91. int objectId = i.id;
  92. int count = i.count;
  93. // Check validity of requested item
  94. L2ItemInstance item = player.checkItemManipulation(objectId, count, "deposit");
  95. if (item == null)
  96. {
  97. _log.warning("Error depositing a warehouse object for char "+player.getName()+" (validity check)");
  98. i.id = 0;
  99. i.count = 0;
  100. continue;
  101. }
  102. if (!item.isTradeable() || item.getItemType() == L2EtcItemType.QUEST) return;
  103. // Calculate needed adena and slots
  104. if (item.getItemId() == 57) currentAdena -= count;
  105. if (!item.isStackable()) slots += count;
  106. else if (warehouse.getItemByItemId(item.getItemId()) == null) slots++;
  107. }
  108. // Item Max Limit Check
  109. if (!warehouse.validateCapacity(slots))
  110. {
  111. sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED));
  112. return;
  113. }
  114. // Check if enough adena and charge the fee
  115. if (currentAdena < fee || !player.reduceAdena("Warehouse", fee, player.getLastFolkNPC(), false))
  116. {
  117. sendPacket(new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));
  118. return;
  119. }
  120. // Proceed to the transfer
  121. InventoryUpdate playerIU = Config.FORCE_INVENTORY_UPDATE ? null : new InventoryUpdate();
  122. for (Item i : _items)
  123. {
  124. int objectId = i.id;
  125. int count = i.count;
  126. // check for an invalid item
  127. if (objectId == 0 && count == 0) continue;
  128. L2ItemInstance oldItem = player.getInventory().getItemByObjectId(objectId);
  129. if (oldItem == null)
  130. {
  131. _log.warning("Error depositing a warehouse object for char "+player.getName()+" (olditem == null)");
  132. continue;
  133. }
  134. if (oldItem.isHeroItem())
  135. continue;
  136. L2ItemInstance newItem = player.getInventory().transferItem("Warehouse", objectId, count, warehouse, player, player.getLastFolkNPC());
  137. if (newItem == null)
  138. {
  139. _log.warning("Error depositing a warehouse object for char "+player.getName()+" (newitem == null)");
  140. continue;
  141. }
  142. if (playerIU != null)
  143. {
  144. if (oldItem.getCount() > 0 && oldItem != newItem) playerIU.addModifiedItem(oldItem);
  145. else playerIU.addRemovedItem(oldItem);
  146. }
  147. }
  148. // Send updated item list to the player
  149. if (playerIU != null) player.sendPacket(playerIU);
  150. else player.sendPacket(new ItemList(player, false));
  151. // Update current load status on player
  152. StatusUpdate su = new StatusUpdate(player.getObjectId());
  153. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  154. player.sendPacket(su);
  155. }
  156. finally
  157. {
  158. target.deleteMe();
  159. }
  160. }
  161. /**
  162. * @see net.sf.l2j.gameserver.BasePacket#getType()
  163. */
  164. @Override
  165. public String getType()
  166. {
  167. return _C_9F_REQUESTPACKAGESEND;
  168. }
  169. private class Item
  170. {
  171. public int id;
  172. public int count;
  173. public Item(int i, int c)
  174. {
  175. id = i;
  176. count = c;
  177. }
  178. }
  179. }