From 3ccf79cff517f1d5cf40f166dc72c420bf90dad2 Mon Sep 17 00:00:00 2001 From: GJ Date: Thu, 7 Feb 2013 21:46:12 -0500 Subject: [PATCH] Make /party join into a subcommand. --- .../com/gmail/nossr50/party/PartyManager.java | 5 +- .../nossr50/party/commands/PartyCommand.java | 77 +---------- .../party/commands/PartyJoinCommand.java | 125 ++++++++++++++++++ .../resources/locale/locale_en_US.properties | 2 +- 4 files changed, 131 insertions(+), 78 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/party/commands/PartyJoinCommand.java diff --git a/src/main/java/com/gmail/nossr50/party/PartyManager.java b/src/main/java/com/gmail/nossr50/party/PartyManager.java index 707acb0d1..11bba388d 100644 --- a/src/main/java/com/gmail/nossr50/party/PartyManager.java +++ b/src/main/java/com/gmail/nossr50/party/PartyManager.java @@ -290,7 +290,7 @@ public final class PartyManager { parties.add(party); } - else if (!checkJoinability(player, party, password)) { + else if (!checkPartyPassword(player, party, password)) { return; } @@ -306,7 +306,7 @@ public final class PartyManager { * @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) { + public static boolean checkPartyPassword(Player player, Party party, String password) { //Don't care about passwords if it isn't locked if (party.isLocked()) { String partyPassword = party.getPassword(); @@ -314,7 +314,6 @@ public final class PartyManager { if (partyPassword != null) { if (password == null) { player.sendMessage(LocaleLoader.getString("Party.Password.None")); - player.sendMessage(LocaleLoader.getString("Commands.Usage.3", "party", "join", "<" + LocaleLoader.getString("Commands.Usage.Player") + ">", "<" + LocaleLoader.getString("Commands.Usage.Password") + ">")); return false; } else if (!password.equals(partyPassword)) { diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java index 73310362a..c3e965c55 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java @@ -23,6 +23,8 @@ public class PartyCommand implements CommandExecutor { private McMMOPlayer mcMMOPlayer; private Player player; + private CommandExecutor partyJoinCommand = new PartyJoinCommand(); + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (CommandHelper.noConsoleUsage(sender)) { @@ -41,7 +43,7 @@ public class PartyCommand implements CommandExecutor { } if (args[0].equalsIgnoreCase("join")) { - return join(args); + return partyJoinCommand.onCommand(sender, command, label, args); } else if (args[0].equalsIgnoreCase("accept")) { return accept(); @@ -169,79 +171,6 @@ public class PartyCommand implements CommandExecutor { return true; } - private boolean join(String[] args) { - if (CommandHelper.noCommandPermissions(player, "mcmmo.commands.party.join")) { - return true; - } - - if (args.length < 2) { - player.sendMessage(LocaleLoader.getString("Party.Help.0")); - return true; - } - - Player target = mcMMO.p.getServer().getPlayer(args[1]); - - if (target == null) { - player.sendMessage(LocaleLoader.getString("Party.NotOnline", args[1])); - return false; - } - - McMMOPlayer mcMMOTarget = Users.getPlayer(target); - - if (!mcMMOTarget.inParty()) { - player.sendMessage(LocaleLoader.getString("Party.PlayerNotInParty", args[1])); - return false; - } - - if (player.equals(target)) { - player.sendMessage(LocaleLoader.getString("Party.Join.Self")); - return true; - } - - Party party = mcMMOPlayer.getParty(); - - if (party != null && party.equals(Users.getPlayer(target).getParty())) { - player.sendMessage(LocaleLoader.getString("Party.Join.Self")); - return true; - } - - String password = null; - - if (args.length > 2) { - password = args[2]; - } - - Party targetParty = mcMMOTarget.getParty(); - - // Check to see if the party exists, and if it does, can the player join it? - if (targetParty == null || !PartyManager.checkJoinability(player, targetParty, null)) { - return true; // End before any event is fired. - } - - if (party != null) { - McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, party.getName(), targetParty.getName(), EventReason.CHANGED_PARTIES); - mcMMO.p.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return true; - } - - PartyManager.removeFromParty(player.getName(), party); - } - else { - McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, null, targetParty.getName(), EventReason.JOINED_PARTY); - mcMMO.p.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return true; - } - } - - - PartyManager.joinParty(player, mcMMOPlayer, targetParty.getName(), password); - return true; - } - private boolean accept() { if (CommandHelper.noCommandPermissions(player, "mcmmo.commands.party.accept")) { return true; diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyJoinCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyJoinCommand.java new file mode 100644 index 000000000..bac51d0e7 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyJoinCommand.java @@ -0,0 +1,125 @@ +package com.gmail.nossr50.party.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.datatypes.PlayerProfile; +import com.gmail.nossr50.events.party.McMMOPartyChangeEvent; +import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason; +import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.party.Party; +import com.gmail.nossr50.party.PartyManager; +import com.gmail.nossr50.util.Permissions; +import com.gmail.nossr50.util.Users; + +public class PartyJoinCommand implements CommandExecutor { + private McMMOPlayer mcMMOTarget; + private Player target; + private Party targetParty; + + private McMMOPlayer mcMMOPlayer; + private Player player; + private Party playerParty; + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!Permissions.hasPermission(sender, "mcmmo.commands.party.join")) { + sender.sendMessage(command.getPermissionMessage()); + return true; + } + + switch (args.length) { + case 2: + case 3: + // Verify target exists and is in a different party than the player + if (!canJoinParty(sender, args[1])) { + return true; + } + + String password = getPassword(args); + + // Make sure party passwords match + if (!PartyManager.checkPartyPassword(player, targetParty, password)) { + return true; + } + + // Changing parties + if (mcMMOPlayer.inParty()) { + handlePartyChangeEvent(playerParty.getName(), EventReason.CHANGED_PARTIES); + PartyManager.removeFromParty(player.getName(), playerParty); + } + else { + handlePartyChangeEvent(null, EventReason.JOINED_PARTY); + } + + PartyManager.joinParty(player, mcMMOPlayer, targetParty.getName(), password); + return true; + + default: + sender.sendMessage(LocaleLoader.getString("Commands.Usage.3", "party", "join", "<" + LocaleLoader.getString("Commands.Usage.Player") + ">", "[" + LocaleLoader.getString("Commands.Usage.Password") + "]")); + return true; + } + } + + private String getPassword(String[] args) { + if (args.length == 3) { + return args[2]; + } + + return null; + } + + private boolean canJoinParty(CommandSender sender, String targetName) { + mcMMOTarget = Users.getPlayer(targetName); + + if (mcMMOTarget == null) { + PlayerProfile playerProfile = new PlayerProfile(targetName, false); + + if (!playerProfile.isLoaded()) { + sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); + return false; + } + } + + target = mcMMOTarget.getPlayer(); + + if (!target.isOnline()) { + sender.sendMessage(LocaleLoader.getString("Party.NotOnline", targetName)); + return false; + } + + if (!mcMMOTarget.inParty()) { + sender.sendMessage(LocaleLoader.getString("Party.PlayerNotInParty", targetName)); + return false; + } + + player = (Player) sender; + mcMMOPlayer = Users.getPlayer(player); + targetParty = mcMMOTarget.getParty(); + + if (player.equals(target) || (mcMMOPlayer.inParty() && playerParty.equals(targetParty))) { + sender.sendMessage(LocaleLoader.getString("Party.Join.Self")); + return false; + } + + return true; + } + + /** + * Handle party change event. + * + * @param partyName The name of the old party + * @param reason The reason for changing parties + * @return true if the change event was successful, false otherwise + */ + private boolean handlePartyChangeEvent(String oldPartyName, EventReason reason) { + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(player, oldPartyName, targetParty.getName(), reason); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + return !event.isCancelled(); + } +} diff --git a/src/main/resources/locale/locale_en_US.properties b/src/main/resources/locale/locale_en_US.properties index 64e49dcb8..f5452830e 100644 --- a/src/main/resources/locale/locale_en_US.properties +++ b/src/main/resources/locale/locale_en_US.properties @@ -528,7 +528,7 @@ Party.NotOwner=[[DARK_RED]]You are not the party leader. Party.Owner.New=[[GREEN]]{0} is the new party leader. Party.Owner.NotLeader=[[DARK_RED]]You are no longer the party leader. Party.Owner.Player =[[GREEN]]You are now the party leader. -Party.Password.None=[[RED]]This party is password protected. +Party.Password.None=[[RED]]This party is password protected. Please provide a password to join. Party.Password.Incorrect=[[RED]]Party password is incorrect. Party.Password.Set=[[GREEN]]Party password set to {0} Party.Password.Removed=[[GREEN]]Party password has been cleared.