123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- package com.gmail.nossr50.party;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import com.gmail.nossr50.mcMMO;
- import com.gmail.nossr50.datatypes.PlayerProfile;
- import com.gmail.nossr50.locale.LocaleLoader;
- import com.gmail.nossr50.util.Misc;
- import com.gmail.nossr50.util.Users;
- public final class PartyManager {
- private static String partiesFilePath = mcMMO.p.getDataFolder().getPath() + File.separator + "FlatFileStuff" + File.separator + "parties.yml";
- private static List<Party> parties = new ArrayList<Party>();
- private PartyManager() {}
- /**
- * Check if two players are in the same party.
- *
- * @param firstPlayer The first player
- * @param secondPlayer The second player
- * @return true if they are in the same party, false otherwise
- */
- public static boolean inSameParty(Player firstPlayer, Player secondPlayer) {
- PlayerProfile firstProfile = Users.getProfile(firstPlayer);
- PlayerProfile secondProfile = Users.getProfile(secondPlayer);
- if (firstProfile == null || secondProfile == null) {
- return false;
- }
- Party firstParty = firstProfile.getParty();
- Party secondParty = secondProfile.getParty();
- if (firstParty == null || secondParty == null || firstParty != secondParty) {
- return false;
- }
- return true;
- }
- /**
- * Get the near party members.
- *
- * @param player The player to check
- * @param range The distance
- * @return the near party members
- */
- public static List<Player> getNearMembers(Player player, Party party, double range) {
- List<Player> nearMembers = new ArrayList<Player>();
- if (party != null) {
- for (Player member : party.getOnlineMembers()) {
- if (player != member && Misc.isNear(player.getLocation(), member.getLocation(), range)) {
- nearMembers.add(member);
- }
- }
- }
- return nearMembers;
- }
- /**
- * Notify party members when a player joins
- *
- * @param playerName The name of the player that joins
- * @param party The concerned party
- */
- private static void informPartyMembersJoin(String playerName, Party party) {
- for (Player member : party.getOnlineMembers()) {
- if (!member.getName().equals(playerName)) {
- member.sendMessage(LocaleLoader.getString("Party.InformedOnJoin", new Object[] {playerName}));
- }
- }
- }
- /**
- * Notify party members when a party member quits.
- *
- * @param playerName The name of the player that quits
- * @param party The concerned party
- */
- private static void informPartyMembersQuit(String playerName, Party party) {
- for (Player member : party.getOnlineMembers()) {
- if (!member.getName().equals(playerName)) {
- member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", new Object[] {playerName}));
- }
- }
- }
- /**
- * Get a list of all players in this player's party.
- *
- * @param player The player to check
- * @return all the players in the player's party
- */
- public static List<String> getAllMembers(Player player) {
- Party party = Users.getProfile(player).getParty();
- if (party == null) {
- return null;
- }
- return party.getMembers();
- }
- /**
- * Get a list of all online players in this party.
- *
- * @param partyName The party to check
- * @return all online players in this party
- */
- public static List<Player> getOnlineMembers(String partyName) {
- Party party = getParty(partyName);
- if (party == null) {
- return null;
- }
- return party.getOnlineMembers();
- }
- /**
- * Get a list of all online players in this party.
- *
- * @param player The player to check
- * @return all online players in this party
- */
- public static List<Player> getOnlineMembers(Player player) {
- return getOnlineMembers(player.getName());
- }
- /**
- * Retrieve a party by its name
- *
- * @param partyName The party name
- * @return the existing party, null otherwise
- */
- public static Party getParty(String partyName) {
- for (Party party : parties) {
- if (party.getName().equals(partyName)) {
- return party;
- }
- }
- return null;
- }
- /**
- * Retrieve a party by a member name
- *
- * @param playerName The member name
- * @return the existing party, null otherwise
- */
- public static Party getPlayerParty(String playerName) {
- for (Party party : parties) {
- if (party.getMembers().contains(playerName)) {
- return party;
- }
- }
- return null;
- }
- /**
- * Get a list of all current parties.
- *
- * @return the list of parties.
- */
- public static List<Party> getParties() {
- return parties;
- }
- /**
- * Remove a player from a party.
- *
- * @param playerName The name of the player to remove
- * @param party The party
- */
- public static void removeFromParty(String playerName, Party party) {
- List<String> members = party.getMembers();
- members.remove(playerName);
- if (members.isEmpty()) {
- parties.remove(party);
- }
- else {
- //If the leaving player was the party leader, appoint a new leader from the party members
- if (party.getLeader().equals(playerName)) {
- String newLeader = members.get(0);
- party.setLeader(newLeader);
- }
- informPartyMembersQuit(playerName, party);
- }
- PlayerProfile playerProfile = Users.getProfile(playerName);
- if (playerProfile != null) {
- playerProfile.removeParty();
- }
- }
- /**
- * Disband a party. Kicks out all members and removes the party.
- *
- * @param party The party to remove
- */
- public static void disbandParty(Party party) {
- List<String> members = party.getMembers();
- for (String member : party.getMembers()) {
- PlayerProfile playerProfile = Users.getProfile(member);
- if (playerProfile != null) {
- playerProfile.removeParty();
- }
- }
- members.clear();
- if (members.isEmpty()) {
- parties.remove(party);
- }
- }
- /**
- * Create a new party
- *
- * @param player The player to add to the party
- * @param playerProfile The profile of the player to add to the party
- * @param partyName The party to add the player to
- * @param password the password for this party, null if there was no password
- */
- public static void createParty(Player player, PlayerProfile playerProfile, String partyName, String password) {
- partyName = partyName.replace(".", "");
- Party party = getParty(partyName);
- String playerName = player.getName();
- if (party == null) {
- party = new Party();
- party.setName(partyName);
- party.setLeader(playerName);
- party.setExpShareMode("NO_SHARE");
- party.setLocked(true);//Parties are now invite-only by default, can be set to open with /party unlock
- if (password != null) {
- party.setPassword(password);
- party.setLocked(true);
- player.sendMessage(LocaleLoader.getString("Party.Password.Set", new Object[] {password}));
- }
- parties.add(party);
- }
- else {
- player.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists"));
- return;
- }
- player.sendMessage(LocaleLoader.getString("Commands.Party.Create", new Object[]{party.getName()}));
- addToParty(player.getName(), playerProfile, party);
- }
- /**
- * Add a player to a party.
- *
- * @param player The player to add to the party
- * @param playerProfile The profile of the player to add to the party
- * @param partyName The party to add the player to
- * @param password the password for this party, null if there was no password
- */
- public static void joinParty(Player player, PlayerProfile playerProfile, String partyName, String password) {
- partyName = partyName.replace(".", "");
- Party party = getParty(partyName);
- String playerName = player.getName();
- if (party == null) {
- party = new Party();
- party.setName(partyName);
- party.setLeader(playerName);
- if (password != null) {
- party.setPassword(password);
- party.setLocked(true);
- }
- parties.add(party);
- }
- else if (!checkJoinability(player, party, password)) {
- return;
- }
- player.sendMessage(LocaleLoader.getString("Commands.Party.Join", new Object[]{party.getName()}));
- addToParty(player.getName(), playerProfile, party);
- }
- /**
- * Check if a player can join a party
- *
- * @param player The player trying to join a party
- * @param playerProfile The profile of the player
- * @param party The party
- * @param password The password provided by the player
- * @return true if the player can join the party
- */
- public static boolean checkJoinability(Player player, Party party, String password) {
- //Don't care about passwords if it isn't locked
- if (party.isLocked()) {
- String partyPassword = party.getPassword();
- if (partyPassword != null) {
- if (password == null) {
- player.sendMessage(LocaleLoader.getString("Party.Password.None"));
- player.sendMessage(LocaleLoader.getString("Commands.Usage.3", new Object[] {"party", "join", "<" + LocaleLoader.getString("Commands.Usage.Player") + ">", "<" + LocaleLoader.getString("Commands.Usage.Password") + ">"}));
- return false;
- }
- else if (!password.equals(partyPassword)) {
- player.sendMessage(LocaleLoader.getString("Party.Password.Incorrect"));
- return false;
- }
- }
- else {
- player.sendMessage(LocaleLoader.getString("Party.Locked"));
- return false;
- }
- }
- return true;
- }
- /**
- * Accept a party invitation
- *
- * @param player The player to add to the party
- * @param playerProfile The profile of the player
- */
- public static void joinInvitedParty(Player player, PlayerProfile playerProfile) {
- Party invite = playerProfile.getInvite();
- if (!parties.contains(invite)) {
- parties.add(invite);
- }
- player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", new Object[]{invite.getName()}));
- playerProfile.removeInvite();
- addToParty(player.getName(), playerProfile, invite);
- }
- /**
- * Add a player to a party
- *
- * @param playerName The name of the player to add to a party
- * @param playerProfile The profile of the player
- * @param party The party
- */
- public static void addToParty(String playerName, PlayerProfile playerProfile, Party party) {
- informPartyMembersJoin(playerName, party);
- playerProfile.setParty(party);
- party.getMembers().add(playerName);
- }
- /**
- * Get the leader of a party.
- *
- * @param partyName The party name
- * @return the leader of the party
- */
- public static String getPartyLeader(String partyName) {
- Party party = getParty(partyName);
- if (party == null) {
- return null;
- }
- return party.getLeader();
- }
- /**
- * Set the leader of a party.
- *
- * @param playerName The name of the player to set as leader
- * @param party The party
- */
- public static void setPartyLeader(String playerName, Party party) {
- String leaderName = party.getLeader();
- for (Player member : party.getOnlineMembers()) {
- if (member.getName().equals(playerName)) {
- member.sendMessage(LocaleLoader.getString("Party.Owner.Player"));
- }
- else if (member.equals(leaderName)) {
- member.sendMessage(LocaleLoader.getString("Party.Owner.NotLeader"));
- }
- else {
- member.sendMessage(LocaleLoader.getString("Party.Owner.New", new Object[] {playerName}));
- }
- }
- party.setLeader(playerName);
- }
- /**
- * Check if a player can invite others to their party.
- *
- * @param player The player to check
- * @param playerProfile The profile of the given player
- * @return true if the player can invite
- */
- public static boolean canInvite(Player player, PlayerProfile playerProfile) {
- Party party = playerProfile.getParty();
- if (party == null || (party.isLocked() && !party.getLeader().equals(player.getName()))) {
- return false;
- }
- return true;
- }
- /**
- * Check if a string is a valid party name.
- *
- * @param partyName The party name to check
- * @return true if this is a valid party, false otherwise
- */
- public static boolean isParty(String partyName) {
- for (Party party : parties) {
- if (party.getName().equals(partyName)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Load party file.
- */
- public static void loadParties() {
- File file = new File(partiesFilePath);
- if (!file.exists()) {
- return;
- }
- YamlConfiguration partiesFile = new YamlConfiguration();
- try {
- partiesFile.load(file);
- } catch (Exception e) {
- e.printStackTrace();
- }
- for (String partyName : partiesFile.getConfigurationSection("").getKeys(false)) {
- Party party = new Party();
- party.setName(partyName);
- party.setLeader(partiesFile.getString(partyName + ".Leader"));
- party.setPassword(partiesFile.getString(partyName + ".Password"));
- party.setLocked(partiesFile.getBoolean(partyName + ".Locked"));
- party.setExpShareMode(partiesFile.getString(partyName + ".ExpShareMode"));
- party.getMembers().addAll(partiesFile.getStringList(partyName + ".Members"));
- parties.add(party);
- }
- }
- /**
- * Save party file.
- */
- public static void saveParties() {
- File file = new File(partiesFilePath);
- if (file.exists()) {
- file.delete();
- }
- YamlConfiguration partiesFile = new YamlConfiguration();
- for (Party party : parties) {
- String partyName = party.getName();
- partiesFile.set(partyName + ".Leader", party.getLeader());
- partiesFile.set(partyName + ".Password", party.getPassword());
- partiesFile.set(partyName + ".Locked", party.isLocked());
- partiesFile.set(partyName + ".ExpShareMode", party.getExpShareMode());
- partiesFile.set(partyName + ".Members", party.getMembers());
- try {
- partiesFile.save(new File(partiesFilePath));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
|