PartyManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package com.gmail.nossr50.party;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.bukkit.configuration.file.YamlConfiguration;
  6. import org.bukkit.entity.Player;
  7. import com.gmail.nossr50.mcMMO;
  8. import com.gmail.nossr50.datatypes.PlayerProfile;
  9. import com.gmail.nossr50.locale.LocaleLoader;
  10. import com.gmail.nossr50.util.Misc;
  11. import com.gmail.nossr50.util.Users;
  12. public final class PartyManager {
  13. private static String partiesFilePath = mcMMO.p.getDataFolder().getPath() + File.separator + "FlatFileStuff" + File.separator + "parties.yml";
  14. private static List<Party> parties = new ArrayList<Party>();
  15. private PartyManager() {}
  16. /**
  17. * Check if two players are in the same party.
  18. *
  19. * @param firstPlayer The first player
  20. * @param secondPlayer The second player
  21. * @return true if they are in the same party, false otherwise
  22. */
  23. public static boolean inSameParty(Player firstPlayer, Player secondPlayer) {
  24. PlayerProfile firstProfile = Users.getProfile(firstPlayer);
  25. PlayerProfile secondProfile = Users.getProfile(secondPlayer);
  26. if (firstProfile == null || secondProfile == null) {
  27. return false;
  28. }
  29. Party firstParty = firstProfile.getParty();
  30. Party secondParty = secondProfile.getParty();
  31. if (firstParty == null || secondParty == null || firstParty != secondParty) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. /**
  37. * Get the near party members.
  38. *
  39. * @param player The player to check
  40. * @param range The distance
  41. * @return the near party members
  42. */
  43. public static List<Player> getNearMembers(Player player, Party party, double range) {
  44. List<Player> nearMembers = new ArrayList<Player>();
  45. if (party != null) {
  46. for (Player member : party.getOnlineMembers()) {
  47. if (player != member && Misc.isNear(player.getLocation(), member.getLocation(), range)) {
  48. nearMembers.add(member);
  49. }
  50. }
  51. }
  52. return nearMembers;
  53. }
  54. /**
  55. * Notify party members when a player joins
  56. *
  57. * @param playerName The name of the player that joins
  58. * @param party The concerned party
  59. */
  60. private static void informPartyMembersJoin(String playerName, Party party) {
  61. for (Player member : party.getOnlineMembers()) {
  62. if (!member.getName().equals(playerName)) {
  63. member.sendMessage(LocaleLoader.getString("Party.InformedOnJoin", new Object[] {playerName}));
  64. }
  65. }
  66. }
  67. /**
  68. * Notify party members when a party member quits.
  69. *
  70. * @param playerName The name of the player that quits
  71. * @param party The concerned party
  72. */
  73. private static void informPartyMembersQuit(String playerName, Party party) {
  74. for (Player member : party.getOnlineMembers()) {
  75. if (!member.getName().equals(playerName)) {
  76. member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", new Object[] {playerName}));
  77. }
  78. }
  79. }
  80. /**
  81. * Get a list of all players in this player's party.
  82. *
  83. * @param player The player to check
  84. * @return all the players in the player's party
  85. */
  86. public static List<String> getAllMembers(Player player) {
  87. Party party = Users.getProfile(player).getParty();
  88. if (party == null) {
  89. return null;
  90. }
  91. return party.getMembers();
  92. }
  93. /**
  94. * Get a list of all online players in this party.
  95. *
  96. * @param partyName The party to check
  97. * @return all online players in this party
  98. */
  99. public static List<Player> getOnlineMembers(String partyName) {
  100. Party party = getParty(partyName);
  101. if (party == null) {
  102. return null;
  103. }
  104. return party.getOnlineMembers();
  105. }
  106. /**
  107. * Get a list of all online players in this party.
  108. *
  109. * @param player The player to check
  110. * @return all online players in this party
  111. */
  112. public static List<Player> getOnlineMembers(Player player) {
  113. return getOnlineMembers(player.getName());
  114. }
  115. /**
  116. * Retrieve a party by its name
  117. *
  118. * @param partyName The party name
  119. * @return the existing party, null otherwise
  120. */
  121. public static Party getParty(String partyName) {
  122. for (Party party : parties) {
  123. if (party.getName().equals(partyName)) {
  124. return party;
  125. }
  126. }
  127. return null;
  128. }
  129. /**
  130. * Retrieve a party by a member name
  131. *
  132. * @param playerName The member name
  133. * @return the existing party, null otherwise
  134. */
  135. public static Party getPlayerParty(String playerName) {
  136. for (Party party : parties) {
  137. if (party.getMembers().contains(playerName)) {
  138. return party;
  139. }
  140. }
  141. return null;
  142. }
  143. /**
  144. * Get a list of all current parties.
  145. *
  146. * @return the list of parties.
  147. */
  148. public static List<Party> getParties() {
  149. return parties;
  150. }
  151. /**
  152. * Remove a player from a party.
  153. *
  154. * @param playerName The name of the player to remove
  155. * @param party The party
  156. */
  157. public static void removeFromParty(String playerName, Party party) {
  158. List<String> members = party.getMembers();
  159. members.remove(playerName);
  160. if (members.isEmpty()) {
  161. parties.remove(party);
  162. }
  163. else {
  164. //If the leaving player was the party leader, appoint a new leader from the party members
  165. if (party.getLeader().equals(playerName)) {
  166. String newLeader = members.get(0);
  167. party.setLeader(newLeader);
  168. }
  169. informPartyMembersQuit(playerName, party);
  170. }
  171. PlayerProfile playerProfile = Users.getProfile(playerName);
  172. if (playerProfile != null) {
  173. playerProfile.removeParty();
  174. }
  175. }
  176. /**
  177. * Disband a party. Kicks out all members and removes the party.
  178. *
  179. * @param party The party to remove
  180. */
  181. public static void disbandParty(Party party) {
  182. List<String> members = party.getMembers();
  183. for (String member : party.getMembers()) {
  184. PlayerProfile playerProfile = Users.getProfile(member);
  185. if (playerProfile != null) {
  186. playerProfile.removeParty();
  187. }
  188. }
  189. members.clear();
  190. if (members.isEmpty()) {
  191. parties.remove(party);
  192. }
  193. }
  194. /**
  195. * Create a new party
  196. *
  197. * @param player The player to add to the party
  198. * @param playerProfile The profile of the player to add to the party
  199. * @param partyName The party to add the player to
  200. * @param password the password for this party, null if there was no password
  201. */
  202. public static void createParty(Player player, PlayerProfile playerProfile, String partyName, String password) {
  203. partyName = partyName.replace(".", "");
  204. Party party = getParty(partyName);
  205. String playerName = player.getName();
  206. if (party == null) {
  207. party = new Party();
  208. party.setName(partyName);
  209. party.setLeader(playerName);
  210. party.setExpShareMode("NO_SHARE");
  211. party.setLocked(true);//Parties are now invite-only by default, can be set to open with /party unlock
  212. if (password != null) {
  213. party.setPassword(password);
  214. party.setLocked(true);
  215. player.sendMessage(LocaleLoader.getString("Party.Password.Set", new Object[] {password}));
  216. }
  217. parties.add(party);
  218. }
  219. else {
  220. player.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists"));
  221. return;
  222. }
  223. player.sendMessage(LocaleLoader.getString("Commands.Party.Create", new Object[]{party.getName()}));
  224. addToParty(player.getName(), playerProfile, party);
  225. }
  226. /**
  227. * Add a player to a party.
  228. *
  229. * @param player The player to add to the party
  230. * @param playerProfile The profile of the player to add to the party
  231. * @param partyName The party to add the player to
  232. * @param password the password for this party, null if there was no password
  233. */
  234. public static void joinParty(Player player, PlayerProfile playerProfile, String partyName, String password) {
  235. partyName = partyName.replace(".", "");
  236. Party party = getParty(partyName);
  237. String playerName = player.getName();
  238. if (party == null) {
  239. party = new Party();
  240. party.setName(partyName);
  241. party.setLeader(playerName);
  242. if (password != null) {
  243. party.setPassword(password);
  244. party.setLocked(true);
  245. }
  246. parties.add(party);
  247. }
  248. else if (!checkJoinability(player, party, password)) {
  249. return;
  250. }
  251. player.sendMessage(LocaleLoader.getString("Commands.Party.Join", new Object[]{party.getName()}));
  252. addToParty(player.getName(), playerProfile, party);
  253. }
  254. /**
  255. * Check if a player can join a party
  256. *
  257. * @param player The player trying to join a party
  258. * @param playerProfile The profile of the player
  259. * @param party The party
  260. * @param password The password provided by the player
  261. * @return true if the player can join the party
  262. */
  263. public static boolean checkJoinability(Player player, Party party, String password) {
  264. //Don't care about passwords if it isn't locked
  265. if (party.isLocked()) {
  266. String partyPassword = party.getPassword();
  267. if (partyPassword != null) {
  268. if (password == null) {
  269. player.sendMessage(LocaleLoader.getString("Party.Password.None"));
  270. player.sendMessage(LocaleLoader.getString("Commands.Usage.3", new Object[] {"party", "join", "<" + LocaleLoader.getString("Commands.Usage.Player") + ">", "<" + LocaleLoader.getString("Commands.Usage.Password") + ">"}));
  271. return false;
  272. }
  273. else if (!password.equals(partyPassword)) {
  274. player.sendMessage(LocaleLoader.getString("Party.Password.Incorrect"));
  275. return false;
  276. }
  277. }
  278. else {
  279. player.sendMessage(LocaleLoader.getString("Party.Locked"));
  280. return false;
  281. }
  282. }
  283. return true;
  284. }
  285. /**
  286. * Accept a party invitation
  287. *
  288. * @param player The player to add to the party
  289. * @param playerProfile The profile of the player
  290. */
  291. public static void joinInvitedParty(Player player, PlayerProfile playerProfile) {
  292. Party invite = playerProfile.getInvite();
  293. if (!parties.contains(invite)) {
  294. parties.add(invite);
  295. }
  296. player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", new Object[]{invite.getName()}));
  297. playerProfile.removeInvite();
  298. addToParty(player.getName(), playerProfile, invite);
  299. }
  300. /**
  301. * Add a player to a party
  302. *
  303. * @param playerName The name of the player to add to a party
  304. * @param playerProfile The profile of the player
  305. * @param party The party
  306. */
  307. public static void addToParty(String playerName, PlayerProfile playerProfile, Party party) {
  308. informPartyMembersJoin(playerName, party);
  309. playerProfile.setParty(party);
  310. party.getMembers().add(playerName);
  311. }
  312. /**
  313. * Get the leader of a party.
  314. *
  315. * @param partyName The party name
  316. * @return the leader of the party
  317. */
  318. public static String getPartyLeader(String partyName) {
  319. Party party = getParty(partyName);
  320. if (party == null) {
  321. return null;
  322. }
  323. return party.getLeader();
  324. }
  325. /**
  326. * Set the leader of a party.
  327. *
  328. * @param playerName The name of the player to set as leader
  329. * @param party The party
  330. */
  331. public static void setPartyLeader(String playerName, Party party) {
  332. String leaderName = party.getLeader();
  333. for (Player member : party.getOnlineMembers()) {
  334. if (member.getName().equals(playerName)) {
  335. member.sendMessage(LocaleLoader.getString("Party.Owner.Player"));
  336. }
  337. else if (member.equals(leaderName)) {
  338. member.sendMessage(LocaleLoader.getString("Party.Owner.NotLeader"));
  339. }
  340. else {
  341. member.sendMessage(LocaleLoader.getString("Party.Owner.New", new Object[] {playerName}));
  342. }
  343. }
  344. party.setLeader(playerName);
  345. }
  346. /**
  347. * Check if a player can invite others to their party.
  348. *
  349. * @param player The player to check
  350. * @param playerProfile The profile of the given player
  351. * @return true if the player can invite
  352. */
  353. public static boolean canInvite(Player player, PlayerProfile playerProfile) {
  354. Party party = playerProfile.getParty();
  355. if (party == null || (party.isLocked() && !party.getLeader().equals(player.getName()))) {
  356. return false;
  357. }
  358. return true;
  359. }
  360. /**
  361. * Check if a string is a valid party name.
  362. *
  363. * @param partyName The party name to check
  364. * @return true if this is a valid party, false otherwise
  365. */
  366. public static boolean isParty(String partyName) {
  367. for (Party party : parties) {
  368. if (party.getName().equals(partyName)) {
  369. return true;
  370. }
  371. }
  372. return false;
  373. }
  374. /**
  375. * Load party file.
  376. */
  377. public static void loadParties() {
  378. File file = new File(partiesFilePath);
  379. if (!file.exists()) {
  380. return;
  381. }
  382. YamlConfiguration partiesFile = new YamlConfiguration();
  383. try {
  384. partiesFile.load(file);
  385. } catch (Exception e) {
  386. e.printStackTrace();
  387. }
  388. for (String partyName : partiesFile.getConfigurationSection("").getKeys(false)) {
  389. Party party = new Party();
  390. party.setName(partyName);
  391. party.setLeader(partiesFile.getString(partyName + ".Leader"));
  392. party.setPassword(partiesFile.getString(partyName + ".Password"));
  393. party.setLocked(partiesFile.getBoolean(partyName + ".Locked"));
  394. party.setExpShareMode(partiesFile.getString(partyName + ".ExpShareMode"));
  395. party.getMembers().addAll(partiesFile.getStringList(partyName + ".Members"));
  396. parties.add(party);
  397. }
  398. }
  399. /**
  400. * Save party file.
  401. */
  402. public static void saveParties() {
  403. File file = new File(partiesFilePath);
  404. if (file.exists()) {
  405. file.delete();
  406. }
  407. YamlConfiguration partiesFile = new YamlConfiguration();
  408. for (Party party : parties) {
  409. String partyName = party.getName();
  410. partiesFile.set(partyName + ".Leader", party.getLeader());
  411. partiesFile.set(partyName + ".Password", party.getPassword());
  412. partiesFile.set(partyName + ".Locked", party.isLocked());
  413. partiesFile.set(partyName + ".ExpShareMode", party.getExpShareMode());
  414. partiesFile.set(partyName + ".Members", party.getMembers());
  415. try {
  416. partiesFile.save(new File(partiesFilePath));
  417. } catch (Exception e) {
  418. e.printStackTrace();
  419. }
  420. }
  421. }
  422. }