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