Broadcast.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.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. *
  56. */
  57. public static void toPlayersTargettingMyself(L2Character character, L2GameServerPacket mov)
  58. {
  59. if (Config.DEBUG)
  60. _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
  61. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  62. // synchronized (character.getKnownList().getKnownPlayers())
  63. {
  64. for (L2PcInstance player : plrs)
  65. {
  66. if (player.getTarget() != character)
  67. continue;
  68. player.sendPacket(mov);
  69. }
  70. }
  71. }
  72. /**
  73. * Send a packet to all L2PcInstance in the _KnownPlayers of the
  74. * L2Character.<BR>
  75. * <BR>
  76. *
  77. * <B><U> Concept</U> :</B><BR>
  78. * L2PcInstance in the detection area of the L2Character are identified in
  79. * <B>_knownPlayers</B>.<BR>
  80. * In order to inform other players of state modification on the
  81. * L2Character, server just need to go through _knownPlayers to send
  82. * Server->Client Packet<BR>
  83. * <BR>
  84. *
  85. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND
  86. * Server->Client packet to this L2Character (to do this use method
  87. * toSelfAndKnownPlayers)</B></FONT><BR>
  88. * <BR>
  89. *
  90. */
  91. public static void toKnownPlayers(L2Character character, L2GameServerPacket mov)
  92. {
  93. if (Config.DEBUG)
  94. _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
  95. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  96. for (L2PcInstance player : plrs)
  97. {
  98. if (player == null)
  99. continue;
  100. try
  101. {
  102. player.sendPacket(mov);
  103. if (mov instanceof CharInfo && character instanceof L2PcInstance)
  104. {
  105. int relation = ((L2PcInstance) character).getRelation(player);
  106. Integer oldrelation = character.getKnownList().getKnownRelations().get(player.getObjectId());
  107. if (oldrelation != null && oldrelation != relation)
  108. {
  109. player.sendPacket(new RelationChanged((L2PcInstance) character, relation, character.isAutoAttackable(player)));
  110. if (((L2PcInstance) character).getPet() != null)
  111. player.sendPacket(new RelationChanged(((L2PcInstance) character).getPet(), relation, character.isAutoAttackable(player)));
  112. }
  113. }
  114. }
  115. catch (NullPointerException e)
  116. {
  117. _log.log(Level.WARNING, e.getMessage(), e);
  118. }
  119. }
  120. }
  121. /**
  122. * Send a packet to all L2PcInstance in the _KnownPlayers (in the specified
  123. * radius) of the L2Character.<BR>
  124. * <BR>
  125. *
  126. * <B><U> Concept</U> :</B><BR>
  127. * L2PcInstance in the detection area of the L2Character are identified in
  128. * <B>_knownPlayers</B>.<BR>
  129. * In order to inform other players of state modification on the
  130. * L2Character, server just needs to go through _knownPlayers to send
  131. * Server->Client Packet and check the distance between the targets.<BR>
  132. * <BR>
  133. *
  134. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND
  135. * Server->Client packet to this L2Character (to do this use method
  136. * toSelfAndKnownPlayers)</B></FONT><BR>
  137. * <BR>
  138. *
  139. */
  140. public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  141. {
  142. if (radius < 0)
  143. radius = 1500;
  144. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  145. for (L2PcInstance player : plrs)
  146. {
  147. if (character.isInsideRadius(player, radius, false, false))
  148. player.sendPacket(mov);
  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. for (L2PcInstance player : plrs)
  176. {
  177. if (player != null && character.getDistanceSq(player) <= radiusSq)
  178. player.sendPacket(mov);
  179. }
  180. }
  181. /**
  182. * Send a packet to all L2PcInstance present in the world.<BR><BR>
  183. *
  184. * <B><U> Concept</U> :</B><BR>
  185. * 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>
  186. *
  187. * <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>
  188. *
  189. */
  190. public static void toAllOnlinePlayers(L2GameServerPacket mov)
  191. {
  192. if (Config.DEBUG)
  193. _log.fine("Players to notify: " + L2World.getInstance().getAllPlayersCount() + " (with packet " + mov.getType() + ")");
  194. L2World.getInstance().forEachPlayer(new ForEachPlayerBroadcast(mov));
  195. }
  196. public static void announceToOnlinePlayers(String text)
  197. {
  198. CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, "", text);
  199. toAllOnlinePlayers(cs);
  200. }
  201. public static void toPlayersInInstance(L2GameServerPacket mov, int instanceId)
  202. {
  203. L2World.getInstance().forEachPlayer(new ForEachPlayerInInstanceBroadcast(mov, instanceId));
  204. }
  205. private static final class ForEachPlayerBroadcast implements TObjectProcedure<L2PcInstance>
  206. {
  207. L2GameServerPacket _packet;
  208. private ForEachPlayerBroadcast(L2GameServerPacket packet)
  209. {
  210. _packet = packet;
  211. }
  212. @Override
  213. public final boolean execute(final L2PcInstance onlinePlayer)
  214. {
  215. if (onlinePlayer != null && onlinePlayer.isOnline())
  216. onlinePlayer.sendPacket(_packet);
  217. return true;
  218. }
  219. }
  220. private static final class ForEachPlayerInInstanceBroadcast implements TObjectProcedure<L2PcInstance>
  221. {
  222. L2GameServerPacket _packet;
  223. int _instanceId;
  224. private ForEachPlayerInInstanceBroadcast(L2GameServerPacket packet, int instanceId)
  225. {
  226. _packet = packet;
  227. _instanceId = instanceId;
  228. }
  229. @Override
  230. public final boolean execute(final L2PcInstance onlinePlayer)
  231. {
  232. if (onlinePlayer != null && onlinePlayer.isOnline() && onlinePlayer.getInstanceId() == _instanceId)
  233. onlinePlayer.sendPacket(_packet);
  234. return true;
  235. }
  236. }
  237. }