2
0

Broadcast.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. //synchronized (character.getKnownList().getKnownPlayers())
  97. {
  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. /**
  125. * Send a packet to all L2PcInstance in the _KnownPlayers (in the specified
  126. * radius) of the L2Character.<BR>
  127. * <BR>
  128. *
  129. * <B><U> Concept</U> :</B><BR>
  130. * L2PcInstance in the detection area of the L2Character are identified in
  131. * <B>_knownPlayers</B>.<BR>
  132. * In order to inform other players of state modification on the
  133. * L2Character, server just needs to go through _knownPlayers to send
  134. * Server->Client Packet and check the distance between the targets.<BR>
  135. * <BR>
  136. *
  137. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND
  138. * Server->Client packet to this L2Character (to do this use method
  139. * toSelfAndKnownPlayers)</B></FONT><BR>
  140. * <BR>
  141. *
  142. */
  143. public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  144. {
  145. if (radius < 0)
  146. radius = 1500;
  147. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  148. //synchronized (character.getKnownList().getKnownPlayers())
  149. {
  150. for (L2PcInstance player : plrs)
  151. {
  152. if (character.isInsideRadius(player, radius, false, false))
  153. player.sendPacket(mov);
  154. }
  155. }
  156. }
  157. /**
  158. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character and to the specified character.<BR><BR>
  159. *
  160. * <B><U> Concept</U> :</B><BR>
  161. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  162. * 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>
  163. *
  164. */
  165. public static void toSelfAndKnownPlayers(L2Character character, L2GameServerPacket mov)
  166. {
  167. if (character instanceof L2PcInstance)
  168. {
  169. character.sendPacket(mov);
  170. }
  171. toKnownPlayers(character, mov);
  172. }
  173. // To improve performance we are comparing values of radius^2 instead of calculating sqrt all the time
  174. public static void toSelfAndKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, long radiusSq)
  175. {
  176. if (radiusSq < 0)
  177. radiusSq = 360000;
  178. if (character instanceof L2PcInstance)
  179. character.sendPacket(mov);
  180. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  181. //synchronized (character.getKnownList().getKnownPlayers())
  182. {
  183. for (L2PcInstance player : plrs)
  184. {
  185. if (player != null && character.getDistanceSq(player) <= radiusSq)
  186. player.sendPacket(mov);
  187. }
  188. }
  189. }
  190. /**
  191. * Send a packet to all L2PcInstance present in the world.<BR><BR>
  192. *
  193. * <B><U> Concept</U> :</B><BR>
  194. * 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>
  195. *
  196. * <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>
  197. *
  198. */
  199. public static void toAllOnlinePlayers(L2GameServerPacket mov)
  200. {
  201. if (Config.DEBUG)
  202. _log.fine("Players to notify: " + L2World.getInstance().getAllPlayersCount() + " (with packet " + mov.getType() + ")");
  203. L2World.getInstance().forEachPlayer(new ForEachPlayerBroadcast(mov));
  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. L2World.getInstance().forEachPlayer(new ForEachPlayerInInstanceBroadcast(mov, instanceId));
  213. }
  214. private static final class ForEachPlayerBroadcast implements TObjectProcedure<L2PcInstance>
  215. {
  216. L2GameServerPacket _packet;
  217. private ForEachPlayerBroadcast(L2GameServerPacket packet)
  218. {
  219. _packet = packet;
  220. }
  221. @Override
  222. public final boolean execute(final L2PcInstance onlinePlayer)
  223. {
  224. if (onlinePlayer != null && onlinePlayer.isOnline())
  225. onlinePlayer.sendPacket(_packet);
  226. return true;
  227. }
  228. }
  229. private static final class ForEachPlayerInInstanceBroadcast implements TObjectProcedure<L2PcInstance>
  230. {
  231. L2GameServerPacket _packet;
  232. int _instanceId;
  233. private ForEachPlayerInInstanceBroadcast(L2GameServerPacket packet, int instanceId)
  234. {
  235. _packet = packet;
  236. _instanceId = instanceId;
  237. }
  238. @Override
  239. public final boolean execute(final L2PcInstance onlinePlayer)
  240. {
  241. if (onlinePlayer != null && onlinePlayer.isOnline() && onlinePlayer.getInstanceId() == _instanceId)
  242. onlinePlayer.sendPacket(_packet);
  243. return true;
  244. }
  245. }
  246. }