Broadcast.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 java.util.Collection;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.gameserver.model.L2World;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. import com.l2jserver.gameserver.network.clientpackets.Say2;
  23. import com.l2jserver.gameserver.network.serverpackets.CharInfo;
  24. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  25. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  26. import com.l2jserver.gameserver.network.serverpackets.RelationChanged;
  27. import gnu.trove.procedure.TObjectProcedure;
  28. /**
  29. * This class ...
  30. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  31. */
  32. public final class Broadcast
  33. {
  34. private static Logger _log = Logger.getLogger(Broadcast.class.getName());
  35. /**
  36. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character that have the Character targeted.<BR>
  37. * <B><U> Concept</U> :</B><BR>
  38. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  39. * 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>
  40. * <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>
  41. * @param character
  42. * @param mov
  43. */
  44. public static void toPlayersTargettingMyself(L2Character character, L2GameServerPacket mov)
  45. {
  46. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  47. for (L2PcInstance player : plrs)
  48. {
  49. if (player.getTarget() != character)
  50. {
  51. continue;
  52. }
  53. player.sendPacket(mov);
  54. }
  55. }
  56. /**
  57. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character.<BR>
  58. * <B><U> Concept</U> :</B><BR>
  59. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  60. * 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>
  61. * <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>
  62. * @param character
  63. * @param mov
  64. */
  65. public static void toKnownPlayers(L2Character character, L2GameServerPacket mov)
  66. {
  67. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  68. for (L2PcInstance player : plrs)
  69. {
  70. if (player == null)
  71. {
  72. continue;
  73. }
  74. try
  75. {
  76. player.sendPacket(mov);
  77. if ((mov instanceof CharInfo) && (character instanceof L2PcInstance))
  78. {
  79. int relation = ((L2PcInstance) character).getRelation(player);
  80. Integer oldrelation = character.getKnownList().getKnownRelations().get(player.getObjectId());
  81. if ((oldrelation != null) && (oldrelation != relation))
  82. {
  83. player.sendPacket(new RelationChanged((L2PcInstance) character, relation, character.isAutoAttackable(player)));
  84. if (((L2PcInstance) character).hasSummon())
  85. {
  86. player.sendPacket(new RelationChanged(character.getSummon(), relation, character.isAutoAttackable(player)));
  87. }
  88. }
  89. }
  90. }
  91. catch (NullPointerException e)
  92. {
  93. _log.log(Level.WARNING, e.getMessage(), e);
  94. }
  95. }
  96. }
  97. /**
  98. * Send a packet to all L2PcInstance in the _KnownPlayers (in the specified radius) of the L2Character.<BR>
  99. * <B><U> Concept</U> :</B><BR>
  100. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  101. * 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>
  102. * <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>
  103. * @param character
  104. * @param mov
  105. * @param radius
  106. */
  107. public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  108. {
  109. if (radius < 0)
  110. {
  111. radius = 1500;
  112. }
  113. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  114. for (L2PcInstance player : plrs)
  115. {
  116. if (character.isInsideRadius(player, radius, false, false))
  117. {
  118. player.sendPacket(mov);
  119. }
  120. }
  121. }
  122. /**
  123. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character and to the specified character.<BR>
  124. * <B><U> Concept</U> :</B><BR>
  125. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  126. * 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>
  127. * @param character
  128. * @param mov
  129. */
  130. public static void toSelfAndKnownPlayers(L2Character character, L2GameServerPacket mov)
  131. {
  132. if (character instanceof L2PcInstance)
  133. {
  134. character.sendPacket(mov);
  135. }
  136. toKnownPlayers(character, mov);
  137. }
  138. // To improve performance we are comparing values of radius^2 instead of calculating sqrt all the time
  139. public static void toSelfAndKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  140. {
  141. if (radius < 0)
  142. {
  143. radius = 600;
  144. }
  145. if (character instanceof L2PcInstance)
  146. {
  147. character.sendPacket(mov);
  148. }
  149. Collection<L2PcInstance> plrs = character.getKnownList().getKnownPlayers().values();
  150. for (L2PcInstance player : plrs)
  151. {
  152. if ((player != null) && Util.checkIfInRange(radius, character, player, false))
  153. {
  154. player.sendPacket(mov);
  155. }
  156. }
  157. }
  158. /**
  159. * Send a packet to all L2PcInstance present in the world.<BR>
  160. * <B><U> Concept</U> :</B><BR>
  161. * 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>
  162. * <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>
  163. * @param mov
  164. */
  165. public static void toAllOnlinePlayers(L2GameServerPacket mov)
  166. {
  167. L2World.getInstance().forEachPlayer(new ForEachPlayerBroadcast(mov));
  168. }
  169. public static void announceToOnlinePlayers(String text, boolean isCritical)
  170. {
  171. CreatureSay cs;
  172. if (isCritical)
  173. {
  174. cs = new CreatureSay(0, Say2.CRITICAL_ANNOUNCE, "", text);
  175. }
  176. else
  177. {
  178. cs = new CreatureSay(0, Say2.ANNOUNCEMENT, "", text);
  179. }
  180. toAllOnlinePlayers(cs);
  181. }
  182. public static void toPlayersInInstance(L2GameServerPacket mov, int instanceId)
  183. {
  184. L2World.getInstance().forEachPlayer(new ForEachPlayerInInstanceBroadcast(mov, instanceId));
  185. }
  186. private static final class ForEachPlayerBroadcast implements TObjectProcedure<L2PcInstance>
  187. {
  188. L2GameServerPacket _packet;
  189. protected ForEachPlayerBroadcast(L2GameServerPacket packet)
  190. {
  191. _packet = packet;
  192. }
  193. @Override
  194. public final boolean execute(final L2PcInstance onlinePlayer)
  195. {
  196. if ((onlinePlayer != null) && onlinePlayer.isOnline())
  197. {
  198. onlinePlayer.sendPacket(_packet);
  199. }
  200. return true;
  201. }
  202. }
  203. private static final class ForEachPlayerInInstanceBroadcast implements TObjectProcedure<L2PcInstance>
  204. {
  205. L2GameServerPacket _packet;
  206. int _instanceId;
  207. protected ForEachPlayerInInstanceBroadcast(L2GameServerPacket packet, int instanceId)
  208. {
  209. _packet = packet;
  210. _instanceId = instanceId;
  211. }
  212. @Override
  213. public final boolean execute(final L2PcInstance onlinePlayer)
  214. {
  215. if ((onlinePlayer != null) && onlinePlayer.isOnline() && (onlinePlayer.getInstanceId() == _instanceId))
  216. {
  217. onlinePlayer.sendPacket(_packet);
  218. }
  219. return true;
  220. }
  221. }
  222. }