From a17d1d3a2f5925e6da6d7d3182404c9d676ecb6b Mon Sep 17 00:00:00 2001 From: GJ Date: Mon, 26 Aug 2013 11:55:35 -0400 Subject: [PATCH] More cleanup to PartyManager. --- .../java/com/gmail/nossr50/api/PartyAPI.java | 7 +- .../commands/party/PartyAcceptCommand.java | 2 +- .../commands/party/PartyCreateCommand.java | 2 +- .../commands/party/PartyInviteCommand.java | 13 +- .../commands/party/PartyJoinCommand.java | 7 +- .../commands/party/PartyQuitCommand.java | 6 +- .../nossr50/datatypes/player/McMMOPlayer.java | 2 +- .../com/gmail/nossr50/party/PartyManager.java | 171 ++++++++---------- 8 files changed, 98 insertions(+), 112 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/api/PartyAPI.java b/src/main/java/com/gmail/nossr50/api/PartyAPI.java index 9dc078b54..656438c4a 100644 --- a/src/main/java/com/gmail/nossr50/api/PartyAPI.java +++ b/src/main/java/com/gmail/nossr50/api/PartyAPI.java @@ -77,13 +77,12 @@ public final class PartyAPI { */ public static void addToParty(Player player, String partyName) { Party party = PartyManager.getParty(partyName); - String playerName = player.getName(); 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 */ public static void removeFromParty(Player player) { - PartyManager.removeFromParty(player, UserManager.getPlayer(player).getParty()); + PartyManager.removeFromParty(UserManager.getPlayer(player)); } /** diff --git a/src/main/java/com/gmail/nossr50/commands/party/PartyAcceptCommand.java b/src/main/java/com/gmail/nossr50/commands/party/PartyAcceptCommand.java index 7de5c09a8..64405557a 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/PartyAcceptCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/PartyAcceptCommand.java @@ -28,7 +28,7 @@ public class PartyAcceptCommand implements CommandExecutor { return true; } - PartyManager.joinInvitedParty(player, mcMMOPlayer); + PartyManager.joinInvitedParty(mcMMOPlayer); return true; default: diff --git a/src/main/java/com/gmail/nossr50/commands/party/PartyCreateCommand.java b/src/main/java/com/gmail/nossr50/commands/party/PartyCreateCommand.java index f748f3556..8ff81ac31 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/PartyCreateCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/PartyCreateCommand.java @@ -29,7 +29,7 @@ public class PartyCreateCommand implements CommandExecutor { return true; } - PartyManager.createParty(player, mcMMOPlayer, args[1], getPassword(args)); + PartyManager.createParty(mcMMOPlayer, args[1], getPassword(args)); return true; default: diff --git a/src/main/java/com/gmail/nossr50/commands/party/PartyInviteCommand.java b/src/main/java/com/gmail/nossr50/commands/party/PartyInviteCommand.java index 6eab8011f..a82c51458 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/PartyInviteCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/PartyInviteCommand.java @@ -18,16 +18,17 @@ public class PartyInviteCommand implements CommandExecutor { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { switch (args.length) { case 2: - String playerName = Misc.getMatchedPlayerName(args[1]); - McMMOPlayer mcMMOTarget = UserManager.getPlayer(playerName); + String targetName = Misc.getMatchedPlayerName(args[1]); + McMMOPlayer mcMMOTarget = UserManager.getPlayer(targetName); - if (!CommandUtils.checkPlayerExistence(sender, playerName, mcMMOTarget)) { + if (!CommandUtils.checkPlayerExistence(sender, targetName, mcMMOTarget)) { return false; } Player target = mcMMOTarget.getPlayer(); Player player = (Player) sender; McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player); + String playerName = player.getName(); if (player.equals(target)) { sender.sendMessage(LocaleLoader.getString("Party.Invite.Self")); @@ -35,13 +36,13 @@ public class PartyInviteCommand implements CommandExecutor { } if (PartyManager.inSameParty(player, target)) { - sender.sendMessage(LocaleLoader.getString("Party.Player.InSameParty", target.getName())); + sender.sendMessage(LocaleLoader.getString("Party.Player.InSameParty", targetName)); return true; } Party playerParty = mcMMOPlayer.getParty(); - if (!PartyManager.canInvite(player, playerParty)) { + if (!PartyManager.canInvite(playerName, playerParty)) { player.sendMessage(LocaleLoader.getString("Party.Locked")); return true; } @@ -49,7 +50,7 @@ public class PartyInviteCommand implements CommandExecutor { mcMMOTarget.setPartyInvite(playerParty); 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")); return true; diff --git a/src/main/java/com/gmail/nossr50/commands/party/PartyJoinCommand.java b/src/main/java/com/gmail/nossr50/commands/party/PartyJoinCommand.java index 0789a4117..23da0f7d5 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/PartyJoinCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/PartyJoinCommand.java @@ -41,12 +41,15 @@ public class PartyJoinCommand implements CommandExecutor { return true; } + String partyName = targetParty.getName(); + // Changing parties - if (!PartyManager.changeOrJoinParty(mcMMOPlayer, targetParty.getName())) { + if (!PartyManager.changeOrJoinParty(mcMMOPlayer, partyName)) { return true; } - PartyManager.joinParty(player, mcMMOPlayer, targetParty, password); + player.sendMessage(LocaleLoader.getString("Commands.Party.Join", partyName)); + PartyManager.addToParty(mcMMOPlayer, targetParty); return true; default: diff --git a/src/main/java/com/gmail/nossr50/commands/party/PartyQuitCommand.java b/src/main/java/com/gmail/nossr50/commands/party/PartyQuitCommand.java index 962a0ed88..dfb49ec50 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/PartyQuitCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/PartyQuitCommand.java @@ -6,6 +6,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; 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.locale.LocaleLoader; import com.gmail.nossr50.party.PartyManager; @@ -17,13 +18,14 @@ public class PartyQuitCommand implements CommandExecutor { switch (args.length) { case 1: 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)) { return true; } - PartyManager.removeFromParty(player, playerParty); + PartyManager.removeFromParty(mcMMOPlayer); sender.sendMessage(LocaleLoader.getString("Commands.Party.Leave")); return true; diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index ce51a77d1..b78c4a533 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -97,7 +97,7 @@ public class McMMOPlayer { this.player = player; 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), diff --git a/src/main/java/com/gmail/nossr50/party/PartyManager.java b/src/main/java/com/gmail/nossr50/party/PartyManager.java index fb336ab60..aa0829e18 100644 --- a/src/main/java/com/gmail/nossr50/party/PartyManager.java +++ b/src/main/java/com/gmail/nossr50/party/PartyManager.java @@ -62,7 +62,7 @@ public final class PartyManager { return false; } - removeFromParty(player, oldParty); + removeFromParty(mcMMOPlayer); } else if (!handlePartyChangeEvent(player, null, newPartyName, EventReason.JOINED_PARTY)) { return false; @@ -121,9 +121,9 @@ public final class PartyManager { * @return all the players in the player's party */ public static LinkedHashSet getAllMembers(Player player) { - Party party = getPlayerParty(player.getName()); + Party party = getParty(player); - return party == null ? null : party.getMembers(); + return party == null ? new LinkedHashSet() : party.getMembers(); } /** @@ -143,11 +143,11 @@ public final class PartyManager { * @return all online players in this party */ public static List getOnlineMembers(Player player) { - return getOnlineMembers(getPlayerParty(player.getName())); + return getOnlineMembers(getParty(player)); } private static List getOnlineMembers(Party party) { - return party == null ? null : party.getOnlineMembers(); + return party == null ? new ArrayList() : 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 */ - public static Party getPlayerParty(String playerName) { + public static Party getParty(Player player) { + McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player); + + if (mcMMOPlayer != null) { + return mcMMOPlayer.getParty(); + } + for (Party party : parties) { - for (String memberName : party.getMembers()) { - if (memberName.equalsIgnoreCase(playerName)) { - return party; - } + if (party.getMembers().contains(player.getName())) { + return party; } } @@ -211,14 +215,21 @@ public final class PartyManager { else { // If the leaving player was the party leader, appoint a new leader from the party members if (party.getLeader().equalsIgnoreCase(playerName)) { - String newLeader = members.iterator().next(); - party.setLeader(newLeader); + setPartyLeader(members.iterator().next(), party); } 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 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); + public static void createParty(McMMOPlayer mcMMOPlayer, String partyName, String password) { + Player player = mcMMOPlayer.getPlayer(); String playerName = player.getName(); - if (party != null) { - player.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists")); - return; - } - - party = new Party(playerName, partyName, password); + Party party = new Party(playerName, partyName.replace(".", ""), password); if (password != null) { player.sendMessage(LocaleLoader.getString("Party.Password.Set", password)); @@ -262,24 +266,7 @@ public final class PartyManager { parties.add(party); player.sendMessage(LocaleLoader.getString("Commands.Party.Create", party.getName())); - addToParty(playerName, 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); + addToParty(mcMMOPlayer, party); } /** @@ -291,28 +278,26 @@ public final class PartyManager { * @return true if the player can join the party */ public static boolean checkPartyPassword(Player player, Party party, String password) { - if (!party.isLocked()) { - return true; + if (party.isLocked()) { + 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; + } + + if (!password.equals(partyPassword)) { + player.sendMessage(LocaleLoader.getString("Party.Password.Incorrect")); + return false; + } } - 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; - } + return true; } /** @@ -321,37 +306,30 @@ public final class PartyManager { * @param player 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(); - if (mcMMOPlayer.getParty() == invite) { - return; - } +// // Pretty sure this isn't possible +// if (!parties.contains(invite)) { +// parties.add(invite); +// } - if (!parties.contains(invite)) { - parties.add(invite); - } - - player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", invite.getName())); + mcMMOPlayer.getPlayer().sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", invite.getName())); mcMMOPlayer.removePartyInvite(); - addToParty(player.getName(), mcMMOPlayer, invite); + addToParty(mcMMOPlayer, invite); } /** * 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 party The party */ - public static void addToParty(String playerName, McMMOPlayer mcMMOPlayer, Party party) { - if (mcMMOPlayer.getParty() == party) { - return; - } + public static void addToParty(McMMOPlayer mcMMOPlayer, Party party) { + String playerName = mcMMOPlayer.getPlayer().getName(); informPartyMembersJoin(party, playerName); mcMMOPlayer.setParty(party); - party.getMembers().add(playerName); } @@ -364,11 +342,7 @@ public final class PartyManager { public static String getPartyLeader(String partyName) { Party party = getParty(partyName); - if (party == null) { - return null; - } - - return party.getLeader(); + return party == null ? null : party.getLeader(); } /** @@ -381,10 +355,12 @@ public final class PartyManager { String leaderName = party.getLeader(); 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")); } - else if (member.getName().equalsIgnoreCase(leaderName)) { + else if (memberName.equalsIgnoreCase(leaderName)) { member.sendMessage(LocaleLoader.getString("Party.Owner.NotLeader")); } else { @@ -398,12 +374,12 @@ public final class PartyManager { /** * 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 * @return true if the player can invite */ - public static boolean canInvite(Player player, Party party) { - return !(party.isLocked() && !party.getLeader().equalsIgnoreCase(player.getName())); + public static boolean canInvite(String playerName, Party party) { + return !party.isLocked() || party.getLeader().equalsIgnoreCase(playerName); } /** @@ -508,6 +484,17 @@ public final class PartyManager { 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. * @@ -531,10 +518,4 @@ public final class PartyManager { member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", playerName)); } } - - private static void processPartyLeaving(McMMOPlayer mcMMOPlayer) { - mcMMOPlayer.removeParty(); - mcMMOPlayer.setPartyChat(false); - mcMMOPlayer.setItemShareModifier(10); - } }