RequestAnswerFriendInvite.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.util.logging.Level;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.network.SystemMessageId;
  26. import com.l2jserver.gameserver.network.serverpackets.FriendPacket;
  27. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  28. public final class RequestAnswerFriendInvite extends L2GameClientPacket
  29. {
  30. private static final String _C__78_REQUESTANSWERFRIENDINVITE = "[C] 78 RequestAnswerFriendInvite";
  31. private int _response;
  32. @Override
  33. protected void readImpl()
  34. {
  35. _response = readD();
  36. }
  37. @Override
  38. protected void runImpl()
  39. {
  40. L2PcInstance player = getClient().getActiveChar();
  41. if (player != null)
  42. {
  43. L2PcInstance requestor = player.getActiveRequester();
  44. if (requestor == null)
  45. {
  46. return;
  47. }
  48. if (_response == 1)
  49. {
  50. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  51. PreparedStatement statement = con.prepareStatement("INSERT INTO character_friends (charId, friendId) VALUES (?, ?), (?, ?)"))
  52. {
  53. statement.setInt(1, requestor.getObjectId());
  54. statement.setInt(2, player.getObjectId());
  55. statement.setInt(3, player.getObjectId());
  56. statement.setInt(4, requestor.getObjectId());
  57. statement.execute();
  58. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.YOU_HAVE_SUCCEEDED_INVITING_FRIEND);
  59. requestor.sendPacket(msg);
  60. // Player added to your friend list
  61. msg = SystemMessage.getSystemMessage(SystemMessageId.S1_ADDED_TO_FRIENDS);
  62. msg.addString(player.getName());
  63. requestor.sendPacket(msg);
  64. requestor.getFriendList().add(player.getObjectId());
  65. // has joined as friend.
  66. msg = SystemMessage.getSystemMessage(SystemMessageId.S1_JOINED_AS_FRIEND);
  67. msg.addString(requestor.getName());
  68. player.sendPacket(msg);
  69. player.getFriendList().add(requestor.getObjectId());
  70. // Send notifications for both player in order to show them online
  71. player.sendPacket(new FriendPacket(true, requestor.getObjectId()));
  72. requestor.sendPacket(new FriendPacket(true, player.getObjectId()));
  73. }
  74. catch (Exception e)
  75. {
  76. _log.log(Level.WARNING, "Could not add friend objectid: " + e.getMessage(), e);
  77. }
  78. }
  79. else
  80. {
  81. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.FAILED_TO_INVITE_A_FRIEND);
  82. requestor.sendPacket(msg);
  83. }
  84. player.setActiveRequester(null);
  85. requestor.onTransactionResponse();
  86. }
  87. }
  88. @Override
  89. public String getType()
  90. {
  91. return _C__78_REQUESTANSWERFRIENDINVITE;
  92. }
  93. }