2019-01-01 01:10:18 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
2017-12-06 15:51:02 +00:00
|
|
|
package dan200.computercraft.shared.command.framework;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
2018-12-29 12:18:05 +00:00
|
|
|
import dan200.computercraft.shared.command.text.ChatHelpers;
|
2017-12-06 15:51:02 +00:00
|
|
|
import joptsimple.internal.Strings;
|
|
|
|
import net.minecraft.command.CommandBase;
|
|
|
|
import net.minecraft.command.CommandException;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
class SubCommandHelp implements ISubCommand
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
|
|
|
private final CommandRoot branchCommand;
|
|
|
|
|
2019-03-16 10:26:40 +00:00
|
|
|
SubCommandHelp( CommandRoot branchCommand )
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
|
|
|
this.branchCommand = branchCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getName()
|
|
|
|
{
|
|
|
|
return "help";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2019-03-16 10:26:40 +00:00
|
|
|
public String getFullName()
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
2019-03-16 10:26:40 +00:00
|
|
|
return "computercraft.help";
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean checkPermission( @Nonnull CommandContext context )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
|
|
|
{
|
|
|
|
ISubCommand command = branchCommand;
|
|
|
|
|
|
|
|
for( int i = 0; i < arguments.size(); i++ )
|
|
|
|
{
|
|
|
|
String commandName = arguments.get( i );
|
|
|
|
if( command instanceof CommandRoot )
|
|
|
|
{
|
|
|
|
command = ((CommandRoot) command).getSubCommands().get( commandName );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-16 10:26:40 +00:00
|
|
|
throw new CommandException( "commands.computercraft.help.no_children", Strings.join( arguments.subList( 0, i ), " " ) );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( command == null )
|
|
|
|
{
|
2019-03-16 10:26:40 +00:00
|
|
|
throw new CommandException( "commands.computercraft.help.no_command", Strings.join( arguments.subList( 0, i + 1 ), " " ) );
|
2017-12-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder prefix = new StringBuilder( context.parent().getFullPath() );
|
|
|
|
for( String argument : arguments )
|
|
|
|
{
|
|
|
|
prefix.append( ' ' ).append( argument );
|
|
|
|
}
|
|
|
|
context.getSender().sendMessage( ChatHelpers.getHelp( context, command, prefix.toString() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
|
|
|
{
|
|
|
|
CommandRoot command = branchCommand;
|
|
|
|
|
|
|
|
for( int i = 0; i < arguments.size() - 1; i++ )
|
|
|
|
{
|
|
|
|
String commandName = arguments.get( i );
|
|
|
|
ISubCommand subCommand = command.getSubCommands().get( commandName );
|
|
|
|
|
|
|
|
if( subCommand instanceof CommandRoot )
|
|
|
|
{
|
|
|
|
command = (CommandRoot) subCommand;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:21:39 +00:00
|
|
|
if( arguments.isEmpty() )
|
2017-12-06 15:51:02 +00:00
|
|
|
{
|
|
|
|
return Lists.newArrayList( command.getSubCommands().keySet() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
List<String> list = Lists.newArrayList();
|
|
|
|
String match = arguments.get( arguments.size() - 1 );
|
|
|
|
|
|
|
|
for( String entry : command.getSubCommands().keySet() )
|
|
|
|
{
|
|
|
|
if( CommandBase.doesStringStartWith( match, entry ) )
|
|
|
|
{
|
|
|
|
list.add( entry );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|