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)) { if (method.isAnnotationPresent(Confirmation.class)) {
metaBuilder.with(CommandConfirmationManager.CONFIRMATION_REQUIRED_META, "true"); metaBuilder.with(CommandConfirmationManager.CONFIRMATION_REQUIRED_META, "true");
} }
@SuppressWarnings("ALL") @SuppressWarnings("ALL")
Command.Builder builder = manager.commandBuilder(commandToken, Command.Builder builder = manager.commandBuilder(commandToken,
tokens.get(commandToken).getMinor(), tokens.get(commandToken).getMinor(),
@ -211,15 +212,26 @@ public final class AnnotationParser<C> {
} catch (final Exception e) { } catch (final Exception e) {
throw new RuntimeException("Failed to construct command execution handler", 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(); final Command<C> builtCommand = builder.build();
commands.add(builtCommand); commands.add(builtCommand);
/* Check if we need to construct a proxy */ /* Check if we need to construct a proxy */
if (method.isAnnotationPresent(ProxiedBy.class)) { 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(" ")) { if (proxy.contains(" ")) {
throw new IllegalArgumentException("@ProxiedBy proxies may only contain single literals"); 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; 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(); @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;
} }

View file

@ -226,6 +226,15 @@ public class Command<C> {
return this.arguments.get(argument).getDescription(); return this.arguments.get(argument).getDescription();
} }
/**
* Check whether or not the command is hidden
*
* @return {@code true} if the command is hidden, {@code false} if not
*/
public boolean isHidden() {
return this.getCommandMeta().getOrDefault("hidden", "true").equals("true");
}
/** /**
* Builder for {@link Command} instances. The builder is immutable, and each * Builder for {@link Command} instances. The builder is immutable, and each
@ -437,6 +446,17 @@ public class Command<C> {
return builder.handler(command.commandExecutionHandler); return builder.handler(command.commandExecutionHandler);
} }
/**
* Indicate that the command should be hidden from help menus
* and other places where commands are exposed to users
*
* @return New builder instance that indicates that the constructed command should be hidden
*/
@Nonnull
public Builder<C> hidden() {
return this.meta("hidden", "true");
}
/** /**
* Build a command using the builder instance * Build a command using the builder instance
* *

View file

@ -52,6 +52,10 @@ public final class StandardParameters {
* Command completions * Command completions
*/ */
public static final ParserParameter<String[]> COMPLETIONS = create("completions", TypeToken.of(String[].class)); public static final ParserParameter<String[]> COMPLETIONS = create("completions", TypeToken.of(String[].class));
/**
* The command should be hidden from help menus, etc
*/
public static final ParserParameter<Boolean> HIDDEN = create("hidden", TypeToken.of(Boolean.class));
private StandardParameters() { private StandardParameters() {
} }