RequestFriendDel.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.SystemMessage;
  25. /**
  26. * This class ...
  27. *
  28. * @version $Revision: 1.3.4.2 $ $Date: 2005/03/27 15:29:30 $
  29. */
  30. public final class RequestFriendDel extends L2GameClientPacket{
  31. private static final String _C__61_REQUESTFRIENDDEL = "[C] 61 RequestFriendDel";
  32. private static Logger _log = Logger.getLogger(RequestFriendDel.class.getName());
  33. private String _name;
  34. @Override
  35. protected void readImpl()
  36. {
  37. _name = readS();
  38. }
  39. @Override
  40. protected void runImpl()
  41. {
  42. SystemMessage sm;
  43. java.sql.Connection con = null;
  44. L2PcInstance activeChar = getClient().getActiveChar();
  45. if (activeChar == null)
  46. return;
  47. try
  48. {
  49. L2PcInstance friend = L2World.getInstance().getPlayer(_name);
  50. con = L2DatabaseFactory.getInstance().getConnection();
  51. PreparedStatement statement;
  52. ResultSet rset;
  53. if (friend != null)
  54. {
  55. statement = con.prepareStatement("SELECT friendId FROM character_friends WHERE charId=? and friendId=?");
  56. statement.setInt(1, activeChar.getObjectId());
  57. statement.setInt(2, friend.getObjectId());
  58. rset = statement.executeQuery();
  59. if (!rset.next())
  60. {
  61. statement.close();
  62. // Player is not in your friendlist
  63. sm = new SystemMessage(SystemMessageId.S1_NOT_ON_YOUR_FRIENDS_LIST);
  64. sm.addString(_name);
  65. activeChar.sendPacket(sm);
  66. sm = null;
  67. return;
  68. }
  69. } else
  70. {
  71. statement = con.prepareStatement("SELECT friendId FROM character_friends AS cf, characters AS c WHERE cf.charId=? AND cf.friendId=c.charId AND char_name=?");
  72. statement.setInt(1, activeChar.getObjectId());
  73. statement.setString(2, _name);
  74. rset = statement.executeQuery();
  75. if (!rset.next())
  76. {
  77. statement.close();
  78. // Player is not in your friendlist
  79. sm = new SystemMessage(SystemMessageId.S1_NOT_ON_YOUR_FRIENDS_LIST);
  80. sm.addString(_name);
  81. activeChar.sendPacket(sm);
  82. sm = null;
  83. return;
  84. }
  85. }
  86. int objectId = rset.getInt("friendId");
  87. statement.close();
  88. rset.close();
  89. statement = con.prepareStatement("DELETE FROM character_friends WHERE (charId=? AND friendId=?) OR (charId=? AND friendId=?)");
  90. statement.setInt(1, activeChar.getObjectId());
  91. statement.setInt(2, objectId);
  92. statement.setInt(3, objectId);
  93. statement.setInt(4, activeChar.getObjectId());
  94. statement.execute();
  95. // Player deleted from your friendlist
  96. sm = new SystemMessage(SystemMessageId.S1_HAS_BEEN_DELETED_FROM_YOUR_FRIENDS_LIST);
  97. sm.addString(_name);
  98. activeChar.sendPacket(sm);
  99. sm = null;
  100. statement.close();
  101. }
  102. catch (Exception e)
  103. {
  104. _log.log(Level.WARNING, "could not del 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__61_REQUESTFRIENDDEL;
  115. }
  116. }