|
| 1 | +package xyz.damt.command.command; |
| 2 | + |
| 3 | +import org.bukkit.entity.Player; |
| 4 | +import xyz.damt.command.annotation.Permission; |
| 5 | +import xyz.damt.command.annotation.Sender; |
| 6 | + |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.lang.reflect.Parameter; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +public class CommandFinder { |
| 14 | + |
| 15 | + private final Object object; |
| 16 | + private final List<Command> commands = new ArrayList<>(); |
| 17 | + |
| 18 | + /** |
| 19 | + * Constructs a new instance of {@link CommandFinder} |
| 20 | + * And finds all commands inside of a class |
| 21 | + * |
| 22 | + * @param object {@link Object} |
| 23 | + */ |
| 24 | + |
| 25 | + public CommandFinder(Object object) { |
| 26 | + this.object = object; |
| 27 | + |
| 28 | + this.init(); |
| 29 | + this.edit(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Gets one command based on it's name |
| 34 | + * |
| 35 | + * @param name name of the command |
| 36 | + * @return {@link Command} |
| 37 | + */ |
| 38 | + |
| 39 | + public final Command getCommand(String name) { |
| 40 | + return commands.stream().filter(command -> command.getName().equalsIgnoreCase(name)).findFirst().orElse(null); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Gets all commands inside of the class |
| 45 | + * |
| 46 | + * @return {@link List<Command>}* |
| 47 | + */ |
| 48 | + |
| 49 | + public final List<Command> getCommands() { |
| 50 | + return commands; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets all the sub commands of a command |
| 55 | + * |
| 56 | + * @param name name of the command |
| 57 | + * @return {@link List<Command>} |
| 58 | + */ |
| 59 | + |
| 60 | + public final List<Command> getCommands(String name) { |
| 61 | + return commands.stream().filter(command -> command.getName().split("\\s+") |
| 62 | + [0].equalsIgnoreCase(name)).collect(Collectors.toList()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Adds all sub commands to a command |
| 67 | + */ |
| 68 | + |
| 69 | + private final void edit() { |
| 70 | + commands.forEach(command -> { |
| 71 | + String[] args = command.getName().split("\\s+"); |
| 72 | + |
| 73 | + if (args.length > 0) |
| 74 | + return; |
| 75 | + |
| 76 | + List<Command> subCommands = getCommands(args[0]); |
| 77 | + command.getSubCommands().addAll(subCommands); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates and finds all sub commands |
| 83 | + */ |
| 84 | + |
| 85 | + private final void init() { |
| 86 | + final Class<?> clazz = object.getClass(); |
| 87 | + |
| 88 | + for (Method method : clazz.getMethods()) { |
| 89 | + method.setAccessible(true); |
| 90 | + |
| 91 | + xyz.damt.command.annotation.Command commandAnnotation = method.getAnnotation(xyz.damt.command.annotation.Command.class); |
| 92 | + |
| 93 | + if (commandAnnotation != null) { |
| 94 | + Permission permissionAnnotation = method.getAnnotation(Permission.class); |
| 95 | + Command command = new Command(commandAnnotation.value(), commandAnnotation.aliases(), method, commandAnnotation.description(), commandAnnotation.usage(), commandAnnotation.async()); |
| 96 | + |
| 97 | + if (permissionAnnotation != null) { |
| 98 | + command.setPermission(permissionAnnotation.permission()); |
| 99 | + command.setPermissionMessage(permissionAnnotation.message()); |
| 100 | + } |
| 101 | + |
| 102 | + for (Parameter parameter : method.getParameters()) { |
| 103 | + Sender senderAnnotation = parameter.getAnnotation(Sender.class); |
| 104 | + |
| 105 | + if (senderAnnotation != null) { |
| 106 | + command.setPlayer(parameter.getType().equals(Player.class)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + commands.add(command); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments