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

50 lines
1.8 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
{
if (clazz.isAnnotationPresent(System.class))
2022-03-19 04:17:43 +00:00
{
System annotation = clazz.getDeclaredAnnotation(System.class);
2022-04-01 07:54:22 +00:00
if (annotation.value().equalsIgnoreCase(plugin.getSystem().toLowerCase()))
2022-03-19 04:17:43 +00:00
{
commands.add(clazz.getConstructor().newInstance());
}
if (plugin.config.getBoolean("debug") && annotation.debug())
{
commands.add(clazz.getConstructor().newInstance());
}
}
else
{
commands.add(clazz.getConstructor().newInstance());
}
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException ex)
{
2022-03-19 19:27:55 +00:00
PlexLog.error("Failed to register " + clazz.getSimpleName() + " as a command!");
}
});
PlexLog.log(String.format("Registered %s commands from %s classes!", commands.size(), commandSet.size()));
}
}