This allows you to create a console with JLine to create commands for the terminal.
<dependencies>
<dependency>
<groupId>world.avionik</groupId>
<artifactId>console-factory</artifactId>
<version>1.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
dependencies {
compileOnly 'world.avionik:console-factory:1.0.1'
}
You can implement the Console class in your Main class. That was actually all. Now you have the possibility to create commands. There is the ConsoleInput class. There you can set what the command should be called and what must be executed.
class MainConsoleClass : Console(
ConsoleInput("first") { println("Second") }
)
To have a "better code structure" there is the ConsoleInputExecutor. You can use it to create a class, implement this executor and write your command there. Here is an example:
class BirdExecutor : ConsoleInputExecutor {
/**
* This method is executed when a certain input is sent to the console.
*/
override fun execute() {
println("Im a bird")
}
}
Once you have implemented this executor, you only need to integrate it into your main class that has implemented the Console interface. There you can register the executor with the ConsoleInput class. Here is another example:
class MainConsoleClass : Console(
ConsoleInput("fly", executor = BirdExecutor())
)
// And this is how you can add aliases
class MainConsoleClass : Console(
ConsoleInput("fly" "bird", executor = BirdExecutor())
)