RequestFriendInvite.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import net.sf.l2j.L2DatabaseFactory;
  21. import net.sf.l2j.gameserver.model.L2World;
  22. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  23. import net.sf.l2j.gameserver.network.SystemMessageId;
  24. import net.sf.l2j.gameserver.serverpackets.FriendAddRequest;
  25. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  26. import net.sf.l2j.gameserver.util.Util;
  27. /**
  28. * This class ...
  29. *
  30. * @version $Revision: 1.3.4.2 $ $Date: 2005/03/27 15:29:30 $
  31. */
  32. public final class RequestFriendInvite extends L2GameClientPacket
  33. {
  34. private static final String _C__5E_REQUESTFRIENDINVITE = "[C] 5E RequestFriendInvite";
  35. private static Logger _log = Logger.getLogger(RequestFriendInvite.class.getName());
  36. private String _name;
  37. @Override
  38. protected void readImpl()
  39. {
  40. _name = readS();
  41. }
  42. @Override
  43. protected void runImpl()
  44. {
  45. SystemMessage sm;
  46. java.sql.Connection con = null;
  47. L2PcInstance activeChar = getClient().getActiveChar();
  48. if (activeChar == null)
  49. return;
  50. L2PcInstance friend = L2World.getInstance().getPlayer(_name);
  51. _name = Util.capitalizeFirst(_name); //FIXME: is it right to capitalize a nickname?
  52. if (friend == null)
  53. {
  54. //Target is not found in the game.
  55. sm = new SystemMessage(SystemMessageId.THE_USER_YOU_REQUESTED_IS_NOT_IN_GAME);
  56. activeChar.sendPacket(sm);
  57. sm = null;
  58. return;
  59. }
  60. else if (friend == activeChar)
  61. {
  62. //You cannot add yourself to your own friend list.
  63. sm = new SystemMessage(SystemMessageId.YOU_CANNOT_ADD_YOURSELF_TO_OWN_FRIEND_LIST);
  64. activeChar.sendPacket(sm);
  65. sm = null;
  66. return;
  67. }
  68. try
  69. {
  70. con = L2DatabaseFactory.getInstance().getConnection();
  71. PreparedStatement statement = con.prepareStatement("SELECT charId FROM character_friends WHERE charId=? AND friendId=?");
  72. statement.setInt(1, activeChar.getObjectId());
  73. statement.setInt(2, friend.getObjectId());
  74. ResultSet rset = statement.executeQuery();
  75. if (rset.next())
  76. {
  77. //Player already is in your friendlist
  78. sm = new SystemMessage(SystemMessageId.S1_ALREADY_IN_FRIENDS_LIST);
  79. sm.addString(_name);
  80. }
  81. else
  82. {
  83. if (!friend.isProcessingRequest())
  84. {
  85. //requets to become friend
  86. activeChar.onTransactionRequest(friend);
  87. sm = new SystemMessage(SystemMessageId.S1_REQUESTED_TO_BECOME_FRIENDS);
  88. sm.addString(_name);
  89. FriendAddRequest ajf = new FriendAddRequest(activeChar.getName());
  90. friend.sendPacket(ajf);
  91. }
  92. else
  93. {
  94. sm = new SystemMessage(SystemMessageId.S1_IS_BUSY_TRY_LATER);
  95. }
  96. }
  97. friend.sendPacket(sm);
  98. sm = null;
  99. rset.close();
  100. statement.close();
  101. }
  102. catch (Exception e)
  103. {
  104. _log.log(Level.WARNING, "could not add friend objectid: ", e);
  105. }
  106. finally
  107. {
  108. try { con.close(); } catch (Exception e) {}
  109. }
  110. }
  111. @Override
  112. public String getType()
  113. {
  114. return _C__5E_REQUESTFRIENDINVITE;
  115. }
  116. }