mirror of
https://github.com/Wurst-Imperium/Wurst7.git
synced 2024-09-20 01:12:13 +02:00
Add HelpCmd
This commit is contained in:
parent
a4866bf7d5
commit
cc83e33f53
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
75
src/main/java/net/wurstclient/commands/HelpCmd.java
Normal file
75
src/main/java/net/wurstclient/commands/HelpCmd.java
Normal file
@ -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 <command>", "List commands: .help [<page>]");
|
||||
}
|
||||
|
||||
@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<Command> 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user