2
0

Broadcast.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 java.util.Collection;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.gameserver.model.L2World;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.network.clientpackets.Say2;
  34. import com.l2jserver.gameserver.network.serverpackets.CharInfo;
  35. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  36. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  37. import com.l2jserver.gameserver.network.serverpackets.RelationChanged;
  38. /**
  39. * This class ...
  40. *
  41. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  42. */
  43. public final class Broadcast
  44. {
  45. private static Logger _log = Logger.getLogger(Broadcast.class.getName());
  46. /**
  47. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character that have the Character targetted.<BR><BR>
  48. *
  49. * <B><U> Concept</U> :</B><BR>
  50. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  51. * 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>
  52. *
  53. * <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>
  54. *
  55. */
  56. public static void toPlayersTargettingMyself(L2Character character, L2GameServerPacket mov)
  57. {
  58. if (Config.DEBUG)
  59. _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
  60. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  61. // synchronized (character.getKnownList().getKnownPlayers())
  62. {
  63. for (L2PcInstance player : plrs)
  64. {
  65. if (player.getTarget() != character)
  66. continue;
  67. player.sendPacket(mov);
  68. }
  69. }
  70. }
  71. /**
  72. * Send a packet to all L2PcInstance in the _KnownPlayers of the
  73. * L2Character.<BR>
  74. * <BR>
  75. *
  76. * <B><U> Concept</U> :</B><BR>
  77. * L2PcInstance in the detection area of the L2Character are identified in
  78. * <B>_knownPlayers</B>.<BR>
  79. * In order to inform other players of state modification on the
  80. * L2Character, server just need to go through _knownPlayers to send
  81. * Server->Client Packet<BR>
  82. * <BR>
  83. *
  84. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND
  85. * Server->Client packet to this L2Character (to do this use method
  86. * toSelfAndKnownPlayers)</B></FONT><BR>
  87. * <BR>
  88. *
  89. */
  90. public static void toKnownPlayers(L2Character character, L2GameServerPacket mov)
  91. {
  92. if (Config.DEBUG)
  93. _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
  94. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  95. //synchronized (character.getKnownList().getKnownPlayers())
  96. {
  97. for (L2PcInstance player : plrs)
  98. {
  99. if (player == null)
  100. continue;
  101. try
  102. {
  103. player.sendPacket(mov);
  104. if (mov instanceof CharInfo && character instanceof L2PcInstance)
  105. {
  106. int relation = ((L2PcInstance) character).getRelation(player);
  107. Integer oldrelation = character.getKnownList().getKnownRelations().get(player.getObjectId());
  108. if (oldrelation != null && oldrelation != relation)
  109. {
  110. player.sendPacket(new RelationChanged((L2PcInstance) character, relation, character.isAutoAttackable(player)));
  111. if (((L2PcInstance) character).getPet() != null)
  112. player.sendPacket(new RelationChanged(((L2PcInstance) character).getPet(), relation, character.isAutoAttackable(player)));
  113. }
  114. }
  115. }
  116. catch (NullPointerException e)
  117. {
  118. _log.log(Level.WARNING, e.getMessage(),e);
  119. }
  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. *
  141. */
  142. public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  143. {
  144. if (radius < 0)
  145. radius = 1500;
  146. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  147. //synchronized (character.getKnownList().getKnownPlayers())
  148. {
  149. for (L2PcInstance player : plrs)
  150. {
  151. if (character.isInsideRadius(player, radius, false, false))
  152. player.sendPacket(mov);
  153. }
  154. }
  155. }
  156. /**
  157. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character and to the specified character.<BR><BR>
  158. *
  159. * <B><U> Concept</U> :</B><BR>
  160. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  161. * 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>
  162. *
  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. //synchronized (character.getKnownList().getKnownPlayers())
  181. {
  182. for (L2PcInstance player : plrs)
  183. {
  184. if (player != null && character.getDistanceSq(player) <= radiusSq)
  185. player.sendPacket(mov);
  186. }
  187. }
  188. }
  189. /**
  190. * Send a packet to all L2PcInstance present in the world.<BR><BR>
  191. *
  192. * <B><U> Concept</U> :</B><BR>
  193. * 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>
  194. *
  195. * <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>
  196. *
  197. */
  198. public static void toAllOnlinePlayers(L2GameServerPacket mov)
  199. {
  200. if (Config.DEBUG)
  201. _log.fine("Players to notify: " + L2World.getInstance().getAllPlayersCount() + " (with packet " + mov.getType() + ")");
  202. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
  203. // synchronized (L2World.getInstance().getAllPlayers())
  204. {
  205. for (L2PcInstance onlinePlayer : pls)
  206. if (onlinePlayer.isOnline() == 1)
  207. onlinePlayer.sendPacket(mov);
  208. }
  209. }
  210. public static void announceToOnlinePlayers(String text)
  211. {
  212. CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, "", text);
  213. toAllOnlinePlayers(cs);
  214. }
  215. public static void toPlayersInInstance(L2GameServerPacket mov, int instanceId)
  216. {
  217. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
  218. //synchronized (character.getKnownList().getKnownPlayers())
  219. {
  220. for (L2PcInstance onlinePlayer : pls)
  221. {
  222. if (onlinePlayer.isOnline() == 1 && onlinePlayer.getInstanceId() == instanceId)
  223. onlinePlayer.sendPacket(mov);
  224. }
  225. }
  226. }
  227. }