Add the ability to "hide" commands.

This does not yet change how commands are treated, but allows for this to be implemented in the future.
This commit is contained in:
Alexander Söderberg 2020-09-25 02:31:20 +02:00
parent c980adac3b
commit 64fa3430a9
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
5 changed files with 82 additions and 2 deletions

View file

@ -148,6 +148,7 @@ public final class AnnotationParser<C> {
if (method.isAnnotationPresent(Confirmation.class)) {
metaBuilder.with(CommandConfirmationManager.CONFIRMATION_REQUIRED_META, "true");
}
@SuppressWarnings("ALL")
Command.Builder builder = manager.commandBuilder(commandToken,
tokens.get(commandToken).getMinor(),
@ -211,15 +212,26 @@ public final class AnnotationParser<C> {
} 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 String proxy = method.getAnnotation(ProxiedBy.class).value();
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");
}
manager.command(manager.commandBuilder(proxy, builtCommand.getCommandMeta()).proxies(builtCommand).build());
Command.Builder<C> proxyBuilder = manager.commandBuilder(proxy, builtCommand.getCommandMeta())
.proxies(builtCommand);
if (proxyAnnotation.hidden()) {
proxyBuilder = proxyBuilder.hidden();
}
manager.command(proxyBuilder.build());
}
}
return commands;

View file

@ -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 com.intellectualsites.commands.annotations;
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 com.intellectualsites.commands.Command.Builder#hidden()}
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Hidden {
}

View file

@ -44,4 +44,11 @@ public @interface ProxiedBy {
*/
@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;
}