Broadcast.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * $Header: Broadcast.java, 18/11/2005 15:33:35 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 18/11/2005 15:33:35 $
  6. * $Revision: 1 $
  7. * $Log: Broadcast.java,v $
  8. * Revision 1 18/11/2005 15:33:35 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.util;
  26. import gnu.trove.procedure.TObjectProcedure;
  27. import java.util.Collection;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.gameserver.model.L2World;
  32. import com.l2jserver.gameserver.model.actor.L2Character;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.network.clientpackets.Say2;
  35. import com.l2jserver.gameserver.network.serverpackets.CharInfo;
  36. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  37. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  38. import com.l2jserver.gameserver.network.serverpackets.RelationChanged;
  39. /**
  40. * This class ...
  41. *
  42. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  43. */
  44. public final class Broadcast
  45. {
  46. private static Logger _log = Logger.getLogger(Broadcast.class.getName());
  47. /**
  48. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character that have the Character targetted.<BR><BR>
  49. *
  50. * <B><U> Concept</U> :</B><BR>
  51. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  52. * 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>
  53. *
  54. * <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>
  55. * @param character
  56. * @param mov
  57. */
  58. public static void toPlayersTargettingMyself(L2Character character, L2GameServerPacket mov)
  59. {
  60. if (Config.DEBUG)
  61. _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
  62. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  63. // synchronized (character.getKnownList().getKnownPlayers())
  64. {
  65. for (L2PcInstance player : plrs)
  66. {
  67. if (player.getTarget() != character)
  68. continue;
  69. player.sendPacket(mov);
  70. }
  71. }
  72. }
  73. /**
  74. * Send a packet to all L2PcInstance in the _KnownPlayers of the
  75. * L2Character.<BR>
  76. * <BR>
  77. *
  78. * <B><U> Concept</U> :</B><BR>
  79. * L2PcInstance in the detection area of the L2Character are identified in
  80. * <B>_knownPlayers</B>.<BR>
  81. * In order to inform other players of state modification on the
  82. * L2Character, server just need to go through _knownPlayers to send
  83. * Server->Client Packet<BR>
  84. * <BR>
  85. *
  86. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND
  87. * Server->Client packet to this L2Character (to do this use method
  88. * toSelfAndKnownPlayers)</B></FONT><BR>
  89. * <BR>
  90. * @param character
  91. * @param mov
  92. */
  93. public static void toKnownPlayers(L2Character character, L2GameServerPacket mov)
  94. {
  95. if (Config.DEBUG)
  96. _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
  97. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  98. for (L2PcInstance player : plrs)
  99. {
  100. if (player == null)
  101. continue;
  102. try
  103. {
  104. player.sendPacket(mov);
  105. if (mov instanceof CharInfo && character instanceof L2PcInstance)
  106. {
  107. int relation = ((L2PcInstance) character).getRelation(player);
  108. Integer oldrelation = character.getKnownList().getKnownRelations().get(player.getObjectId());
  109. if (oldrelation != null && oldrelation != relation)
  110. {
  111. player.sendPacket(new RelationChanged((L2PcInstance) character, relation, character.isAutoAttackable(player)));
  112. if (((L2PcInstance) character).getPet() != null)
  113. player.sendPacket(new RelationChanged(((L2PcInstance) character).getPet(), relation, character.isAutoAttackable(player)));
  114. }
  115. }
  116. }
  117. catch (NullPointerException e)
  118. {
  119. _log.log(Level.WARNING, e.getMessage(), e);
  120. }
  121. }
  122. }
  123. /**
  124. * Send a packet to all L2PcInstance in the _KnownPlayers (in the specified
  125. * radius) of the L2Character.<BR>
  126. * <BR>
  127. *
  128. * <B><U> Concept</U> :</B><BR>
  129. * L2PcInstance in the detection area of the L2Character are identified in
  130. * <B>_knownPlayers</B>.<BR>
  131. * In order to inform other players of state modification on the
  132. * L2Character, server just needs to go through _knownPlayers to send
  133. * Server->Client Packet and check the distance between the targets.<BR>
  134. * <BR>
  135. *
  136. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND
  137. * Server->Client packet to this L2Character (to do this use method
  138. * toSelfAndKnownPlayers)</B></FONT><BR>
  139. * <BR>
  140. * @param character
  141. * @param mov
  142. * @param radius
  143. */
  144. public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  145. {
  146. if (radius < 0)
  147. radius = 1500;
  148. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  149. for (L2PcInstance player : plrs)
  150. {
  151. if (character.isInsideRadius(player, radius, false, false))
  152. player.sendPacket(mov);
  153. }
  154. }
  155. /**
  156. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character and to the specified character.<BR><BR>
  157. *
  158. * <B><U> Concept</U> :</B><BR>
  159. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  160. * 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>
  161. * @param character
  162. * @param mov
  163. */
  164. public static void toSelfAndKnownPlayers(L2Character character, L2GameServerPacket mov)
  165. {
  166. if (character instanceof L2PcInstance)
  167. {
  168. character.sendPacket(mov);
  169. }
  170. toKnownPlayers(character, mov);
  171. }
  172. // To improve performance we are comparing values of radius^2 instead of calculating sqrt all the time
  173. public static void toSelfAndKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, long radiusSq)
  174. {
  175. if (radiusSq < 0)
  176. radiusSq = 360000;
  177. if (character instanceof L2PcInstance)
  178. character.sendPacket(mov);
  179. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  180. for (L2PcInstance player : plrs)
  181. {
  182. if (player != null && character.getDistanceSq(player) <= radiusSq)
  183. player.sendPacket(mov);
  184. }
  185. }
  186. /**
  187. * Send a packet to all L2PcInstance present in the world.<BR><BR>
  188. *
  189. * <B><U> Concept</U> :</B><BR>
  190. * 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>
  191. *
  192. * <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>
  193. * @param mov
  194. */
  195. public static void toAllOnlinePlayers(L2GameServerPacket mov)
  196. {
  197. if (Config.DEBUG)
  198. _log.fine("Players to notify: " + L2World.getInstance().getAllPlayersCount() + " (with packet " + mov.getType() + ")");
  199. L2World.getInstance().forEachPlayer(new ForEachPlayerBroadcast(mov));
  200. }
  201. public static void announceToOnlinePlayers(String text)
  202. {
  203. CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, "", text);
  204. toAllOnlinePlayers(cs);
  205. }
  206. public static void toPlayersInInstance(L2GameServerPacket mov, int instanceId)
  207. {
  208. L2World.getInstance().forEachPlayer(new ForEachPlayerInInstanceBroadcast(mov, instanceId));
  209. }
  210. private static final class ForEachPlayerBroadcast implements TObjectProcedure<L2PcInstance>
  211. {
  212. L2GameServerPacket _packet;
  213. private ForEachPlayerBroadcast(L2GameServerPacket packet)
  214. {
  215. _packet = packet;
  216. }
  217. @Override
  218. public final boolean execute(final L2PcInstance onlinePlayer)
  219. {
  220. if (onlinePlayer != null && onlinePlayer.isOnline())
  221. onlinePlayer.sendPacket(_packet);
  222. return true;
  223. }
  224. }
  225. private static final class ForEachPlayerInInstanceBroadcast implements TObjectProcedure<L2PcInstance>
  226. {
  227. L2GameServerPacket _packet;
  228. int _instanceId;
  229. private ForEachPlayerInInstanceBroadcast(L2GameServerPacket packet, int instanceId)
  230. {
  231. _packet = packet;
  232. _instanceId = instanceId;
  233. }
  234. @Override
  235. public final boolean execute(final L2PcInstance onlinePlayer)
  236. {
  237. if (onlinePlayer != null && onlinePlayer.isOnline() && onlinePlayer.getInstanceId() == _instanceId)
  238. onlinePlayer.sendPacket(_packet);
  239. return true;
  240. }
  241. }
  242. }