blob: 063d381b8cf6cd7926ab0b75329b5e935ccb642a (
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
|
package io.dico.dicore.command.parameter.type;
import io.dico.dicore.command.CommandException;
import io.dico.dicore.command.parameter.ArgumentBuffer;
import io.dico.dicore.command.parameter.Parameter;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.List;
public class EnumParameterType<E extends Enum> extends SimpleParameterType<E, Void> {
private final E[] universe;
public EnumParameterType(Class<E> returnType) {
super(returnType);
universe = returnType.getEnumConstants();
if (universe == null) {
throw new IllegalArgumentException("returnType must be an enum");
}
}
@Override
protected E parse(Parameter<E, Void> parameter, CommandSender sender, String input) throws CommandException {
for (E constant : universe) {
if (constant.name().equalsIgnoreCase(input)) {
return constant;
}
}
throw CommandException.invalidArgument(parameter.getName(), "the enum value does not exist");
}
@Override
public List<String> complete(Parameter<E, Void> parameter, CommandSender sender, Location location, ArgumentBuffer buffer) {
String input = buffer.next().toUpperCase();
List<String> result = new ArrayList<>();
for (E constant : universe) {
if (constant.name().toUpperCase().startsWith(input.toUpperCase())) {
result.add(constant.name().toLowerCase());
}
}
return result;
}
}
|