From cb8ec2ce20e6b4421397d538b80269870bcf2691 Mon Sep 17 00:00:00 2001 From: Matsv Date: Thu, 31 Mar 2016 20:50:22 +0200 Subject: [PATCH 1/2] Add tab-complete possibility and help subcommand --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 10 +-- .../ViaVersion/api/command/ViaSubCommand.java | 24 +++++-- .../commands/ViaCommandHandler.java | 62 ++++++++++++++----- .../commands/defaultsubs/HelpSubCmd.java | 27 ++++++++ 4 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 src/main/java/us/myles/ViaVersion/commands/defaultsubs/HelpSubCmd.java diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 9d1569ec9..3dac25c1f 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -5,7 +5,6 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; -import lombok.Getter; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -81,6 +80,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe Bukkit.getPluginManager().registerEvents(new UpdateListener(this), this); getCommand("viaversion").setExecutor(commandHandler = new ViaCommandHandler()); + getCommand("viaversion").setTabCompleter(new ViaCommandHandler()); } public void gatherProtocolVersion() { @@ -286,15 +286,15 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe return this.debug; } + public void setDebug(boolean value) { + this.debug = value; + } + @Override public ViaVersionCommand getCommandHandler() { return commandHandler; } - public void setDebug(boolean value) { - this.debug = value; - } - public boolean isCheckForUpdates() { return getConfig().getBoolean("checkforupdates", true); } 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 3d95deb03..d1a68683d 100644 --- a/src/main/java/us/myles/ViaVersion/api/command/ViaSubCommand.java +++ b/src/main/java/us/myles/ViaVersion/api/command/ViaSubCommand.java @@ -1,9 +1,11 @@ package us.myles.ViaVersion.api.command; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import us.myles.ViaVersion.commands.ViaCommandHandler; +import java.util.Collections; +import java.util.List; + public abstract class ViaSubCommand { /** * Subcommand name @@ -25,15 +27,16 @@ public abstract class ViaSubCommand { * * @return your input */ - public String usage(){ + public String usage() { return name(); } /** * Permission, null for everyone + * * @return */ - public String permission(){ + public String permission() { return "viaversion.admin"; } @@ -41,12 +44,23 @@ public abstract class ViaSubCommand { * Gets triggered on execution * * @param sender Command sender - * @param args Arguments + * @param args Arguments * @return command executed succesfully if false, show usage */ public abstract boolean execute(CommandSender sender, String[] args); - public String color(String s){ + /** + * Yay, possibility to implement tab-completion + * + * @param sender Command sender + * @param args args + * @return tab complete possibilities + */ + public List onTabComplete(CommandSender sender, String[] args) { + return Collections.emptyList(); + } + + public String color(String s) { return ViaCommandHandler.color(s); } } diff --git a/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java b/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java index f7f15995d..478bf1424 100644 --- a/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java +++ b/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java @@ -6,6 +6,7 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.command.ViaSubCommand; import us.myles.ViaVersion.api.command.ViaVersionCommand; @@ -13,7 +14,7 @@ import us.myles.ViaVersion.commands.defaultsubs.*; import java.util.*; -public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor { +public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor, TabCompleter { private Map commandMap; public ViaCommandHandler() { @@ -25,6 +26,14 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor { } } + public static String color(String string) { + try { + string = ChatColor.translateAlternateColorCodes('&', string); //Dont replace all & with $ like we did before. + } catch (Exception ignored) { + } + return string; + } + @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"); @@ -50,14 +59,14 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor { return false; } - if (!hasSubCommand(args[0])){ + if (!hasSubCommand(args[0])) { sender.sendMessage(color("&cThis command is not found")); showHelp(sender); return false; } ViaSubCommand handler = getSubCommand(args[0]); - if (!hasPermission(sender, handler.permission())){ + if (!hasPermission(sender, handler.permission())) { sender.sendMessage(color("&cYou are not allowed to use this command!")); return false; } @@ -69,9 +78,42 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor { return result; } + @Override + public List onTabComplete(CommandSender sender, Command command, String arg, String[] args) { + Set allowed = calculateAllowedCommands(sender); + List output = new ArrayList<>(); + + //SubCommands tabcomplete + if (args.length == 1) { + if (!args[0].equals("")) { + for (ViaSubCommand sub : allowed) + if (sub.name().toLowerCase().startsWith(args[0].toLowerCase())) + output.add(sub.name()); + } else { + for (ViaSubCommand sub : allowed) + output.add(sub.name()); + } + } + //Let the SubCommand handle it + else if (args.length >= 2) { + if (getSubCommand(args[0]) != null) { + ViaSubCommand sub = getSubCommand(args[0]); + if (!allowed.contains(sub)) + return output; + + String[] subArgs = Arrays.copyOfRange(args, 1, args.length); + + List tab = sub.onTabComplete(sender, subArgs); + Collections.sort(tab); + return tab; + } + } + return output; + } + public void showHelp(CommandSender sender) { Set allowed = calculateAllowedCommands(sender); - if (allowed.size() == 0){ + if (allowed.size() == 0) { sender.sendMessage(color("&cYou are not allowed to use this command!")); return; } @@ -90,24 +132,16 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor { return cmds; } - private boolean hasPermission(CommandSender sender, String permission){ + private boolean hasPermission(CommandSender sender, String permission) { return permission == null || sender.hasPermission(permission); } - - public static String color(String string) { - try { - string = ChatColor.translateAlternateColorCodes('&', string); //Dont replace all & with $ like we did before. - } catch (Exception ignored) { - } - return string; - } - private void registerDefaults() throws Exception { registerSubCommand(new ListSubCmd()); registerSubCommand(new DebugSubCmd()); registerSubCommand(new DisplayLeaksSubCmd()); registerSubCommand(new DontBugMeSubCmd()); registerSubCommand(new AutoTeamSubCmd()); + registerSubCommand(new HelpSubCmd()); } } diff --git a/src/main/java/us/myles/ViaVersion/commands/defaultsubs/HelpSubCmd.java b/src/main/java/us/myles/ViaVersion/commands/defaultsubs/HelpSubCmd.java new file mode 100644 index 000000000..40f936107 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/commands/defaultsubs/HelpSubCmd.java @@ -0,0 +1,27 @@ +package us.myles.ViaVersion.commands.defaultsubs; + +import org.bukkit.command.CommandSender; +import us.myles.ViaVersion.ViaVersionPlugin; +import us.myles.ViaVersion.api.ViaVersion; +import us.myles.ViaVersion.api.command.ViaSubCommand; +import us.myles.ViaVersion.commands.ViaCommandHandler; + +public class HelpSubCmd extends ViaSubCommand { + @Override + public String name() { + return "help"; + } + + @Override + public String description() { + return "You are looking at it right now!"; + } + + @Override + public boolean execute(CommandSender sender, String[] args) { + ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance(); + + ((ViaCommandHandler) plugin.getCommandHandler()).showHelp(sender); + return true; + } +} From 2905d77206759ab17298fe7f3b91d0ee3853bb67 Mon Sep 17 00:00:00 2001 From: Matsv Date: Thu, 31 Mar 2016 20:53:03 +0200 Subject: [PATCH 2/2] Reformatting moved color to the top ;( --- .../ViaVersion/commands/ViaCommandHandler.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java b/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java index 478bf1424..46880b3fe 100644 --- a/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java +++ b/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java @@ -26,14 +26,6 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor, Ta } } - public static String color(String string) { - try { - string = ChatColor.translateAlternateColorCodes('&', string); //Dont replace all & with $ like we did before. - } catch (Exception ignored) { - } - return string; - } - @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"); @@ -144,4 +136,12 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor, Ta registerSubCommand(new AutoTeamSubCmd()); registerSubCommand(new HelpSubCmd()); } + + public static String color(String string) { + try { + string = ChatColor.translateAlternateColorCodes('&', string); //Dont replace all & with $ like we did before. + } catch (Exception ignored) { + } + return string; + } }