L2CommandChannel.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.List;
  21. import javolution.util.FastList;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.model.actor.L2Character;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.interfaces.IL2Procedure;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.ExCloseMPCC;
  28. import com.l2jserver.gameserver.network.serverpackets.ExMPCCPartyInfoUpdate;
  29. import com.l2jserver.gameserver.network.serverpackets.ExOpenMPCC;
  30. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  31. /**
  32. * This class serves as a container for command channels.
  33. * @author chris_00
  34. */
  35. public class L2CommandChannel extends AbstractPlayerGroup
  36. {
  37. private final List<L2Party> _parties;
  38. private L2PcInstance _commandLeader = null;
  39. private int _channelLvl;
  40. /**
  41. * Create a new command channel and add the leader's party to it.
  42. * @param leader the leader of this command channel
  43. */
  44. public L2CommandChannel(L2PcInstance leader)
  45. {
  46. _commandLeader = leader;
  47. L2Party party = leader.getParty();
  48. _parties = new FastList<L2Party>().shared();
  49. _parties.add(party);
  50. _channelLvl = party.getLevel();
  51. party.setCommandChannel(this);
  52. party.broadcastMessage(SystemMessageId.COMMAND_CHANNEL_FORMED);
  53. party.broadcastPacket(ExOpenMPCC.STATIC_PACKET);
  54. }
  55. /**
  56. * Add a party to this command channel.
  57. * @param party the party to add
  58. */
  59. public void addParty(L2Party party)
  60. {
  61. if (party == null)
  62. {
  63. return;
  64. }
  65. // Update the CCinfo for existing players
  66. broadcastPacket(new ExMPCCPartyInfoUpdate(party, 1));
  67. _parties.add(party);
  68. if (party.getLevel() > _channelLvl)
  69. {
  70. _channelLvl = party.getLevel();
  71. }
  72. party.setCommandChannel(this);
  73. party.broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.JOINED_COMMAND_CHANNEL));
  74. party.broadcastPacket(ExOpenMPCC.STATIC_PACKET);
  75. }
  76. /**
  77. * Remove a party from this command channel.
  78. * @param party the party to remove
  79. */
  80. public void removeParty(L2Party party)
  81. {
  82. if (party == null)
  83. {
  84. return;
  85. }
  86. _parties.remove(party);
  87. _channelLvl = 0;
  88. for (L2Party pty : _parties)
  89. {
  90. if (pty.getLevel() > _channelLvl)
  91. {
  92. _channelLvl = pty.getLevel();
  93. }
  94. }
  95. party.setCommandChannel(null);
  96. party.broadcastPacket(new ExCloseMPCC());
  97. if (_parties.size() < 2)
  98. {
  99. broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.COMMAND_CHANNEL_DISBANDED));
  100. disbandChannel();
  101. }
  102. else
  103. {
  104. // Update the CCinfo for existing players
  105. broadcastPacket(new ExMPCCPartyInfoUpdate(party, 0));
  106. }
  107. }
  108. /**
  109. * Disband this command channel.
  110. */
  111. public void disbandChannel()
  112. {
  113. if (_parties != null)
  114. {
  115. for (L2Party party : _parties)
  116. {
  117. if (party != null)
  118. {
  119. removeParty(party);
  120. }
  121. }
  122. _parties.clear();
  123. }
  124. }
  125. /**
  126. * @return the total count of all members of this command channel
  127. */
  128. @Override
  129. public int getMemberCount()
  130. {
  131. int count = 0;
  132. for (L2Party party : _parties)
  133. {
  134. if (party != null)
  135. {
  136. count += party.getMemberCount();
  137. }
  138. }
  139. return count;
  140. }
  141. /**
  142. * @return a list of all parties in this command channel
  143. */
  144. public List<L2Party> getPartys()
  145. {
  146. return _parties;
  147. }
  148. /**
  149. * @return a list of all members in this command channel
  150. */
  151. @Override
  152. public List<L2PcInstance> getMembers()
  153. {
  154. List<L2PcInstance> members = new FastList<L2PcInstance>().shared();
  155. for (L2Party party : getPartys())
  156. {
  157. members.addAll(party.getMembers());
  158. }
  159. return members;
  160. }
  161. /**
  162. * @return the level of this command channel (equals the level of the highest-leveled character in this command channel)
  163. */
  164. @Override
  165. public int getLevel()
  166. {
  167. return _channelLvl;
  168. }
  169. @Override
  170. public void setLeader(L2PcInstance leader)
  171. {
  172. _commandLeader = leader;
  173. if (leader.getLevel() > _channelLvl)
  174. {
  175. _channelLvl = leader.getLevel();
  176. }
  177. }
  178. /**
  179. * @param obj
  180. * @return true if proper condition for RaidWar
  181. */
  182. public boolean meetRaidWarCondition(L2Object obj)
  183. {
  184. if (!((obj instanceof L2Character) && ((L2Character) obj).isRaid()))
  185. {
  186. return false;
  187. }
  188. return (getMemberCount() >= Config.LOOT_RAIDS_PRIVILEGE_CC_SIZE);
  189. }
  190. /**
  191. * @return the leader of this command channel
  192. */
  193. @Override
  194. public L2PcInstance getLeader()
  195. {
  196. return _commandLeader;
  197. }
  198. /**
  199. * Check if a given player is in this command channel.
  200. * @param player the player to check
  201. * @return {@code true} if he does, {@code false} otherwise
  202. */
  203. @Override
  204. public boolean containsPlayer(L2PcInstance player)
  205. {
  206. if ((_parties != null) && !_parties.isEmpty())
  207. {
  208. for (L2Party party : _parties)
  209. {
  210. if (party.containsPlayer(player))
  211. {
  212. return true;
  213. }
  214. }
  215. }
  216. return false;
  217. }
  218. /**
  219. * Iterates over all command channel members without the need to allocate a new list
  220. * @see com.l2jserver.gameserver.model.AbstractPlayerGroup#forEachMember(IL2Procedure)
  221. */
  222. @Override
  223. public boolean forEachMember(IL2Procedure<L2PcInstance> procedure)
  224. {
  225. if ((_parties != null) && !_parties.isEmpty())
  226. {
  227. for (L2Party party : _parties)
  228. {
  229. if (!party.forEachMember(procedure))
  230. {
  231. return false;
  232. }
  233. }
  234. }
  235. return true;
  236. }
  237. /**
  238. * Check whether the leader of this command channel is the same as the leader of the specified command channel<br>
  239. * (which essentially means they're the same group).
  240. * @param cc the other command channel to check against
  241. * @return {@code true} if this command channel equals the specified command channel, {@code false} otherwise
  242. */
  243. public boolean equals(L2CommandChannel cc)
  244. {
  245. return (cc != null) && (getLeaderObjectId() == cc.getLeaderObjectId());
  246. }
  247. }