Search in sources :

Example 1 with CommandBuilder

use of io.github.nucleuspowered.nucleus.internal.command.CommandBuilder in project Nucleus by NucleusPowered.

the class StandardModule method loadCommands.

@SuppressWarnings("unchecked")
private void loadCommands() {
    Set<Class<? extends AbstractCommand<?>>> cmds;
    if (this.msls != null) {
        cmds = new HashSet<>();
        List<String> l = this.msls.get(Constants.COMMAND);
        if (l == null) {
            return;
        }
        for (String s : l) {
            try {
                checkPlatformOpt((Class<? extends AbstractCommand<?>>) Class.forName(s)).ifPresent(cmds::add);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        cmds = new HashSet<>(performFilter(getStreamForModule(AbstractCommand.class).map(x -> (Class<? extends AbstractCommand<?>>) x)).collect(Collectors.toSet()));
        // Find all commands that are also scannable.
        performFilter(plugin.getModuleContainer().getLoadedClasses().stream().filter(x -> x.getPackage().getName().startsWith(packageName)).filter(x -> x.isAnnotationPresent(Scan.class)).flatMap(x -> Arrays.stream(x.getDeclaredClasses())).filter(AbstractCommand.class::isAssignableFrom).map(x -> (Class<? extends AbstractCommand<?>>) x)).forEach(cmds::add);
    }
    // We all love the special injector. We just want to provide the module with more commands, in case it needs a child.
    Set<Class<? extends AbstractCommand>> commandBases = cmds.stream().filter(x -> {
        RegisterCommand rc = x.getAnnotation(RegisterCommand.class);
        return (rc != null && rc.subcommandOf().equals(AbstractCommand.class));
    }).collect(Collectors.toSet());
    CommandBuilder builder = new CommandBuilder(plugin, cmds, moduleId, moduleName);
    commandBases.forEach(builder::buildCommand);
    try {
        commandsConfig.mergeDefaults(builder.getNodeToMerge());
        commandsConfig.save();
    } catch (Exception e) {
        plugin.getLogger().error("Could not save defaults.");
        e.printStackTrace();
    }
}
Also used : Arrays(java.util.Arrays) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NucleusPlugin(io.github.nucleuspowered.nucleus.NucleusPlugin) BiFunction(java.util.function.BiFunction) RequiresPlatform(io.github.nucleuspowered.nucleus.internal.annotations.RequiresPlatform) Tokens(io.github.nucleuspowered.nucleus.internal.text.Tokens) Map(java.util.Map) Constants(io.github.nucleuspowered.nucleus.internal.Constants) ServiceChangeListener(io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener) Subject(org.spongepowered.api.service.permission.Subject) Method(java.lang.reflect.Method) AbstractConfigAdapter(uk.co.drnaylor.quickstart.config.AbstractConfigAdapter) CommandPermissionHandler(io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler) User(org.spongepowered.api.entity.living.player.User) ImmutableMap(com.google.common.collect.ImmutableMap) CommandSource(org.spongepowered.api.command.CommandSource) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) ContextCalculator(org.spongepowered.api.service.context.ContextCalculator) Stream(java.util.stream.Stream) RegisterService(io.github.nucleuspowered.nucleus.internal.annotations.RegisterService) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ServerOnly(io.github.nucleuspowered.nucleus.internal.annotations.ServerOnly) DocGenCache(io.github.nucleuspowered.nucleus.internal.docgen.DocGenCache) SkipOnError(io.github.nucleuspowered.nucleus.internal.annotations.SkipOnError) Platform(org.spongepowered.api.Platform) Scan(io.github.nucleuspowered.nucleus.internal.annotations.command.Scan) HashSet(java.util.HashSet) Text(org.spongepowered.api.text.Text) ICommandInterceptor(io.github.nucleuspowered.nucleus.internal.command.ICommandInterceptor) Task(org.spongepowered.api.scheduler.Task) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) RegisterCommandInterceptors(io.github.nucleuspowered.nucleus.internal.annotations.RegisterCommandInterceptors) ModuleData(uk.co.drnaylor.quickstart.annotations.ModuleData) Nullable(javax.annotation.Nullable) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandBuilder(io.github.nucleuspowered.nucleus.internal.command.CommandBuilder) Context(org.spongepowered.api.service.context.Context) CommandsConfig(io.github.nucleuspowered.nucleus.config.CommandsConfig) InternalServiceManager(io.github.nucleuspowered.nucleus.internal.InternalServiceManager) RequireExistenceOf(io.github.nucleuspowered.nucleus.internal.annotations.RequireExistenceOf) Store(io.github.nucleuspowered.nucleus.annotationprocessor.Store) Module(uk.co.drnaylor.quickstart.Module) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) TimeUnit(java.util.concurrent.TimeUnit) SeenHandler(io.github.nucleuspowered.nucleus.modules.playerinfo.handlers.SeenHandler) MissingDependencyException(uk.co.drnaylor.quickstart.exceptions.MissingDependencyException) TaskBase(io.github.nucleuspowered.nucleus.internal.TaskBase) BasicSeenInformationProvider(io.github.nucleuspowered.nucleus.modules.playerinfo.handlers.BasicSeenInformationProvider) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) MissingDependencyException(uk.co.drnaylor.quickstart.exceptions.MissingDependencyException) CommandBuilder(io.github.nucleuspowered.nucleus.internal.command.CommandBuilder)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)1 NucleusPlugin (io.github.nucleuspowered.nucleus.NucleusPlugin)1 Store (io.github.nucleuspowered.nucleus.annotationprocessor.Store)1 CommandsConfig (io.github.nucleuspowered.nucleus.config.CommandsConfig)1 CommandPermissionHandler (io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler)1 Constants (io.github.nucleuspowered.nucleus.internal.Constants)1 InternalServiceManager (io.github.nucleuspowered.nucleus.internal.InternalServiceManager)1 ListenerBase (io.github.nucleuspowered.nucleus.internal.ListenerBase)1 TaskBase (io.github.nucleuspowered.nucleus.internal.TaskBase)1 RegisterCommandInterceptors (io.github.nucleuspowered.nucleus.internal.annotations.RegisterCommandInterceptors)1 RegisterService (io.github.nucleuspowered.nucleus.internal.annotations.RegisterService)1 RequireExistenceOf (io.github.nucleuspowered.nucleus.internal.annotations.RequireExistenceOf)1 RequiresPlatform (io.github.nucleuspowered.nucleus.internal.annotations.RequiresPlatform)1 ServerOnly (io.github.nucleuspowered.nucleus.internal.annotations.ServerOnly)1 SkipOnError (io.github.nucleuspowered.nucleus.internal.annotations.SkipOnError)1 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)1 Scan (io.github.nucleuspowered.nucleus.internal.annotations.command.Scan)1 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)1 CommandBuilder (io.github.nucleuspowered.nucleus.internal.command.CommandBuilder)1