BlockList.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * $Header: BlockList.java, 21/11/2005 14:53:53 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 21/11/2005 14:53:53 $
  6. * $Revision: 1 $
  7. * $Log: BlockList.java,v $
  8. * Revision 1 21/11/2005 14:53:53 luisantonioa
  9. * Added copyright notice
  10. *
  11. *
  12. * This program is free software: you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option) any later
  15. * version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. package com.l2jserver.gameserver.model;
  26. import java.sql.Connection;
  27. import java.sql.PreparedStatement;
  28. import java.sql.ResultSet;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.logging.Logger;
  32. import javolution.util.FastList;
  33. import javolution.util.FastMap;
  34. import com.l2jserver.L2DatabaseFactory;
  35. import com.l2jserver.gameserver.datatables.CharNameTable;
  36. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  37. import com.l2jserver.gameserver.network.SystemMessageId;
  38. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  39. /**
  40. * This class ...
  41. *
  42. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  43. */
  44. public class BlockList
  45. {
  46. private static Logger _log = Logger.getLogger(BlockList.class.getName());
  47. private static Map<Integer, List<Integer>> _offlineList = new FastMap<Integer, List<Integer>>();
  48. private final L2PcInstance _owner;
  49. private List<Integer> _blockList;
  50. public BlockList(L2PcInstance owner)
  51. {
  52. _owner = owner;
  53. _blockList = _offlineList.get(owner.getObjectId());
  54. if (_blockList == null)
  55. _blockList = loadList(_owner.getObjectId());
  56. }
  57. private synchronized void addToBlockList(int target)
  58. {
  59. _blockList.add(target);
  60. updateInDB(target, true);
  61. }
  62. private synchronized void removeFromBlockList(int target)
  63. {
  64. _blockList.remove(Integer.valueOf(target));
  65. updateInDB(target, false);
  66. }
  67. public void playerLogout()
  68. {
  69. _offlineList.put(_owner.getObjectId(), _blockList);
  70. }
  71. private static List<Integer> loadList(int ObjId)
  72. {
  73. Connection con = null;
  74. List<Integer> list = new FastList<Integer>();
  75. try
  76. {
  77. con = L2DatabaseFactory.getInstance().getConnection();
  78. PreparedStatement statement = con.prepareStatement("SELECT friendId FROM character_friends WHERE charId=? AND relation=1");
  79. statement.setInt(1, ObjId);
  80. ResultSet rset = statement.executeQuery();
  81. int friendId;
  82. while (rset.next())
  83. {
  84. friendId = rset.getInt("friendId");
  85. if (friendId == ObjId)
  86. continue;
  87. list.add(friendId);
  88. }
  89. rset.close();
  90. statement.close();
  91. }
  92. catch (Exception e)
  93. {
  94. _log.warning("Error found in " + ObjId + " FriendList while loading BlockList: " + e);
  95. }
  96. finally
  97. {
  98. try { con.close(); } catch (Exception e) {}
  99. }
  100. return list;
  101. }
  102. private void updateInDB(int targetId, boolean state)
  103. {
  104. Connection con = null;
  105. try
  106. {
  107. con = L2DatabaseFactory.getInstance().getConnection();
  108. PreparedStatement statement;
  109. if (state) //add
  110. {
  111. statement = con.prepareStatement("INSERT INTO character_friends (charId, friendId, relation) VALUES (?, ?, 1)");
  112. statement.setInt(1, _owner.getObjectId());
  113. statement.setInt(2, targetId);
  114. }
  115. else //remove
  116. {
  117. statement = con.prepareStatement("DELETE FROM character_friends WHERE charId=? AND friendId=? AND relation=1");
  118. statement.setInt(1, _owner.getObjectId());
  119. statement.setInt(2, targetId);
  120. }
  121. statement.execute();
  122. statement.close();
  123. }
  124. catch (Exception e)
  125. {
  126. _log.warning("Could not add block player: " + e);
  127. }
  128. finally
  129. {
  130. try
  131. {
  132. con.close();
  133. }
  134. catch (Exception e)
  135. {}
  136. }
  137. }
  138. public boolean isInBlockList(L2PcInstance target)
  139. {
  140. return _blockList.contains(target.getObjectId());
  141. }
  142. public boolean isInBlockList(int targetId)
  143. {
  144. return _blockList.contains(targetId);
  145. }
  146. private boolean isBlockAll()
  147. {
  148. return _owner.getMessageRefusal();
  149. }
  150. public static boolean isBlocked(L2PcInstance listOwner, L2PcInstance target)
  151. {
  152. BlockList blockList = listOwner.getBlockList();
  153. return blockList.isBlockAll() || blockList.isInBlockList(target);
  154. }
  155. public static boolean isBlocked(L2PcInstance listOwner, int targetId)
  156. {
  157. BlockList blockList = listOwner.getBlockList();
  158. return blockList.isBlockAll() || blockList.isInBlockList(targetId);
  159. }
  160. private void setBlockAll(boolean state)
  161. {
  162. _owner.setMessageRefusal(state);
  163. }
  164. private List<Integer> getBlockList()
  165. {
  166. return _blockList;
  167. }
  168. public static void addToBlockList(L2PcInstance listOwner, int targetId)
  169. {
  170. if (listOwner == null)
  171. return;
  172. String charName = CharNameTable.getInstance().getNameById(targetId);
  173. if (listOwner.getFriendList().contains(targetId))
  174. {
  175. SystemMessage sm = new SystemMessage(SystemMessageId.S1_ALREADY_IN_FRIENDS_LIST);
  176. sm.addString(charName);
  177. listOwner.sendPacket(sm);
  178. return;
  179. }
  180. if (listOwner.getBlockList().getBlockList().contains(targetId))
  181. {
  182. listOwner.sendMessage("Already in ignore list.");
  183. return;
  184. }
  185. listOwner.getBlockList().addToBlockList(targetId);
  186. SystemMessage sm = new SystemMessage(SystemMessageId.S1_WAS_ADDED_TO_YOUR_IGNORE_LIST);
  187. sm.addString(charName);
  188. listOwner.sendPacket(sm);
  189. L2PcInstance player = L2World.getInstance().getPlayer(targetId);
  190. if (player != null)
  191. {
  192. sm = new SystemMessage(SystemMessageId.S1_HAS_ADDED_YOU_TO_IGNORE_LIST);
  193. sm.addString(listOwner.getName());
  194. player.sendPacket(sm);
  195. }
  196. }
  197. public static void removeFromBlockList(L2PcInstance listOwner, int targetId)
  198. {
  199. if (listOwner == null)
  200. return;
  201. SystemMessage sm;
  202. String charName = CharNameTable.getInstance().getNameById(targetId);
  203. if (!listOwner.getBlockList().getBlockList().contains(targetId))
  204. {
  205. sm = new SystemMessage(SystemMessageId.TARGET_IS_INCORRECT);
  206. listOwner.sendPacket(sm);
  207. return;
  208. }
  209. listOwner.getBlockList().removeFromBlockList(targetId);
  210. sm = new SystemMessage(SystemMessageId.S1_WAS_REMOVED_FROM_YOUR_IGNORE_LIST);
  211. sm.addString(charName);
  212. listOwner.sendPacket(sm);
  213. }
  214. public static boolean isInBlockList(L2PcInstance listOwner, L2PcInstance target)
  215. {
  216. return listOwner.getBlockList().isInBlockList(target);
  217. }
  218. public boolean isBlockAll(L2PcInstance listOwner)
  219. {
  220. return listOwner.getBlockList().isBlockAll();
  221. }
  222. public static void setBlockAll(L2PcInstance listOwner, boolean newValue)
  223. {
  224. listOwner.getBlockList().setBlockAll(newValue);
  225. }
  226. public static void sendListToOwner(L2PcInstance listOwner)
  227. {
  228. int i = 1;
  229. listOwner.sendPacket(SystemMessageId.BLOCK_LIST_HEADER);
  230. for (int playerId : listOwner.getBlockList().getBlockList())
  231. {
  232. listOwner.sendMessage((i++)+". "+CharNameTable.getInstance().getNameById(playerId));
  233. }
  234. listOwner.sendPacket(SystemMessageId.FRIEND_LIST_FOOTER);
  235. }
  236. /**
  237. *
  238. * @param ownerId object id of owner block list
  239. * @param targetId object id of potential blocked player
  240. * @return true if blocked
  241. */
  242. public static boolean isInBlockList(int ownerId, int targetId)
  243. {
  244. L2PcInstance player = L2World.getInstance().getPlayer(ownerId);
  245. if (player != null)
  246. {
  247. return BlockList.isBlocked(player, targetId);
  248. }
  249. if (!_offlineList.containsKey(ownerId))
  250. {
  251. _offlineList.put(ownerId, loadList(ownerId));
  252. }
  253. return _offlineList.get(ownerId).contains(targetId);
  254. }
  255. }