l2j_mobius/trunk/java/com/l2jserver/gameserver/util/Broadcast.java
2015-02-07 23:48:56 +00:00

234 lines
8.4 KiB
Java

/*
* 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.util;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.l2jserver.gameserver.enums.ChatType;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.actor.L2Summon;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.CharInfo;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
import com.l2jserver.gameserver.network.serverpackets.ExShowScreenMessage;
import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
import com.l2jserver.gameserver.network.serverpackets.RelationChanged;
/**
* This class ...
* @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
*/
public final class Broadcast
{
private static Logger _log = Logger.getLogger(Broadcast.class.getName());
/**
* Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character that have the Character targeted.<BR>
* <B><U> Concept</U> :</B><BR>
* L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
* In order to inform other players of state modification on the L2Character, server just need to go through _knownPlayers to send Server->Client Packet<BR>
* <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packet to this L2Character (to do this use method toSelfAndKnownPlayers)</B></FONT><BR>
* @param character
* @param mov
*/
public static void toPlayersTargettingMyself(L2Character character, L2GameServerPacket mov)
{
Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
for (L2PcInstance player : plrs)
{
if (player.getTarget() != character)
{
continue;
}
player.sendPacket(mov);
}
}
/**
* Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character.<BR>
* <B><U> Concept</U> :</B><BR>
* L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
* In order to inform other players of state modification on the L2Character, server just need to go through _knownPlayers to send Server->Client Packet<BR>
* <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packet to this L2Character (to do this use method toSelfAndKnownPlayers)</B></FONT><BR>
* @param character
* @param mov
*/
public static void toKnownPlayers(L2Character character, L2GameServerPacket mov)
{
final Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
for (L2PcInstance player : plrs)
{
if (player == null)
{
continue;
}
try
{
player.sendPacket(mov);
if ((mov instanceof CharInfo) && (character.isPlayer()))
{
int relation = ((L2PcInstance) character).getRelation(player);
Integer oldrelation = character.getKnownList().getKnownRelations().get(player.getObjectId());
if ((oldrelation != null) && (oldrelation != relation))
{
final RelationChanged rc = new RelationChanged();
rc.addRelation((L2PcInstance) character, relation, character.isAutoAttackable(player));
if (character.hasSummon())
{
final L2Summon pet = character.getPet();
if (pet != null)
{
rc.addRelation(pet, relation, character.isAutoAttackable(player));
}
if (character.hasServitors())
{
character.getServitors().values().forEach(s -> rc.addRelation(s, relation, character.isAutoAttackable(player)));
}
}
player.sendPacket(rc);
character.getKnownList().getKnownRelations().put(player.getObjectId(), relation);
}
}
}
catch (NullPointerException e)
{
_log.log(Level.WARNING, e.getMessage(), e);
}
}
}
/**
* Send a packet to all L2PcInstance in the _KnownPlayers (in the specified radius) of the L2Character.<BR>
* <B><U> Concept</U> :</B><BR>
* L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
* In order to inform other players of state modification on the L2Character, server just needs to go through _knownPlayers to send Server->Client Packet and check the distance between the targets.<BR>
* <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packet to this L2Character (to do this use method toSelfAndKnownPlayers)</B></FONT><BR>
* @param character
* @param mov
* @param radius
*/
public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
{
if (radius < 0)
{
radius = 1500;
}
Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
for (L2PcInstance player : plrs)
{
if (character.isInsideRadius(player, radius, false, false))
{
player.sendPacket(mov);
}
}
}
/**
* Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character and to the specified character.<BR>
* <B><U> Concept</U> :</B><BR>
* L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
* In order to inform other players of state modification on the L2Character, server just need to go through _knownPlayers to send Server->Client Packet<BR>
* @param character
* @param mov
*/
public static void toSelfAndKnownPlayers(L2Character character, L2GameServerPacket mov)
{
if (character instanceof L2PcInstance)
{
character.sendPacket(mov);
}
toKnownPlayers(character, mov);
}
// To improve performance we are comparing values of radius^2 instead of calculating sqrt all the time
public static void toSelfAndKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
{
if (radius < 0)
{
radius = 600;
}
if (character instanceof L2PcInstance)
{
character.sendPacket(mov);
}
Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
for (L2PcInstance player : plrs)
{
if ((player != null) && Util.checkIfInRange(radius, character, player, false))
{
player.sendPacket(mov);
}
}
}
/**
* Send a packet to all L2PcInstance present in the world.<BR>
* <B><U> Concept</U> :</B><BR>
* In order to inform other players of state modification on the L2Character, server just need to go through _allPlayers to send Server->Client Packet<BR>
* <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packet to this L2Character (to do this use method toSelfAndKnownPlayers)</B></FONT><BR>
* @param packet
*/
public static void toAllOnlinePlayers(L2GameServerPacket packet)
{
for (L2PcInstance player : L2World.getInstance().getPlayers())
{
if (player.isOnline())
{
player.sendPacket(packet);
}
}
}
public static void toAllOnlinePlayers(String text)
{
toAllOnlinePlayers(text, false);
}
public static void toAllOnlinePlayers(String text, boolean isCritical)
{
toAllOnlinePlayers(new CreatureSay(0, isCritical ? ChatType.CRITICAL_ANNOUNCE : ChatType.ANNOUNCEMENT, "", text));
}
public static void toPlayersInInstance(L2GameServerPacket packet, int instanceId)
{
for (L2PcInstance player : L2World.getInstance().getPlayers())
{
if (player.isOnline() && (player.getInstanceId() == instanceId))
{
player.sendPacket(packet);
}
}
}
public static void toAllOnlinePlayersOnScreen(String text)
{
toAllOnlinePlayers(new ExShowScreenMessage(text, 10000));
}
}