2021-06-26 19:08:15 -07:00
|
|
|
package net.nuggetmc.ai.commands;
|
|
|
|
|
|
|
|
|
|
import com.jonahseguin.drink.Drink;
|
|
|
|
|
import com.jonahseguin.drink.annotation.Command;
|
|
|
|
|
import com.jonahseguin.drink.command.DrinkCommandService;
|
|
|
|
|
import net.nuggetmc.ai.PlayerAI;
|
|
|
|
|
import net.nuggetmc.ai.commands.commands.PlayerAICommand;
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
2021-06-27 01:09:29 -05:00
|
|
|
import java.util.*;
|
2021-06-26 19:08:15 -07:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
public class CommandHandler {
|
|
|
|
|
|
|
|
|
|
private final DrinkCommandService drink;
|
2021-06-27 01:09:29 -05:00
|
|
|
private final Map<Class<? extends CommandInstance>, List<String>> help;
|
2021-06-26 19:08:15 -07:00
|
|
|
|
2021-06-26 23:44:07 -05:00
|
|
|
public CommandHandler(PlayerAI plugin) {
|
|
|
|
|
drink = (DrinkCommandService) Drink.get(plugin);
|
|
|
|
|
drink.register(new PlayerAICommand(this), "bot", "playerai", "pai", "ai", "npc");
|
2021-06-26 19:08:15 -07:00
|
|
|
drink.registerCommands();
|
2021-06-27 01:09:29 -05:00
|
|
|
|
|
|
|
|
help = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
setHelps(new Class[] {
|
|
|
|
|
PlayerAICommand.class
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setHelps(Class<? extends CommandInstance>[] cls) {
|
|
|
|
|
for (Class<? extends CommandInstance> clazz : cls) {
|
|
|
|
|
help.put(clazz, getUsage(clazz));
|
|
|
|
|
}
|
2021-06-26 19:08:15 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-27 01:09:29 -05:00
|
|
|
public List<String> getHelp(Class<? extends CommandInstance> clazz) {
|
|
|
|
|
return help.get(clazz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<String> getUsage(Class<? extends CommandInstance> clazz) {
|
2021-06-26 23:44:07 -05:00
|
|
|
String rootName = getRootName(clazz);
|
2021-06-27 01:09:29 -05:00
|
|
|
|
2021-06-26 19:08:15 -07:00
|
|
|
return getSubCommands(clazz).stream().map(c -> {
|
|
|
|
|
Command command = c.getAnnotation(Command.class);
|
2021-06-26 23:44:07 -05:00
|
|
|
return ChatColor.GRAY + " ▪ " + ChatColor.YELLOW + "/" + rootName + " " + command.name() + ChatColor.GRAY + " ▪ "
|
2021-06-26 19:08:15 -07:00
|
|
|
+ ChatColor.RESET + command.desc();
|
2021-06-27 01:09:29 -05:00
|
|
|
}).sorted().collect(Collectors.toList());
|
2021-06-26 19:08:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getRootName(Class<? extends CommandInstance> clazz) {
|
|
|
|
|
return drink.getCommands().entrySet().stream()
|
|
|
|
|
.filter(c -> c.getValue().getObject().getClass().isAssignableFrom(clazz)).map(Map.Entry::getKey).findFirst().orElse(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Method> getSubCommands(Class<? extends CommandInstance> clazz) {
|
2021-06-26 23:44:07 -05:00
|
|
|
return Arrays.stream(clazz.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(Command.class) && !m.getAnnotation(Command.class).name().isEmpty()).collect(Collectors.toList());
|
2021-06-26 19:08:15 -07:00
|
|
|
}
|
|
|
|
|
}
|