Updates to our parties.

This commit is contained in:
GJ 2013-05-22 13:02:26 -04:00
parent 030c8e59d7
commit aa0b77a64c
3 changed files with 132 additions and 123 deletions

View File

@ -77,14 +77,13 @@ public final class PartyAPI {
*/ */
public static void addToParty(Player player, String partyName) { public static void addToParty(Player player, String partyName) {
Party party = PartyManager.getParty(partyName); Party party = PartyManager.getParty(partyName);
String playerName = player.getName();
if (party == null) { if (party == null) {
party = new Party(); party = new Party(playerName, partyName);
party.setName(partyName);
party.setLeader(player.getName());
} }
PartyManager.addToParty(player, UserManager.getPlayer(player), party); PartyManager.addToParty(playerName, UserManager.getPlayer(player), party);
} }
/** /**

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
@ -12,7 +11,8 @@ import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.ShareHandler.ShareMode; import com.gmail.nossr50.party.ShareHandler.ShareMode;
public class Party { public class Party {
private LinkedHashSet<String> members = new LinkedHashSet<String>(); private final LinkedHashSet<String> members = new LinkedHashSet<String>();
private String leader; private String leader;
private String name; private String name;
private String password; private String password;
@ -27,6 +27,30 @@ public class Party {
private boolean shareWoodcuttingDrops = true; private boolean shareWoodcuttingDrops = true;
private boolean shareMiscDrops = true; private boolean shareMiscDrops = true;
public Party(String name) {
this.name = name;
}
public Party(String leader, String name) {
this.leader = leader;
this.name = name;
this.locked = true;
}
public Party(String leader, String name, String password) {
this.leader = leader;
this.name = name;
this.password = password;
this.locked = true;
}
public Party(String leader, String name, String password, boolean locked) {
this.leader = leader;
this.name = name;
this.password = password;
this.locked = locked;
}
public LinkedHashSet<String> getMembers() { public LinkedHashSet<String> getMembers() {
return members; return members;
} }
@ -35,9 +59,10 @@ public class Party {
List<Player> onlineMembers = new ArrayList<Player>(); List<Player> onlineMembers = new ArrayList<Player>();
for (String memberName : members) { for (String memberName : members) {
OfflinePlayer member = mcMMO.p.getServer().getOfflinePlayer(memberName); Player member = mcMMO.p.getServer().getPlayerExact(memberName);
if (member.isOnline()) {
onlineMembers.add(member.getPlayer()); if (member != null) {
onlineMembers.add(member);
} }
} }
@ -166,4 +191,28 @@ public class Party {
return; return;
} }
} }
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Party)) {
return false;
}
Party other = (Party) obj;
if ((this.getName() == null) || (other.getName() == null)) {
return false;
}
return this.getName().equals(other.getName());
}
@Override
public int hashCode() {
return super.hashCode();
}
} }

View File

@ -64,14 +64,7 @@ public final class PartyManager {
return false; return false;
} }
Party firstParty = firstMcMMOPlayer.getParty(); return firstMcMMOPlayer.getParty().equals(secondMcMMOPlayer.getParty());
Party secondParty = secondMcMMOPlayer.getParty();
if (firstParty == null || secondParty == null || firstParty != secondParty) {
return false;
}
return true;
} }
/** /**
@ -83,13 +76,15 @@ public final class PartyManager {
*/ */
public static List<Player> getNearMembers(Player player, Party party, double range) { public static List<Player> getNearMembers(Player player, Party party, double range) {
List<Player> nearMembers = new ArrayList<Player>(); List<Player> nearMembers = new ArrayList<Player>();
if (party != null) { if (party != null) {
for (Player member : party.getOnlineMembers()) { for (Player member : party.getOnlineMembers()) {
if (!player.getName().equalsIgnoreCase(member.getName()) && !member.isDead() && Misc.isNear(player.getLocation(), member.getLocation(), range)) { if (!player.equals(member) && member.isValid() && Misc.isNear(player.getLocation(), member.getLocation(), range)) {
nearMembers.add(member); nearMembers.add(member);
} }
} }
} }
return nearMembers; return nearMembers;
} }
@ -100,13 +95,9 @@ public final class PartyManager {
* @return all the players in the player's party * @return all the players in the player's party
*/ */
public static LinkedHashSet<String> getAllMembers(Player player) { public static LinkedHashSet<String> getAllMembers(Player player) {
Party party = UserManager.getPlayer(player).getParty(); Party party = getPlayerParty(player.getName());
if (party == null) { return party == null ? null : party.getMembers();
return null;
}
return party.getMembers();
} }
/** /**
@ -116,13 +107,7 @@ public final class PartyManager {
* @return all online players in this party * @return all online players in this party
*/ */
public static List<Player> getOnlineMembers(String partyName) { public static List<Player> getOnlineMembers(String partyName) {
Party party = getParty(partyName); return getOnlineMembers(getParty(partyName));
if (party == null) {
return null;
}
return party.getOnlineMembers();
} }
/** /**
@ -132,13 +117,11 @@ public final class PartyManager {
* @return all online players in this party * @return all online players in this party
*/ */
public static List<Player> getOnlineMembers(Player player) { public static List<Player> getOnlineMembers(Player player) {
Party party = getPlayerParty(player.getName()); return getOnlineMembers(getPlayerParty(player.getName()));
}
if (party == null) { private static List<Player> getOnlineMembers(Party party) {
return null; return party == null ? null : party.getOnlineMembers();
}
return getOnlineMembers(party.getName());
} }
/** /**
@ -192,29 +175,24 @@ public final class PartyManager {
*/ */
public static void removeFromParty(OfflinePlayer player, Party party) { public static void removeFromParty(OfflinePlayer player, Party party) {
LinkedHashSet<String> members = party.getMembers(); LinkedHashSet<String> members = party.getMembers();
String playerName = player.getName();
members.remove(player.getName()); members.remove(playerName);
if (members.isEmpty()) { if (members.isEmpty()) {
parties.remove(party); parties.remove(party);
} }
else { else {
// If the leaving player was the party leader, appoint a new leader from the party members // If the leaving player was the party leader, appoint a new leader from the party members
if (party.getLeader().equalsIgnoreCase(player.getName())) { if (party.getLeader().equalsIgnoreCase(playerName)) {
String newLeader = members.iterator().next(); String newLeader = members.iterator().next();
party.setLeader(newLeader); party.setLeader(newLeader);
} }
informPartyMembersQuit(player, party); informPartyMembersQuit(party, playerName);
} }
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player.getName()); processPartyLeaving(UserManager.getPlayer(player));
if (mcMMOPlayer != null) {
mcMMOPlayer.removeParty();
mcMMOPlayer.setPartyChat(false);
mcMMOPlayer.setItemShareModifier(10);
}
} }
/** /**
@ -223,19 +201,10 @@ public final class PartyManager {
* @param party The party to remove * @param party The party to remove
*/ */
public static void disbandParty(Party party) { public static void disbandParty(Party party) {
LinkedHashSet<String> members = party.getMembers(); for (String memberName : party.getMembers()) {
processPartyLeaving(UserManager.getPlayer(memberName));
for (String memberName : members) {
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(memberName);
if (mcMMOPlayer != null) {
mcMMOPlayer.removeParty();
mcMMOPlayer.setPartyChat(false);
mcMMOPlayer.setItemShareModifier(10);
}
} }
members.clear();
parties.remove(party); parties.remove(party);
} }
@ -249,29 +218,25 @@ public final class PartyManager {
*/ */
public static void createParty(Player player, McMMOPlayer mcMMOPlayer, String partyName, String password) { public static void createParty(Player player, McMMOPlayer mcMMOPlayer, String partyName, String password) {
partyName = partyName.replace(".", ""); partyName = partyName.replace(".", "");
Party party = getParty(partyName); Party party = getParty(partyName);
String playerName = player.getName();
if (party == null) { 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);
player.sendMessage(LocaleLoader.getString("Party.Password.Set", password));
}
parties.add(party);
}
else {
player.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists")); player.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists"));
return; return;
} }
party = new Party(playerName, partyName, password);
if (password != null) {
player.sendMessage(LocaleLoader.getString("Party.Password.Set", password));
}
parties.add(party);
player.sendMessage(LocaleLoader.getString("Commands.Party.Create", party.getName())); player.sendMessage(LocaleLoader.getString("Commands.Party.Create", party.getName()));
addToParty(player, mcMMOPlayer, party); addToParty(playerName, mcMMOPlayer, party);
} }
/** /**
@ -283,16 +248,12 @@ public final class PartyManager {
* @param password the password for this party, null if there was no password * @param password the password for this party, null if there was no password
*/ */
public static void joinParty(Player player, McMMOPlayer mcMMOPlayer, Party party, String password) { public static void joinParty(Player player, McMMOPlayer mcMMOPlayer, Party party, String password) {
if (!checkPartyPassword(player, party, password)) { if (!checkPartyPassword(player, party, password) || mcMMOPlayer.getParty() == party) {
return;
}
if (mcMMOPlayer.getParty() == party) {
return; return;
} }
player.sendMessage(LocaleLoader.getString("Commands.Party.Join", party.getName())); player.sendMessage(LocaleLoader.getString("Commands.Party.Join", party.getName()));
addToParty(player, mcMMOPlayer, party); addToParty(player.getName(), mcMMOPlayer, party);
} }
/** /**
@ -304,27 +265,28 @@ public final class PartyManager {
* @return true if the player can join the party * @return true if the player can join the party
*/ */
public static boolean checkPartyPassword(Player player, Party party, String password) { public static boolean checkPartyPassword(Player player, Party party, String password) {
// Don't care about passwords if it isn't locked if (!party.isLocked()) {
if (party.isLocked()) { return true;
String partyPassword = party.getPassword();
if (partyPassword != null) {
if (password == null) {
player.sendMessage(LocaleLoader.getString("Party.Password.None"));
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; String partyPassword = party.getPassword();
if (partyPassword == null) {
player.sendMessage(LocaleLoader.getString("Party.Locked"));
return false;
}
if (password == null) {
player.sendMessage(LocaleLoader.getString("Party.Password.None"));
return false;
}
else if (!password.equals(partyPassword)) {
player.sendMessage(LocaleLoader.getString("Party.Password.Incorrect"));
return false;
}
else {
return true;
}
} }
/** /**
@ -346,7 +308,7 @@ public final class PartyManager {
player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", invite.getName())); player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", invite.getName()));
mcMMOPlayer.removePartyInvite(); mcMMOPlayer.removePartyInvite();
addToParty(player, mcMMOPlayer, invite); addToParty(player.getName(), mcMMOPlayer, invite);
} }
/** /**
@ -356,15 +318,15 @@ public final class PartyManager {
* @param mcMMOPlayer The player to add to the party * @param mcMMOPlayer The player to add to the party
* @param party The party * @param party The party
*/ */
public static void addToParty(OfflinePlayer player, McMMOPlayer mcMMOPlayer, Party party) { public static void addToParty(String playerName, McMMOPlayer mcMMOPlayer, Party party) {
if (mcMMOPlayer.getParty() == party) { if (mcMMOPlayer.getParty() == party) {
return; return;
} }
informPartyMembersJoin(player, party); informPartyMembersJoin(party, playerName);
mcMMOPlayer.setParty(party); mcMMOPlayer.setParty(party);
party.getMembers().add(player.getName()); party.getMembers().add(playerName);
} }
/** /**
@ -429,13 +391,7 @@ public final class PartyManager {
* @return true if this is a valid party, false otherwise * @return true if this is a valid party, false otherwise
*/ */
public static boolean isParty(String partyName) { public static boolean isParty(String partyName) {
for (Party party : parties) { return getParty(partyName) != null;
if (party.getName().equals(partyName)) {
return true;
}
}
return false;
} }
/** /**
@ -449,9 +405,8 @@ public final class PartyManager {
YamlConfiguration partiesFile = YamlConfiguration.loadConfiguration(partyFile); YamlConfiguration partiesFile = YamlConfiguration.loadConfiguration(partyFile);
for (String partyName : partiesFile.getConfigurationSection("").getKeys(false)) { for (String partyName : partiesFile.getConfigurationSection("").getKeys(false)) {
Party party = new Party(); Party party = new Party(partyName);
party.setName(partyName);
party.setLeader(partiesFile.getString(partyName + ".Leader")); party.setLeader(partiesFile.getString(partyName + ".Leader"));
party.setPassword(partiesFile.getString(partyName + ".Password")); party.setPassword(partiesFile.getString(partyName + ".Password"));
party.setLocked(partiesFile.getBoolean(partyName + ".Locked")); party.setLocked(partiesFile.getBoolean(partyName + ".Locked"));
@ -524,30 +479,36 @@ public final class PartyManager {
} }
/** /**
* Notify party members when a player joins * Notify party members when a player joins.
* *
* @param player The player that joins
* @param party The concerned party * @param party The concerned party
* @param playerName The name of the player that joined
*/ */
private static void informPartyMembersJoin(OfflinePlayer player, Party party) { private static void informPartyMembersJoin(Party party, String playerName) {
for (Player member : party.getOnlineMembers()) { for (Player member : party.getOnlineMembers()) {
if (!member.equals(player)) { member.sendMessage(LocaleLoader.getString("Party.InformedOnJoin", playerName));
member.sendMessage(LocaleLoader.getString("Party.InformedOnJoin", player.getName()));
}
} }
} }
/** /**
* Notify party members when a party member quits. * Notify party members when a party member quits.
* *
* @param player The player that quits
* @param party The concerned party * @param party The concerned party
* @param playerName The name of the player that left
*/ */
private static void informPartyMembersQuit(OfflinePlayer player, Party party) { private static void informPartyMembersQuit(Party party, String playerName) {
for (Player member : party.getOnlineMembers()) { for (Player member : party.getOnlineMembers()) {
if (!member.equals(player)) { member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", playerName));
member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", player.getName()));
}
} }
} }
private static void processPartyLeaving(McMMOPlayer mcMMOPlayer) {
if (mcMMOPlayer == null) {
return;
}
mcMMOPlayer.removeParty();
mcMMOPlayer.setPartyChat(false);
mcMMOPlayer.setItemShareModifier(10);
}
} }