Broadcast.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.util;
  16. import gnu.trove.procedure.TObjectProcedure;
  17. import java.util.Collection;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.model.L2World;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.network.clientpackets.Say2;
  25. import com.l2jserver.gameserver.network.serverpackets.CharInfo;
  26. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  27. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  28. import com.l2jserver.gameserver.network.serverpackets.RelationChanged;
  29. /**
  30. * This class ...
  31. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  32. */
  33. public final class Broadcast
  34. {
  35. private static Logger _log = Logger.getLogger(Broadcast.class.getName());
  36. /**
  37. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character that have the Character targeted.<BR>
  38. * <B><U> Concept</U> :</B><BR>
  39. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  40. * 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>
  41. * <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>
  42. * @param character
  43. * @param mov
  44. */
  45. public static void toPlayersTargettingMyself(L2Character character, L2GameServerPacket mov)
  46. {
  47. if (Config.DEBUG)
  48. {
  49. _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
  50. }
  51. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  52. // synchronized (character.getKnownList().getKnownPlayers())
  53. {
  54. for (L2PcInstance player : plrs)
  55. {
  56. if (player.getTarget() != character)
  57. {
  58. continue;
  59. }
  60. player.sendPacket(mov);
  61. }
  62. }
  63. }
  64. /**
  65. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character.<BR>
  66. * <B><U> Concept</U> :</B><BR>
  67. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  68. * 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>
  69. * <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>
  70. * @param character
  71. * @param mov
  72. */
  73. public static void toKnownPlayers(L2Character character, L2GameServerPacket mov)
  74. {
  75. if (Config.DEBUG)
  76. {
  77. _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:" + mov.getType());
  78. }
  79. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  80. for (L2PcInstance player : plrs)
  81. {
  82. if (player == null)
  83. {
  84. continue;
  85. }
  86. try
  87. {
  88. player.sendPacket(mov);
  89. if ((mov instanceof CharInfo) && (character instanceof L2PcInstance))
  90. {
  91. int relation = ((L2PcInstance) character).getRelation(player);
  92. Integer oldrelation = character.getKnownList().getKnownRelations().get(player.getObjectId());
  93. if ((oldrelation != null) && (oldrelation != relation))
  94. {
  95. player.sendPacket(new RelationChanged((L2PcInstance) character, relation, character.isAutoAttackable(player)));
  96. if (((L2PcInstance) character).getPet() != null)
  97. {
  98. player.sendPacket(new RelationChanged(((L2PcInstance) character).getPet(), relation, character.isAutoAttackable(player)));
  99. }
  100. }
  101. }
  102. }
  103. catch (NullPointerException e)
  104. {
  105. _log.log(Level.WARNING, e.getMessage(), e);
  106. }
  107. }
  108. }
  109. /**
  110. * Send a packet to all L2PcInstance in the _KnownPlayers (in the specified radius) of the L2Character.<BR>
  111. * <B><U> Concept</U> :</B><BR>
  112. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  113. * In order to inform other players of state modification on the L2Character, server just needs to go through _knownPlayers to send Server->Client Packet and check the distance between the targets.<BR>
  114. * <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>
  115. * @param character
  116. * @param mov
  117. * @param radius
  118. */
  119. public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  120. {
  121. if (radius < 0)
  122. {
  123. radius = 1500;
  124. }
  125. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  126. for (L2PcInstance player : plrs)
  127. {
  128. if (character.isInsideRadius(player, radius, false, false))
  129. {
  130. player.sendPacket(mov);
  131. }
  132. }
  133. }
  134. /**
  135. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character and to the specified character.<BR>
  136. * <B><U> Concept</U> :</B><BR>
  137. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  138. * 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>
  139. * @param character
  140. * @param mov
  141. */
  142. public static void toSelfAndKnownPlayers(L2Character character, L2GameServerPacket mov)
  143. {
  144. if (character instanceof L2PcInstance)
  145. {
  146. character.sendPacket(mov);
  147. }
  148. toKnownPlayers(character, mov);
  149. }
  150. // To improve performance we are comparing values of radius^2 instead of calculating sqrt all the time
  151. public static void toSelfAndKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  152. {
  153. if (radius < 0)
  154. radius = 600;
  155. if (character instanceof L2PcInstance)
  156. character.sendPacket(mov);
  157. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  158. for (L2PcInstance player : plrs)
  159. {
  160. if (player != null && Util.checkIfInRange(radius, character, player, false))
  161. player.sendPacket(mov);
  162. }
  163. }
  164. /**
  165. * Send a packet to all L2PcInstance present in the world.<BR>
  166. * <B><U> Concept</U> :</B><BR>
  167. * 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>
  168. * <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>
  169. * @param mov
  170. */
  171. public static void toAllOnlinePlayers(L2GameServerPacket mov)
  172. {
  173. if (Config.DEBUG)
  174. {
  175. _log.fine("Players to notify: " + L2World.getInstance().getAllPlayersCount() + " (with packet " + mov.getType() + ")");
  176. }
  177. L2World.getInstance().forEachPlayer(new ForEachPlayerBroadcast(mov));
  178. }
  179. public static void announceToOnlinePlayers(String text, boolean isCritical)
  180. {
  181. CreatureSay cs;
  182. if (isCritical)
  183. {
  184. cs = new CreatureSay(0, Say2.CRITICAL_ANNOUNCE, "", text);
  185. }
  186. else
  187. {
  188. cs = new CreatureSay(0, Say2.ANNOUNCEMENT, "", text);
  189. }
  190. toAllOnlinePlayers(cs);
  191. }
  192. public static void toPlayersInInstance(L2GameServerPacket mov, int instanceId)
  193. {
  194. L2World.getInstance().forEachPlayer(new ForEachPlayerInInstanceBroadcast(mov, instanceId));
  195. }
  196. private static final class ForEachPlayerBroadcast implements TObjectProcedure<L2PcInstance>
  197. {
  198. L2GameServerPacket _packet;
  199. protected ForEachPlayerBroadcast(L2GameServerPacket packet)
  200. {
  201. _packet = packet;
  202. }
  203. @Override
  204. public final boolean execute(final L2PcInstance onlinePlayer)
  205. {
  206. if ((onlinePlayer != null) && onlinePlayer.isOnline())
  207. {
  208. onlinePlayer.sendPacket(_packet);
  209. }
  210. return true;
  211. }
  212. }
  213. private static final class ForEachPlayerInInstanceBroadcast implements TObjectProcedure<L2PcInstance>
  214. {
  215. L2GameServerPacket _packet;
  216. int _instanceId;
  217. protected ForEachPlayerInInstanceBroadcast(L2GameServerPacket packet, int instanceId)
  218. {
  219. _packet = packet;
  220. _instanceId = instanceId;
  221. }
  222. @Override
  223. public final boolean execute(final L2PcInstance onlinePlayer)
  224. {
  225. if ((onlinePlayer != null) && onlinePlayer.isOnline() && (onlinePlayer.getInstanceId() == _instanceId))
  226. {
  227. onlinePlayer.sendPacket(_packet);
  228. }
  229. return true;
  230. }
  231. }
  232. }