Try and make toggles DRYer without loosing command flow.

Allow socialspy to match multiple players.
This commit is contained in:
KHobbits 2013-06-08 12:53:23 +01:00
parent 34daa5691c
commit a3e6996ef9
6 changed files with 215 additions and 222 deletions

View File

@ -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()

View File

@ -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;
toggleOtherPlayers(server, user, args);
}
else
{
user.setAllowFlight(!user.getAllowFlight());
togglePlayer(user, user, toggle);
}
}
else if (args.length == 2 && user.isAuthorized(othersPermission))
{
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", _(user.getAllowFlight() ? "enabled" : "disabled"), user.getDisplayName()));
}
private void flyOtherPlayers(final Server server, final CommandSender sender, final String[] args) throws NotEnoughArgumentsException
user.sendMessage(_("flyMode", _(enabled ? "enabled" : "disabled"), user.getDisplayName()));
if (!sender.equals(user))
{
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)
{
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());
}
if (!player.getAllowFlight())
{
player.setFlying(false);
}
sender.sendMessage(_("flyMode", _(player.getAllowFlight() ? "enabled" : "disabled"), player.getDisplayName()));
}
if (!foundUser)
{
throw new NotEnoughArgumentsException(_("playerNotFound"));
sender.sendMessage(_("flyMode", _(enabled ? "enabled" : "disabled"), user.getDisplayName()));
}
}
}

View File

@ -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<Player> 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;
toggleOtherPlayers(server, user, args);
}
else
{
enabled = false;
togglePlayer(user, user, toggle);
}
}
else if (args.length == 2 && user.isAuthorized(othersPermission))
{
toggleOtherPlayers(server, user, args);
}
else
{
enabled = !player.isGodModeEnabled();
togglePlayer(user, user, null);
}
}
godPlayer(player, enabled);
player.sendMessage(_("godMode", (enabled ? _("enabled") : _("disabled"))));
sender.sendMessage(_("godMode", _(enabled ? "godEnabledFor" : "godDisabledFor", matchPlayer.getDisplayName())));
}
if (!foundUser)
@Override
void togglePlayer(CommandSender sender, User user, Boolean enabled)
{
throw new NotEnoughArgumentsException(_("playerNotFound"));
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())));
}
}
}

View File

@ -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
{
User target = getPlayer(server, user, args, 0);
user.sendMessage(_("socialSpy", target.getDisplayName(), target.toggleSocialSpy() ? _("enabled") : _("disabled")));
if (args.length == 1)
{
Boolean toggle = matchToggleArgument(args[0]);
if (toggle == null && user.isAuthorized(othersPermission))
{
toggleOtherPlayers(server, user, args);
}
else
{
user.sendMessage(_("socialSpy", user.getDisplayName(), user.toggleSocialSpy() ? _("enabled") : _("disabled")));
togglePlayer(user, user, toggle);
}
}
else if (args.length == 2 && user.isAuthorized(othersPermission))
{
toggleOtherPlayers(server, user, args);
}
else
{
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")));
}
}
}

View File

@ -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<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)
{
if (args[1].contains("on") || args[1].contains("ena") || args[1].equalsIgnoreCase("1"))
{
player.setTeleportEnabled(true);
}
else
{
player.setTeleportEnabled(false);
togglePlayer(user, user, toggle);
}
}
else if (args.length == 2 && user.isAuthorized(othersPermission))
{
toggleOtherPlayers(server, user, args);
}
else
{
player.toggleTeleportEnabled();
togglePlayer(user, user, null);
}
}
final boolean enabled = player.isTeleportEnabled();
player.sendMessage(enabled ? _("teleportationEnabled") : _("teleportationDisabled"));
sender.sendMessage(enabled ? _("teleportationEnabledFor", matchPlayer.getDisplayName()) : _("teleportationDisabledFor", matchPlayer.getDisplayName()));
}
if (!foundUser)
@Override
void togglePlayer(CommandSender sender, User user, Boolean enabled)
{
throw new NotEnoughArgumentsException(_("playerNotFound"));
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()));
}
}
}

View File

@ -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;
}