AbstractPlayerGroup.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2004-2013 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.model;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.interfaces.IL2Procedure;
  24. import com.l2jserver.gameserver.network.SystemMessageId;
  25. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  26. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  27. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  28. import com.l2jserver.util.Rnd;
  29. /**
  30. * @author Battlecruiser
  31. */
  32. public abstract class AbstractPlayerGroup
  33. {
  34. /**
  35. * @return a list of all members of this group
  36. */
  37. public abstract List<L2PcInstance> getMembers();
  38. /**
  39. * @return a list of object IDs of the members of this group
  40. */
  41. public List<Integer> getMembersObjectId()
  42. {
  43. final List<Integer> ids = new ArrayList<>();
  44. forEachMember(new IL2Procedure<L2PcInstance>()
  45. {
  46. @Override
  47. public boolean execute(L2PcInstance member)
  48. {
  49. ids.add(member.getObjectId());
  50. return true;
  51. }
  52. });
  53. return ids;
  54. }
  55. /**
  56. * @return the leader of this group
  57. */
  58. public abstract L2PcInstance getLeader();
  59. /**
  60. * Change the leader of this group to the specified player.
  61. * @param leader the player to set as the new leader of this group
  62. */
  63. public abstract void setLeader(L2PcInstance leader);
  64. /**
  65. * @return the leader's object ID
  66. */
  67. public int getLeaderObjectId()
  68. {
  69. return getLeader().getObjectId();
  70. }
  71. /**
  72. * Check if a given player is the leader of this group.
  73. * @param player the player to check
  74. * @return {@code true} if the specified player is the leader of this group, {@code false} otherwise
  75. */
  76. public boolean isLeader(L2PcInstance player)
  77. {
  78. return (getLeaderObjectId() == player.getObjectId());
  79. }
  80. /**
  81. * @return the count of all players in this group
  82. */
  83. public int getMemberCount()
  84. {
  85. return getMembers().size();
  86. }
  87. /**
  88. * @return the level of this group
  89. */
  90. public abstract int getLevel();
  91. /**
  92. * Broadcast a packet to every member of this group.
  93. * @param packet the packet to broadcast
  94. */
  95. public void broadcastPacket(final L2GameServerPacket packet)
  96. {
  97. forEachMember(new IL2Procedure<L2PcInstance>()
  98. {
  99. @Override
  100. public boolean execute(L2PcInstance member)
  101. {
  102. if (member != null)
  103. {
  104. member.sendPacket(packet);
  105. }
  106. return true;
  107. }
  108. });
  109. }
  110. /**
  111. * Broadcast a system message to this group.
  112. * @param message the system message to broadcast
  113. */
  114. public void broadcastMessage(SystemMessageId message)
  115. {
  116. broadcastPacket(SystemMessage.getSystemMessage(message));
  117. }
  118. /**
  119. * Broadcast a text message to this group.
  120. * @param text to broadcast
  121. */
  122. public void broadcastString(String text)
  123. {
  124. broadcastPacket(SystemMessage.sendString(text));
  125. }
  126. public void broadcastCreatureSay(final CreatureSay msg, final L2PcInstance broadcaster)
  127. {
  128. forEachMember(new IL2Procedure<L2PcInstance>()
  129. {
  130. @Override
  131. public boolean execute(L2PcInstance member)
  132. {
  133. if ((member != null) && !BlockList.isBlocked(member, broadcaster))
  134. {
  135. member.sendPacket(msg);
  136. }
  137. return true;
  138. }
  139. });
  140. }
  141. /**
  142. * Check if this group contains a given player.
  143. * @param player the player to check
  144. * @return {@code true} if this group contains the specified player, {@code false} otherwise
  145. */
  146. public boolean containsPlayer(L2PcInstance player)
  147. {
  148. return getMembers().contains(player);
  149. }
  150. /**
  151. * @return a random member of this group
  152. */
  153. public L2PcInstance getRandomPlayer()
  154. {
  155. return getMembers().get(Rnd.get(getMemberCount()));
  156. }
  157. /**
  158. * Iterates over the group and executes procedure on each member
  159. * @param procedure the prodecure to be executed on each member.<br>
  160. * If executing the procedure on a member returns {@code true}, the loop continues to the next member, otherwise it breaks the loop
  161. * @return {@code true} if the procedure executed correctly, {@code false} if the loop was broken prematurely
  162. */
  163. public boolean forEachMember(IL2Procedure<L2PcInstance> procedure)
  164. {
  165. for (L2PcInstance player : getMembers())
  166. {
  167. if (!procedure.execute(player))
  168. {
  169. return false;
  170. }
  171. }
  172. return true;
  173. }
  174. }