Skip to content

Commit

Permalink
Add new method to register commands. (experimental API)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Nov 16, 2023
1 parent e8330c7 commit d832c1a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package dev.rollczi.litecommands.annotations.argument;

import org.jetbrains.annotations.ApiStatus;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@ApiStatus.Experimental
public @interface Key {

@ApiStatus.Experimental
String value();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.rollczi.litecommands;

import dev.rollczi.litecommands.annotations.LiteCommandsAnnotations;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.ArgumentKey;
import dev.rollczi.litecommands.argument.parser.Parser;
Expand Down Expand Up @@ -28,6 +29,8 @@
import dev.rollczi.litecommands.permission.MissingPermissions;
import dev.rollczi.litecommands.permission.MissingPermissionsHandler;
import dev.rollczi.litecommands.platform.PlatformSettingsConfigurator;
import dev.rollczi.litecommands.programmatic.LiteCommand;
import dev.rollczi.litecommands.programmatic.LiteCommandsProgrammatic;
import dev.rollczi.litecommands.scheduler.Scheduler;
import dev.rollczi.litecommands.scheduler.SchedulerSameThreadImpl;
import dev.rollczi.litecommands.schematic.SchematicFormat;
Expand All @@ -54,6 +57,7 @@
import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -115,6 +119,51 @@ public LiteCommandsBuilder<SENDER, C, B> commands(LiteCommandsProvider<SENDER> c
return this;
}

@Override
@SuppressWarnings("unchecked")
public LiteCommandsBuilder<SENDER, C, B> commands(Object... commands) {
List<LiteCommandsProvider<SENDER>> providers = new ArrayList<>();
Collection<LiteCommand<SENDER>> programmatic = new ArrayList<>();
List<Class<?>> classes = new ArrayList<>();
List<Object> instances = new ArrayList<>();

for (Object command : commands) {
if (command instanceof LiteCommandsProvider) {
providers.add((LiteCommandsProvider<SENDER>) command);
continue;
}

if (command instanceof LiteCommand) {
programmatic.add((LiteCommand<SENDER>) command);
continue;
}

if (command instanceof Class) {
classes.add((Class<?>) command);
continue;
}

instances.add(command);
}

for (LiteCommandsProvider<SENDER> provider : providers) {
this.commands(provider);
}

if (!programmatic.isEmpty()) {
this.commands(LiteCommandsProgrammatic.of(programmatic));
}

if (!classes.isEmpty() || !instances.isEmpty()) {
this.commands(LiteCommandsAnnotations.<SENDER>create()
.load(instances.toArray(new Object[0]))
.loadClasses(classes.toArray(new Class<?>[0]))
);
}

return this;
}

/**
* Pre-process extensions on provider before executing {@link CommandBuilderCollector#collectAndMergeCommands()}
* Kinda magic, but it works.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import dev.rollczi.litecommands.validator.Validator;
import dev.rollczi.litecommands.validator.ValidatorScope;
import dev.rollczi.litecommands.wrapper.Wrapper;
import org.jetbrains.annotations.ApiStatus;

import java.util.function.Supplier;
import java.util.function.UnaryOperator;
Expand Down Expand Up @@ -56,12 +57,35 @@ public interface LiteCommandsBuilder<SENDER, SETTINGS extends PlatformSettings,
/**
* Register commands from given provider.
*
* @see LiteCommandsBuilder#commands(Object...)
* @see LiteCommandsProvider
* @param commandsProvider provider of commands
* @return this builder
*/
LiteCommandsBuilder<SENDER, SETTINGS, B> commands(LiteCommandsProvider<SENDER> commandsProvider);

/**
* This method is used to register additional commands.
* There are several types of objects that can be registered:
* <ul>
* <b>Using annotations:</b>
* <li>An instance annotated with {@link dev.rollczi.litecommands.annotations.command.Command}</li>
* <li>An instance annotated with {@link dev.rollczi.litecommands.annotations.command.RootCommand}</li>
* <li>A class annotated with {@link dev.rollczi.litecommands.annotations.command.Command}</li>
* <li>A class annotated with {@link dev.rollczi.litecommands.annotations.command.RootCommand}</li>
* <b>Programmatically:</b>
* <li>An instance of {@link dev.rollczi.litecommands.programmatic.LiteCommand}</li>
* </ul>
* Please note that this method is experimental and may be deprecated or removed in the future.
*
* @see LiteCommandsBuilder#commands(LiteCommandsProvider)
* @param commands commands to register
* @return This method returns the current LiteCommand builder instance
* This allows you to chain multiple calls together, using the builder design pattern.
*/
@ApiStatus.Experimental
LiteCommandsBuilder<SENDER, SETTINGS, B> commands(Object... commands);

<IN, T, PARSER extends Parser<SENDER, IN, T>>
LiteCommandsBuilder<SENDER, SETTINGS, B> argumentParser(Class<T> type, PARSER parser);

Expand Down

0 comments on commit d832c1a

Please sign in to comment.