🚚 Switch namespace
This commit is contained in:
parent
0064093dbf
commit
c74cda3a0f
207 changed files with 2689 additions and 611 deletions
|
|
@ -0,0 +1,299 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import cloud.commandframework.Command;
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.Description;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.arguments.parser.ArgumentParser;
|
||||
import cloud.commandframework.arguments.parser.ParserParameter;
|
||||
import cloud.commandframework.arguments.parser.StandardParameters;
|
||||
import cloud.commandframework.execution.CommandExecutionHandler;
|
||||
import cloud.commandframework.extra.confirmation.CommandConfirmationManager;
|
||||
import cloud.commandframework.meta.CommandMeta;
|
||||
import cloud.commandframework.meta.SimpleCommandMeta;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import cloud.commandframework.arguments.parser.ParserParameters;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Parser that parses class instances {@link Command commands}
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public final class AnnotationParser<C> {
|
||||
|
||||
private final SyntaxParser syntaxParser = new SyntaxParser();
|
||||
private final ArgumentExtractor argumentExtractor = new ArgumentExtractor();
|
||||
|
||||
private final CommandManager<C> manager;
|
||||
private final Map<Class<? extends Annotation>, Function<? extends Annotation, ParserParameters>> annotationMappers;
|
||||
private final Class<C> commandSenderClass;
|
||||
private final MetaFactory metaFactory;
|
||||
|
||||
/**
|
||||
* Construct a new annotation parser
|
||||
*
|
||||
* @param manager Command manager instance
|
||||
* @param commandSenderClass Command sender class
|
||||
* @param metaMapper Function that is used to create {@link CommandMeta} instances from annotations on the
|
||||
* command methods. These annotations will be mapped to
|
||||
* {@link ParserParameter}. Mappers for the
|
||||
* parser parameters can be registered using {@link #registerAnnotationMapper(Class, Function)}
|
||||
*/
|
||||
public AnnotationParser(@Nonnull final CommandManager<C> manager,
|
||||
@Nonnull final Class<C> commandSenderClass,
|
||||
@Nonnull final Function<ParserParameters, CommandMeta> metaMapper) {
|
||||
this.commandSenderClass = commandSenderClass;
|
||||
this.manager = manager;
|
||||
this.metaFactory = new MetaFactory(this, metaMapper);
|
||||
this.annotationMappers = Maps.newHashMap();
|
||||
this.registerAnnotationMapper(CommandDescription.class, d ->
|
||||
ParserParameters.single(StandardParameters.DESCRIPTION, d.value()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an annotation mapper
|
||||
*
|
||||
* @param annotation Annotation class
|
||||
* @param mapper Mapping function
|
||||
* @param <A> Annotation type
|
||||
*/
|
||||
public <A extends Annotation> void registerAnnotationMapper(@Nonnull final Class<A> annotation,
|
||||
@Nonnull final Function<A, ParserParameters> mapper) {
|
||||
this.annotationMappers.put(annotation, mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan a class instance of {@link CommandMethod} annotations and attempt to
|
||||
* compile them into {@link Command} instances
|
||||
*
|
||||
* @param instance Instance to scan
|
||||
* @param <T> Type of the instance
|
||||
* @return Collection of parsed annotations
|
||||
*/
|
||||
@Nonnull
|
||||
public <T> Collection<Command<C>> parse(@Nonnull final T instance) {
|
||||
final Method[] methods = instance.getClass().getDeclaredMethods();
|
||||
final Collection<CommandMethodPair> commandMethodPairs = new ArrayList<>();
|
||||
for (final Method method : methods) {
|
||||
final CommandMethod commandMethod = method.getAnnotation(CommandMethod.class);
|
||||
if (commandMethod == null) {
|
||||
continue;
|
||||
}
|
||||
if (!method.isAccessible()) {
|
||||
method.setAccessible(true);
|
||||
}
|
||||
if (method.getReturnType() != Void.TYPE) {
|
||||
throw new IllegalArgumentException(String.format("@CommandMethod annotated method '%s' has non-void return type",
|
||||
method.getName()));
|
||||
}
|
||||
commandMethodPairs.add(new CommandMethodPair(method, commandMethod));
|
||||
}
|
||||
final Collection<Command<C>> commands = this.construct(instance, commandMethodPairs);
|
||||
for (final Command<C> command : commands) {
|
||||
@SuppressWarnings("ALL") final CommandManager commandManager = this.manager;
|
||||
//noinspection all
|
||||
commandManager.command(command);
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@SuppressWarnings("unchecked")
|
||||
private Collection<Command<C>> construct(@Nonnull final Object instance,
|
||||
@Nonnull final Collection<CommandMethodPair> methodPairs) {
|
||||
final Collection<Command<C>> commands = new ArrayList<>();
|
||||
for (final CommandMethodPair commandMethodPair : methodPairs) {
|
||||
final CommandMethod commandMethod = commandMethodPair.getCommandMethod();
|
||||
final Method method = commandMethodPair.getMethod();
|
||||
final LinkedHashMap<String, SyntaxFragment> tokens = this.syntaxParser.apply(commandMethod.value());
|
||||
/* Determine command name */
|
||||
final String commandToken = commandMethod.value().split(" ")[0].split("\\|")[0];
|
||||
@SuppressWarnings("ALL") final CommandManager manager = this.manager;
|
||||
final SimpleCommandMeta.Builder metaBuilder = SimpleCommandMeta.builder()
|
||||
.with(this.metaFactory.apply(method.getAnnotations()));
|
||||
if (method.isAnnotationPresent(Confirmation.class)) {
|
||||
metaBuilder.with(CommandConfirmationManager.CONFIRMATION_REQUIRED_META, "true");
|
||||
}
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
Command.Builder builder = manager.commandBuilder(commandToken,
|
||||
tokens.get(commandToken).getMinor(),
|
||||
metaBuilder.build());
|
||||
final Collection<ArgumentParameterPair> arguments = this.argumentExtractor.apply(method);
|
||||
final Map<String, CommandArgument<C, ?>> commandArguments = Maps.newHashMap();
|
||||
final Map<CommandArgument<C, ?>, String> argumentDescriptions = Maps.newHashMap();
|
||||
/* Go through all annotated parameters and build up the argument tree */
|
||||
for (final ArgumentParameterPair argumentPair : arguments) {
|
||||
final CommandArgument<C, ?> argument = this.buildArgument(method,
|
||||
tokens.get(argumentPair.getArgument().value()),
|
||||
argumentPair);
|
||||
commandArguments.put(argument.getName(), argument);
|
||||
argumentDescriptions.put(argument, argumentPair.getArgument().description());
|
||||
}
|
||||
boolean commandNameFound = false;
|
||||
/* Build the command tree */
|
||||
for (final Map.Entry<String, SyntaxFragment> entry : tokens.entrySet()) {
|
||||
if (!commandNameFound) {
|
||||
commandNameFound = true;
|
||||
continue;
|
||||
}
|
||||
if (entry.getValue().getArgumentMode() == ArgumentMode.LITERAL) {
|
||||
builder = builder.literal(entry.getKey(), entry.getValue().getMinor().toArray(new String[0]));
|
||||
} else {
|
||||
final CommandArgument<C, ?> argument = commandArguments.get(entry.getKey());
|
||||
if (argument == null) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Found no mapping for argument '%s' in method '%s'",
|
||||
entry.getKey(), method.getName()
|
||||
));
|
||||
}
|
||||
|
||||
final String description = argumentDescriptions.getOrDefault(argument, "");
|
||||
builder = builder.argument(argument, Description.of(description));
|
||||
}
|
||||
}
|
||||
/* Try to find the command sender type */
|
||||
Class<? extends C> senderType = null;
|
||||
for (final Parameter parameter : method.getParameters()) {
|
||||
if (parameter.isAnnotationPresent(Argument.class)) {
|
||||
continue;
|
||||
}
|
||||
if (this.commandSenderClass.isAssignableFrom(parameter.getType())) {
|
||||
senderType = (Class<? extends C>) parameter.getType();
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Decorate command data */
|
||||
builder = builder.withPermission(commandMethod.permission());
|
||||
if (commandMethod.requiredSender() != Object.class) {
|
||||
builder = builder.withSenderType(commandMethod.requiredSender());
|
||||
} else if (senderType != null) {
|
||||
builder = builder.withSenderType(senderType);
|
||||
}
|
||||
try {
|
||||
/* Construct the handler */
|
||||
final CommandExecutionHandler<C> commandExecutionHandler
|
||||
= new MethodCommandExecutionHandler<>(instance, commandArguments, method);
|
||||
builder = builder.handler(commandExecutionHandler);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException("Failed to construct command execution handler", e);
|
||||
}
|
||||
/* Check if the command should be hidden */
|
||||
if (method.isAnnotationPresent(Hidden.class)) {
|
||||
builder = builder.hidden();
|
||||
}
|
||||
/* Construct and register the command */
|
||||
final Command<C> builtCommand = builder.build();
|
||||
commands.add(builtCommand);
|
||||
/* Check if we need to construct a proxy */
|
||||
if (method.isAnnotationPresent(ProxiedBy.class)) {
|
||||
final ProxiedBy proxyAnnotation = method.getAnnotation(ProxiedBy.class);
|
||||
final String proxy = proxyAnnotation.value();
|
||||
if (proxy.contains(" ")) {
|
||||
throw new IllegalArgumentException("@ProxiedBy proxies may only contain single literals");
|
||||
}
|
||||
Command.Builder<C> proxyBuilder = manager.commandBuilder(proxy, builtCommand.getCommandMeta())
|
||||
.proxies(builtCommand);
|
||||
if (proxyAnnotation.hidden()) {
|
||||
proxyBuilder = proxyBuilder.hidden();
|
||||
}
|
||||
manager.command(proxyBuilder.build());
|
||||
}
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@SuppressWarnings("unchecked")
|
||||
private CommandArgument<C, ?> buildArgument(@Nonnull final Method method,
|
||||
@Nullable final SyntaxFragment syntaxFragment,
|
||||
@Nonnull final ArgumentParameterPair argumentPair) {
|
||||
final Parameter parameter = argumentPair.getParameter();
|
||||
final Collection<Annotation> annotations = Arrays.asList(parameter.getAnnotations());
|
||||
final TypeToken<?> token = TypeToken.of(parameter.getParameterizedType());
|
||||
final ParserParameters parameters = this.manager.getParserRegistry()
|
||||
.parseAnnotations(token, annotations);
|
||||
|
||||
final ArgumentParser<C, ?> parser;
|
||||
if (argumentPair.getArgument().parserName().isEmpty()) {
|
||||
parser = this.manager.getParserRegistry()
|
||||
.createParser(token, parameters)
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
String.format("Parameter '%s' in method '%s' "
|
||||
+ "has parser '%s' but no parser exists "
|
||||
+ "for that type",
|
||||
parameter.getName(), method.getName(),
|
||||
token.toString())));
|
||||
} else {
|
||||
parser = this.manager.getParserRegistry()
|
||||
.createParser(argumentPair.getArgument().parserName(), parameters)
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
String.format("Parameter '%s' in method '%s' "
|
||||
+ "has parser '%s' but no parser exists "
|
||||
+ "for that type",
|
||||
parameter.getName(), method.getName(),
|
||||
token.toString())));
|
||||
}
|
||||
|
||||
if (syntaxFragment == null || syntaxFragment.getArgumentMode() == ArgumentMode.LITERAL) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Invalid command argument '%s' in method '%s': "
|
||||
+ "Missing syntax mapping", argumentPair.getArgument().value(), method.getName()));
|
||||
}
|
||||
final Argument argument = argumentPair.getArgument();
|
||||
@SuppressWarnings("ALL") final CommandArgument.Builder argumentBuilder = CommandArgument.ofType(parameter.getType(),
|
||||
argument.value());
|
||||
if (syntaxFragment.getArgumentMode() == ArgumentMode.OPTIONAL) {
|
||||
if (argument.defaultValue().isEmpty()) {
|
||||
argumentBuilder.asOptional();
|
||||
} else {
|
||||
argumentBuilder.asOptionalWithDefault(argument.defaultValue());
|
||||
}
|
||||
} else {
|
||||
argumentBuilder.asRequired();
|
||||
}
|
||||
return argumentBuilder.manager(this.manager).withParser(parser).build();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
Map<Class<? extends Annotation>, Function<? extends Annotation, ParserParameters>> getAnnotationMappers() {
|
||||
return this.annotationMappers;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Annotation used to indicate that a method parameter is a command argument
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface Argument {
|
||||
|
||||
/**
|
||||
* Argument name
|
||||
*
|
||||
* @return Argument name
|
||||
*/
|
||||
String value();
|
||||
|
||||
/**
|
||||
* Name of the argument parser
|
||||
*
|
||||
* @return Argument name
|
||||
*/
|
||||
String parserName() default "";
|
||||
|
||||
/**
|
||||
* Get the default value
|
||||
*
|
||||
* @return Default value
|
||||
*/
|
||||
String defaultValue() default "";
|
||||
|
||||
/**
|
||||
* The argument description
|
||||
*
|
||||
* @return Argument description
|
||||
*/
|
||||
String description() default "";
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Utility that extract {@link Argument arguments} from
|
||||
* {@link java.lang.reflect.Method method} {@link java.lang.reflect.Parameter parameters}
|
||||
*/
|
||||
class ArgumentExtractor implements Function<Method, Collection<ArgumentParameterPair>> {
|
||||
|
||||
@Override
|
||||
public Collection<ArgumentParameterPair> apply(@Nonnull final Method method) {
|
||||
final Collection<ArgumentParameterPair> arguments = new ArrayList<>();
|
||||
for (final Parameter parameter : method.getParameters()) {
|
||||
if (!parameter.isAnnotationPresent(Argument.class)) {
|
||||
continue;
|
||||
}
|
||||
arguments.add(new ArgumentParameterPair(parameter, parameter.getAnnotation(Argument.class)));
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
enum ArgumentMode {
|
||||
LITERAL,
|
||||
OPTIONAL,
|
||||
REQUIRED
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
final class ArgumentParameterPair {
|
||||
|
||||
private final Parameter parameter;
|
||||
private final Argument argument;
|
||||
|
||||
ArgumentParameterPair(@Nonnull final Parameter parameter, @Nonnull final Argument argument) {
|
||||
this.parameter = parameter;
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
Parameter getParameter() {
|
||||
return this.parameter;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
Argument getArgument() {
|
||||
return this.argument;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import cloud.commandframework.arguments.parser.StandardParameters;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Maps to {@link StandardParameters#DESCRIPTION}
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface CommandDescription {
|
||||
|
||||
/**
|
||||
* Command description
|
||||
*
|
||||
* @return Command syntax
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Used to declare a class method as a command method
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface CommandMethod {
|
||||
|
||||
/**
|
||||
* Command syntax
|
||||
*
|
||||
* @return Command syntax
|
||||
*/
|
||||
String value();
|
||||
|
||||
/**
|
||||
* The command permission node
|
||||
*
|
||||
* @return Command permission node
|
||||
*/
|
||||
String permission() default "";
|
||||
|
||||
/**
|
||||
* The required sender
|
||||
*
|
||||
* @return Required sender
|
||||
*/
|
||||
Class<?> requiredSender() default Object.class;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
final class CommandMethodPair {
|
||||
|
||||
private final Method method;
|
||||
private final CommandMethod commandMethod;
|
||||
|
||||
CommandMethodPair(@Nonnull final Method method, @Nonnull final CommandMethod commandMethod) {
|
||||
this.method = method;
|
||||
this.commandMethod = commandMethod;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
Method getMethod() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
CommandMethod getCommandMethod() {
|
||||
return this.commandMethod;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Require confirmation for the command
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Confirmation {
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import cloud.commandframework.Command;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Indicates that the command should be hidden. Similar to {@link Command.Builder#hidden()}
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Hidden {
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import cloud.commandframework.arguments.parser.ParserParameters;
|
||||
import cloud.commandframework.meta.CommandMeta;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.function.Function;
|
||||
|
||||
class MetaFactory implements Function<Annotation[], CommandMeta> {
|
||||
|
||||
private final AnnotationParser<?> annotationParser;
|
||||
private final Function<ParserParameters, CommandMeta> metaMapper;
|
||||
|
||||
MetaFactory(@Nonnull final AnnotationParser<?> annotationParser,
|
||||
@Nonnull final Function<ParserParameters, CommandMeta> metaMapper) {
|
||||
this.annotationParser = annotationParser;
|
||||
this.metaMapper = metaMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandMeta apply(@Nonnull final Annotation[] annotations) {
|
||||
final ParserParameters parameters = ParserParameters.empty();
|
||||
for (final Annotation annotation : annotations) {
|
||||
@SuppressWarnings("ALL") final Function function = this.annotationParser.getAnnotationMappers()
|
||||
.get(annotation.annotationType());
|
||||
if (function == null) {
|
||||
continue;
|
||||
}
|
||||
//noinspection unchecked
|
||||
parameters.merge((ParserParameters) function.apply(annotation));
|
||||
}
|
||||
return this.metaMapper.apply(parameters);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import cloud.commandframework.execution.CommandExecutionHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class MethodCommandExecutionHandler<C> implements CommandExecutionHandler<C> {
|
||||
|
||||
private final Parameter[] parameters;
|
||||
private final MethodHandle methodHandle;
|
||||
private final Map<String, CommandArgument<C, ?>> commandArguments;
|
||||
|
||||
MethodCommandExecutionHandler(@Nonnull final Object instance,
|
||||
@Nonnull final Map<String, CommandArgument<C, ?>> commandArguments,
|
||||
@Nonnull final Method method) throws Exception {
|
||||
this.commandArguments = commandArguments;
|
||||
method.setAccessible(true);
|
||||
this.methodHandle = MethodHandles.lookup().unreflect(method).bindTo(instance);
|
||||
this.parameters = method.getParameters();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(@Nonnull final CommandContext<C> commandContext) {
|
||||
final List<Object> arguments = new ArrayList<>(this.parameters.length);
|
||||
|
||||
/* Bind parameters to context */
|
||||
for (final Parameter parameter : this.parameters) {
|
||||
if (parameter.isAnnotationPresent(Argument.class)) {
|
||||
final Argument argument = parameter.getAnnotation(Argument.class);
|
||||
final CommandArgument<C, ?> commandArgument = this.commandArguments.get(argument.value());
|
||||
if (commandArgument.isRequired()) {
|
||||
arguments.add(commandContext.getRequired(argument.value()));
|
||||
} else {
|
||||
final Object optional = commandContext.get(argument.value()).orElse(null);
|
||||
arguments.add(optional);
|
||||
}
|
||||
} else {
|
||||
if (parameter.getType().isAssignableFrom(commandContext.getSender().getClass())) {
|
||||
arguments.add(commandContext.getSender());
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Unknown command parameter '%s' in method '%s'",
|
||||
parameter.getName(), this.methodHandle.toString()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Invoke the command method */
|
||||
try {
|
||||
this.methodHandle.invokeWithArguments(arguments);
|
||||
} catch (final Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import cloud.commandframework.Command;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Creates a command proxy for the command. This is similar to
|
||||
* {@link Command.Builder#proxies(Command)}.
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ProxiedBy {
|
||||
|
||||
/**
|
||||
* Syntax of the proxying command
|
||||
*
|
||||
* @return Proxy syntax
|
||||
*/
|
||||
@Nonnull String value();
|
||||
|
||||
/**
|
||||
* Whether or not the proxying command should be {@link Hidden}
|
||||
*
|
||||
* @return {@code true} if the proxying command should be hidden, {@code false} if not
|
||||
*/
|
||||
boolean hidden() default false;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
|
||||
final class SyntaxFragment {
|
||||
|
||||
private final String major;
|
||||
private final List<String> minor;
|
||||
private final ArgumentMode argumentMode;
|
||||
|
||||
SyntaxFragment(@Nonnull final String major,
|
||||
@Nonnull final List<String> minor,
|
||||
@Nonnull final ArgumentMode argumentMode) {
|
||||
this.major = major;
|
||||
this.minor = minor;
|
||||
this.argumentMode = argumentMode;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
String getMajor() {
|
||||
return this.major;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
List<String> getMinor() {
|
||||
return this.minor;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
ArgumentMode getArgumentMode() {
|
||||
return this.argumentMode;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Parses command syntax into syntax fragments
|
||||
*/
|
||||
final class SyntaxParser implements Function<String, LinkedHashMap<String, SyntaxFragment>> {
|
||||
|
||||
private static final Predicate<String> PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*")
|
||||
.asPredicate();
|
||||
private static final Predicate<String> PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<([A-Za-z0-9]+)>")
|
||||
.asPredicate();
|
||||
private static final Predicate<String> PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)]")
|
||||
.asPredicate();
|
||||
|
||||
@Override
|
||||
public LinkedHashMap<String, SyntaxFragment> apply(@Nonnull final String syntax) {
|
||||
final StringTokenizer stringTokenizer = new StringTokenizer(syntax, " ");
|
||||
final LinkedHashMap<String, SyntaxFragment> map = new LinkedHashMap<>();
|
||||
while (stringTokenizer.hasMoreTokens()) {
|
||||
final String token = stringTokenizer.nextToken();
|
||||
String major;
|
||||
List<String> minor = new ArrayList<>();
|
||||
ArgumentMode mode;
|
||||
if (PATTERN_ARGUMENT_REQUIRED.test(token)) {
|
||||
major = token.substring(1, token.length() - 1);
|
||||
mode = ArgumentMode.REQUIRED;
|
||||
} else if (PATTERN_ARGUMENT_OPTIONAL.test(token)) {
|
||||
major = token.substring(1, token.length() - 1);
|
||||
mode = ArgumentMode.OPTIONAL;
|
||||
} else if (PATTERN_ARGUMENT_LITERAL.test(token)) {
|
||||
final String[] literals = token.split("\\|");
|
||||
/* Actually use the other literals as well */
|
||||
major = literals[0];
|
||||
minor.addAll(Arrays.asList(literals).subList(1, literals.length));
|
||||
mode = ArgumentMode.LITERAL;
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format("Unrecognizable syntax token '%s'", syntax));
|
||||
}
|
||||
map.put(major, new SyntaxFragment(major, minor, mode));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
/**
|
||||
* Annotation parsing related classes
|
||||
*/
|
||||
package cloud.commandframework.annotations;
|
||||
Loading…
Add table
Add a link
Reference in a new issue