aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java
blob: 7e1f1caab04858dedcc0b0b08ad75f646342b407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * This file is part of OneConfig.
 * OneConfig - Next Generation Config Library for Minecraft: Java Edition
 * Copyright (C) 2021, 2022 Polyfrost.
 *   <https://polyfrost.cc> <https://github.com/Polyfrost/>
 * Co-author: Pinkulu <pinkulumc@gmail.com> <https://github.com/pinkulu>
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *   OneConfig is licensed under the terms of version 3 of the GNU Lesser
 * General Public License as published by the Free Software Foundation, AND
 * under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
 * either version 1.0 of the Additional Terms, 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.  If not, see <https://www.gnu.org/licenses/>. You should
 * have also received a copy of the Additional Terms Applicable
 * to OneConfig, as published by Polyfrost. If not, see
 * <https://polyfrost.cc/legal/oneconfig/additional-terms>
 */

package cc.polyfrost.oneconfig.utils.commands.annotations;

import cc.polyfrost.oneconfig.internal.command.OneConfigCommand;
import cc.polyfrost.oneconfig.libs.universal.ChatColor;
import cc.polyfrost.oneconfig.utils.commands.CommandManager;
import cc.polyfrost.oneconfig.utils.commands.arguments.ArgumentParser;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Marks a class as a command.
 * <p>
 * To start, create a class which is annotated with this annotation, and then a method which is annotated with {@link Main}.
 * <pre>{@code
 *     @literal @Command(name = "test", description = "A test command", aliases = {"t"})
 *     public class TestCommand {
 *         @literal @Main
 *         public static void mainCommandMethod() {
 *             // do stuff
 *         }
 *     }
 *     }</pre>
 * <p>
 * Keep in mind how {@code mainCommandMethod} is a private and static method.
 * With OneConfig's command utility, methods for commands can be any kind of visibility, and <b><i>must be static</i></b>.
 * If the methods are not static, the {@link CommandManager} will throw an exception.
 *
 * <p>
 * Command methods can also having multiple parameters of virtually any type, as long as it is a
 * {@link String}, {@code boolean}, {@code int}, {@code double}, {@code float},
 * or is added as an {@link ArgumentParser} and added to the
 * {@link CommandManager} via {@link CommandManager#addParser(ArgumentParser)}.
 * Parameters can also be annotated with various annotations, such as {@link Name}, which names the parameter
 * for the user, {@link Greedy}, which takes all following arguments along with itself, and more to come.
 * For example, the following command method:
 * <pre>{@code
 *     @literal @Main
 *     private static void mainCommandMethod(String arg1, @Name("nameOfSecondArgument") boolean arg2, int arg3, double arg4, float arg5, @Greedy String greedyArgument) {
 *         // do things here
 *         System.out.println(greedyArgument); // Greedily takes all remaining arguments after greedyArgument as well as itself. 1984
 *     }
 *     }</pre>
 * </p>
 *
 * <p>
 * Of course, {@link SubCommand}s can be added and "stacked". For example, the following command class:
 * <pre>{@code
 *     @literal @Command(name = "mycommand", aliases = {"alias1"}, description = "My command description")
 *     public class TestCommand {
 *         @literal @Main
 *         private static void mainCommandMethod() {
 *             // do things here
 *         }
 *
 *         @literal @SubCommand(name = "subcommand", aliases = {"subalias1"}, description = "My subcommand description")
 *         private static class SubCommandClass {
 *             @literal @Main
 *             private static void subCommandMethod() {
 *                 // do things here
 *             }
 *
 *             @literal @SubCommand(name = "subsubcommand", aliases = {"subsubalias1"}, description = "My subsubcommand description")
 *             private static class SubSubCommandClass {
 *                 @literal @Main
 *                 private static void subSubCommandMethod() {
 *                     // do things here
 *                 }
 *             }
 *         }
 *     }
 *     }</pre>
 * </p>
 * <p>
 * To register commands, use {@link CommandManager#registerCommand(Class)}.
 *
 * <p>
 * Note: if you're viewing this in IntelliJ or just see the @literal tag everywhere, please ignore that.
 * </p>
 *
 * @see OneConfigCommand
 * @see Main
 * @see CommandManager
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Command {
    /**
     * The name of the command.
     *
     * @return The name of the command.
     */
    String value();

    /**
     * The aliases of the command.
     *
     * @return The aliases of the command.
     */
    String[] aliases() default {};

    /**
     * The description of the command.
     *
     * @return The description of the command.
     */
    String description() default "";

    /**
     * The color of the command.
     *
     * @return The color of the command.
     */
    ChatColor color() default ChatColor.GOLD;

    /**
     * Whether the command generates a help command.
     */
    boolean helpCommand() default true;
}