diff options
author | KHobbits <rob@khobbits.co.uk> | 2013-06-08 12:53:23 +0100 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2013-06-08 15:13:51 +0100 |
commit | b4e484ea32c8ec6d6247a7b0c1fd81804b36b735 (patch) | |
tree | a9b020d3e6861bd231afc753bf579e74efc2bd3e | |
parent | bd3a8a3aed6b701f89c183547eb622c7c2aa9a2c (diff) | |
download | Essentials-b4e484ea32c8ec6d6247a7b0c1fd81804b36b735.tar Essentials-b4e484ea32c8ec6d6247a7b0c1fd81804b36b735.tar.gz Essentials-b4e484ea32c8ec6d6247a7b0c1fd81804b36b735.tar.lz Essentials-b4e484ea32c8ec6d6247a7b0c1fd81804b36b735.tar.xz Essentials-b4e484ea32c8ec6d6247a7b0c1fd81804b36b735.zip |
Try and make toggles DRYer without loosing command flow.
Allow socialspy to match multiple players.
6 files changed, 195 insertions, 202 deletions
diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index 59d3849e5..014adb08c 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -474,19 +474,6 @@ public abstract class UserData extends PlayerExtension implements IConf config.save(); } - public boolean toggleTeleportEnabled() - { - boolean ret = !isTeleportEnabled(); - setTeleportEnabled(ret); - return ret; - } - - public boolean toggleSocialSpy() - { - boolean ret = !isSocialSpyEnabled(); - setSocialSpyEnabled(ret); - return ret; - } private List<String> ignoredPlayers; public List<String> _getIgnoredPlayers() diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandfly.java b/Essentials/src/com/earth2me/essentials/commands/Commandfly.java index 126d7e5d1..7b320102d 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandfly.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandfly.java @@ -2,28 +2,21 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; -import java.util.List; import org.bukkit.Server; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -public class Commandfly extends EssentialsCommand +public class Commandfly extends EssentialsToggleCommand { public Commandfly() { - super("fly"); + super("fly", "essentials.fly.others"); } @Override protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception { - if (args.length < 1) - { - throw new NotEnoughArgumentsException(); - } - - flyOtherPlayers(server, sender, args); + toggleOtherPlayers(server, sender, args); } @Override @@ -31,82 +24,45 @@ public class Commandfly extends EssentialsCommand { if (args.length == 1) { - if (args[0].equalsIgnoreCase("on") || args[0].startsWith("ena") || args[0].equalsIgnoreCase("1")) + Boolean toggle = matchToggleArgument(args[0]); + if (toggle == null && user.isAuthorized(othersPermission)) { - user.setAllowFlight(true); + toggleOtherPlayers(server, user, args); } - else if (args[0].equalsIgnoreCase("off") || args[0].startsWith("dis") || args[0].equalsIgnoreCase("0")) - { - user.setAllowFlight(false); - } - else if (user.isAuthorized("essentials.fly.others")) + else { - if (args[0].trim().length() < 2) - { - throw new Exception(_("playerNotFound")); - } - flyOtherPlayers(server, user, args); - return; + togglePlayer(user, user, toggle); } } - else if (args.length == 2 && user.isAuthorized("essentials.fly.others")) + else if (args.length == 2 && user.isAuthorized(othersPermission)) { - if (args[0].trim().length() < 2) - { - throw new Exception(_("playerNotFound")); - } - flyOtherPlayers(server, user, args); - return; + toggleOtherPlayers(server, user, args); } else { - user.setAllowFlight(!user.getAllowFlight()); - if (!user.getAllowFlight()) - { - user.setFlying(false); - } + togglePlayer(user, user, null); } - user.sendMessage(_("flyMode", _(user.getAllowFlight() ? "enabled" : "disabled"), user.getDisplayName())); } - private void flyOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException + @Override + void togglePlayer(CommandSender sender, User user, Boolean enabled) { - boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact"); - boolean foundUser = false; - final List<Player> matchedPlayers = server.matchPlayer(args[0]); - for (Player matchPlayer : matchedPlayers) + if (enabled == null) { - final User player = ess.getUser(matchPlayer); - if (skipHidden && player.isHidden()) - { - continue; - } - foundUser = true; - if (args.length > 1) - { - if (args[1].contains("on") || args[1].contains("ena") || args[1].equalsIgnoreCase("1")) - { - player.setAllowFlight(true); - } - else - { - player.setAllowFlight(false); - } - } - else - { - player.setAllowFlight(!player.getAllowFlight()); - } + enabled = !user.getAllowFlight(); + } - if (!player.getAllowFlight()) - { - player.setFlying(false); - } - sender.sendMessage(_("flyMode", _(player.getAllowFlight() ? "enabled" : "disabled"), player.getDisplayName())); + user.setAllowFlight(enabled); + + if (!user.getAllowFlight()) + { + user.setFlying(false); } - if (!foundUser) + + user.sendMessage(_("flyMode", _(enabled ? "enabled" : "disabled"), user.getDisplayName())); + if (!sender.equals(user)) { - throw new NotEnoughArgumentsException(_("playerNotFound")); + sender.sendMessage(_("flyMode", _(enabled ? "enabled" : "disabled"), user.getDisplayName())); } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgod.java b/Essentials/src/com/earth2me/essentials/commands/Commandgod.java index efe2a7f42..8566e47d6 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgod.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgod.java @@ -2,93 +2,68 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; -import java.util.List; import org.bukkit.Server; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -public class Commandgod extends EssentialsCommand +public class Commandgod extends EssentialsToggleCommand { public Commandgod() { - super("god"); + super("god", "essentials.god.others"); } @Override protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception { - if (args.length < 1) - { - throw new NotEnoughArgumentsException(); - } - - godOtherPlayers(server, sender, args); + toggleOtherPlayers(server, sender, args); } @Override protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - if (args.length > 0 && user.isAuthorized("essentials.god.others")) + if (args.length == 1) { - if (args[0].trim().length() < 2) + Boolean toggle = matchToggleArgument(args[0]); + if (toggle == null && user.isAuthorized(othersPermission)) + { + toggleOtherPlayers(server, user, args); + } + else { - throw new Exception(_("playerNotFound")); + togglePlayer(user, user, toggle); } - godOtherPlayers(server, user, args); - return; } - godPlayer(user, !user.isGodModeEnabled()); - user.sendMessage(_("godMode", (user.isGodModeEnabled() ? _("enabled") : _("disabled")))); - } - - private void godPlayer(User player, boolean enabled) - { - player.setGodModeEnabled(enabled); - if (enabled && player.getHealth() != 0) + else if (args.length == 2 && user.isAuthorized(othersPermission)) + { + toggleOtherPlayers(server, user, args); + } + else { - player.setHealth(player.getMaxHealth()); - player.setFoodLevel(20); + togglePlayer(user, user, null); } } - private void godOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException + @Override + void togglePlayer(CommandSender sender, User user, Boolean enabled) { - boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact"); - boolean foundUser = false; - final List<Player> matchedPlayers = server.matchPlayer(args[0]); - for (Player matchPlayer : matchedPlayers) + if (enabled == null) { - final User player = ess.getUser(matchPlayer); - if (skipHidden && player.isHidden()) - { - continue; - } - foundUser = true; - boolean enabled; - if (args.length > 1) - { - if (args[1].contains("on") || args[1].contains("ena") || args[1].equalsIgnoreCase("1")) - { - enabled = true; - } - else - { - enabled = false; - } - } - else - { - enabled = !player.isGodModeEnabled(); - } + enabled = !user.isGodModeEnabled(); + } + + user.setGodModeEnabled(enabled); - godPlayer(player, enabled); - player.sendMessage(_("godMode", (enabled ? _("enabled") : _("disabled")))); - sender.sendMessage(_("godMode", _(enabled ? "godEnabledFor" : "godDisabledFor", matchPlayer.getDisplayName()))); + if (enabled && user.getHealth() != 0) + { + user.setHealth(user.getMaxHealth()); + user.setFoodLevel(20); } - if (!foundUser) + + user.sendMessage(_("godMode", enabled ? _("enabled") : _("disabled"))); + if (!sender.equals(user)) { - throw new NotEnoughArgumentsException(_("playerNotFound")); + sender.sendMessage(_("godMode", _(enabled ? "godEnabledFor" : "godDisabledFor", user.getDisplayName()))); } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsocialspy.java b/Essentials/src/com/earth2me/essentials/commands/Commandsocialspy.java index e5985a2fa..12cf46ecc 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsocialspy.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsocialspy.java @@ -6,38 +6,59 @@ import org.bukkit.command.CommandSender; import org.bukkit.Server; -public class Commandsocialspy extends EssentialsCommand +public class Commandsocialspy extends EssentialsToggleCommand { public Commandsocialspy() { - super("socialspy"); + super("socialspy", "essentials.socialspy.others"); } @Override - public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception + protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception { - if (args.length > 0 && user.isAuthorized("essentials.socialspy.others")) + toggleOtherPlayers(server, sender, args); + } + + @Override + protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception + { + if (args.length == 1) + { + Boolean toggle = matchToggleArgument(args[0]); + if (toggle == null && user.isAuthorized(othersPermission)) + { + toggleOtherPlayers(server, user, args); + } + else + { + togglePlayer(user, user, toggle); + } + } + else if (args.length == 2 && user.isAuthorized(othersPermission)) { - User target = getPlayer(server, user, args, 0); - user.sendMessage(_("socialSpy", target.getDisplayName(), target.toggleSocialSpy() ? _("enabled") : _("disabled"))); + toggleOtherPlayers(server, user, args); } else { - user.sendMessage(_("socialSpy", user.getDisplayName(), user.toggleSocialSpy() ? _("enabled") : _("disabled"))); + togglePlayer(user, user, null); } } @Override - public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception + void togglePlayer(CommandSender sender, User user, Boolean enabled) throws NotEnoughArgumentsException { - if (args.length > 0) + if (enabled == null) { - User target = getPlayer(server, args, 0, true, false); - sender.sendMessage(_("socialSpy", target.getDisplayName(), target.toggleSocialSpy() ? _("enabled") : _("disabled"))); + throw new NotEnoughArgumentsException(); } - else + + user.setSocialSpyEnabled(enabled); + + + user.sendMessage(_("socialSpy", user.getDisplayName(), enabled ? _("enabled") : _("disabled"))); + if (!sender.equals(user)) { - throw new NotEnoughArgumentsException(); + sender.sendMessage(_("socialSpy", user.getDisplayName(), enabled ? _("enabled") : _("disabled"))); } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtptoggle.java b/Essentials/src/com/earth2me/essentials/commands/Commandtptoggle.java index 00e65aab7..777473091 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtptoggle.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtptoggle.java @@ -2,84 +2,62 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; -import java.util.List; import org.bukkit.Server; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -public class Commandtptoggle extends EssentialsCommand +public class Commandtptoggle extends EssentialsToggleCommand { public Commandtptoggle() { - super("tptoggle"); + super("tptoggle", "essentials.tptoggle.others"); } @Override - public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception + protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception { - if (args.length < 1) - { - throw new NotEnoughArgumentsException(); - } - toggleOtherPlayers(server, sender, args); } @Override - public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception + protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - if (args.length > 0 && user.isAuthorized("essentials.tptoggle.others")) + if (args.length == 1) { - if (args[0].trim().length() < 2) + Boolean toggle = matchToggleArgument(args[0]); + if (toggle == null && user.isAuthorized(othersPermission)) + { + toggleOtherPlayers(server, user, args); + } + else { - throw new Exception(_("playerNotFound")); + togglePlayer(user, user, toggle); } + } + else if (args.length == 2 && user.isAuthorized(othersPermission)) + { toggleOtherPlayers(server, user, args); - return; } - - user.sendMessage(user.toggleTeleportEnabled() ? _("teleportationEnabled") : _("teleportationDisabled")); + else + { + togglePlayer(user, user, null); + } } - private void toggleOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException + @Override + void togglePlayer(CommandSender sender, User user, Boolean enabled) { - boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact"); - boolean foundUser = false; - final List<Player> matchedPlayers = server.matchPlayer(args[0]); - for (Player matchPlayer : matchedPlayers) + if (enabled == null) { - final User player = ess.getUser(matchPlayer); - if (skipHidden && player.isHidden()) - { - continue; - } - foundUser = true; - if (args.length > 1) - { - if (args[1].contains("on") || args[1].contains("ena") || args[1].equalsIgnoreCase("1")) - { - player.setTeleportEnabled(true); - } - else - { - player.setTeleportEnabled(false); - } - } - else - { - player.toggleTeleportEnabled(); - } - - final boolean enabled = player.isTeleportEnabled(); + enabled = !user.isTeleportEnabled(); + } + user.setTeleportEnabled(enabled); - player.sendMessage(enabled ? _("teleportationEnabled") : _("teleportationDisabled")); - sender.sendMessage(enabled ? _("teleportationEnabledFor", matchPlayer.getDisplayName()) : _("teleportationDisabledFor", matchPlayer.getDisplayName())); - } - if (!foundUser) + user.sendMessage(enabled ? _("teleportationEnabled") : _("teleportationDisabled")); + if (!sender.equals(user)) { - throw new NotEnoughArgumentsException(_("playerNotFound")); + sender.sendMessage(enabled ? _("teleportationEnabledFor", user.getDisplayName()) : _("teleportationDisabledFor", user.getDisplayName())); } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/EssentialsToggleCommand.java b/Essentials/src/com/earth2me/essentials/commands/EssentialsToggleCommand.java new file mode 100644 index 000000000..022bc2fcc --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/EssentialsToggleCommand.java @@ -0,0 +1,76 @@ +package com.earth2me.essentials.commands; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.User; +import java.util.List; +import org.bukkit.Server; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + + +public abstract class EssentialsToggleCommand extends EssentialsCommand +{ + String othersPermission; + + public EssentialsToggleCommand(String command, String othersPermission) + { + super(command); + this.othersPermission = othersPermission; + } + + protected Boolean matchToggleArgument(final String arg) + { + if (arg.equalsIgnoreCase("on") || arg.startsWith("ena") || arg.equalsIgnoreCase("1")) + { + return true; + } + else if (arg.equalsIgnoreCase("off") || arg.startsWith("dis") || arg.equalsIgnoreCase("0")) + { + return false; + } + return null; + } + + protected void toggleOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException + { + if (args.length < 1 || args[0].trim().length() < 2) + { + throw new NotEnoughArgumentsException(_("playerNotFound")); + } + + boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact"); + boolean foundUser = false; + final List<Player> matchedPlayers = server.matchPlayer(args[0]); + for (Player matchPlayer : matchedPlayers) + { + final User player = ess.getUser(matchPlayer); + if (skipHidden && player.isHidden()) + { + continue; + } + foundUser = true; + if (args.length > 1) + { + Boolean toggle = matchToggleArgument(args[1]); + if (toggle == true) + { + togglePlayer(sender, player, true); + } + else + { + togglePlayer(sender, player, false); + } + } + else + { + togglePlayer(sender, player, null); + } + } + if (!foundUser) + { + throw new NotEnoughArgumentsException(_("playerNotFound")); + } + } + + abstract void togglePlayer(CommandSender sender, User user, Boolean enabled) throws NotEnoughArgumentsException; +} |