Broadcast.java 8.7 KB

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