Skip to content

Command system for parsing strings into types. General purpose with in and output API.

License

Notifications You must be signed in to change notification settings

svdgoor/StrInput

Repository files navigation

license-shield docs-shield

StrInput

StrInput provides a feature-rich general-use command parsing system. This library helps implement command systems through reducing type conversion complexity and command parsing improvements.

Summary

  1. Installation
  2. Setup (simple)
  3. Setup (advanced)
  4. To-do

Release with jitpack support

StrInput is now ready to be used. Though lacking some features, its main flow is working. Examples for both Discord (JDA) and Spigot (SpigotMC) are present in the examples module.

Installation

Make sure to replace VERSION with the latest version:

For the latest (potentially unstable) available builds replace VERSION with main-SNAPSHOT.

Gradle

repositories {
    maven { url "https://jitpack.io" }
    mavenCentral()
}

dependencies {
    implementation 'com.github.CocoTheOwner:StrInput:VERSION'
}

Maven

<dependency>
    <groupId>com.github.CocoTheOwner</groupId>
    <artifactId>StrInput</artifactId>
    <version>VERSION</version>
</dependency>

Setup (simple)

Setting up the command system is quite simple and requires only three steps.

  1. User
  2. Command Center
  3. First Category

User

To interact with the system you will need an implementation of the abstract class StrUser. This implementation will handle messages from the system to the user.

public class CustomUser implements StrUser {
    // To be defined
}

Command Center

The command center is an instance of StrCenter. You provide in the constructor at least a settings directory and one (or more) class instances (implementing StrCategory), which are your root commands. Additionally, you can (as shown in Setup (advanced)) optionally specify a console StrUser (which by default simply uses System.out.println()), extra StrParameterHandlers and StrContextHandlers. It is advisable to store the command system in a main class (such as in the Spigot example, SpigotPlugin).

public class MainClass {
    private static final StrCenter COMMAND_CENTER = new StrCenter(
            new File("settings"), // The file where settings are created, stored and can be edited
            new ExampleCategory() // The 'root' category (this is a vararg, so there can be multiple!)
    );
}

Notice the user of ExampleCategory. This class will be the first category made (the main command category). See First Category.

After creating the command center, whenever your system receives a user command, send it to the command center.

public class MainClass {

    // Seen previously
    private static final StrCenter COMMAND_CENTER = new StrCenter(
            new File("settings"),
            new ExampleCategory()
    );
    
    public void onCommand(String command, SomeUser user) {
    
        // Handle prefixes
        if (!command.startsWith("!")) {
            return;
        }
        
        // Run other logic to prevent users from using commands
        if (!user.isCool()) {
            return;
        }
        
        // Pass the command to the command system
        COMMAND_CENTER.onCommand(command.split(" "), new CustomUser(user.getName()));
    }
}

Now all that is left is creating the first command category!

First Category

Each command (sub)category gets its own class. Command categories can extend StrCategory (or a custom expansion of that, which you can create, see StrCategory Extension) to gain access to the current #user() (the command sender) and the current #center() (the active command center).__ The category and commands therein must be annotated with @StrInput and parameters with @Param. These annotation also contain settings such as the name, aliases and a description (and more, see the classes).

public final class ExampleCategory implements SpigotCommandCategory {

    /**
     * Send a message to the user
     */
    @StrInput(description = "Send a message", aliases = "msg")
    public void message() {
        user().sendMessage(C.G + "Hey!");
    }

    /**
     * Send a message to debug
     *
     * @param message the message to send
     */
    @StrInput(description = "Send a message to debug")
    public void debug(
            @Param(
                    description = "The message to send to debug",
                    name = "message"
            )
            final String message
    ) {
        center().debug(message);
    }
}

Note ). Str is the custom text format which can contain colors, color gradients, clickable event and text-hovering.

Setup (advanced)

A set of custom options are available to further simplify command creation.

  1. Advanced User
  2. Extra Parameter Handlers
  3. Extra Context Handlers
  4. StrCategory Extension

Advanced User

TBD

Extra Parameter Handlers

TBD

Extra Context Handlers

TBD

StrCategory Extension

TBD

To-do

  • Implement help messages (categories, commands & parameters)
  • Add prepared Discord category, guild and permission handlers
  • Add suggestions (auto-completions)
  • Add Spigot colours + colour gradients
  • Add Discord & Spigot on-click implementation
  • Add Discord & Spigot on-hover implementation
  • Expand range of testcases for:
    • Individual parameter & context handlers
    • Discord & Spigot user and Center implementations
  • Style and documentation on Discord examples
  • Parameter performance improvements (it's fast enough, but can be faster)
  • Write advanced setup
  • Add support for only-root commands (/kill, instead of /plugin kill)
  • Test if multiple instances of the system running at once (meaning different implementations, such as on multiple Spigot plugins) will interfere with one-another.
  • Make sure parameters' names are saved