Plex/src/main/java/dev/plex/handlers/CommandHandler.java

57 lines
2.2 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.handlers;
import com.google.common.collect.Lists;
import dev.plex.PlexBase;
2021-01-03 07:21:15 +00:00
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.System;
2021-01-03 07:21:15 +00:00
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import java.lang.reflect.InvocationTargetException;
2022-01-04 03:04:39 +00:00
import java.util.List;
import java.util.Set;
2022-01-04 03:04:39 +00:00
public class CommandHandler extends PlexBase
{
public CommandHandler()
{
Set<Class<? extends PlexCommand>> commandSet = PlexUtils.getClassesBySubType("dev.plex.command.impl", PlexCommand.class);
2021-06-20 08:02:07 +00:00
List<PlexCommand> commands = Lists.newArrayList();
commandSet.forEach(clazz ->
{
try
{
System annotation = clazz.getDeclaredAnnotation(System.class);
2022-04-01 07:54:22 +00:00
// TODO: Annotations are always null?
2022-03-19 04:17:43 +00:00
if (annotation != null)
{
2022-04-01 07:54:22 +00:00
PlexLog.debug(clazz.getName() + " has annotations");
if (annotation.value().equalsIgnoreCase(plugin.getSystem().toLowerCase()))
2022-03-19 04:17:43 +00:00
{
commands.add(clazz.getConstructor().newInstance());
2022-04-01 07:54:22 +00:00
PlexLog.debug("Adding " + clazz.getName() + " as a rank command");
2022-03-19 04:17:43 +00:00
}
if (plugin.config.getBoolean("debug") && annotation.debug())
{
commands.add(clazz.getConstructor().newInstance());
2022-04-01 07:54:22 +00:00
PlexLog.debug("Adding " + clazz.getName() + " as a debug command");
2022-03-19 04:17:43 +00:00
}
}
else
{
commands.add(clazz.getConstructor().newInstance());
2022-04-01 07:54:22 +00:00
// PlexLog.debug("Adding command normally " + clazz.getName());
}
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException ex)
{
2022-03-19 19:27:55 +00:00
PlexLog.error("Failed to register " + clazz.getSimpleName() + " as a command!");
}
});
2022-04-01 07:54:22 +00:00
PlexLog.debug("Test");
PlexLog.log(String.format("Registered %s commands from %s classes!", commands.size(), commandSet.size()));
}
}