From a3e6996ef9864d4089453e3afd26bc4c40bcc47d Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sat, 8 Jun 2013 12:53:23 +0100 Subject: [PATCH] Try and make toggles DRYer without loosing command flow. Allow socialspy to match multiple players. --- .../src/com/earth2me/essentials/UserData.java | 13 -- .../essentials/commands/Commandfly.java | 114 ++++++------------ .../essentials/commands/Commandgod.java | 99 ++++++--------- .../essentials/commands/Commandsocialspy.java | 49 +++++--- .../essentials/commands/Commandtptoggle.java | 86 +++++-------- .../commands/EssentialsToggleCommand.java | 76 ++++++++++++ 6 files changed, 215 insertions(+), 222 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/commands/EssentialsToggleCommand.java 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 ignoredPlayers; public List _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); - } - else if (args[0].equalsIgnoreCase("off") || args[0].startsWith("dis") || args[0].equalsIgnoreCase("0")) - { - user.setAllowFlight(false); - } - else if (user.isAuthorized("essentials.fly.others")) - { - if (args[0].trim().length() < 2) - { - throw new Exception(_("playerNotFound")); - } - flyOtherPlayers(server, user, args); - return; - } - } - else if (args.length == 2 && user.isAuthorized("essentials.fly.others")) - { - if (args[0].trim().length() < 2) - { - throw new Exception(_("playerNotFound")); - } - flyOtherPlayers(server, user, args); - return; - } - else - { - user.setAllowFlight(!user.getAllowFlight()); - if (!user.getAllowFlight()) - { - user.setFlying(false); - } - } - user.sendMessage(_("flyMode", _(user.getAllowFlight() ? "enabled" : "disabled"), user.getDisplayName())); - } - - private void flyOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException - { - boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact"); - boolean foundUser = false; - final List 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) - { - if (args[1].contains("on") || args[1].contains("ena") || args[1].equalsIgnoreCase("1")) - { - player.setAllowFlight(true); - } - else - { - player.setAllowFlight(false); - } + toggleOtherPlayers(server, user, args); } else { - player.setAllowFlight(!player.getAllowFlight()); + togglePlayer(user, user, toggle); } - - if (!player.getAllowFlight()) - { - player.setFlying(false); - } - sender.sendMessage(_("flyMode", _(player.getAllowFlight() ? "enabled" : "disabled"), player.getDisplayName())); } - if (!foundUser) + else if (args.length == 2 && user.isAuthorized(othersPermission)) { - throw new NotEnoughArgumentsException(_("playerNotFound")); + toggleOtherPlayers(server, user, args); + } + else + { + togglePlayer(user, user, null); + } + } + + @Override + void togglePlayer(CommandSender sender, User user, Boolean enabled) + { + if (enabled == null) + { + enabled = !user.getAllowFlight(); + } + + user.setAllowFlight(enabled); + + if (!user.getAllowFlight()) + { + user.setFlying(false); + } + + user.sendMessage(_("flyMode", _(enabled ? "enabled" : "disabled"), user.getDisplayName())); + if (!sender.equals(user)) + { + 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)) { - throw new Exception(_("playerNotFound")); - } - 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) - { - player.setHealth(player.getMaxHealth()); - player.setFoodLevel(20); - } - } - - private void godOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException - { - boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact"); - boolean foundUser = false; - final List matchedPlayers = server.matchPlayer(args[0]); - for (Player matchPlayer : matchedPlayers) - { - 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; - } + toggleOtherPlayers(server, user, args); } else { - enabled = !player.isGodModeEnabled(); + togglePlayer(user, user, toggle); } - - godPlayer(player, enabled); - player.sendMessage(_("godMode", (enabled ? _("enabled") : _("disabled")))); - sender.sendMessage(_("godMode", _(enabled ? "godEnabledFor" : "godDisabledFor", matchPlayer.getDisplayName()))); } - if (!foundUser) + else if (args.length == 2 && user.isAuthorized(othersPermission)) + { + toggleOtherPlayers(server, user, args); + } + else { - throw new NotEnoughArgumentsException(_("playerNotFound")); + togglePlayer(user, user, null); + } + } + + @Override + void togglePlayer(CommandSender sender, User user, Boolean enabled) + { + if (enabled == null) + { + enabled = !user.isGodModeEnabled(); + } + + user.setGodModeEnabled(enabled); + + if (enabled && user.getHealth() != 0) + { + user.setHealth(user.getMaxHealth()); + user.setFoodLevel(20); + } + + user.sendMessage(_("godMode", enabled ? _("enabled") : _("disabled"))); + if (!sender.equals(user)) + { + 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) { - User target = getPlayer(server, user, args, 0); - user.sendMessage(_("socialSpy", target.getDisplayName(), target.toggleSocialSpy() ? _("enabled") : _("disabled"))); + 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)) + { + 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) - { - User target = getPlayer(server, args, 0, true, false); - sender.sendMessage(_("socialSpy", target.getDisplayName(), target.toggleSocialSpy() ? _("enabled") : _("disabled"))); - } - else + if (enabled == null) { throw new NotEnoughArgumentsException(); } + + user.setSocialSpyEnabled(enabled); + + + user.sendMessage(_("socialSpy", user.getDisplayName(), enabled ? _("enabled") : _("disabled"))); + if (!sender.equals(user)) + { + 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)) { - throw new Exception(_("playerNotFound")); - } - toggleOtherPlayers(server, user, args); - return; - } - - user.sendMessage(user.toggleTeleportEnabled() ? _("teleportationEnabled") : _("teleportationDisabled")); - } - - private void toggleOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException - { - boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact"); - boolean foundUser = false; - final List 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) - { - if (args[1].contains("on") || args[1].contains("ena") || args[1].equalsIgnoreCase("1")) - { - player.setTeleportEnabled(true); - } - else - { - player.setTeleportEnabled(false); - } + toggleOtherPlayers(server, user, args); } else { - player.toggleTeleportEnabled(); + togglePlayer(user, user, toggle); } - - final boolean enabled = player.isTeleportEnabled(); - - - player.sendMessage(enabled ? _("teleportationEnabled") : _("teleportationDisabled")); - sender.sendMessage(enabled ? _("teleportationEnabledFor", matchPlayer.getDisplayName()) : _("teleportationDisabledFor", matchPlayer.getDisplayName())); } - if (!foundUser) + else if (args.length == 2 && user.isAuthorized(othersPermission)) { - throw new NotEnoughArgumentsException(_("playerNotFound")); + toggleOtherPlayers(server, user, args); + } + else + { + togglePlayer(user, user, null); + } + } + + @Override + void togglePlayer(CommandSender sender, User user, Boolean enabled) + { + if (enabled == null) + { + enabled = !user.isTeleportEnabled(); + } + + user.setTeleportEnabled(enabled); + + user.sendMessage(enabled ? _("teleportationEnabled") : _("teleportationDisabled")); + if (!sender.equals(user)) + { + 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 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; +}