BlockList.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  30. import com.l2jserver.gameserver.data.sql.impl.CharNameTable;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. import com.l2jserver.gameserver.network.SystemMessageId;
  33. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  34. public class BlockList
  35. {
  36. private static Logger _log = Logger.getLogger(BlockList.class.getName());
  37. private static final Map<Integer, List<Integer>> OFFLINE_LIST = new ConcurrentHashMap<>();
  38. private final L2PcInstance _owner;
  39. private List<Integer> _blockList;
  40. public BlockList(L2PcInstance owner)
  41. {
  42. _owner = owner;
  43. _blockList = OFFLINE_LIST.get(owner.getObjectId());
  44. if (_blockList == null)
  45. {
  46. _blockList = loadList(_owner.getObjectId());
  47. }
  48. }
  49. private void addToBlockList(int target)
  50. {
  51. _blockList.add(target);
  52. persistInDB(target);
  53. }
  54. private void removeFromBlockList(int target)
  55. {
  56. _blockList.remove(Integer.valueOf(target));
  57. removeFromDB(target);
  58. }
  59. public void playerLogout()
  60. {
  61. OFFLINE_LIST.put(_owner.getObjectId(), _blockList);
  62. }
  63. private static List<Integer> loadList(int ObjId)
  64. {
  65. List<Integer> list = new ArrayList<>();
  66. try (Connection con = ConnectionFactory.getInstance().getConnection();
  67. PreparedStatement ps = con.prepareStatement("SELECT friendId FROM character_friends WHERE charId=? AND relation=1"))
  68. {
  69. ps.setInt(1, ObjId);
  70. try (ResultSet rs = ps.executeQuery())
  71. {
  72. int friendId;
  73. while (rs.next())
  74. {
  75. friendId = rs.getInt("friendId");
  76. if (friendId == ObjId)
  77. {
  78. continue;
  79. }
  80. list.add(friendId);
  81. }
  82. }
  83. }
  84. catch (Exception e)
  85. {
  86. _log.log(Level.WARNING, "Error found in " + ObjId + " FriendList while loading BlockList: " + e.getMessage(), e);
  87. }
  88. return list;
  89. }
  90. private void removeFromDB(int targetId)
  91. {
  92. try (Connection con = ConnectionFactory.getInstance().getConnection();
  93. PreparedStatement ps = con.prepareStatement("DELETE FROM character_friends WHERE charId=? AND friendId=? AND relation=1"))
  94. {
  95. ps.setInt(1, _owner.getObjectId());
  96. ps.setInt(2, targetId);
  97. ps.execute();
  98. }
  99. catch (Exception e)
  100. {
  101. _log.log(Level.WARNING, "Could not remove blocked player: " + e.getMessage(), e);
  102. }
  103. }
  104. private void persistInDB(int targetId)
  105. {
  106. try (Connection con = ConnectionFactory.getInstance().getConnection();
  107. PreparedStatement ps = con.prepareStatement("INSERT INTO character_friends (charId, friendId, relation) VALUES (?, ?, 1)"))
  108. {
  109. ps.setInt(1, _owner.getObjectId());
  110. ps.setInt(2, targetId);
  111. ps.execute();
  112. }
  113. catch (Exception e)
  114. {
  115. _log.log(Level.WARNING, "Could not add blocked player: " + e.getMessage(), e);
  116. }
  117. }
  118. public boolean isInBlockList(L2PcInstance target)
  119. {
  120. return _blockList.contains(target.getObjectId());
  121. }
  122. public boolean isInBlockList(int targetId)
  123. {
  124. return _blockList.contains(targetId);
  125. }
  126. private boolean isBlockAll()
  127. {
  128. return _owner.getMessageRefusal();
  129. }
  130. public static boolean isBlocked(L2PcInstance listOwner, L2PcInstance target)
  131. {
  132. BlockList blockList = listOwner.getBlockList();
  133. return blockList.isBlockAll() || blockList.isInBlockList(target);
  134. }
  135. public static boolean isBlocked(L2PcInstance listOwner, int targetId)
  136. {
  137. BlockList blockList = listOwner.getBlockList();
  138. return blockList.isBlockAll() || blockList.isInBlockList(targetId);
  139. }
  140. private void setBlockAll(boolean state)
  141. {
  142. _owner.setMessageRefusal(state);
  143. }
  144. private List<Integer> getBlockList()
  145. {
  146. return _blockList;
  147. }
  148. public static void addToBlockList(L2PcInstance listOwner, int targetId)
  149. {
  150. if (listOwner == null)
  151. {
  152. return;
  153. }
  154. String charName = CharNameTable.getInstance().getNameById(targetId);
  155. if (listOwner.getFriendList().contains(targetId))
  156. {
  157. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_ALREADY_IN_FRIENDS_LIST);
  158. sm.addString(charName);
  159. listOwner.sendPacket(sm);
  160. return;
  161. }
  162. if (listOwner.getBlockList().getBlockList().contains(targetId))
  163. {
  164. listOwner.sendMessage("Already in ignore list.");
  165. return;
  166. }
  167. listOwner.getBlockList().addToBlockList(targetId);
  168. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_WAS_ADDED_TO_YOUR_IGNORE_LIST);
  169. sm.addString(charName);
  170. listOwner.sendPacket(sm);
  171. L2PcInstance player = L2World.getInstance().getPlayer(targetId);
  172. if (player != null)
  173. {
  174. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_HAS_ADDED_YOU_TO_IGNORE_LIST);
  175. sm.addString(listOwner.getName());
  176. player.sendPacket(sm);
  177. }
  178. }
  179. public static void removeFromBlockList(L2PcInstance listOwner, int targetId)
  180. {
  181. if (listOwner == null)
  182. {
  183. return;
  184. }
  185. SystemMessage sm;
  186. String charName = CharNameTable.getInstance().getNameById(targetId);
  187. if (!listOwner.getBlockList().getBlockList().contains(targetId))
  188. {
  189. sm = SystemMessage.getSystemMessage(SystemMessageId.TARGET_IS_INCORRECT);
  190. listOwner.sendPacket(sm);
  191. return;
  192. }
  193. listOwner.getBlockList().removeFromBlockList(targetId);
  194. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_WAS_REMOVED_FROM_YOUR_IGNORE_LIST);
  195. sm.addString(charName);
  196. listOwner.sendPacket(sm);
  197. }
  198. public static boolean isInBlockList(L2PcInstance listOwner, L2PcInstance target)
  199. {
  200. return listOwner.getBlockList().isInBlockList(target);
  201. }
  202. public boolean isBlockAll(L2PcInstance listOwner)
  203. {
  204. return listOwner.getBlockList().isBlockAll();
  205. }
  206. public static void setBlockAll(L2PcInstance listOwner, boolean newValue)
  207. {
  208. listOwner.getBlockList().setBlockAll(newValue);
  209. }
  210. public static void sendListToOwner(L2PcInstance listOwner)
  211. {
  212. int i = 1;
  213. listOwner.sendPacket(SystemMessageId.BLOCK_LIST_HEADER);
  214. for (int playerId : listOwner.getBlockList().getBlockList())
  215. {
  216. listOwner.sendMessage((i++) + ". " + CharNameTable.getInstance().getNameById(playerId));
  217. }
  218. listOwner.sendPacket(SystemMessageId.FRIEND_LIST_FOOTER);
  219. }
  220. /**
  221. * @param ownerId object id of owner block list
  222. * @param targetId object id of potential blocked player
  223. * @return true if blocked
  224. */
  225. public static boolean isInBlockList(int ownerId, int targetId)
  226. {
  227. L2PcInstance player = L2World.getInstance().getPlayer(ownerId);
  228. if (player != null)
  229. {
  230. return BlockList.isBlocked(player, targetId);
  231. }
  232. if (!OFFLINE_LIST.containsKey(ownerId))
  233. {
  234. OFFLINE_LIST.put(ownerId, loadList(ownerId));
  235. }
  236. return OFFLINE_LIST.get(ownerId).contains(targetId);
  237. }
  238. }