BlockList.java 7.9 KB

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