PartyManager.java 12 KB

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