PartyManager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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.Users;
  11. public class PartyManager {
  12. private static String partiesFilePath;
  13. private static List<Party> parties = new ArrayList<Party>();
  14. private static mcMMO plugin;
  15. private static PartyManager instance;
  16. private PartyManager() {
  17. plugin = mcMMO.p;
  18. partiesFilePath = plugin.getDataFolder().getPath() + File.separator + "FlatFileStuff" + File.separator + "parties.yml";
  19. loadParties();
  20. }
  21. public static PartyManager getInstance() {
  22. if (instance == null) {
  23. instance = new PartyManager();
  24. }
  25. return instance;
  26. }
  27. /**
  28. * Check if two players are in the same party.
  29. *
  30. * @param firstPlayer The first player
  31. * @param secondPlayer The second player
  32. * @return true if they are in the same party, false otherwise
  33. */
  34. public boolean inSameParty(Player firstPlayer, Player secondPlayer) {
  35. Party firstParty = Users.getProfile(firstPlayer).getParty();
  36. Party secondParty = Users.getProfile(secondPlayer).getParty();
  37. if (firstParty == null || secondParty == null || firstParty != secondParty) {
  38. return false;
  39. }
  40. return true;
  41. }
  42. /**
  43. * Notify party members when a player joins
  44. *
  45. * @param playerName The name of the player that joins
  46. * @param party The concerned party
  47. */
  48. private void informPartyMembersJoin(String playerName, Party party) {
  49. for (Player member : party.getOnlineMembers()) {
  50. if (member.getName().equals(playerName)) {
  51. member.sendMessage(LocaleLoader.getString("Party.InformedOnJoin", new Object[] {playerName}));
  52. }
  53. }
  54. }
  55. /**
  56. * Notify party members when a party member quits.
  57. *
  58. * @param playerName The name of the player that quits
  59. * @param party The concerned party
  60. */
  61. private void informPartyMembersQuit(String playerName, Party party) {
  62. for (Player member : party.getOnlineMembers()) {
  63. if (member.getName().equals(playerName)) {
  64. member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", new Object[] {playerName}));
  65. }
  66. }
  67. }
  68. /**
  69. * Get a list of all players in this player's party.
  70. *
  71. * @param player The player to check
  72. * @return all the players in the player's party
  73. */
  74. public List<String> getAllMembers(Player player) {
  75. Party party = Users.getProfile(player).getParty();
  76. if (party == null) {
  77. return null;
  78. }
  79. return party.getMembers();
  80. }
  81. /**
  82. * Get a list of all online players in this party.
  83. *
  84. * @param partyName The party to check
  85. * @return all online players in this party
  86. */
  87. public List<Player> getOnlineMembers(String partyName) {
  88. Party party = getParty(partyName);
  89. if (party == null) {
  90. return null;
  91. }
  92. return party.getOnlineMembers();
  93. }
  94. /**
  95. * Get a list of all online players in this party.
  96. *
  97. * @param player The player to check
  98. * @return all online players in this party
  99. */
  100. public List<Player> getOnlineMembers(Player player) {
  101. return getOnlineMembers(player.getName());
  102. }
  103. /**
  104. * Retrieve a party by its name
  105. *
  106. * @param partyName The party name
  107. * @return the existing party, null otherwise
  108. */
  109. public Party getParty(String partyName) {
  110. for (Party party : parties) {
  111. if (party.getName().equals(partyName)) {
  112. return party;
  113. }
  114. }
  115. return null;
  116. }
  117. /**
  118. * Retrieve a party by a member name
  119. *
  120. * @param playerName The member name
  121. * @return the existing party, null otherwise
  122. */
  123. public Party getPlayerParty(String playerName) {
  124. for (Party party : parties) {
  125. if (party.getMembers().contains(playerName)) {
  126. return party;
  127. }
  128. }
  129. return null;
  130. }
  131. /**
  132. * Get a list of all current parties.
  133. *
  134. * @return the list of parties.
  135. */
  136. public List<Party> getParties() {
  137. return parties;
  138. }
  139. /**
  140. * Remove a player from a party.
  141. *
  142. * @param playerName The name of the player to remove
  143. * @param party The party
  144. */
  145. public void removeFromParty(String playerName, Party party) {
  146. List<String> members = party.getMembers();
  147. members.remove(playerName);
  148. if (members.isEmpty()) {
  149. parties.remove(party);
  150. }
  151. else {
  152. if (party.getLeader().equals(playerName)) {
  153. party.setLocked(false);
  154. }
  155. informPartyMembersQuit(playerName, party);
  156. }
  157. PlayerProfile playerProfile = Users.getProfile(playerName);
  158. if (playerProfile != null) {
  159. playerProfile.removeParty();
  160. }
  161. }
  162. /**
  163. * Add a player to a party.
  164. *
  165. * @param player The player to add to the party
  166. * @param playerProfile The profile of the player to add to the party
  167. * @param partyName The party to add the player to
  168. * @param password the password for this party, null if there was no password
  169. */
  170. public void joinParty(Player player, PlayerProfile playerProfile, String partyName, String password) {
  171. partyName = partyName.replace(".", "");
  172. Party party = getParty(partyName);
  173. String playerName = player.getName();
  174. if (party == null) {
  175. party = new Party();
  176. party.setName(partyName);
  177. party.setLeader(playerName);
  178. if (password != null) {
  179. party.setPassword(password);
  180. party.setLocked(true);
  181. }
  182. parties.add(party);
  183. }
  184. else if (!checkJoinability(player, playerProfile, party, password)) {
  185. return;
  186. }
  187. player.sendMessage(LocaleLoader.getString("Commands.Party.Join", new Object[]{party.getName()}));
  188. addToParty(player.getName(), playerProfile, party);
  189. }
  190. /**
  191. * Check if a player can join a party
  192. *
  193. * @param player The player trying to join a party
  194. * @param playerProfile The profile of the player
  195. * @param party The party
  196. * @param password The password provided by the player
  197. * @return true if the player can join the party
  198. */
  199. private boolean checkJoinability(Player player, PlayerProfile playerProfile, Party party, String password) {
  200. //Don't care about passwords if it isn't locked
  201. if (party.isLocked()) {
  202. String partyPassword = party.getPassword();
  203. if (partyPassword != null) {
  204. if (password == null) {
  205. player.sendMessage("This party requires a password. Use /party <party> <password> to join it."); //TODO: Needs more locale.
  206. return false;
  207. }
  208. else if (!password.equals(partyPassword)) {
  209. player.sendMessage("Party password incorrect."); //TODO: Needs more locale.
  210. return false;
  211. }
  212. }
  213. else {
  214. player.sendMessage("Party is locked."); //TODO: Needs more locale.
  215. return false;
  216. }
  217. }
  218. return true;
  219. }
  220. /**
  221. * Accept a party invitation
  222. *
  223. * @param player The player to add to the party
  224. * @param playerProfile The profile of the player
  225. */
  226. public void joinInvitedParty(Player player, PlayerProfile playerProfile) {
  227. Party invite = playerProfile.getInvite();
  228. if (!parties.contains(invite)) {
  229. parties.add(invite);
  230. }
  231. player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", new Object[]{invite.getName()}));
  232. playerProfile.removeInvite();
  233. addToParty(player.getName(), playerProfile, invite);
  234. }
  235. /**
  236. * Add a player to a party
  237. *
  238. * @param playerName The name of the player to add to a party
  239. * @param playerProfile The profile of the player
  240. * @param party The party
  241. */
  242. public void addToParty(String playerName, PlayerProfile playerProfile, Party party) {
  243. informPartyMembersJoin(playerName, party);
  244. playerProfile.setParty(party);
  245. party.getMembers().add(playerName);
  246. }
  247. /**
  248. * Get the leader of a party.
  249. *
  250. * @param partyName The party name
  251. * @return the leader of the party
  252. */
  253. public String getPartyLeader(String partyName) {
  254. Party party = getParty(partyName);
  255. if (party == null) {
  256. return null;
  257. }
  258. return party.getLeader();
  259. }
  260. /**
  261. * Set the leader of a party.
  262. *
  263. * @param playerName The name of the player to set as leader
  264. * @param party The party
  265. */
  266. public void setPartyLeader(String playerName, Party party) {
  267. String leaderName = party.getLeader();
  268. for (Player member : party.getOnlineMembers()) {
  269. if (member.getName().equals(playerName)) {
  270. member.sendMessage("You are now the party owner."); //TODO: Needs more locale.
  271. }
  272. else if (member.equals(leaderName)) {
  273. member.sendMessage("You are no longer party owner."); //TODO: Needs more locale.
  274. }
  275. else {
  276. member.sendMessage(playerName + " is the new party owner."); //TODO: Needs more Locale.
  277. }
  278. }
  279. party.setLeader(playerName);
  280. }
  281. /**
  282. * Check if a player can invite others to their party.
  283. *
  284. * @param player The player to check
  285. * @param playerProfile The profile of the given player
  286. * @return true if the player can invite
  287. */
  288. public boolean canInvite(Player player, PlayerProfile playerProfile) {
  289. Party party = playerProfile.getParty();
  290. if (party == null || (party.isLocked() && !party.getLeader().equals(player.getName()))) {
  291. return false;
  292. }
  293. return true;
  294. }
  295. /**
  296. * Check if a string is a valid party name.
  297. *
  298. * @param partyName The party name to check
  299. * @return true if this is a valid party, false otherwise
  300. */
  301. public boolean isParty(String partyName) {
  302. for (Party party : parties) {
  303. if (party.getName().equals(partyName)) {
  304. return true;
  305. }
  306. }
  307. return false;
  308. }
  309. /**
  310. * Load party file.
  311. */
  312. private void loadParties() {
  313. File file = new File(partiesFilePath);
  314. if (!file.exists()) {
  315. return;
  316. }
  317. YamlConfiguration partiesFile = new YamlConfiguration();
  318. try {
  319. partiesFile.load(file);
  320. } catch (Exception e) {
  321. e.printStackTrace();
  322. }
  323. for (String partyName : partiesFile.getConfigurationSection("").getKeys(false)) {
  324. Party party = new Party();
  325. party.setName(partyName);
  326. party.setLeader(partiesFile.getString(partyName + ".Leader"));
  327. party.setPassword(partiesFile.getString(partyName + ".Password"));
  328. party.setLocked(partiesFile.getBoolean(partyName + ".Locked"));
  329. party.getMembers().addAll(partiesFile.getStringList(partyName + ".Members"));
  330. parties.add(party);
  331. }
  332. }
  333. /**
  334. * Save party file.
  335. */
  336. public void saveParties() {
  337. File file = new File(partiesFilePath);
  338. if (file.exists()) {
  339. file.delete();
  340. }
  341. YamlConfiguration partiesFile = new YamlConfiguration();
  342. for (Party party : parties) {
  343. String partyName = party.getName();
  344. partiesFile.set(partyName + ".Leader", party.getLeader());
  345. partiesFile.set(partyName + ".Password", party.getPassword());
  346. partiesFile.set(partyName + ".Locked", party.isLocked());
  347. partiesFile.set(partyName + ".Members", party.getMembers());
  348. try {
  349. partiesFile.save(new File(partiesFilePath));
  350. } catch (Exception e) {
  351. e.printStackTrace();
  352. }
  353. }
  354. }
  355. }