From cc83e33f532f509f23465002f69b817e51c5e705 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 8 Jul 2019 18:49:07 +0200 Subject: [PATCH] Add HelpCmd --- .../java/net/wurstclient/command/CmdList.java | 3 +- .../java/net/wurstclient/command/Command.java | 15 ++-- .../net/wurstclient/commands/HelpCmd.java | 75 +++++++++++++++++++ 3 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/wurstclient/commands/HelpCmd.java diff --git a/src/main/java/net/wurstclient/command/CmdList.java b/src/main/java/net/wurstclient/command/CmdList.java index 6a6cac1c..76ff93ec 100644 --- a/src/main/java/net/wurstclient/command/CmdList.java +++ b/src/main/java/net/wurstclient/command/CmdList.java @@ -13,6 +13,7 @@ import java.util.TreeMap; import net.minecraft.util.crash.CrashException; import net.minecraft.util.crash.CrashReport; +import net.wurstclient.commands.HelpCmd; public final class CmdList { @@ -35,7 +36,7 @@ public final class CmdList // public final GiveCmd giveCmd = new GiveCmd(); // public final GmCmd gmCmd = new GmCmd(); // public final GoToCmd goToCmd = new GoToCmd(); - // public final HelpCmd HhelpCmd = new HelpCmd(); + public final HelpCmd helpCmd = new HelpCmd(); // public final InvseeCmd invseeCmd = new InvseeCmd(); // public final IpCmd ipCmd = new IpCmd(); // public final JumpCmd jumpCmd = new JumpCmd(); diff --git a/src/main/java/net/wurstclient/command/Command.java b/src/main/java/net/wurstclient/command/Command.java index a8368134..ffe4d950 100644 --- a/src/main/java/net/wurstclient/command/Command.java +++ b/src/main/java/net/wurstclient/command/Command.java @@ -10,6 +10,7 @@ package net.wurstclient.command; import java.util.Objects; import net.wurstclient.Feature; +import net.wurstclient.util.ChatUtils; public abstract class Command extends Feature { @@ -35,13 +36,17 @@ public abstract class Command extends Feature return name; } - public final String getDescription() - { - return description; - } - public final String[] getSyntax() { return syntax; } + + public final void printHelp() + { + for(String line : description.split("\n")) + ChatUtils.message(line); + + for(String line : syntax) + ChatUtils.message(line); + } } diff --git a/src/main/java/net/wurstclient/commands/HelpCmd.java b/src/main/java/net/wurstclient/commands/HelpCmd.java new file mode 100644 index 00000000..6ca155fe --- /dev/null +++ b/src/main/java/net/wurstclient/commands/HelpCmd.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved. + * + * This source code is subject to the terms of the GNU General Public + * License, version 3. If a copy of the GPL was not distributed with this + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt + */ +package net.wurstclient.commands; + +import java.util.ArrayList; + +import net.wurstclient.command.CmdException; +import net.wurstclient.command.CmdSyntaxError; +import net.wurstclient.command.Command; +import net.wurstclient.util.ChatUtils; +import net.wurstclient.util.MathUtils; + +public final class HelpCmd extends Command +{ + private static final int CMDS_PER_PAGE = 8; + + public HelpCmd() + { + super("help", "Shows help for a command or a list of commands.", + ".help ", "List commands: .help []"); + } + + @Override + public void call(String[] args) throws CmdException + { + if(args.length > 1) + throw new CmdSyntaxError(this); + + String arg = args.length > 0 ? args[0] : "1"; + + if(MathUtils.isInteger(arg)) + listCommands(Integer.parseInt(arg)); + else + help(arg); + } + + private void listCommands(int page) throws CmdException + { + ArrayList cmds = new ArrayList<>(WURST.getCmds().getAllCmds()); + int pages = (int)Math.ceil(cmds.size() / (double)CMDS_PER_PAGE); + pages = Math.max(pages, 1); + + if(page > pages || page < 1) + throw new CmdSyntaxError(this, "Invalid page: " + page); + + String total = "Total: " + cmds.size() + " command"; + total += cmds.size() != 1 ? "s" : ""; + ChatUtils.message(total); + + int start = (page - 1) * CMDS_PER_PAGE; + int end = Math.min(page * CMDS_PER_PAGE, cmds.size()); + + ChatUtils.message("Command list (page " + page + "/" + pages + ")"); + for(int i = start; i < end; i++) + ChatUtils.message("- ." + cmds.get(i).getName()); + } + + private void help(String cmdName) throws CmdException + { + if(cmdName.startsWith(".")) + cmdName = cmdName.substring(1); + + Command cmd = WURST.getCmds().getCmdByName(cmdName); + if(cmd == null) + throw new CmdSyntaxError(this, "Unknown command: ." + cmdName); + + ChatUtils.message("Available help for ." + cmdName + ":"); + cmd.printHelp(); + } +}