2016-03-02 20:28:01 +01:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
2015-10-19 19:43:46 +02:00
|
|
|
|
2020-06-30 21:51:06 -04:00
|
|
|
import java.util.ArrayList;
|
2020-07-01 12:45:09 -04:00
|
|
|
import java.util.Arrays;
|
2020-06-30 21:51:06 -04:00
|
|
|
import java.util.List;
|
2020-07-31 21:10:44 -07:00
|
|
|
import java.util.Set;
|
2016-03-01 17:47:01 +01:00
|
|
|
import me.totalfreedom.totalfreedommod.FreedomService;
|
2020-07-31 21:10:44 -07:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
|
|
|
import org.reflections.Reflections;
|
2015-10-19 19:43:46 +02:00
|
|
|
|
2016-03-01 17:47:01 +01:00
|
|
|
public class CommandLoader extends FreedomService
|
2015-10-19 19:43:46 +02:00
|
|
|
{
|
2020-06-30 21:51:06 -04:00
|
|
|
private final List<FreedomCommand> commands;
|
2015-10-19 19:43:46 +02:00
|
|
|
|
2020-06-30 21:51:06 -04:00
|
|
|
public CommandLoader()
|
2015-10-19 19:43:46 +02:00
|
|
|
{
|
2020-06-30 21:51:06 -04:00
|
|
|
commands = new ArrayList<>();
|
2015-10-19 19:43:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-06-30 21:51:06 -04:00
|
|
|
public void onStart()
|
2015-10-19 19:43:46 +02:00
|
|
|
{
|
2020-06-30 21:51:06 -04:00
|
|
|
}
|
2015-11-16 00:32:04 +01:00
|
|
|
|
2020-06-30 21:51:06 -04:00
|
|
|
@Override
|
|
|
|
public void onStop()
|
|
|
|
{
|
|
|
|
}
|
2015-10-19 19:43:46 +02:00
|
|
|
|
2020-06-30 21:51:06 -04:00
|
|
|
public void add(FreedomCommand command)
|
|
|
|
{
|
|
|
|
commands.add(command);
|
|
|
|
command.register();
|
2015-10-19 19:43:46 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 21:51:06 -04:00
|
|
|
public FreedomCommand getByName(String name)
|
2015-10-19 19:43:46 +02:00
|
|
|
{
|
2020-06-30 21:51:06 -04:00
|
|
|
for (FreedomCommand command : commands)
|
|
|
|
{
|
|
|
|
if (name.equals(command.getName()))
|
2020-12-03 19:28:53 -05:00
|
|
|
{
|
2020-06-30 21:51:06 -04:00
|
|
|
return command;
|
2020-12-03 19:28:53 -05:00
|
|
|
}
|
2020-06-30 21:51:06 -04:00
|
|
|
}
|
|
|
|
return null;
|
2015-10-19 19:43:46 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:45:09 -04:00
|
|
|
public boolean isAlias(String alias)
|
|
|
|
{
|
|
|
|
for (FreedomCommand command : commands)
|
|
|
|
{
|
|
|
|
if (Arrays.asList(command.getAliases().split(",")).contains(alias))
|
2020-12-03 19:28:53 -05:00
|
|
|
{
|
2020-07-01 12:45:09 -04:00
|
|
|
return true;
|
2020-12-03 19:28:53 -05:00
|
|
|
}
|
2020-07-01 12:45:09 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-31 21:10:44 -07: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
|
|
|
|
{
|
|
|
|
add(commandClass.newInstance());
|
|
|
|
}
|
|
|
|
catch (InstantiationException | IllegalAccessException | ExceptionInInitializerError ex)
|
|
|
|
{
|
2020-12-03 19:28:53 -05:00
|
|
|
FLog.warning("Failed to register command: /" + commandClass.getSimpleName().replace("Command_", ""));
|
2020-07-31 21:10:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 15:16:11 -07:00
|
|
|
FLog.info("Loaded " + commands.size() + " commands");
|
2020-06-30 21:51:06 -04:00
|
|
|
}
|
2020-12-29 21:37:50 -06:00
|
|
|
|
|
|
|
public List<FreedomCommand> getCommands()
|
|
|
|
{
|
|
|
|
return commands;
|
|
|
|
}
|
2017-10-13 23:35:11 +05:00
|
|
|
}
|