RequestPostAttachment.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 com.l2jserver.Config;
  17. import com.l2jserver.gameserver.datatables.ItemTable;
  18. import com.l2jserver.gameserver.instancemanager.MailManager;
  19. import com.l2jserver.gameserver.model.L2ItemInstance;
  20. import com.l2jserver.gameserver.model.L2World;
  21. import com.l2jserver.gameserver.model.L2ItemInstance.ItemLocation;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.entity.Message;
  24. import com.l2jserver.gameserver.model.itemcontainer.ItemContainer;
  25. import com.l2jserver.gameserver.network.SystemMessageId;
  26. import com.l2jserver.gameserver.network.serverpackets.ExChangePostState;
  27. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  28. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  29. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  30. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  31. import com.l2jserver.gameserver.util.Util;
  32. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.ADENA_ID;
  33. import static com.l2jserver.gameserver.model.actor.L2Character.ZONE_PEACE;
  34. /**
  35. * @author Migi, DS
  36. */
  37. public final class RequestPostAttachment extends L2GameClientPacket
  38. {
  39. private static final String _C__D0_6A_REQUESTPOSTATTACHMENT = "[C] D0:6A RequestPostAttachment";
  40. private int _msgId;
  41. @Override
  42. protected void readImpl()
  43. {
  44. _msgId = readD();
  45. }
  46. @Override
  47. public void runImpl()
  48. {
  49. if (!Config.ALLOW_MAIL || !Config.ALLOW_ATTACHMENTS)
  50. return;
  51. final L2PcInstance activeChar = getClient().getActiveChar();
  52. if (activeChar == null)
  53. return;
  54. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("getattach"))
  55. return;
  56. if (!activeChar.getAccessLevel().allowTransaction())
  57. {
  58. activeChar.sendMessage("Transactions are disabled for your Access Level");
  59. return;
  60. }
  61. if (!activeChar.isInsideZone(ZONE_PEACE))
  62. {
  63. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_RECEIVE_NOT_IN_PEACE_ZONE));
  64. return;
  65. }
  66. if (activeChar.getActiveTradeList() != null)
  67. {
  68. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_RECEIVE_DURING_EXCHANGE));
  69. return;
  70. }
  71. if (activeChar.isEnchanting())
  72. {
  73. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_RECEIVE_DURING_ENCHANT));
  74. return;
  75. }
  76. if (activeChar.getPrivateStoreType() > 0)
  77. {
  78. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_RECEIVE_PRIVATE_STORE));
  79. return;
  80. }
  81. final Message msg = MailManager.getInstance().getMessage(_msgId);
  82. if (msg == null)
  83. return;
  84. if (msg.getReceiverId() != activeChar.getObjectId())
  85. {
  86. Util.handleIllegalPlayerAction(activeChar,
  87. "Player "+activeChar.getName()+" tried to get not own attachment!", Config.DEFAULT_PUNISH);
  88. return;
  89. }
  90. if (!msg.hasAttachments())
  91. return;
  92. final ItemContainer attachments = msg.getAttachments();
  93. if (attachments == null)
  94. return;
  95. int weight = 0;
  96. int slots = 0;
  97. for (L2ItemInstance item : attachments.getItems())
  98. {
  99. if (item == null)
  100. continue;
  101. // Calculate needed slots
  102. if (item.getOwnerId() != msg.getSenderId())
  103. {
  104. Util.handleIllegalPlayerAction(activeChar,
  105. "Player "+activeChar.getName()+" tried to get wrong item (ownerId != senderId) from attachment!", Config.DEFAULT_PUNISH);
  106. return;
  107. }
  108. if (!item.getLocation().equals(ItemLocation.MAIL))
  109. {
  110. Util.handleIllegalPlayerAction(activeChar,
  111. "Player "+activeChar.getName()+" tried to get wrong item (Location != MAIL) from attachment!", Config.DEFAULT_PUNISH);
  112. return;
  113. }
  114. if (item.getLocationSlot() != msg.getId())
  115. {
  116. Util.handleIllegalPlayerAction(activeChar,
  117. "Player "+activeChar.getName()+" tried to get items from different attachment!", Config.DEFAULT_PUNISH);
  118. return;
  119. }
  120. weight += item.getCount() * item.getItem().getWeight();
  121. if (!item.isStackable())
  122. slots += item.getCount();
  123. else if (activeChar.getInventory().getItemByItemId(item.getItemId()) == null)
  124. slots++;
  125. }
  126. // Item Max Limit Check
  127. if (!activeChar.getInventory().validateCapacity(slots))
  128. {
  129. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_RECEIVE_INVENTORY_FULL));
  130. return;
  131. }
  132. // Weight limit Check
  133. if (!activeChar.getInventory().validateWeight(weight))
  134. {
  135. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_RECEIVE_INVENTORY_FULL));
  136. return;
  137. }
  138. long adena = msg.getReqAdena();
  139. if (adena > 0 && !activeChar.reduceAdena("PayMail", adena, null, true))
  140. {
  141. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_RECEIVE_NO_ADENA));
  142. return;
  143. }
  144. // Proceed to the transfer
  145. InventoryUpdate playerIU = Config.FORCE_INVENTORY_UPDATE ? null : new InventoryUpdate();
  146. for (L2ItemInstance item : attachments.getItems())
  147. {
  148. if (item == null)
  149. continue;
  150. if (item.getOwnerId() != msg.getSenderId())
  151. {
  152. Util.handleIllegalPlayerAction(activeChar,
  153. "Player "+activeChar.getName()+" tried to get items with owner != sender !", Config.DEFAULT_PUNISH);
  154. return;
  155. }
  156. long count = item.getCount();
  157. final L2ItemInstance newItem = attachments.transferItem(attachments.getName(), item.getObjectId(), item.getCount(), activeChar.getInventory(), activeChar, null);
  158. if (newItem == null)
  159. return;
  160. if (playerIU != null)
  161. {
  162. if (newItem.getCount() > count)
  163. playerIU.addModifiedItem(newItem);
  164. else
  165. playerIU.addNewItem(newItem);
  166. }
  167. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_ACQUIRED_S2_S1);
  168. sm.addItemName(item.getItemId());
  169. sm.addItemNumber(count);
  170. activeChar.sendPacket(sm);
  171. }
  172. // Send updated item list to the player
  173. if (playerIU != null)
  174. activeChar.sendPacket(playerIU);
  175. else
  176. activeChar.sendPacket(new ItemList(activeChar, false));
  177. msg.removeAttachments();
  178. // Update current load status on player
  179. StatusUpdate su = new StatusUpdate(activeChar);
  180. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  181. activeChar.sendPacket(su);
  182. SystemMessage sm;
  183. final L2PcInstance sender = L2World.getInstance().getPlayer(msg.getSenderId());
  184. if (adena > 0)
  185. {
  186. if (sender != null)
  187. {
  188. sender.addAdena("PayMail", adena, activeChar, false);
  189. sm = new SystemMessage(SystemMessageId.PAYMENT_OF_S1_ADENA_COMPLETED_BY_S2);
  190. sm.addItemNumber(adena);
  191. sm.addCharName(activeChar);
  192. sender.sendPacket(sm);
  193. }
  194. else
  195. {
  196. L2ItemInstance paidAdena = ItemTable.getInstance().createItem("PayMail", ADENA_ID, adena, activeChar, null);
  197. paidAdena.setOwnerId(msg.getSenderId());
  198. paidAdena.setLocation(ItemLocation.INVENTORY);
  199. paidAdena.updateDatabase(true);
  200. L2World.getInstance().removeObject(paidAdena);
  201. }
  202. }
  203. else if (sender != null)
  204. {
  205. sm = new SystemMessage(SystemMessageId.S1_ACQUIRED_ATTACHED_ITEM);
  206. sm.addCharName(activeChar);
  207. sender.sendPacket(sm);
  208. }
  209. activeChar.sendPacket(new ExChangePostState(true, _msgId, Message.READED));
  210. activeChar.sendPacket(new SystemMessage(SystemMessageId.MAIL_SUCCESSFULLY_RECEIVED));
  211. }
  212. @Override
  213. public String getType()
  214. {
  215. return _C__D0_6A_REQUESTPOSTATTACHMENT;
  216. }
  217. @Override
  218. protected boolean triggersOnActionRequest()
  219. {
  220. return false;
  221. }
  222. }