-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
61 changed files
with
5,770 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Intake, a command processing library | ||
* Copyright (C) sk89q <http://www.sk89q.com> | ||
* Copyright (C) Intake team and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.intake; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotates a method that is to be registered as a command. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface Command { | ||
|
||
/** | ||
* A list of aliases for the command. The first alias is the name of | ||
* the command and considered the main alias. | ||
* | ||
* @return aliases for a command | ||
*/ | ||
String[] aliases(); | ||
|
||
/** | ||
* An example usage string of the command. | ||
* | ||
* <p>An example would be | ||
* {@code [-h <value>] <name> <message>}.</p> | ||
* | ||
* @return usage instructions for a command | ||
*/ | ||
String usage() default ""; | ||
|
||
/** | ||
* A short description of the command. | ||
* | ||
* @return a short description for the command. | ||
*/ | ||
String desc(); | ||
|
||
/** | ||
* The minimum number of arguments. This should be 0 or above. | ||
* | ||
* @return the minimum number of arguments | ||
*/ | ||
int min() default 0; | ||
|
||
/** | ||
* The maximum number of arguments. Use -1 for an unlimited number | ||
* of arguments. | ||
* | ||
* @return the maximum number of arguments | ||
*/ | ||
int max() default -1; | ||
|
||
/** | ||
* Flags allow special processing for flags such as -h in the command, | ||
* allowing users to easily turn on a flag. This is a string with | ||
* each character being a flag. Use A-Z and a-z as possible flags. | ||
* Appending a flag with a : makes the flag character before a value flag, | ||
* meaning that if it is given, it must have a value. | ||
* | ||
* @return flags matching a-zA-Z | ||
* @see #anyFlags() to see accept any flag | ||
*/ | ||
String flags() default ""; | ||
|
||
/** | ||
* A long description for the command. | ||
* | ||
* @return A long description for the command. | ||
*/ | ||
String help() default ""; | ||
|
||
/** | ||
* Get whether any flag can be used. | ||
* | ||
* <p>The value of this property overrides {@link #flags()}.</p> | ||
* | ||
* @return true if so | ||
*/ | ||
boolean anyFlags() default false; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Intake, a command processing library | ||
* Copyright (C) sk89q <http://www.sk89q.com> | ||
* Copyright (C) Intake team and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.intake; | ||
|
||
import com.sk89q.intake.completion.CommandCompleter; | ||
import com.sk89q.intake.context.CommandLocals; | ||
import com.sk89q.intake.util.auth.AuthorizationException; | ||
|
||
/** | ||
* A command that can be executed. | ||
*/ | ||
public interface CommandCallable extends CommandCompleter { | ||
|
||
/** | ||
* Execute the correct command based on the input. | ||
* | ||
* <p>The implementing class must perform the necessary permission | ||
* checks.</p> | ||
* | ||
* @param arguments the arguments | ||
* @param locals the locals | ||
* @param parentCommands a list of parent commands, with the first most entry being the top-level command | ||
* @return the called command, or null if there was no command found | ||
* @throws CommandException thrown on a command error | ||
*/ | ||
boolean call(String arguments, CommandLocals locals, String[] parentCommands) throws CommandException, AuthorizationException; | ||
|
||
/** | ||
* Get an object describing this command. | ||
* | ||
* @return the command description | ||
*/ | ||
Description getDescription(); | ||
|
||
/** | ||
* Test whether this command can be executed with the given context. | ||
* | ||
* @param locals the locals | ||
* @return true if execution is permitted | ||
*/ | ||
boolean testPermission(CommandLocals locals); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Intake, a command processing library | ||
* Copyright (C) sk89q <http://www.sk89q.com> | ||
* Copyright (C) Intake team and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.intake; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Thrown when an executed command raises an error or when execution of | ||
* the command failed. | ||
*/ | ||
public class CommandException extends Exception { | ||
|
||
private final List<String> commandStack = new ArrayList<String>(); | ||
|
||
/** | ||
* Constructs a new exception with {@code null} as its detail message. | ||
* The cause is not initialized, and may subsequently be initialized by a | ||
* call to {@link #initCause}. | ||
*/ | ||
public CommandException() { | ||
} | ||
|
||
/** | ||
* Constructs a new exception with the specified detail message. The | ||
* cause is not initialized, and may subsequently be initialized by | ||
* a call to {@link #initCause}. | ||
* | ||
* @param message the detail message. The detail message is saved for | ||
* later retrieval by the {@link #getMessage()} method. | ||
*/ | ||
public CommandException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Constructs a new exception with the specified detail message and | ||
* cause. <p>Note that the detail message associated with | ||
* {@code cause} is <i>not</i> automatically incorporated in | ||
* this exception's detail message. | ||
* | ||
* @param message the detail message (which is saved for later retrieval | ||
* by the {@link #getMessage()} method). | ||
* @param cause the cause (which is saved for later retrieval by the | ||
* {@link #getCause()} method). (A <tt>null</tt> value is | ||
* permitted, and indicates that the cause is nonexistent or | ||
* unknown.) | ||
*/ | ||
public CommandException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Constructs a new exception with the specified cause and a detail | ||
* message of <tt>(cause==null ? null : cause.toString())</tt> (which | ||
* typically contains the class and detail message of <tt>cause</tt>). | ||
* | ||
* @param cause the cause (which is saved for later retrieval by the | ||
* {@link #getCause()} method). (A <tt>null</tt> value is | ||
* permitted, and indicates that the cause is nonexistent or | ||
* unknown.) | ||
*/ | ||
public CommandException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
/** | ||
* Add the command to the stack of commands that are a parent to the | ||
* command that was called. | ||
* | ||
* @param name the parent command | ||
*/ | ||
public void prependStack(String name) { | ||
checkNotNull(name); | ||
commandStack.add(name); | ||
} | ||
|
||
/** | ||
* Gets the command that was called, which will include the sub-command | ||
* (i.e. "/br sphere"). | ||
* | ||
* @param prefix the command shebang character (such as "/") -- may be empty | ||
* @param spacedSuffix a suffix to put at the end (optional) -- may be null | ||
* @return the command that was used | ||
*/ | ||
public String getCommandUsed(String prefix, String spacedSuffix) { | ||
checkNotNull(prefix); | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append(prefix); | ||
ListIterator<String> li = commandStack.listIterator(commandStack.size()); | ||
while (li.hasPrevious()) { | ||
if (li.previousIndex() != commandStack.size() - 1) { | ||
builder.append(" "); | ||
} | ||
builder.append(li.previous()); | ||
} | ||
if (spacedSuffix != null) { | ||
if (builder.length() > 0) { | ||
builder.append(" "); | ||
} | ||
builder.append(spacedSuffix); | ||
} | ||
return builder.toString().trim(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Intake, a command processing library | ||
* Copyright (C) sk89q <http://www.sk89q.com> | ||
* Copyright (C) Intake team and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.intake; | ||
|
||
/** | ||
* Provides information about a mapping between a command and its aliases. | ||
*/ | ||
public interface CommandMapping { | ||
|
||
/** | ||
* Get the primary alias. | ||
* | ||
* @return the primary alias | ||
*/ | ||
String getPrimaryAlias(); | ||
|
||
/** | ||
* Get a list of all aliases. | ||
* | ||
* @return aliases | ||
*/ | ||
String[] getAllAliases(); | ||
|
||
/** | ||
* Get the callable | ||
* | ||
* @return the callable | ||
*/ | ||
CommandCallable getCallable(); | ||
|
||
/** | ||
* Get the {@link Description} form the callable. | ||
* | ||
* @return the description | ||
*/ | ||
Description getDescription(); | ||
|
||
} |
Oops, something went wrong.