More cleanup to PartyManager.

This commit is contained in:
GJ 2013-08-26 11:55:35 -04:00
parent 94a5bef62c
commit a17d1d3a2f
8 changed files with 98 additions and 112 deletions

View File

@ -77,13 +77,12 @@ 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(playerName, partyName); party = new Party(player.getName(), partyName);
} }
PartyManager.addToParty(playerName, UserManager.getPlayer(player), party); PartyManager.addToParty(UserManager.getPlayer(player), party);
} }
/** /**
@ -94,7 +93,7 @@ public final class PartyAPI {
* @param player The player to remove * @param player The player to remove
*/ */
public static void removeFromParty(Player player) { public static void removeFromParty(Player player) {
PartyManager.removeFromParty(player, UserManager.getPlayer(player).getParty()); PartyManager.removeFromParty(UserManager.getPlayer(player));
} }
/** /**

View File

@ -28,7 +28,7 @@ public class PartyAcceptCommand implements CommandExecutor {
return true; return true;
} }
PartyManager.joinInvitedParty(player, mcMMOPlayer); PartyManager.joinInvitedParty(mcMMOPlayer);
return true; return true;
default: default:

View File

@ -29,7 +29,7 @@ public class PartyCreateCommand implements CommandExecutor {
return true; return true;
} }
PartyManager.createParty(player, mcMMOPlayer, args[1], getPassword(args)); PartyManager.createParty(mcMMOPlayer, args[1], getPassword(args));
return true; return true;
default: default:

View File

@ -18,16 +18,17 @@ public class PartyInviteCommand implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
switch (args.length) { switch (args.length) {
case 2: case 2:
String playerName = Misc.getMatchedPlayerName(args[1]); String targetName = Misc.getMatchedPlayerName(args[1]);
McMMOPlayer mcMMOTarget = UserManager.getPlayer(playerName); McMMOPlayer mcMMOTarget = UserManager.getPlayer(targetName);
if (!CommandUtils.checkPlayerExistence(sender, playerName, mcMMOTarget)) { if (!CommandUtils.checkPlayerExistence(sender, targetName, mcMMOTarget)) {
return false; return false;
} }
Player target = mcMMOTarget.getPlayer(); Player target = mcMMOTarget.getPlayer();
Player player = (Player) sender; Player player = (Player) sender;
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player); McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
String playerName = player.getName();
if (player.equals(target)) { if (player.equals(target)) {
sender.sendMessage(LocaleLoader.getString("Party.Invite.Self")); sender.sendMessage(LocaleLoader.getString("Party.Invite.Self"));
@ -35,13 +36,13 @@ public class PartyInviteCommand implements CommandExecutor {
} }
if (PartyManager.inSameParty(player, target)) { if (PartyManager.inSameParty(player, target)) {
sender.sendMessage(LocaleLoader.getString("Party.Player.InSameParty", target.getName())); sender.sendMessage(LocaleLoader.getString("Party.Player.InSameParty", targetName));
return true; return true;
} }
Party playerParty = mcMMOPlayer.getParty(); Party playerParty = mcMMOPlayer.getParty();
if (!PartyManager.canInvite(player, playerParty)) { if (!PartyManager.canInvite(playerName, playerParty)) {
player.sendMessage(LocaleLoader.getString("Party.Locked")); player.sendMessage(LocaleLoader.getString("Party.Locked"));
return true; return true;
} }
@ -49,7 +50,7 @@ public class PartyInviteCommand implements CommandExecutor {
mcMMOTarget.setPartyInvite(playerParty); mcMMOTarget.setPartyInvite(playerParty);
sender.sendMessage(LocaleLoader.getString("Commands.Invite.Success")); sender.sendMessage(LocaleLoader.getString("Commands.Invite.Success"));
target.sendMessage(LocaleLoader.getString("Commands.Party.Invite.0", playerParty.getName(), player.getName())); target.sendMessage(LocaleLoader.getString("Commands.Party.Invite.0", playerParty.getName(), playerName));
target.sendMessage(LocaleLoader.getString("Commands.Party.Invite.1")); target.sendMessage(LocaleLoader.getString("Commands.Party.Invite.1"));
return true; return true;

View File

@ -41,12 +41,15 @@ public class PartyJoinCommand implements CommandExecutor {
return true; return true;
} }
String partyName = targetParty.getName();
// Changing parties // Changing parties
if (!PartyManager.changeOrJoinParty(mcMMOPlayer, targetParty.getName())) { if (!PartyManager.changeOrJoinParty(mcMMOPlayer, partyName)) {
return true; return true;
} }
PartyManager.joinParty(player, mcMMOPlayer, targetParty, password); player.sendMessage(LocaleLoader.getString("Commands.Party.Join", partyName));
PartyManager.addToParty(mcMMOPlayer, targetParty);
return true; return true;
default: default:

View File

@ -6,6 +6,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason; import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.party.PartyManager;
@ -17,13 +18,14 @@ public class PartyQuitCommand implements CommandExecutor {
switch (args.length) { switch (args.length) {
case 1: case 1:
Player player = (Player) sender; Player player = (Player) sender;
Party playerParty = UserManager.getPlayer(player).getParty(); McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
Party playerParty = mcMMOPlayer.getParty();
if (!PartyManager.handlePartyChangeEvent(player, playerParty.getName(), null, EventReason.LEFT_PARTY)) { if (!PartyManager.handlePartyChangeEvent(player, playerParty.getName(), null, EventReason.LEFT_PARTY)) {
return true; return true;
} }
PartyManager.removeFromParty(player, playerParty); PartyManager.removeFromParty(mcMMOPlayer);
sender.sendMessage(LocaleLoader.getString("Commands.Party.Leave")); sender.sendMessage(LocaleLoader.getString("Commands.Party.Leave"));
return true; return true;

View File

@ -97,7 +97,7 @@ public class McMMOPlayer {
this.player = player; this.player = player;
profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true); profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true);
party = PartyManager.getPlayerParty(playerName); party = PartyManager.getParty(player);
/* /*
* I'm using this method because it makes code shorter and safer (we don't have to add all SkillTypes manually), * I'm using this method because it makes code shorter and safer (we don't have to add all SkillTypes manually),

View File

@ -62,7 +62,7 @@ public final class PartyManager {
return false; return false;
} }
removeFromParty(player, oldParty); removeFromParty(mcMMOPlayer);
} }
else if (!handlePartyChangeEvent(player, null, newPartyName, EventReason.JOINED_PARTY)) { else if (!handlePartyChangeEvent(player, null, newPartyName, EventReason.JOINED_PARTY)) {
return false; return false;
@ -121,9 +121,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 = getPlayerParty(player.getName()); Party party = getParty(player);
return party == null ? null : party.getMembers(); return party == null ? new LinkedHashSet<String>() : party.getMembers();
} }
/** /**
@ -143,11 +143,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) {
return getOnlineMembers(getPlayerParty(player.getName())); return getOnlineMembers(getParty(player));
} }
private static List<Player> getOnlineMembers(Party party) { private static List<Player> getOnlineMembers(Party party) {
return party == null ? null : party.getOnlineMembers(); return party == null ? new ArrayList<Player>() : party.getOnlineMembers();
} }
/** /**
@ -167,17 +167,21 @@ public final class PartyManager {
} }
/** /**
* Retrieve a party by a member name * Retrieve a party by member
* *
* @param playerName The member name * @param player The member
* @return the existing party, null otherwise * @return the existing party, null otherwise
*/ */
public static Party getPlayerParty(String playerName) { public static Party getParty(Player player) {
for (Party party : parties) { McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
for (String memberName : party.getMembers()) {
if (memberName.equalsIgnoreCase(playerName)) { if (mcMMOPlayer != null) {
return party; return mcMMOPlayer.getParty();
} }
for (Party party : parties) {
if (party.getMembers().contains(player.getName())) {
return party;
} }
} }
@ -211,14 +215,21 @@ public final class PartyManager {
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(playerName)) { if (party.getLeader().equalsIgnoreCase(playerName)) {
String newLeader = members.iterator().next(); setPartyLeader(members.iterator().next(), party);
party.setLeader(newLeader);
} }
informPartyMembersQuit(party, playerName); informPartyMembersQuit(party, playerName);
} }
}
processPartyLeaving(UserManager.getPlayer(player)); /**
* Remove a player from a party.
*
* @param mcMMOPlayer The player to remove
*/
public static void removeFromParty(McMMOPlayer mcMMOPlayer) {
removeFromParty(mcMMOPlayer.getPlayer(), mcMMOPlayer.getParty());
processPartyLeaving(mcMMOPlayer);
} }
/** /**
@ -242,18 +253,11 @@ public final class PartyManager {
* @param partyName The party to add the player to * @param partyName The party to add the player to
* @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 createParty(Player player, McMMOPlayer mcMMOPlayer, String partyName, String password) { public static void createParty(McMMOPlayer mcMMOPlayer, String partyName, String password) {
partyName = partyName.replace(".", ""); Player player = mcMMOPlayer.getPlayer();
Party party = getParty(partyName);
String playerName = player.getName(); String playerName = player.getName();
if (party != null) { Party party = new Party(playerName, partyName.replace(".", ""), password);
player.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists"));
return;
}
party = new Party(playerName, partyName, password);
if (password != null) { if (password != null) {
player.sendMessage(LocaleLoader.getString("Party.Password.Set", password)); player.sendMessage(LocaleLoader.getString("Party.Password.Set", password));
@ -262,24 +266,7 @@ public final class PartyManager {
parties.add(party); parties.add(party);
player.sendMessage(LocaleLoader.getString("Commands.Party.Create", party.getName())); player.sendMessage(LocaleLoader.getString("Commands.Party.Create", party.getName()));
addToParty(playerName, mcMMOPlayer, party); addToParty(mcMMOPlayer, party);
}
/**
* Add a player to a party.
*
* @param player The player to add to the party
* @param mcMMOPlayer The player to add to the party
* @param party 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, Party party, String password) {
if (!checkPartyPassword(player, party, password) || mcMMOPlayer.getParty() == party) {
return;
}
player.sendMessage(LocaleLoader.getString("Commands.Party.Join", party.getName()));
addToParty(player.getName(), mcMMOPlayer, party);
} }
/** /**
@ -291,10 +278,7 @@ 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) {
if (!party.isLocked()) { if (party.isLocked()) {
return true;
}
String partyPassword = party.getPassword(); String partyPassword = party.getPassword();
if (partyPassword == null) { if (partyPassword == null) {
@ -306,13 +290,14 @@ public final class PartyManager {
player.sendMessage(LocaleLoader.getString("Party.Password.None")); player.sendMessage(LocaleLoader.getString("Party.Password.None"));
return false; return false;
} }
else if (!password.equals(partyPassword)) {
if (!password.equals(partyPassword)) {
player.sendMessage(LocaleLoader.getString("Party.Password.Incorrect")); player.sendMessage(LocaleLoader.getString("Party.Password.Incorrect"));
return false; return false;
} }
else {
return true;
} }
return true;
} }
/** /**
@ -321,37 +306,30 @@ public final class PartyManager {
* @param player The player to add to the party * @param player The player to add to the party
* @param mcMMOPlayer The player to add to the party * @param mcMMOPlayer The player to add to the party
*/ */
public static void joinInvitedParty(Player player, McMMOPlayer mcMMOPlayer) { public static void joinInvitedParty(McMMOPlayer mcMMOPlayer) {
Party invite = mcMMOPlayer.getPartyInvite(); Party invite = mcMMOPlayer.getPartyInvite();
if (mcMMOPlayer.getParty() == invite) { // // Pretty sure this isn't possible
return; // if (!parties.contains(invite)) {
} // parties.add(invite);
// }
if (!parties.contains(invite)) { mcMMOPlayer.getPlayer().sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", invite.getName()));
parties.add(invite);
}
player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", invite.getName()));
mcMMOPlayer.removePartyInvite(); mcMMOPlayer.removePartyInvite();
addToParty(player.getName(), mcMMOPlayer, invite); addToParty(mcMMOPlayer, invite);
} }
/** /**
* Add a player to a party * Add a player to a party
* *
* @param playerName The name of the player to add to a party
* @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(String playerName, McMMOPlayer mcMMOPlayer, Party party) { public static void addToParty(McMMOPlayer mcMMOPlayer, Party party) {
if (mcMMOPlayer.getParty() == party) { String playerName = mcMMOPlayer.getPlayer().getName();
return;
}
informPartyMembersJoin(party, playerName); informPartyMembersJoin(party, playerName);
mcMMOPlayer.setParty(party); mcMMOPlayer.setParty(party);
party.getMembers().add(playerName); party.getMembers().add(playerName);
} }
@ -364,11 +342,7 @@ public final class PartyManager {
public static String getPartyLeader(String partyName) { public static String getPartyLeader(String partyName) {
Party party = getParty(partyName); Party party = getParty(partyName);
if (party == null) { return party == null ? null : party.getLeader();
return null;
}
return party.getLeader();
} }
/** /**
@ -381,10 +355,12 @@ public final class PartyManager {
String leaderName = party.getLeader(); String leaderName = party.getLeader();
for (Player member : party.getOnlineMembers()) { for (Player member : party.getOnlineMembers()) {
if (member.getName().equalsIgnoreCase(playerName)) { String memberName = member.getName();
if (memberName.equalsIgnoreCase(playerName)) {
member.sendMessage(LocaleLoader.getString("Party.Owner.Player")); member.sendMessage(LocaleLoader.getString("Party.Owner.Player"));
} }
else if (member.getName().equalsIgnoreCase(leaderName)) { else if (memberName.equalsIgnoreCase(leaderName)) {
member.sendMessage(LocaleLoader.getString("Party.Owner.NotLeader")); member.sendMessage(LocaleLoader.getString("Party.Owner.NotLeader"));
} }
else { else {
@ -398,12 +374,12 @@ public final class PartyManager {
/** /**
* Check if a player can invite others to his party. * Check if a player can invite others to his party.
* *
* @param player The player to check * @param playerName The name of the player to check
* @param party The party to check * @param party The party to check
* @return true if the player can invite * @return true if the player can invite
*/ */
public static boolean canInvite(Player player, Party party) { public static boolean canInvite(String playerName, Party party) {
return !(party.isLocked() && !party.getLeader().equalsIgnoreCase(player.getName())); return !party.isLocked() || party.getLeader().equalsIgnoreCase(playerName);
} }
/** /**
@ -508,6 +484,17 @@ public final class PartyManager {
return !event.isCancelled(); return !event.isCancelled();
} }
/**
* Remove party data from the mcMMOPlayer.
*
* @param mcMMOPlayer The player to remove party data from.
*/
public static void processPartyLeaving(McMMOPlayer mcMMOPlayer) {
mcMMOPlayer.removeParty();
mcMMOPlayer.setPartyChat(false);
mcMMOPlayer.setItemShareModifier(10);
}
/** /**
* Notify party members when a player joins. * Notify party members when a player joins.
* *
@ -531,10 +518,4 @@ public final class PartyManager {
member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", playerName)); member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", playerName));
} }
} }
private static void processPartyLeaving(McMMOPlayer mcMMOPlayer) {
mcMMOPlayer.removeParty();
mcMMOPlayer.setPartyChat(false);
mcMMOPlayer.setItemShareModifier(10);
}
} }