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

52 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;
2022-04-19 20:19:55 +00:00
import dev.plex.util.ReflectionsUtil;
2023-03-08 20:26:10 +00:00
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
2022-04-17 05:27:04 +00:00
public class CommandHandler implements PlexBase
{
public CommandHandler()
{
2022-04-19 20:19:55 +00:00
Set<Class<? extends PlexCommand>> commandSet = ReflectionsUtil.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());
}
}
2022-04-13 02:22:17 +00:00
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()));
}
}