From 98a64a09486028a678091b8014361ed2da3ee226 Mon Sep 17 00:00:00 2001 From: GJ Date: Mon, 11 Feb 2013 17:31:09 -0500 Subject: [PATCH] Convert /party lock & /party unlock to a subcommand. --- .../nossr50/party/commands/PartyCommand.java | 71 +------------ .../party/commands/PartyLockCommand.java | 99 +++++++++++++++++++ 2 files changed, 102 insertions(+), 68 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/party/commands/PartyLockCommand.java 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 7c59f5ee9..53380f291 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java @@ -33,6 +33,7 @@ public class PartyCommand implements CommandExecutor { private CommandExecutor partyKickCommand = new PartyKickCommand(); private CommandExecutor partyDisbandCommand = new PartyDisbandCommand(); private CommandExecutor partyChangeOwnerCommand = new PartyChangeOwnerCommand(); + private CommandExecutor partyLockCommand = new PartyLockCommand(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -91,11 +92,8 @@ public class PartyCommand implements CommandExecutor { else if (args[0].equalsIgnoreCase("owner")) { return partyChangeOwnerCommand.onCommand(sender, command, label, args); } - else if (args[0].equalsIgnoreCase("lock")) { - return lock(); - } - else if (args[0].equalsIgnoreCase("unlock")) { - return unlock(); + else if (args[0].equalsIgnoreCase("lock") || args[0].equalsIgnoreCase("unlock")) { + return partyLockCommand.onCommand(sender, command, label, args); } else if (args[0].equalsIgnoreCase("password")) { return changePassword(args); @@ -191,69 +189,6 @@ public class PartyCommand implements CommandExecutor { return true; } - /** - * Lock the current party - */ - private boolean lock() { - if (CommandHelper.noCommandPermissions(player, "mcmmo.commands.party.lock")) { - return true; - } - - String playerName = player.getName(); - Party party = mcMMOPlayer.getParty(); - - if (party == null) { - player.sendMessage("Commands.Party.None"); - return true; - } - - if (!party.getLeader().equals(playerName)) { - player.sendMessage(LocaleLoader.getString("Party.NotOwner")); - return true; - } - - if (party.isLocked()) { - player.sendMessage(LocaleLoader.getString("Party.IsLocked")); - } - else { - party.setLocked(true); - player.sendMessage(LocaleLoader.getString("Party.Locked")); - } - - return true; - } - - /** - * Unlock the current party - */ - private boolean unlock() { - if (CommandHelper.noCommandPermissions(player, "mcmmo.commands.party.unlock")) { - return true; - } - - String playerName = player.getName(); - Party party = mcMMOPlayer.getParty(); - - if (party == null) { - player.sendMessage("Commands.Party.None"); - return true; - } - - if (!party.getLeader().equals(playerName)) { - player.sendMessage(LocaleLoader.getString("Party.NotOwner")); - return true; - } - - if (!party.isLocked()) { - player.sendMessage(LocaleLoader.getString("Party.IsntLocked")); - } - else { - party.setLocked(false); - player.sendMessage(LocaleLoader.getString("Party.Unlocked")); - } - return true; - } - private boolean changePassword(String[] args) { if (CommandHelper.noCommandPermissions(player, "mcmmo.commands.party.password")) { return true; diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyLockCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyLockCommand.java new file mode 100644 index 000000000..9458af459 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyLockCommand.java @@ -0,0 +1,99 @@ +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.locale.LocaleLoader; +import com.gmail.nossr50.party.Party; +import com.gmail.nossr50.util.Permissions; +import com.gmail.nossr50.util.Users; + +public class PartyLockCommand implements CommandExecutor { + private Player player; + private Party playerParty; + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + player = (Player) sender; + playerParty = Users.getPlayer(player).getParty(); + + if (!playerParty.getLeader().equals(player.getName())) { + sender.sendMessage(LocaleLoader.getString("Party.NotOwner")); + return true; + } + + switch (args.length) { + case 1: + if (args[0].equalsIgnoreCase("lock")) { + lockParty(sender, command); + } + else if (args[0].equalsIgnoreCase("unlock")) { + unlockParty(sender, command); + } + + return true; + + case 2: + if (!args[0].equalsIgnoreCase("lock")) { + sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "lock", "[on|off]")); + return true; + } + + if (args[1].equalsIgnoreCase("on") || args[1].equalsIgnoreCase("true")) { + lockParty(sender, command); + } + else if (args[1].equalsIgnoreCase("off") || args[1].equalsIgnoreCase("false")) { + unlockParty(sender, command); + } + else { + sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "lock", "[on|off]")); + } + + return true; + + default: + sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "lock", "[on|off]")); + return true; + } + } + + /** + * Handle locking a party. + */ + private void lockParty(CommandSender sender, Command command) { + if (!Permissions.hasPermission(sender, "mcmmo.commands.party.lock")) { + sender.sendMessage(command.getPermissionMessage()); + return; + } + + if (playerParty.isLocked()) { + sender.sendMessage(LocaleLoader.getString("Party.IsLocked")); + return; + } + + playerParty.setLocked(true); + sender.sendMessage(LocaleLoader.getString("Party.Locked")); + } + + /** + * Handle unlocking a party. + * + * @return true if party is successfully unlocked, false otherwise. + */ + private void unlockParty(CommandSender sender, Command command) { + if (!Permissions.hasPermission(sender, "mcmmo.commands.party.unlock")) { + sender.sendMessage(command.getPermissionMessage()); + return; + } + + if (!playerParty.isLocked()) { + player.sendMessage(LocaleLoader.getString("Party.IsntLocked")); + return; + } + + playerParty.setLocked(false); + player.sendMessage(LocaleLoader.getString("Party.Unlocked")); + } +}