An opinionated annotation-driven command framework for Java applications.
The only difference is that while others rely on runtime reflection to fetch and handle annotations, this framework constructs the native code with annotation processor and JavaPoet, allows near native speed with minimal reflection usage.
It is recommended to import craftcommand-bom in your <dependencyManagement> section to manage version configurations of CraftCommand modules:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.github.projectunified</groupId>
<artifactId>craftcommand-bom</artifactId>
<version>VERSION</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Then, add the annotation and runtime dependency to your pom.xml without specifying a version:
<dependency>
<groupId>io.github.projectunified</groupId>
<artifactId>craftcommand-annotations</artifactId>
</dependency>
<dependency>
<groupId>io.github.projectunified</groupId>
<artifactId>craftcommand-standalone-runtime</artifactId>
</dependency>And configure the annotation processor in your compiler plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.github.projectunified</groupId>
<artifactId>craftcommand-standalone-processor</artifactId>
<version>VERSION</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>package myapp;
import io.github.projectunified.craftcommand.annotation.*;
@Command(value = "calc", description = "Simple Calculator Command")
public class CalculatorCommand {
@Default
public void defaultAction(Object sender) {
System.out.println("Use /calc add <num1> <num2>");
}
@Subcommand(value = "add", aliases = {"sum"})
public void add(Object sender,
@Min(value = 0, message = "Inputs must be positive") double a,
double b) {
System.out.println("Result: " + (a + b));
}
}import io.github.projectunified.craftcommand.standalone.StandaloneCommandManager;
import io.github.projectunified.craftcommand.standalone.StandaloneCommand;
public class Main {
public static void main(String[] args) {
StandaloneCommandManager manager = new StandaloneCommandManager();
// Register the annotated command instance
manager.register(new CalculatorCommand());
// Customizing translation keys/messages in the dictionary
manager.setMessage("validation.min", "Error: %s cannot be less than %s!");
// Execute the command
StandaloneCommand cmd = manager.getCommand("calc");
cmd.execute("sender", new String[]{"add", "5.2", "10.0"}); // Prints: Result: 15.2
}
}