RequestCancelPost.java 6.9 KB

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