AbstractPlayerGroup.java 4.7 KB

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