123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.model;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
- import com.l2jserver.gameserver.data.sql.impl.CharNameTable;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.network.SystemMessageId;
- import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
- public class BlockList
- {
- private static Logger _log = Logger.getLogger(BlockList.class.getName());
- private static final Map<Integer, List<Integer>> OFFLINE_LIST = new ConcurrentHashMap<>();
-
- private final L2PcInstance _owner;
- private List<Integer> _blockList;
-
- public BlockList(L2PcInstance owner)
- {
- _owner = owner;
- _blockList = OFFLINE_LIST.get(owner.getObjectId());
- if (_blockList == null)
- {
- _blockList = loadList(_owner.getObjectId());
- }
- }
-
- private void addToBlockList(int target)
- {
- _blockList.add(target);
- persistInDB(target);
- }
-
- private void removeFromBlockList(int target)
- {
- _blockList.remove(Integer.valueOf(target));
- removeFromDB(target);
- }
-
- public void playerLogout()
- {
- OFFLINE_LIST.put(_owner.getObjectId(), _blockList);
- }
-
- private static List<Integer> loadList(int ObjId)
- {
- List<Integer> list = new ArrayList<>();
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT friendId FROM character_friends WHERE charId=? AND relation=1"))
- {
- ps.setInt(1, ObjId);
- try (ResultSet rs = ps.executeQuery())
- {
- int friendId;
- while (rs.next())
- {
- friendId = rs.getInt("friendId");
- if (friendId == ObjId)
- {
- continue;
- }
- list.add(friendId);
- }
- }
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Error found in " + ObjId + " FriendList while loading BlockList: " + e.getMessage(), e);
- }
- return list;
- }
-
- private void removeFromDB(int targetId)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("DELETE FROM character_friends WHERE charId=? AND friendId=? AND relation=1"))
- {
- ps.setInt(1, _owner.getObjectId());
- ps.setInt(2, targetId);
- ps.execute();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Could not remove blocked player: " + e.getMessage(), e);
- }
- }
-
- private void persistInDB(int targetId)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("INSERT INTO character_friends (charId, friendId, relation) VALUES (?, ?, 1)"))
- {
- ps.setInt(1, _owner.getObjectId());
- ps.setInt(2, targetId);
- ps.execute();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Could not add blocked player: " + e.getMessage(), e);
- }
- }
-
- public boolean isInBlockList(L2PcInstance target)
- {
- return _blockList.contains(target.getObjectId());
- }
-
- public boolean isInBlockList(int targetId)
- {
- return _blockList.contains(targetId);
- }
-
- private boolean isBlockAll()
- {
- return _owner.getMessageRefusal();
- }
-
- public static boolean isBlocked(L2PcInstance listOwner, L2PcInstance target)
- {
- BlockList blockList = listOwner.getBlockList();
- return blockList.isBlockAll() || blockList.isInBlockList(target);
- }
-
- public static boolean isBlocked(L2PcInstance listOwner, int targetId)
- {
- BlockList blockList = listOwner.getBlockList();
- return blockList.isBlockAll() || blockList.isInBlockList(targetId);
- }
-
- private void setBlockAll(boolean state)
- {
- _owner.setMessageRefusal(state);
- }
-
- private List<Integer> getBlockList()
- {
- return _blockList;
- }
-
- public static void addToBlockList(L2PcInstance listOwner, int targetId)
- {
- if (listOwner == null)
- {
- return;
- }
-
- String charName = CharNameTable.getInstance().getNameById(targetId);
-
- if (listOwner.getFriendList().contains(targetId))
- {
- SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_ALREADY_IN_FRIENDS_LIST);
- sm.addString(charName);
- listOwner.sendPacket(sm);
- return;
- }
-
- if (listOwner.getBlockList().getBlockList().contains(targetId))
- {
- listOwner.sendMessage("Already in ignore list.");
- return;
- }
-
- listOwner.getBlockList().addToBlockList(targetId);
-
- SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_WAS_ADDED_TO_YOUR_IGNORE_LIST);
- sm.addString(charName);
- listOwner.sendPacket(sm);
-
- L2PcInstance player = L2World.getInstance().getPlayer(targetId);
-
- if (player != null)
- {
- sm = SystemMessage.getSystemMessage(SystemMessageId.S1_HAS_ADDED_YOU_TO_IGNORE_LIST);
- sm.addString(listOwner.getName());
- player.sendPacket(sm);
- }
- }
-
- public static void removeFromBlockList(L2PcInstance listOwner, int targetId)
- {
- if (listOwner == null)
- {
- return;
- }
-
- SystemMessage sm;
-
- String charName = CharNameTable.getInstance().getNameById(targetId);
-
- if (!listOwner.getBlockList().getBlockList().contains(targetId))
- {
- sm = SystemMessage.getSystemMessage(SystemMessageId.TARGET_IS_INCORRECT);
- listOwner.sendPacket(sm);
- return;
- }
-
- listOwner.getBlockList().removeFromBlockList(targetId);
-
- sm = SystemMessage.getSystemMessage(SystemMessageId.S1_WAS_REMOVED_FROM_YOUR_IGNORE_LIST);
- sm.addString(charName);
- listOwner.sendPacket(sm);
- }
-
- public static boolean isInBlockList(L2PcInstance listOwner, L2PcInstance target)
- {
- return listOwner.getBlockList().isInBlockList(target);
- }
-
- public boolean isBlockAll(L2PcInstance listOwner)
- {
- return listOwner.getBlockList().isBlockAll();
- }
-
- public static void setBlockAll(L2PcInstance listOwner, boolean newValue)
- {
- listOwner.getBlockList().setBlockAll(newValue);
- }
-
- public static void sendListToOwner(L2PcInstance listOwner)
- {
- int i = 1;
- listOwner.sendPacket(SystemMessageId.BLOCK_LIST_HEADER);
- for (int playerId : listOwner.getBlockList().getBlockList())
- {
- listOwner.sendMessage((i++) + ". " + CharNameTable.getInstance().getNameById(playerId));
- }
- listOwner.sendPacket(SystemMessageId.FRIEND_LIST_FOOTER);
- }
-
- /**
- * @param ownerId object id of owner block list
- * @param targetId object id of potential blocked player
- * @return true if blocked
- */
- public static boolean isInBlockList(int ownerId, int targetId)
- {
- L2PcInstance player = L2World.getInstance().getPlayer(ownerId);
- if (player != null)
- {
- return BlockList.isBlocked(player, targetId);
- }
- if (!OFFLINE_LIST.containsKey(ownerId))
- {
- OFFLINE_LIST.put(ownerId, loadList(ownerId));
- }
- return OFFLINE_LIST.get(ownerId).contains(targetId);
- }
- }
|