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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021, 2022 Polyfrost and Pinkulu.
* <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;
import cc.polyfrost.oneconfig.libs.universal.ChatColor;
import cc.polyfrost.oneconfig.utils.commands.annotations.Command;
import cc.polyfrost.oneconfig.utils.commands.annotations.Main;
import cc.polyfrost.oneconfig.utils.commands.annotations.SubCommand;
import cc.polyfrost.oneconfig.utils.commands.arguments.*;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.*;
/**
* Handles the registration of OneConfig commands.
*
* @see Command
*/
public class CommandManager {
private static final PlatformCommandManager platform = ServiceLoader.load(PlatformCommandManager.class, PlatformCommandManager.class.getClassLoader()).iterator().next();
public static final CommandManager INSTANCE = new CommandManager();
static final String NOT_FOUND_TEXT = "Command not found! Type /@ROOT_COMMAND@ help for help.";
static final String TOO_MANY_PARAMETERS = "There were too many / little parameters for this command! Type /@ROOT_COMMAND@ help for help.";
static final String METHOD_RUN_ERROR = "Error while running @ROOT_COMMAND@ method! Please report this to the developer.";
final HashMap<Class<?>, ArgumentParser<?>> parsers = new HashMap<>();
private CommandManager() {
addParser(new StringParser());
addParser(new IntegerParser());
addParser(new IntegerParser(), Integer.TYPE);
addParser(new DoubleParser());
addParser(new DoubleParser(), Double.TYPE);
addParser(new FloatParser());
addParser(new FloatParser(), Float.TYPE);
addParser(new BooleanParser());
addParser(new BooleanParser(), Boolean.TYPE);
}
/**
* Adds a parser to the parsers map.
*
* @param parser The parser to add.
* @param clazz The class of the parser.
*/
public void addParser(ArgumentParser<?> parser, Class<?> clazz) {
parsers.put(clazz, parser);
}
/**
* Adds a parser to the parsers map.
*
* @param parser The parser to add.
*/
public void addParser(ArgumentParser<?> parser) {
addParser(parser, parser.typeClass);
}
/**
* Registers the provided command.
*
* @param clazz The command to register as a class.
*/
public void registerCommand(Class<?> clazz) {
if (clazz.isAnnotationPresent(Command.class)) {
final Command annotation = clazz.getAnnotation(Command.class);
final InternalCommand root = new InternalCommand(annotation.value(), annotation.aliases(), annotation.description().trim().isEmpty()
? "Main command for " + annotation.value() : annotation.description(), annotation.color(), null);
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(Main.class) && method.getParameterCount() == 0) {
root.invokers.add(new InternalCommand.InternalCommandInvoker(annotation.value(), annotation.aliases(), method, root));
break;
}
}
addToInvokers(clazz.getDeclaredClasses(), root);
platform.createCommand(root, annotation);
}
}
private void addToInvokers(Class<?>[] classes, InternalCommand parent) {
for (Class<?> clazz : classes) {
if (clazz.isAnnotationPresent(SubCommand.class)) {
SubCommand annotation = clazz.getAnnotation(SubCommand.class);
InternalCommand command = new InternalCommand(annotation.value(), annotation.aliases(), annotation.description(), annotation.color() == ChatColor.RESET ? parent.color : annotation.color(), parent);
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(Main.class)) {
command.invokers.add(new InternalCommand.InternalCommandInvoker(annotation.value(), annotation.aliases(), method, command));
}
}
parent.children.add(command);
addToInvokers(clazz.getDeclaredClasses(), command);
}
}
}
static class CustomError {
public String message;
public CustomError(String message) {
this.message = message;
}
}
static class InternalCommand {
public final String name;
public final String[] aliases;
public final String description;
public final ChatColor color;
public final ArrayList<InternalCommandInvoker> invokers = new ArrayList<>();
public final InternalCommand parent;
public final ArrayList<InternalCommand> children = new ArrayList<>();
public InternalCommand(String name, String[] aliases, String description, ChatColor color, InternalCommand parent) {
this.name = name;
this.aliases = aliases;
this.description = description;
this.parent = parent;
this.color = color;
}
public boolean isValid(String name, boolean tabCompletion) {
String lowerCaseName = this.name.toLowerCase(Locale.ENGLISH);
String lowerCaseOtherName = name.toLowerCase(Locale.ENGLISH);
if (!tabCompletion ? lowerCaseName.equals(lowerCaseOtherName) : lowerCaseName.startsWith(lowerCaseOtherName)) {
return true;
} else {
for (String alias : aliases) {
String lowerCaseAlias = alias.toLowerCase(Locale.ENGLISH);
if (!tabCompletion ? lowerCaseAlias.equals(lowerCaseOtherName) : lowerCaseAlias.startsWith(lowerCaseOtherName)) {
return true;
}
}
}
return false;
}
@Override
public String toString() {
return "InternalCommand{" +
"name='" + name + '\'' +
", aliases=" + Arrays.toString(aliases) +
", description='" + description + '\'' +
", invokers=" + invokers +
'}';
}
public static class InternalCommandInvoker {
public final String name;
public final String[] aliases;
public final Method method;
public final Parameter[] parameterTypes;
public final InternalCommand parent;
public InternalCommandInvoker(String name, String[] aliases, Method method, InternalCommand parent) {
if (!Modifier.isStatic(method.getModifiers())) {
throw new IllegalArgumentException("All command methods must be static!");
}
this.name = name;
this.aliases = aliases;
this.method = method;
this.parameterTypes = method.getParameters().clone();
this.parent = parent;
if (Modifier.isPrivate(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
method.setAccessible(true);
}
}
@Override
public String toString() {
return "InternalCommandInvoker{" +
"name='" + name + '\'' +
", aliases=" + Arrays.toString(aliases) +
", method=" + method +
", parameterTypes=" + Arrays.toString(parameterTypes) +
'}';
}
}
}
}
|