TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/CommandLoader.java

82 lines
2.0 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2020-07-01 01:51:06 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2020-07-01 01:51:06 +00:00
import java.util.List;
2020-08-01 04:10:44 +00:00
import java.util.Set;
import lombok.Getter;
import me.totalfreedom.totalfreedommod.FreedomService;
2020-08-01 04:10:44 +00:00
import me.totalfreedom.totalfreedommod.util.FLog;
import org.reflections.Reflections;
public class CommandLoader extends FreedomService
{
@Getter
2020-07-01 01:51:06 +00:00
private final List<FreedomCommand> commands;
2020-07-01 01:51:06 +00:00
public CommandLoader()
{
2020-07-01 01:51:06 +00:00
commands = new ArrayList<>();
}
@Override
2020-07-01 01:51:06 +00:00
public void onStart()
{
2020-07-01 01:51:06 +00:00
}
2020-07-01 01:51:06 +00:00
@Override
public void onStop()
{
}
2020-07-01 01:51:06 +00:00
public void add(FreedomCommand command)
{
commands.add(command);
command.register();
}
2020-07-01 01:51:06 +00:00
public FreedomCommand getByName(String name)
{
2020-07-01 01:51:06 +00:00
for (FreedomCommand command : commands)
{
if (name.equals(command.getName()))
return command;
}
return null;
}
public boolean isAlias(String alias)
{
for (FreedomCommand command : commands)
{
if (Arrays.asList(command.getAliases().split(",")).contains(alias))
return true;
}
return false;
}
2020-08-01 04:10:44 +00:00
public void loadCommands()
{
Reflections commandDir = new Reflections("me.totalfreedom.totalfreedommod.command");
Set<Class<? extends FreedomCommand>> commandClasses = commandDir.getSubTypesOf(FreedomCommand.class);
for (Class<? extends FreedomCommand> commandClass : commandClasses)
{
try
{
FLog.debug("Loading command class " + commandClass.getSimpleName());
add(commandClass.newInstance());
}
catch (InstantiationException | IllegalAccessException | ExceptionInInitializerError ex)
{
FLog.warning("Failed to register command: /" + commandClass.getSimpleName().replace("Command_" , ""));
}
}
}
2020-07-01 01:51:06 +00:00
public int getCommandAmount()
{
return commands.size();
}
}