Broadcast.java 8.7 KB

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