RequestCancelPostAttachment.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 com.l2jserver.Config;
  21. import com.l2jserver.gameserver.enums.ItemLocation;
  22. import com.l2jserver.gameserver.enums.PrivateStoreType;
  23. import com.l2jserver.gameserver.instancemanager.MailManager;
  24. import com.l2jserver.gameserver.model.L2World;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.entity.Message;
  27. import com.l2jserver.gameserver.model.itemcontainer.ItemContainer;
  28. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  29. import com.l2jserver.gameserver.model.zone.ZoneId;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.serverpackets.ExChangePostState;
  32. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  33. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  34. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  35. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  36. import com.l2jserver.gameserver.util.Util;
  37. /**
  38. * @author Migi, DS
  39. */
  40. public final class RequestCancelPostAttachment extends L2GameClientPacket
  41. {
  42. private static final String _C__D0_6F_REQUESTCANCELPOSTATTACHMENT = "[C] D0:6F RequestCancelPostAttachment";
  43. private int _msgId;
  44. @Override
  45. protected void readImpl()
  46. {
  47. _msgId = readD();
  48. }
  49. @Override
  50. public void runImpl()
  51. {
  52. final L2PcInstance activeChar = getClient().getActiveChar();
  53. if ((activeChar == null) || !Config.ALLOW_MAIL || !Config.ALLOW_ATTACHMENTS)
  54. {
  55. return;
  56. }
  57. if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("cancelpost"))
  58. {
  59. return;
  60. }
  61. Message msg = MailManager.getInstance().getMessage(_msgId);
  62. if (msg == null)
  63. {
  64. return;
  65. }
  66. if (msg.getSenderId() != activeChar.getObjectId())
  67. {
  68. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to cancel not own post!", Config.DEFAULT_PUNISH);
  69. return;
  70. }
  71. if (!activeChar.isInsideZone(ZoneId.PEACE))
  72. {
  73. activeChar.sendPacket(SystemMessageId.CANT_CANCEL_NOT_IN_PEACE_ZONE);
  74. return;
  75. }
  76. if (activeChar.getActiveTradeList() != null)
  77. {
  78. activeChar.sendPacket(SystemMessageId.CANT_CANCEL_DURING_EXCHANGE);
  79. return;
  80. }
  81. if (activeChar.isEnchanting())
  82. {
  83. activeChar.sendPacket(SystemMessageId.CANT_CANCEL_DURING_ENCHANT);
  84. return;
  85. }
  86. if (activeChar.getPrivateStoreType() != PrivateStoreType.NONE)
  87. {
  88. activeChar.sendPacket(SystemMessageId.CANT_CANCEL_PRIVATE_STORE);
  89. return;
  90. }
  91. if (!msg.hasAttachments())
  92. {
  93. activeChar.sendPacket(SystemMessageId.YOU_CANT_CANCEL_RECEIVED_MAIL);
  94. return;
  95. }
  96. final ItemContainer attachments = msg.getAttachments();
  97. if ((attachments == null) || (attachments.getSize() == 0))
  98. {
  99. activeChar.sendPacket(SystemMessageId.YOU_CANT_CANCEL_RECEIVED_MAIL);
  100. return;
  101. }
  102. int weight = 0;
  103. int slots = 0;
  104. for (L2ItemInstance item : attachments.getItems())
  105. {
  106. if (item == null)
  107. {
  108. continue;
  109. }
  110. if (item.getOwnerId() != activeChar.getObjectId())
  111. {
  112. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to get not own item from cancelled attachment!", Config.DEFAULT_PUNISH);
  113. return;
  114. }
  115. if (item.getItemLocation() != ItemLocation.MAIL)
  116. {
  117. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to get items not from mail !", Config.DEFAULT_PUNISH);
  118. return;
  119. }
  120. if (item.getLocationSlot() != msg.getId())
  121. {
  122. Util.handleIllegalPlayerAction(activeChar, "Player " + activeChar.getName() + " tried to get items from different attachment!", Config.DEFAULT_PUNISH);
  123. return;
  124. }
  125. weight += item.getCount() * item.getItem().getWeight();
  126. if (!item.isStackable())
  127. {
  128. slots += item.getCount();
  129. }
  130. else if (activeChar.getInventory().getItemByItemId(item.getId()) == null)
  131. {
  132. slots++;
  133. }
  134. }
  135. if (!activeChar.getInventory().validateCapacity(slots))
  136. {
  137. activeChar.sendPacket(SystemMessageId.CANT_CANCEL_INVENTORY_FULL);
  138. return;
  139. }
  140. if (!activeChar.getInventory().validateWeight(weight))
  141. {
  142. activeChar.sendPacket(SystemMessageId.CANT_CANCEL_INVENTORY_FULL);
  143. return;
  144. }
  145. // Proceed to the transfer
  146. InventoryUpdate playerIU = Config.FORCE_INVENTORY_UPDATE ? null : new InventoryUpdate();
  147. for (L2ItemInstance item : attachments.getItems())
  148. {
  149. if (item == null)
  150. {
  151. continue;
  152. }
  153. long count = item.getCount();
  154. final L2ItemInstance newItem = attachments.transferItem(attachments.getName(), item.getObjectId(), count, activeChar.getInventory(), activeChar, null);
  155. if (newItem == null)
  156. {
  157. return;
  158. }
  159. if (playerIU != null)
  160. {
  161. if (newItem.getCount() > count)
  162. {
  163. playerIU.addModifiedItem(newItem);
  164. }
  165. else
  166. {
  167. playerIU.addNewItem(newItem);
  168. }
  169. }
  170. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_ACQUIRED_S2_S1);
  171. sm.addItemName(item.getId());
  172. sm.addLong(count);
  173. activeChar.sendPacket(sm);
  174. }
  175. msg.removeAttachments();
  176. // Send updated item list to the player
  177. if (playerIU != null)
  178. {
  179. activeChar.sendPacket(playerIU);
  180. }
  181. else
  182. {
  183. activeChar.sendPacket(new ItemList(activeChar, false));
  184. }
  185. // Update current load status on player
  186. StatusUpdate su = new StatusUpdate(activeChar);
  187. su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
  188. activeChar.sendPacket(su);
  189. final L2PcInstance receiver = L2World.getInstance().getPlayer(msg.getReceiverId());
  190. if (receiver != null)
  191. {
  192. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_CANCELLED_MAIL);
  193. sm.addCharName(activeChar);
  194. receiver.sendPacket(sm);
  195. receiver.sendPacket(new ExChangePostState(true, _msgId, Message.DELETED));
  196. }
  197. MailManager.getInstance().deleteMessageInDb(_msgId);
  198. activeChar.sendPacket(new ExChangePostState(false, _msgId, Message.DELETED));
  199. activeChar.sendPacket(SystemMessageId.MAIL_SUCCESSFULLY_CANCELLED);
  200. }
  201. @Override
  202. public String getType()
  203. {
  204. return _C__D0_6F_REQUESTCANCELPOSTATTACHMENT;
  205. }
  206. @Override
  207. protected boolean triggersOnActionRequest()
  208. {
  209. return false;
  210. }
  211. }