From 34cc783cac4bf73d072cdf2068af188c15bdc003 Mon Sep 17 00:00:00 2001 From: Matsv Date: Thu, 31 Mar 2016 17:48:15 +0200 Subject: [PATCH] Fix missing description, add javadocs, make sure subcommand is valid, check permission --- .../ViaVersion/api/command/ViaSubCommand.java | 20 +++++++++++++++++++ .../api/command/ViaVersionCommand.java | 19 +++++++++++++++++- .../commands/ViaCommandHandler.java | 17 +++++++++++++--- .../commands/defaultsubs/DontBugMeSubCmd.java | 2 +- src/main/resources/plugin.yml | 1 - 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/api/command/ViaSubCommand.java b/src/main/java/us/myles/ViaVersion/api/command/ViaSubCommand.java index 09b459893..2d55ed470 100644 --- a/src/main/java/us/myles/ViaVersion/api/command/ViaSubCommand.java +++ b/src/main/java/us/myles/ViaVersion/api/command/ViaSubCommand.java @@ -5,14 +5,34 @@ import org.bukkit.entity.Player; import us.myles.ViaVersion.commands.ViaCommandHandler; public abstract class ViaSubCommand { + /** + * Subcommand name + * + * @return your input + */ public abstract String name(); + /** + * subcommand description, this'll show in /viaversion list + * + * @return your input + */ public abstract String description(); + /** + * Usage example: + * "playerversion [name]" + * + * @return your input + */ public String usage(){ return name(); } + /** + * Permission, null for everyone + * @return + */ public String permission(){ return "viaversion.admin"; } diff --git a/src/main/java/us/myles/ViaVersion/api/command/ViaVersionCommand.java b/src/main/java/us/myles/ViaVersion/api/command/ViaVersionCommand.java index 073004f82..707cac6b7 100644 --- a/src/main/java/us/myles/ViaVersion/api/command/ViaVersionCommand.java +++ b/src/main/java/us/myles/ViaVersion/api/command/ViaVersionCommand.java @@ -1,10 +1,27 @@ package us.myles.ViaVersion.api.command; public interface ViaVersionCommand { - + /** + * Register your own subcommand inside ViaVersion + * + * @param command Your own SubCommand instance to handle it. + * @throws Exception throws an exception when the subcommand already exists or if it's not valid, example: spacee + */ void registerSubCommand(ViaSubCommand command) throws Exception; + /** + * Check if a subcommand is registered. + * + * @param name Subcommand name + * @return true if it exists + */ boolean hasSubCommand(String name); + /** + * Get subcommand instance by name + * + * @param name subcommand name + * @return ViaSubCommand instance + */ ViaSubCommand getSubCommand(String name); } diff --git a/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java b/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java index 457c36d57..a398f3107 100644 --- a/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java +++ b/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java @@ -1,6 +1,7 @@ package us.myles.ViaVersion.commands; import lombok.NonNull; +import org.apache.commons.lang.Validate; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -25,6 +26,7 @@ public class ViaCommandHandler implements us.myles.ViaVersion.api.command.ViaVer @Override public void registerSubCommand(@NonNull ViaSubCommand command) throws Exception { + Validate.isTrue(command.name().matches("^[a-z0-9_-]{3,15}$"), command.name() + " is not a valid subcommand name"); if (hasSubCommand(command.name())) throw new Exception("ViaSubCommand " + command.name() + " does already exists!"); //Maybe another exception later. commandMap.put(command.name().toLowerCase(), command); @@ -52,8 +54,13 @@ public class ViaCommandHandler implements us.myles.ViaVersion.api.command.ViaVer showHelp(sender); return false; } - ViaSubCommand handler = getSubCommand(args[0]); + + if (!hasPermission(sender, handler.permission())){ + sender.sendMessage(color("&cYou are not allowed to use this command!")); + return false; + } + String[] subArgs = Arrays.copyOfRange(args, 1, args.length); boolean result = handler.execute(sender, subArgs); if (!result) @@ -64,7 +71,7 @@ public class ViaCommandHandler implements us.myles.ViaVersion.api.command.ViaVer public void showHelp(CommandSender sender) { Set allowed = calculateAllowedCommands(sender); if (allowed.size() == 0){ - sender.sendMessage("&cYou are not allowed to use this command!"); + sender.sendMessage(color("&cYou are not allowed to use this command!")); return; } sender.sendMessage(color("&aViaVersion &c" + ViaVersion.getInstance().getVersion())); @@ -77,11 +84,15 @@ public class ViaCommandHandler implements us.myles.ViaVersion.api.command.ViaVer private Set calculateAllowedCommands(CommandSender sender) { Set cmds = new HashSet<>(); for (ViaSubCommand sub : commandMap.values()) - if (sub.permission() == null || sender.hasPermission(sub.permission())) + if (hasPermission(sender, sub.permission())) cmds.add(sub); return cmds; } + private boolean hasPermission(CommandSender sender, String permission){ + return permission == null || sender.hasPermission(permission); + } + public static String color(String string) { try { diff --git a/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DontBugMeSubCmd.java b/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DontBugMeSubCmd.java index ee9c99b83..0ca7033a4 100644 --- a/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DontBugMeSubCmd.java +++ b/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DontBugMeSubCmd.java @@ -13,7 +13,7 @@ public class DontBugMeSubCmd extends ViaSubCommand { @Override public String description() { - return null; + return "Toggle checking for updates"; } @Override diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8d5cb9082..314f3d36e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -7,5 +7,4 @@ loadbefore: [ProtocolLib, ProxyPipe] commands: viaversion: description: Shows ViaVersion Version and more. - permission: viaversion.admin aliases: [viaver] \ No newline at end of file