123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /*
- * $Header: Broadcast.java, 18/11/2005 15:33:35 luisantonioa Exp $
- *
- * $Author: luisantonioa $
- * $Date: 18/11/2005 15:33:35 $
- * $Revision: 1 $
- * $Log: Broadcast.java,v $
- * Revision 1 18/11/2005 15:33:35 luisantonioa
- * Added copyright notice
- *
- *
- * This program 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.
- *
- * This program 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 gnu.trove.TObjectProcedure;
- import java.util.Collection;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import com.l2jserver.Config;
- import com.l2jserver.gameserver.model.L2World;
- import com.l2jserver.gameserver.model.actor.L2Character;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.network.clientpackets.Say2;
- import com.l2jserver.gameserver.network.serverpackets.CharInfo;
- import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
- 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 targetted.<BR><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><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><BR>
- *
- */
- public static void toPlayersTargettingMyself(L2Character character, L2GameServerPacket mov)
- {
- if (Config.DEBUG)
- _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
-
- Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
- // synchronized (character.getKnownList().getKnownPlayers())
- {
- 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>
- * <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>
- * <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>
- * <BR>
- *
- */
- public static void toKnownPlayers(L2Character character, L2GameServerPacket mov)
- {
- if (Config.DEBUG)
- _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
-
- Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
- //synchronized (character.getKnownList().getKnownPlayers())
- {
- for (L2PcInstance player : plrs)
- {
- if (player == null)
- continue;
- try
- {
- player.sendPacket(mov);
- if (mov instanceof CharInfo && character instanceof L2PcInstance)
- {
- int relation = ((L2PcInstance) character).getRelation(player);
- Integer oldrelation = character.getKnownList().getKnownRelations().get(player.getObjectId());
- if (oldrelation != null && oldrelation != relation)
- {
- player.sendPacket(new RelationChanged((L2PcInstance) character, relation, character.isAutoAttackable(player)));
- if (((L2PcInstance) character).getPet() != null)
- player.sendPacket(new RelationChanged(((L2PcInstance) character).getPet(), relation, character.isAutoAttackable(player)));
- }
- }
- }
- 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>
- * <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>
- * <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>
- * <BR>
- *
- */
- public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
- {
- if (radius < 0)
- radius = 1500;
-
- Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
- //synchronized (character.getKnownList().getKnownPlayers())
- {
- 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><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><BR>
- *
- */
- 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, long radiusSq)
- {
- if (radiusSq < 0)
- radiusSq = 360000;
-
- if (character instanceof L2PcInstance)
- character.sendPacket(mov);
-
- Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
- //synchronized (character.getKnownList().getKnownPlayers())
- {
- for (L2PcInstance player : plrs)
- {
- if (player != null && character.getDistanceSq(player) <= radiusSq)
- player.sendPacket(mov);
- }
- }
- }
-
- /**
- * Send a packet to all L2PcInstance present in the world.<BR><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><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><BR>
- *
- */
- public static void toAllOnlinePlayers(L2GameServerPacket mov)
- {
- if (Config.DEBUG)
- _log.fine("Players to notify: " + L2World.getInstance().getAllPlayersCount() + " (with packet " + mov.getType() + ")");
-
- L2World.getInstance().forEachPlayer(new ForEachPlayerBroadcast(mov));
- }
-
- public static void announceToOnlinePlayers(String text)
- {
- CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, "", text);
- toAllOnlinePlayers(cs);
- }
-
- public static void toPlayersInInstance(L2GameServerPacket mov, int instanceId)
- {
- L2World.getInstance().forEachPlayer(new ForEachPlayerInInstanceBroadcast(mov, instanceId));
- }
-
- private static final class ForEachPlayerBroadcast implements TObjectProcedure<L2PcInstance>
- {
- L2GameServerPacket _packet;
- private ForEachPlayerBroadcast(L2GameServerPacket packet)
- {
- _packet = packet;
- }
-
- @Override
- public final boolean execute(final L2PcInstance onlinePlayer)
- {
- if (onlinePlayer != null && onlinePlayer.isOnline())
- onlinePlayer.sendPacket(_packet);
- return true;
- }
- }
-
- private static final class ForEachPlayerInInstanceBroadcast implements TObjectProcedure<L2PcInstance>
- {
- L2GameServerPacket _packet;
- int _instanceId;
- private ForEachPlayerInInstanceBroadcast(L2GameServerPacket packet, int instanceId)
- {
- _packet = packet;
- _instanceId = instanceId;
- }
-
- @Override
- public final boolean execute(final L2PcInstance onlinePlayer)
- {
- if (onlinePlayer != null && onlinePlayer.isOnline() && onlinePlayer.getInstanceId() == _instanceId)
- onlinePlayer.sendPacket(_packet);
- return true;
- }
- }
- }
|