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.L2DatabaseFactory;
  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. updateInDB(target, true);
  53. }
  54. private void removeFromBlockList(int target)
  55. {
  56. _blockList.remove(Integer.valueOf(target));
  57. updateInDB(target, false);
  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 = L2DatabaseFactory.getInstance().getConnection();
  67. PreparedStatement statement = con.prepareStatement("SELECT friendId FROM character_friends WHERE charId=? AND relation=1"))
  68. {
  69. statement.setInt(1, ObjId);
  70. try (ResultSet rset = statement.executeQuery())
  71. {
  72. int friendId;
  73. while (rset.next())
  74. {
  75. friendId = rset.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 updateInDB(int targetId, boolean state)
  91. {
  92. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  93. {
  94. if (state) // add
  95. {
  96. try (PreparedStatement statement = con.prepareStatement("INSERT INTO character_friends (charId, friendId, relation) VALUES (?, ?, 1)"))
  97. {
  98. statement.setInt(1, _owner.getObjectId());
  99. statement.setInt(2, targetId);
  100. statement.execute();
  101. }
  102. }
  103. else
  104. // remove
  105. {
  106. try (PreparedStatement statement = con.prepareStatement("DELETE FROM character_friends WHERE charId=? AND friendId=? AND relation=1"))
  107. {
  108. statement.setInt(1, _owner.getObjectId());
  109. statement.setInt(2, targetId);
  110. statement.execute();
  111. }
  112. }
  113. }
  114. catch (Exception e)
  115. {
  116. _log.log(Level.WARNING, "Could not add block player: " + e.getMessage(), e);
  117. }
  118. }
  119. public boolean isInBlockList(L2PcInstance target)
  120. {
  121. return _blockList.contains(target.getObjectId());
  122. }
  123. public boolean isInBlockList(int targetId)
  124. {
  125. return _blockList.contains(targetId);
  126. }
  127. private boolean isBlockAll()
  128. {
  129. return _owner.getMessageRefusal();
  130. }
  131. public static boolean isBlocked(L2PcInstance listOwner, L2PcInstance target)
  132. {
  133. BlockList blockList = listOwner.getBlockList();
  134. return blockList.isBlockAll() || blockList.isInBlockList(target);
  135. }
  136. public static boolean isBlocked(L2PcInstance listOwner, int targetId)
  137. {
  138. BlockList blockList = listOwner.getBlockList();
  139. return blockList.isBlockAll() || blockList.isInBlockList(targetId);
  140. }
  141. private void setBlockAll(boolean state)
  142. {
  143. _owner.setMessageRefusal(state);
  144. }
  145. private List<Integer> getBlockList()
  146. {
  147. return _blockList;
  148. }
  149. public static void addToBlockList(L2PcInstance listOwner, int targetId)
  150. {
  151. if (listOwner == null)
  152. {
  153. return;
  154. }
  155. String charName = CharNameTable.getInstance().getNameById(targetId);
  156. if (listOwner.getFriendList().contains(targetId))
  157. {
  158. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_ALREADY_IN_FRIENDS_LIST);
  159. sm.addString(charName);
  160. listOwner.sendPacket(sm);
  161. return;
  162. }
  163. if (listOwner.getBlockList().getBlockList().contains(targetId))
  164. {
  165. listOwner.sendMessage("Already in ignore list.");
  166. return;
  167. }
  168. listOwner.getBlockList().addToBlockList(targetId);
  169. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_WAS_ADDED_TO_YOUR_IGNORE_LIST);
  170. sm.addString(charName);
  171. listOwner.sendPacket(sm);
  172. L2PcInstance player = L2World.getInstance().getPlayer(targetId);
  173. if (player != null)
  174. {
  175. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_HAS_ADDED_YOU_TO_IGNORE_LIST);
  176. sm.addString(listOwner.getName());
  177. player.sendPacket(sm);
  178. }
  179. }
  180. public static void removeFromBlockList(L2PcInstance listOwner, int targetId)
  181. {
  182. if (listOwner == null)
  183. {
  184. return;
  185. }
  186. SystemMessage sm;
  187. String charName = CharNameTable.getInstance().getNameById(targetId);
  188. if (!listOwner.getBlockList().getBlockList().contains(targetId))
  189. {
  190. sm = SystemMessage.getSystemMessage(SystemMessageId.TARGET_IS_INCORRECT);
  191. listOwner.sendPacket(sm);
  192. return;
  193. }
  194. listOwner.getBlockList().removeFromBlockList(targetId);
  195. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_WAS_REMOVED_FROM_YOUR_IGNORE_LIST);
  196. sm.addString(charName);
  197. listOwner.sendPacket(sm);
  198. }
  199. public static boolean isInBlockList(L2PcInstance listOwner, L2PcInstance target)
  200. {
  201. return listOwner.getBlockList().isInBlockList(target);
  202. }
  203. public boolean isBlockAll(L2PcInstance listOwner)
  204. {
  205. return listOwner.getBlockList().isBlockAll();
  206. }
  207. public static void setBlockAll(L2PcInstance listOwner, boolean newValue)
  208. {
  209. listOwner.getBlockList().setBlockAll(newValue);
  210. }
  211. public static void sendListToOwner(L2PcInstance listOwner)
  212. {
  213. int i = 1;
  214. listOwner.sendPacket(SystemMessageId.BLOCK_LIST_HEADER);
  215. for (int playerId : listOwner.getBlockList().getBlockList())
  216. {
  217. listOwner.sendMessage((i++) + ". " + CharNameTable.getInstance().getNameById(playerId));
  218. }
  219. listOwner.sendPacket(SystemMessageId.FRIEND_LIST_FOOTER);
  220. }
  221. /**
  222. * @param ownerId object id of owner block list
  223. * @param targetId object id of potential blocked player
  224. * @return true if blocked
  225. */
  226. public static boolean isInBlockList(int ownerId, int targetId)
  227. {
  228. L2PcInstance player = L2World.getInstance().getPlayer(ownerId);
  229. if (player != null)
  230. {
  231. return BlockList.isBlocked(player, targetId);
  232. }
  233. if (!OFFLINE_LIST.containsKey(ownerId))
  234. {
  235. OFFLINE_LIST.put(ownerId, loadList(ownerId));
  236. }
  237. return OFFLINE_LIST.get(ownerId).contains(targetId);
  238. }
  239. }