From 64fa3430a9a09a279e96a482df3f19a96ad6b775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Fri, 25 Sep 2020 02:31:20 +0200 Subject: [PATCH] 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. --- .../annotations/AnnotationParser.java | 16 +++++++- .../commands/annotations/Hidden.java | 37 +++++++++++++++++++ .../commands/annotations/ProxiedBy.java | 7 ++++ .../intellectualsites/commands/Command.java | 20 ++++++++++ .../arguments/parser/StandardParameters.java | 4 ++ 5 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/Hidden.java diff --git a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java index dc2d9fb4..26c2c51b 100644 --- a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java +++ b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java @@ -148,6 +148,7 @@ public final class AnnotationParser { 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 { } 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 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 proxyBuilder = manager.commandBuilder(proxy, builtCommand.getCommandMeta()) + .proxies(builtCommand); + if (proxyAnnotation.hidden()) { + proxyBuilder = proxyBuilder.hidden(); + } + manager.command(proxyBuilder.build()); } } return commands; diff --git a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/Hidden.java b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/Hidden.java new file mode 100644 index 00000000..e8ac3b48 --- /dev/null +++ b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/Hidden.java @@ -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 { +} diff --git a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/ProxiedBy.java b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/ProxiedBy.java index fd52e572..7c688501 100644 --- a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/ProxiedBy.java +++ b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/ProxiedBy.java @@ -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; + } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/Command.java b/cloud-core/src/main/java/com/intellectualsites/commands/Command.java index 3b112758..e9ddd9b9 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/Command.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/Command.java @@ -226,6 +226,15 @@ public class Command { 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 @@ -437,6 +446,17 @@ public class Command { 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 hidden() { + return this.meta("hidden", "true"); + } + /** * Build a command using the builder instance * diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParameters.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParameters.java index efe46e12..9327759b 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParameters.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParameters.java @@ -52,6 +52,10 @@ public final class StandardParameters { * Command completions */ public static final ParserParameter COMPLETIONS = create("completions", TypeToken.of(String[].class)); + /** + * The command should be hidden from help menus, etc + */ + public static final ParserParameter HIDDEN = create("hidden", TypeToken.of(Boolean.class)); private StandardParameters() { }