mcMMO/src/main/java/com/gmail/nossr50/party/PartyManager.java

542 lines
17 KiB
Java
Raw Normal View History

2012-06-08 23:48:41 +02:00
package com.gmail.nossr50.party;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
2012-06-08 23:48:41 +02:00
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent;
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason;
2012-06-08 23:48:41 +02:00
import com.gmail.nossr50.locale.LocaleLoader;
2013-01-30 15:17:50 +01:00
import com.gmail.nossr50.util.Misc;
2012-06-08 23:48:41 +02:00
import com.gmail.nossr50.util.Users;
2013-01-26 23:01:55 +01:00
public final class PartyManager {
private static String partiesFilePath = mcMMO.p.getDataFolder().getPath() + File.separator + "FlatFileStuff" + File.separator + "parties.yml";
2012-06-08 23:48:41 +02:00
private static List<Party> parties = new ArrayList<Party>();
private PartyManager() {}
public static boolean changeOrJoinParty(McMMOPlayer mcMMOPlayer, Player player, Party oldParty, String newPartyName) {
if (mcMMOPlayer.inParty()) {
if (!handlePartyChangeEvent(player, oldParty.getName(), newPartyName, EventReason.CHANGED_PARTIES)) {
return false;
}
removeFromParty(player, oldParty);
}
else if (!handlePartyChangeEvent(player, null, newPartyName, EventReason.JOINED_PARTY)) {
return false;
}
return true;
}
2012-06-08 23:48:41 +02:00
/**
* 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
*/
2013-01-26 23:01:55 +01:00
public static boolean inSameParty(Player firstPlayer, Player secondPlayer) {
McMMOPlayer firstMcMMOPlayer = Users.getPlayer(firstPlayer);
McMMOPlayer secondMcMMOPlayer = Users.getPlayer(secondPlayer);
if (firstMcMMOPlayer == null || secondMcMMOPlayer == null) {
return false;
}
Party firstParty = firstMcMMOPlayer.getParty();
Party secondParty = secondMcMMOPlayer.getParty();
2012-06-28 14:14:30 +02:00
if (firstParty == null || secondParty == null || firstParty != secondParty) {
2012-06-08 23:48:41 +02:00
return false;
}
return true;
}
2013-01-30 15:17:50 +01:00
/**
* 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;
}
2012-06-08 23:48:41 +02:00
/**
* Notify party members when a player joins
*
* @param player The player that joins
2012-06-08 23:48:41 +02:00
* @param party The concerned party
*/
private static void informPartyMembersJoin(OfflinePlayer player, Party party) {
2012-06-08 23:48:41 +02:00
for (Player member : party.getOnlineMembers()) {
if (!member.equals(player)) {
member.sendMessage(LocaleLoader.getString("Party.InformedOnJoin", player.getName()));
2012-06-08 23:48:41 +02:00
}
}
}
/**
* Notify party members when a party member quits.
*
* @param player The player that quits
2012-06-08 23:48:41 +02:00
* @param party The concerned party
*/
private static void informPartyMembersQuit(OfflinePlayer player, Party party) {
2012-06-08 23:48:41 +02:00
for (Player member : party.getOnlineMembers()) {
if (!member.equals(player)) {
member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", player.getName()));
2012-06-08 23:48:41 +02:00
}
}
}
/**
* 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<OfflinePlayer> getAllMembers(Player player) {
Party party = Users.getPlayer(player).getParty();
2012-06-08 23:48:41 +02:00
if (party == null) {
2012-06-11 07:45:14 +02:00
return null;
2012-06-08 23:48:41 +02:00
}
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
*/
2013-01-26 23:01:55 +01:00
public static List<Player> getOnlineMembers(String partyName) {
2012-06-08 23:48:41 +02:00
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
*/
2013-01-26 23:01:55 +01:00
public static List<Player> getOnlineMembers(Player player) {
2012-06-08 23:48:41 +02:00
return getOnlineMembers(player.getName());
}
/**
* Retrieve a party by its name
*
* @param partyName The party name
* @return the existing party, null otherwise
*/
2013-01-26 23:01:55 +01:00
public static Party getParty(String partyName) {
2012-06-08 23:48:41 +02:00
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
*/
2013-01-26 23:01:55 +01:00
public static Party getPlayerParty(String playerName) {
for (Party party : parties) {
if (party.getMembers().contains(playerName)) {
return party;
}
}
return null;
}
2012-06-08 23:48:41 +02:00
/**
2012-06-11 08:20:39 +02:00
* Get a list of all current parties.
2012-06-08 23:48:41 +02:00
*
* @return the list of parties.
*/
2013-01-26 23:01:55 +01:00
public static List<Party> getParties() {
2012-06-08 23:48:41 +02:00
return parties;
}
/**
* Remove a player from a party.
*
* @param player The player to remove
2012-06-11 07:45:14 +02:00
* @param party The party
2012-06-08 23:48:41 +02:00
*/
public static void removeFromParty(OfflinePlayer player, Party party) {
List<OfflinePlayer> members = party.getMembers();
2012-06-08 23:48:41 +02:00
members.remove(player);
2012-06-08 23:48:41 +02:00
2012-06-11 07:45:14 +02:00
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(player)) {
String newLeader = members.get(0).toString();
party.setLeader(newLeader);
2012-06-08 23:48:41 +02:00
}
informPartyMembersQuit(player, party);
2012-06-08 23:48:41 +02:00
}
McMMOPlayer mcMMOPlayer = Users.getPlayer(player.getName());
if (mcMMOPlayer != null) {
mcMMOPlayer.removeParty();
2012-06-11 07:45:14 +02:00
}
2012-06-08 23:48:41 +02:00
}
/**
* Disband a party. Kicks out all members and removes the party.
*
* @param party The party to remove
*/
2013-01-26 23:01:55 +01:00
public static void disbandParty(Party party) {
List<OfflinePlayer> members = party.getMembers();
for (OfflinePlayer member : members) {
McMMOPlayer mcMMOPlayer = Users.getPlayer(member.getName());
if (mcMMOPlayer != null) {
mcMMOPlayer.removeParty();
}
}
members.clear();
parties.remove(party);
}
/**
* Create a new party
*
* @param player The player to add to the party
* @param mcMMOPlayer 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, McMMOPlayer mcMMOPlayer, String partyName, String password) {
partyName = partyName.replace(".", "");
Party party = getParty(partyName);
if (party == null) {
party = new Party();
party.setName(partyName);
party.setLeader(player.getName());
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);
2013-02-02 08:55:49 +01:00
player.sendMessage(LocaleLoader.getString("Party.Password.Set", password));
}
parties.add(party);
}
else {
player.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists"));
return;
}
2013-02-02 08:55:49 +01:00
player.sendMessage(LocaleLoader.getString("Commands.Party.Create", party.getName()));
addToParty(player, mcMMOPlayer, party);
}
2012-06-08 23:48:41 +02:00
/**
* Add a player to a party.
*
* @param player The player to add to the party
* @param mcMMOPlayer The player to add to the party
2012-06-08 23:48:41 +02:00
* @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, McMMOPlayer mcMMOPlayer, String partyName, String password) {
2012-06-09 20:32:35 +02:00
partyName = partyName.replace(".", "");
2012-06-08 23:48:41 +02:00
Party party = getParty(partyName);
if (party == null) {
party = new Party();
party.setName(partyName);
party.setLeader(player.getName());
2012-06-09 20:32:35 +02:00
2012-06-08 23:48:41 +02:00
if (password != null) {
party.setPassword(password);
party.setLocked(true);
}
parties.add(party);
}
2013-02-08 03:46:12 +01:00
else if (!checkPartyPassword(player, party, password)) {
2012-06-11 07:45:14 +02:00
return;
}
2013-02-02 08:55:49 +01:00
player.sendMessage(LocaleLoader.getString("Commands.Party.Join", party.getName()));
addToParty(player, mcMMOPlayer, party);
2012-06-11 07:45:14 +02:00
}
/**
* Check if a player can join a party
*
* @param player The player trying to join a party
* @param party The party
* @param password The password provided by the player
* @return true if the player can join the party
*/
2013-02-08 03:46:12 +01:00
public static boolean checkPartyPassword(Player player, Party party, String password) {
// Don't care about passwords if it isn't locked
2012-06-11 07:45:14 +02:00
if (party.isLocked()) {
String partyPassword = party.getPassword();
if (partyPassword != null) {
if (password == null) {
player.sendMessage(LocaleLoader.getString("Party.Password.None"));
2012-06-11 07:45:14 +02:00
return false;
2012-06-08 23:48:41 +02:00
}
2012-06-11 07:45:14 +02:00
else if (!password.equals(partyPassword)) {
player.sendMessage(LocaleLoader.getString("Party.Password.Incorrect"));
2012-06-11 07:45:14 +02:00
return false;
2012-06-08 23:48:41 +02:00
}
}
2012-06-11 07:45:14 +02:00
else {
player.sendMessage(LocaleLoader.getString("Party.Locked"));
2012-06-11 07:45:14 +02:00
return false;
}
2012-06-08 23:48:41 +02:00
}
2012-06-11 07:45:14 +02:00
return true;
2012-06-08 23:48:41 +02:00
}
/**
* Accept a party invitation
*
* @param Player The plaer to add to the party
* @param mcMMOPlayer The player to add to the party
2012-06-08 23:48:41 +02:00
*/
public static void joinInvitedParty(Player player, McMMOPlayer mcMMOPlayer) {
Party invite = mcMMOPlayer.getPartyInvite();
2012-06-11 07:45:14 +02:00
if (!parties.contains(invite)) {
parties.add(invite);
2012-06-08 23:48:41 +02:00
}
2013-02-02 08:55:49 +01:00
player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", invite.getName()));
mcMMOPlayer.removePartyInvite();
addToParty(player, mcMMOPlayer, invite);
2012-06-11 07:45:14 +02:00
}
/**
* Add a player to a party
*
* @param player The player to add to a party
* @param mcMMOPlayer The player to add to the party
2012-06-11 07:45:14 +02:00
* @param party The party
*/
public static void addToParty(OfflinePlayer player, McMMOPlayer mcMMOPlayer, Party party) {
informPartyMembersJoin(player, party);
mcMMOPlayer.setParty(party);
party.getMembers().add(player);
2012-06-08 23:48:41 +02:00
}
/**
* Get the leader of a party.
*
* @param partyName The party name
* @return the leader of the party
*/
2013-01-26 23:01:55 +01:00
public static String getPartyLeader(String partyName) {
2012-06-08 23:48:41 +02:00
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
2012-06-11 07:45:14 +02:00
* @param party The party
2012-06-08 23:48:41 +02:00
*/
2013-01-26 23:01:55 +01:00
public static void setPartyLeader(String playerName, Party party) {
2012-06-08 23:48:41 +02:00
String leaderName = party.getLeader();
for (Player member : party.getOnlineMembers()) {
if (member.getName().equals(playerName)) {
member.sendMessage(LocaleLoader.getString("Party.Owner.Player"));
2012-06-08 23:48:41 +02:00
}
else if (member.getName().equals(leaderName)) {
member.sendMessage(LocaleLoader.getString("Party.Owner.NotLeader"));
2012-06-08 23:48:41 +02:00
}
else {
2013-02-02 08:55:49 +01:00
member.sendMessage(LocaleLoader.getString("Party.Owner.New", playerName));
2012-06-08 23:48:41 +02:00
}
}
party.setLeader(playerName);
}
/**
* Check if a player can invite others to his party.
2012-06-08 23:48:41 +02:00
*
* @param player The player to check
* @param mcMMOPlayer The player to check
2012-06-11 07:45:14 +02:00
* @return true if the player can invite
2012-06-08 23:48:41 +02:00
*/
2013-02-08 22:56:54 +01:00
public static boolean canInvite(Player player, Party party) {
if (party.isLocked() && !party.getLeader().equals(player.getName())) {
2012-06-08 23:48:41 +02:00
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
*/
2013-01-26 23:01:55 +01:00
public static boolean isParty(String partyName) {
2012-06-08 23:48:41 +02:00
for (Party party : parties) {
if (party.getName().equals(partyName)) {
return true;
}
}
return false;
}
/**
2012-06-09 20:11:11 +02:00
* Load party file.
2012-06-08 23:48:41 +02:00
*/
public static void loadParties() {
2012-06-08 23:48:41 +02:00
File file = new File(partiesFilePath);
2012-12-24 22:56:25 +01:00
2012-06-08 23:48:41 +02:00
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"));
2013-02-08 17:53:49 +01:00
party.setXpShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ExpShareMode")));
party.setItemShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ItemShareMode")));
List<String> memberNames = partiesFile.getStringList(partyName + ".Members");
List<OfflinePlayer> members = party.getMembers();
Server server = mcMMO.p.getServer();
for (String memberName : memberNames) {
members.add(server.getOfflinePlayer(memberName));
}
2012-06-08 23:48:41 +02:00
parties.add(party);
}
}
/**
2012-06-09 20:11:11 +02:00
* Save party file.
2012-06-08 23:48:41 +02:00
*/
2013-01-26 23:01:55 +01:00
public static void saveParties() {
2012-06-08 23:48:41 +02:00
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.getXpShareMode().toString());
partiesFile.set(partyName + ".ItemShareMode", party.getItemShareMode().toString());
2013-02-09 15:26:41 +01:00
List<String> memberNames = new ArrayList<String>();
for (OfflinePlayer member : party.getMembers()) {
memberNames.add(member.getName());
}
partiesFile.set(partyName + ".Members", memberNames);
2012-06-08 23:48:41 +02:00
try {
partiesFile.save(new File(partiesFilePath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Handle party change event.
*
* @param player The player changing parties
* @param oldPartyName The name of the old party
* @param newPartyName The name of the new party
* @param reason The reason for changing parties
* @return true if the change event was successful, false otherwise
*/
public static boolean handlePartyChangeEvent(Player player, String oldPartyName, String newPartyName, EventReason reason) {
McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, oldPartyName, newPartyName, reason);
mcMMO.p.getServer().getPluginManager().callEvent(event);
return !event.isCancelled();
}
2012-06-08 23:48:41 +02:00
}