Broadcast.java 7.7 KB

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