L2ContactList.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.model;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.List;
  24. import java.util.concurrent.CopyOnWriteArrayList;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  28. import com.l2jserver.gameserver.data.sql.impl.CharNameTable;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  32. /**
  33. * TODO: System messages:<br>
  34. * ADD: 3223: The previous name is being registered. Please try again later.<br>
  35. * DEL 3219: $s1 was successfully deleted from your Contact List.<br>
  36. * DEL 3217: The name is not currently registered.
  37. * @author UnAfraid, mrTJO
  38. */
  39. public class L2ContactList
  40. {
  41. private final Logger _log = Logger.getLogger(getClass().getName());
  42. private final L2PcInstance activeChar;
  43. private final List<String> _contacts = new CopyOnWriteArrayList<>();
  44. private static final String QUERY_ADD = "INSERT INTO character_contacts (charId, contactId) VALUES (?, ?)";
  45. private static final String QUERY_REMOVE = "DELETE FROM character_contacts WHERE charId = ? and contactId = ?";
  46. private static final String QUERY_LOAD = "SELECT contactId FROM character_contacts WHERE charId = ?";
  47. public L2ContactList(L2PcInstance player)
  48. {
  49. activeChar = player;
  50. restore();
  51. }
  52. public void restore()
  53. {
  54. _contacts.clear();
  55. try (Connection con = ConnectionFactory.getInstance().getConnection();
  56. PreparedStatement ps = con.prepareStatement(QUERY_LOAD))
  57. {
  58. ps.setInt(1, activeChar.getObjectId());
  59. try (ResultSet rs = ps.executeQuery())
  60. {
  61. int contactId;
  62. String contactName;
  63. while (rs.next())
  64. {
  65. contactId = rs.getInt(1);
  66. contactName = CharNameTable.getInstance().getNameById(contactId);
  67. if ((contactName == null) || contactName.equals(activeChar.getName()) || (contactId == activeChar.getObjectId()))
  68. {
  69. continue;
  70. }
  71. _contacts.add(contactName);
  72. }
  73. }
  74. }
  75. catch (Exception e)
  76. {
  77. _log.log(Level.WARNING, "Error found in " + activeChar.getName() + "'s ContactsList: " + e.getMessage(), e);
  78. }
  79. }
  80. public boolean add(String name)
  81. {
  82. SystemMessage sm;
  83. int contactId = CharNameTable.getInstance().getIdByName(name);
  84. if (_contacts.contains(name))
  85. {
  86. activeChar.sendPacket(SystemMessageId.NAME_ALREADY_EXIST_ON_CONTACT_LIST);
  87. return false;
  88. }
  89. else if (activeChar.getName().equals(name))
  90. {
  91. activeChar.sendPacket(SystemMessageId.CANNOT_ADD_YOUR_NAME_ON_CONTACT_LIST);
  92. return false;
  93. }
  94. else if (_contacts.size() >= 100)
  95. {
  96. activeChar.sendPacket(SystemMessageId.CONTACT_LIST_LIMIT_REACHED);
  97. return false;
  98. }
  99. else if (contactId < 1)
  100. {
  101. sm = SystemMessage.getSystemMessage(SystemMessageId.NAME_S1_NOT_EXIST_TRY_ANOTHER_NAME);
  102. sm.addString(name);
  103. activeChar.sendPacket(sm);
  104. return false;
  105. }
  106. else
  107. {
  108. for (String contactName : _contacts)
  109. {
  110. if (contactName.equalsIgnoreCase(name))
  111. {
  112. activeChar.sendPacket(SystemMessageId.NAME_ALREADY_EXIST_ON_CONTACT_LIST);
  113. return false;
  114. }
  115. }
  116. }
  117. try (Connection con = ConnectionFactory.getInstance().getConnection();
  118. PreparedStatement ps = con.prepareStatement(QUERY_ADD))
  119. {
  120. ps.setInt(1, activeChar.getObjectId());
  121. ps.setInt(2, contactId);
  122. ps.execute();
  123. _contacts.add(name);
  124. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_SUCCESSFULLY_ADDED_TO_CONTACT_LIST);
  125. sm.addString(name);
  126. activeChar.sendPacket(sm);
  127. }
  128. catch (Exception e)
  129. {
  130. _log.log(Level.WARNING, "Error found in " + activeChar.getName() + "'s ContactsList: " + e.getMessage(), e);
  131. }
  132. return true;
  133. }
  134. public void remove(String name)
  135. {
  136. int contactId = CharNameTable.getInstance().getIdByName(name);
  137. if (!_contacts.contains(name))
  138. {
  139. activeChar.sendPacket(SystemMessageId.NAME_NOT_REGISTERED_ON_CONTACT_LIST);
  140. return;
  141. }
  142. else if (contactId < 1)
  143. {
  144. // TODO: Message?
  145. return;
  146. }
  147. _contacts.remove(name);
  148. try (Connection con = ConnectionFactory.getInstance().getConnection();
  149. PreparedStatement ps = con.prepareStatement(QUERY_REMOVE))
  150. {
  151. ps.setInt(1, activeChar.getObjectId());
  152. ps.setInt(2, contactId);
  153. ps.execute();
  154. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_SUCCESFULLY_DELETED_FROM_CONTACT_LIST);
  155. sm.addString(name);
  156. activeChar.sendPacket(sm);
  157. }
  158. catch (Exception e)
  159. {
  160. _log.log(Level.WARNING, "Error found in " + activeChar.getName() + "'s ContactsList: " + e.getMessage(), e);
  161. }
  162. }
  163. public List<String> getAllContacts()
  164. {
  165. return _contacts;
  166. }
  167. }